diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..0c064ad --- /dev/null +++ b/handlers.go @@ -0,0 +1,78 @@ +package main + +import ( + "net/http" + "encoding/json" + "fmt" + "text/template" +) + +type Page struct { + Host string + Title string + Body string +} + +// Load the main Page with an Imput field for the URL to short. +func index(w http.ResponseWriter, r *http.Request) { + t, _ := template.ParseFiles("template/index.gohtml") + t.Execute(w, Page{Title: "Shrt"}) +} + +// Redirect to the URL behind the requested token. +func redirect(w http.ResponseWriter, r *http.Request){ + if redirect, ok := short.Get(r.URL.Path[1:]); ok{ + http.Redirect(w, r, string(redirect), 301) + fmt.Printf("Token: %s Redirect to: %s \n", string(r.URL.Path[1:]), redirect) + } else { + http.Redirect(w, r, "/", 301) + } +} + +// +func shorten(w http.ResponseWriter, r *http.Request){ + var test string + token, err := short.Add(r.FormValue("url"), &test) + if err != nil { + panic(err) + } + P := Page{Title: "Url:", Body: r.Host + "/" + token} + t, _ := template.ParseFiles("template/short.gohtml") + t.Execute(w, P) +} + +// GET: http://example.com/api/action/shorten?key=API_KEY_HERE&url=https://google.com&custom_ending=CUSTOM_ENDING +// Response: {"action": "shorten","result": "https://example.com/5kq"} +func shortenJSON(w http.ResponseWriter, r *http.Request){ + decoder := json.NewDecoder(r.Body) + var response shrt + err := decoder.Decode(&response) + if err != nil { + panic(err) + } + defer r.Body.Close() + fmt.Println(response) + /*response := map[string]string{ + "action": "shorten", + "result": "", + }*/ +} + +// GET: http://example.com/api/action/lookup?key=API_KEY_HERE&url_ending=5kq +// Response: {"action":"lookup","result": {"long_url": "https:\/\/google.com","created_at": {"date":"2016-02-12 15:20:34.000000","timezone_type":3,"timezone":"UTC"},"clicks":"0"}} +func lookup(w http.ResponseWriter, r *http.Request){ + + /*response := map[string]interface{}{ + "action": "lookup", + "result": map[string]string{ + "long_url": "", + "created_at": "", + "clicks": "", + } + }*/ +} + +func all(w http.ResponseWriter, r *http.Request){ + t, _ := template.New("all").Parse("{{.}}") + t.Execute(w, short.All()) +} \ No newline at end of file diff --git a/routs.go b/routs.go new file mode 100644 index 0000000..ff58917 --- /dev/null +++ b/routs.go @@ -0,0 +1,51 @@ +package main + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +type Route struct { + Name string + Method string + Pattern string + HandlerFunc http.HandlerFunc +} + +type Routes []Route + +func NewRouter() *mux.Router { + + router := mux.NewRouter().StrictSlash(true) + for _, route := range routes { + router. + Methods(route.Method). + Path(route.Pattern). + Name(route.Name). + Handler(route.HandlerFunc) + } + + return router +} + +var routes = Routes{ + Route{ + "Index", + "GET", + "/", + index, + }, + Route{ + "TodoIndex", + "POST", + "/shorten", + shorten, + }, + Route{ + "TodoShow", + "GET", + "/{token}", + lookup, + }, +} \ No newline at end of file diff --git a/template/css/app.css b/template/css/app.css new file mode 100644 index 0000000..8e6c518 --- /dev/null +++ b/template/css/app.css @@ -0,0 +1,82 @@ +body { + margin: 0px; +} +main { + height: 85vh; + display: flex; + flex-direction: column; + justify-content: center; +} +#container{ + max-width: 1170px; + width: 100%; + margin: 0 auto; + text-align: center; +} +footer { + position: absolute; + bottom: 2em; + font-size: 1.2em; + text-align: center; + width: 100%; +} +h1 { + font-size: 5em; + line-height: 1; + margin: 21px 0 10px 0; +} +form { + display: block; + margin-top: 0em; +} +form input { + padding: 7px; + font-size: 1em; + display: block; + width: 100%; + magin: 1em -7px; +} +form button { + display: inline-block; + margin-bottom: 0; + font-size: 1em; + font-weight: normal; + line-height: 1.428571429; + text-align: center; + white-space: nowrap; + vertical-align: middle; + background-image: none; + border: 1px solid transparent; + border-radius: 0; + color: #fff; + padding: 14px 28px; + background-color: #39b3d7; + border-color: #269abc; +} +input { + padding: 0.7em; + font-size: 1em; + display: block; + width: 100%; + margin-bottom: 1em; +} +button { + display: inline-block; + margin-bottom: 0; + font-size: 1em; + font-weight: normal; + line-height: 1.428571429; + text-align: center; + white-space: nowrap; + vertical-align: middle; + background-image: none; + border: 1px solid transparent; + border-radius: 0; + color: #fff; + padding: 14px 28px; + background-color: #39b3d7; + border-color: #269abc; +} +footer img { + height: 2em; +} \ No newline at end of file diff --git a/template/img/github.png b/template/img/github.png new file mode 100644 index 0000000..efd7dee Binary files /dev/null and b/template/img/github.png differ diff --git a/template/img/github.svg b/template/img/github.svg new file mode 100644 index 0000000..ba1a7c0 --- /dev/null +++ b/template/img/github.svg @@ -0,0 +1,101 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/template/index.gohtml b/template/index.gohtml index e03f918..8ee0b73 100644 --- a/template/index.gohtml +++ b/template/index.gohtml @@ -3,61 +3,26 @@ Shrt - + -
+
+

{{.Title}}

-
- + +
+
+ \ No newline at end of file diff --git a/template/short.gohtml b/template/short.gohtml new file mode 100644 index 0000000..3732cd2 --- /dev/null +++ b/template/short.gohtml @@ -0,0 +1,27 @@ + + + + + Shrt + + + + +
+
+

{{.Title}}

+ + +
+
+ + + + \ No newline at end of file