-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a simple frontend #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>url shortener</title> | ||
<meta name="viewport" content="width=device-width" /> | ||
<link | ||
rel="stylesheet" | ||
href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css"> | ||
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
<body> | ||
|
||
<div class="container"> | ||
<div class="bar"> | ||
|
||
<div class="mdc-textfield mdc-textfield--upgraded"> | ||
<input type="text" id="my-textfield" class="urltochange mdc-textfield__input"> | ||
<label class="mdc-textfield__label mdc-textfield__label--float-above" for="my-textfield"> | ||
URL to shorten | ||
</label> | ||
</div> | ||
|
||
<button type="submit" class="button">shorten</button> | ||
</div> | ||
</div> | ||
<div class="resultUrl"></div> | ||
<style> | ||
|
||
html { | ||
background: #e0e0e0; | ||
background-size:cover; | ||
font-family: 'Open Sans', sans-serif; | ||
text-align: center; | ||
font-size: 14px; | ||
} | ||
|
||
.container { | ||
display: flex; | ||
justify-content: center; | ||
margin-top: 100px; | ||
} | ||
|
||
.mdc-textfield__input { | ||
width: 280px; | ||
} | ||
|
||
button { | ||
background: none; | ||
border: 0; | ||
cursor: pointer; | ||
color: white; | ||
font-size: 1.1rem; | ||
text-transform: uppercase; | ||
background: #2196f3; | ||
border-bottom: 3px solid rgba(0, 0, 0, 0.2); | ||
border-right: 1px solid rgba(0, 0, 0, 0.2); | ||
|
||
} | ||
|
||
.resultUrl { | ||
margin-top: 20px; | ||
font-size: 2rem; | ||
} | ||
|
||
.smallText { | ||
font-size: 0.75rem; | ||
color: #757575; | ||
} | ||
|
||
</style> | ||
<script> | ||
const inputField = document.querySelector('.urltochange'); | ||
const shortenButton = document.querySelector('.button'); | ||
const requestURL = "/encode/" | ||
const xhr = new XMLHttpRequest(); | ||
const newUrl = document.querySelector('.resultUrl'); | ||
|
||
function shorten() { | ||
let urlToShorten = document.querySelector('.urltochange'); | ||
xhr.open("POST", requestURL, false); | ||
xhr.setRequestHeader("Content-Type", "application/json"); | ||
xhr.send(JSON.stringify({url:urlToShorten.value})); | ||
let respo = JSON.parse(xhr.responseText).response; | ||
newUrl.innerHTML = respo; | ||
let p = document.createElement("p"); | ||
document.querySelector('.resultUrl').appendChild(p); | ||
p.setAttribute("class", "smallText") | ||
p.innerHTML = "Click shortened UR to copy to clipboard"; | ||
|
||
const span = document.querySelector('.resultUrl'); | ||
|
||
span.onclick = function() { | ||
document.execCommand("copy"); | ||
} | ||
|
||
|
||
span.addEventListener("copy", function(event) { | ||
event.preventDefault(); | ||
if (event.clipboardData) { | ||
event.clipboardData.setData("text/plain", respo); | ||
} | ||
}); | ||
|
||
} | ||
|
||
function clear(e){ | ||
this.value = " " | ||
} | ||
|
||
shortenButton.addEventListener('click', shorten); | ||
inputField.addEventListener('click', clear); | ||
|
||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,8 +94,14 @@ func (h handler) redirect(w http.ResponseWriter, r *http.Request) { | |
|
||
model, err := h.storage.Load(code) | ||
if err != nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Write([]byte("URL Not Found")) | ||
data, err := ioutil.ReadFile("frontend/index.html") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use there http.ServeFile(). So you don't need to handle errors etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh! I didn't know about that... thanks for pointing that out. I will check this one also. |
||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte("Internal Server Error.")) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(data) | ||
return | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You start there by using ECMA6, so why not using directly the new Fetch API which was introduced in ECMA6?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxibanki Good point, thanks for bringing this up!
I'm fairly new to JS so I don't know all the tools yet. I will take a look and see how to make this change.