-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (65 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"html/template"
"log"
"net/http"
)
var tpl *template.Template
const ERROR string = "err.gohtml"
const INDEX string = "index.gohtml"
const RESULT string = "result.gohtml"
const PORT string = ":8888"
type le struct {
Result string
}
var l = le{
Result: "asdf",
}
func init() {
tpl = template.Must(tpl.ParseGlob("templates/*.gohtml"))
}
func getURL(w http.ResponseWriter, req *http.Request) {
url := req.FormValue("url")
if req.Method != "POST" {
http.Redirect(w, req, "/", http.StatusSeeOther)
log.Println("asdfjkalsdhfkjasdlhf")
}
if len(url) > 0 {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}
resp, client_err := client.Get(url)
if client_err != nil {
log.Println("Link not correct.")
http.Redirect(w, req, "/", http.StatusSeeOther)
} else {
l = le{
Result: resp.Header.Get("Location"),
}
log.Printf("\"Location\": %s\n", resp.Header.Get("Location"))
}
}
}
func result(w http.ResponseWriter, req *http.Request) {
getURL(w, req)
tpl.ExecuteTemplate(w, RESULT, l)
}
func docRoot(w http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(w, INDEX, nil)
err := req.ParseForm()
if err != nil {
log.Fatal(err)
}
}
func err(w http.ResponseWriter, req *http.Request) {
tpl.ExecuteTemplate(w, ERROR, nil)
}
func main() {
http.HandleFunc("/", docRoot)
http.HandleFunc("/result", result)
http.HandleFunc("/err", err)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
log.Println("Server starting...")
log.Fatal(http.ListenAndServe(PORT, nil))
}