|
|
|
@ -9,9 +9,8 @@ import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Page struct {
|
|
|
|
|
Url string
|
|
|
|
|
Token string
|
|
|
|
|
Count int
|
|
|
|
|
Title string
|
|
|
|
|
Body string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
@ -27,21 +26,32 @@ func init() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
fmt.Println("method:", r.Method) //get request method
|
|
|
|
|
fmt.Println("method: ", r.Method) //get request method
|
|
|
|
|
fmt.Println("Path: ", r.URL.Path[1:])
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case "GET":
|
|
|
|
|
t, _ := template.ParseFiles("template/index.gohtml")
|
|
|
|
|
t.Execute(w, nil)
|
|
|
|
|
var redirect string
|
|
|
|
|
if ok := short.Get(r.URL.Path[1:], &redirect); ok{
|
|
|
|
|
P := Page{Title: ToString(r.URL.Path[1:]), Body: redirect}
|
|
|
|
|
fmt.Printf("Redirect: %s \n", redirect)
|
|
|
|
|
t, _ := template.ParseFiles("template/index.gohtml")
|
|
|
|
|
t.Execute(w, P)
|
|
|
|
|
fmt.Println("Url by Token:", P)
|
|
|
|
|
} else {
|
|
|
|
|
t, _ := template.ParseFiles("template/index.gohtml")
|
|
|
|
|
t.Execute(w, nil)
|
|
|
|
|
}
|
|
|
|
|
case "POST":
|
|
|
|
|
r.ParseForm()
|
|
|
|
|
token, err := short.Add(ToString(r.Form["url"]))
|
|
|
|
|
var test string
|
|
|
|
|
token, err := short.Add(ToString(r.Form["url"]), &test)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
P := Page{Url: ToString(r.Form["url"]), Token: token}
|
|
|
|
|
P := Page{Title: ToString(r.Form["url"]), Body: token}
|
|
|
|
|
t, _ := template.ParseFiles("template/index.gohtml")
|
|
|
|
|
t.Execute(w, P)
|
|
|
|
|
fmt.Println("url:", P)
|
|
|
|
|
fmt.Println("Token by Url:", P)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|