From 984fa97c4c5220f40873732b950a511649d94962 Mon Sep 17 00:00:00 2001 From: kreativmonkey Date: Fri, 12 May 2017 22:55:10 +0200 Subject: [PATCH] Change some logigs --- handlers.go | 78 +++++++++++++++++++++++++++++++ routs.go | 51 ++++++++++++++++++++ template/css/app.css | 82 ++++++++++++++++++++++++++++++++ template/img/github.png | Bin 0 -> 4090 bytes template/img/github.svg | 101 ++++++++++++++++++++++++++++++++++++++++ template/index.gohtml | 63 ++++++------------------- template/short.gohtml | 27 +++++++++++ 7 files changed, 353 insertions(+), 49 deletions(-) create mode 100644 handlers.go create mode 100644 routs.go create mode 100644 template/css/app.css create mode 100644 template/img/github.png create mode 100644 template/img/github.svg create mode 100644 template/short.gohtml 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 0000000000000000000000000000000000000000..efd7deea24822d6f0a366fb645e1c8b8eeaf232a GIT binary patch literal 4090 zcmV}GE7%+4nHJjwIyIrE>S%k>ZpbG_B73Kgf0xb%M2y9ar1O^4(R>*>GLFHvJ`icbda&ueTs~b0N9G>p) zRScQ#3`|CQdwW}RIQ)KK83fBfjsrp#O%w#42l*NBvz__;69WSSS&RBsXc*@#Y;W(( zN6bf2xd?bS5U!*7Jn%H44~BEWcee}-Wa?;pN~YC0U0q$XayxgP2YM;+0U$D+`57ZX zc}fiT4d;XV`}_O1Pgh2!#W}H9tlb2`wTfN`%$trplt@;EZ#CtEuWs(|_v&rDuFi=@ zqfXe!rvoe}s)x-QF)ZjEIhC*Y zzCT!NV>Ne9%yG^W;Z9&dt!>qjH0T#nUf+GSG*UC?Bsw~}HEdR*oKY(q(~T&rMfIBA zzP{ILqF)QqL}&Cu4P+I<+*;^27+F!SO8Wl&wuP-ar>m=L*2wVCEuvg!Q+H+{|DwiR zl}@L3SW~nm6pO_cOHgXm=)*Dcij^M^hZF44ZRu8Rt@yL zbYI_-)e4zv1!9hKMu4v2pku~4RE(@nxb8XC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 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