From 89aa61cca21345cd1fd70ce72fe9f33ba82954be Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Thu, 19 Sep 2019 16:23:04 +0200 Subject: [PATCH 1/3] Switch to using a single conditional template --- main.go | 107 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/main.go b/main.go index 98b310f..06a1cb8 100644 --- a/main.go +++ b/main.go @@ -32,29 +32,26 @@ const appVersion = "1.1.0" var src = rand.NewSource(time.Now().UnixNano()) var pool = cache.New(240*time.Hour, 1*time.Hour) -var templates *template.Template -var allFiles []string +var t = template.Must(template.ParseFiles("templates/response.html")) -func init() { - files, err := ioutil.ReadDir("templates") - if err != nil { - log.Fatalln(err) - } - for _, file := range files { - filename := file.Name() - if strings.HasSuffix(filename, ".html") { - allFiles = append(allFiles, "templates/"+filename) - } - } - templates, err = template.ParseFiles(allFiles...) - if err != nil { - log.Fatalln(err) - } +type body struct { + FullHeader bool + IsGhost bool + HasForm bool + IsLink bool + H1 string + H3 string + Line1 string + Line2 string } func index(w http.ResponseWriter, r *http.Request) { - t := templates.Lookup("index.html") - t.Execute(w, nil) + b := body{ + HasForm: true, + Line1: "Welcome to Short, the simple URL shortener,", + Line2: "Type an URL below to shorten it", + } + t.Execute(w, b) } // get executes the GET command @@ -77,7 +74,15 @@ func set(key, suffix string) { func redirect(w http.ResponseWriter, r *http.Request, path string) { vals := mux.Vars(r) key := vals["key"] - t := templates.Lookup("404.html") + b := body{ + FullHeader: true, + IsGhost: true, + HasForm: true, + H1: "404", + H3: "page not found", + Line1: "Boo, looks like this ghost stole this page!", + Line2: "But you can type an URL below to shorten it", + } if path != "" { key = strings.Replace(key, path, "", 1) } @@ -86,7 +91,7 @@ func redirect(w http.ResponseWriter, r *http.Request, path string) { key, status := get(key) if !status { w.WriteHeader(http.StatusNotFound) - t.Execute(w, nil) + t.Execute(w, b) return } u, _ := url.Parse(key) @@ -102,11 +107,18 @@ func redirect(w http.ResponseWriter, r *http.Request, path string) { // then if writes the kv pair suffix, url to the database and return the // shortened url to the user func shortner(w http.ResponseWriter, r *http.Request, proto, domain, hostSuf, path string, urlSize int) { - ret := templates.Lookup("returnPage.html") - badR := templates.Lookup("400.html") if !govalidator.IsURL(r.FormValue("url")) { + b := body{ + FullHeader: true, + IsGhost: true, + HasForm: true, + H1: "400", + H3: "bad request", + Line1: "Boo, looks like this ghost stole this page!", + Line2: "But you can type an URL below to shorten it", + } w.WriteHeader(http.StatusBadRequest) - badR.Execute(w, nil) + t.Execute(w, b) return } u, _ := url.Parse(r.FormValue("url")) @@ -121,7 +133,11 @@ func shortner(w http.ResponseWriter, r *http.Request, proto, domain, hostSuf, pa } set(u.String(), suffix) shortend := proto + "://" + domain + hostSuf + path + suffix - ret.Execute(w, shortend) + b := body{ + IsLink: true, + Line1: shortend, + } + t.Execute(w, b) } // randStringBytesMaskImprSrc Generate random string of n size @@ -145,10 +161,18 @@ func randStringBytesMaskImprSrc(n int) string { // internalError receives a http.ResponseWriter, msg and error and // return a internal error page with http code 500 to the user func internalError(w http.ResponseWriter, msg string, err error) { - t := templates.Lookup("500.html") + b := body{ + FullHeader: true, + IsGhost: true, + HasForm: true, + H1: "500", + H3: "internal erver error", + Line1: "Boo, the ghost is broken :(", + Line2: "His last words where: " + err.Error(), + } log.Println(err) w.WriteHeader(http.StatusInternalServerError) - t.Execute(w, msg+err.Error()) + t.Execute(w, b) } // itemsCount returns the number of kv pairs on the in meomry database @@ -177,44 +201,51 @@ func itemsDump(w http.ResponseWriter, r *http.Request) { // itemsFromFile loads kv pairs from the dumpFile json to the in memory database func itemsFromFile(w http.ResponseWriter, r *http.Request, dumpFile string) { - t := templates.Lookup("ok.html") jsonFile, err := ioutil.ReadFile(dumpFile) var dumpObj map[string]cache.Item json.Unmarshal([]byte(jsonFile), &dumpObj) if err != nil { internalError(w, "Cannot open file "+dumpFile+": ", err) - } else { - pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj) - t.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB") + return } + pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj) + b := body{ + Line1: "Imported " + strconv.Itoa(len(dumpObj)) + " items to the DB", + } + t.Execute(w, b) } // itemsFromPost loads kv pairs from a json POST to the in memory database func itemsFromPost(w http.ResponseWriter, r *http.Request) { - t := templates.Lookup("ok.html") decoder := json.NewDecoder(r.Body) var dumpObj map[string]cache.Item err := decoder.Decode(&dumpObj) if err != nil { internalError(w, "Cannot parse JSON: ", err) - } else { - pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj) - t.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB") + return } + pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj) + b := body{ + Line1: "Imported " + strconv.Itoa(len(dumpObj)) + " items to the DB", + } + t.Execute(w, b) } // itemsDumpToFile dumps the kv pairs from the in memory database to the dumpFile func itemsDumpToFile(w http.ResponseWriter, r *http.Request, dumpFile string) { - t := templates.Lookup("ok.html") dumpObj, _ := json.Marshal( pool.Items(), ) err := ioutil.WriteFile(dumpFile, dumpObj, 0644) if err != nil { internalError(w, "Failed to open json file: ", err) - } else { - t.Execute(w, "Dump writen to: "+dumpFile) + return } + b := body{ + Line1: "Imported " + "Dump writen to: " + dumpFile, + } + t.Execute(w, b) + } func main() { From fd871ec411d56c04336033055fc03079107f7338 Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Thu, 19 Sep 2019 16:23:49 +0200 Subject: [PATCH 2/3] Delete old templates and add response.html that deals with all responses --- templates/400.html | 40 ------ templates/404.html | 41 ------ templates/500.html | 35 ----- templates/footer.html | 5 - templates/index.html | 18 --- templates/ok.html | 10 -- templates/{header.html => response.html} | 170 ++++++++++++++++------- templates/returnPage.html | 10 -- templates/slimheader.html | 72 ---------- 9 files changed, 122 insertions(+), 279 deletions(-) delete mode 100644 templates/400.html delete mode 100644 templates/404.html delete mode 100644 templates/500.html delete mode 100644 templates/footer.html delete mode 100644 templates/index.html delete mode 100644 templates/ok.html rename templates/{header.html => response.html} (73%) delete mode 100644 templates/returnPage.html delete mode 100644 templates/slimheader.html diff --git a/templates/400.html b/templates/400.html deleted file mode 100644 index 0184d44..0000000 --- a/templates/400.html +++ /dev/null @@ -1,40 +0,0 @@ - - - {{ template "header" }} - -
-
-

400

-

Bad Request

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

Boo, looks like a ghost didn't like your request! -
- But you can try again, type an URL below to shorten it

- -
- - {{ template "footer" }} - - \ No newline at end of file diff --git a/templates/404.html b/templates/404.html deleted file mode 100644 index be7cd44..0000000 --- a/templates/404.html +++ /dev/null @@ -1,41 +0,0 @@ - - - {{ template "header" }} - - -
-
-

404

-

page not found

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

Boo, looks like a ghost stole this page! -
- But you can type an URL below to shorten it

- -
- - {{ template "footer" }} - - \ No newline at end of file diff --git a/templates/500.html b/templates/500.html deleted file mode 100644 index df07b3a..0000000 --- a/templates/500.html +++ /dev/null @@ -1,35 +0,0 @@ - - - {{ template "header" }} - - -
-
-

500

-

Internal server error

-
-
-
-
-
-
-
-
-
-
-

X

-

X

-
-
-
-
-
-
-

Boo, the ghost is broken :( -
- His last words where: {{ . }}

-
- - {{ template "footer" }} - - \ No newline at end of file diff --git a/templates/footer.html b/templates/footer.html deleted file mode 100644 index 9e1b755..0000000 --- a/templates/footer.html +++ /dev/null @@ -1,5 +0,0 @@ -{{ define "footer" }} - -{{ end }} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html deleted file mode 100644 index b4e2f7b..0000000 --- a/templates/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - - {{ template "slimheader" }} - -
-

Welcome to Short, the simple URL shortener, -
- Type an URL below to shorten it

- -
- {{ template "footer" }} - - \ No newline at end of file diff --git a/templates/ok.html b/templates/ok.html deleted file mode 100644 index ef328cc..0000000 --- a/templates/ok.html +++ /dev/null @@ -1,10 +0,0 @@ - - - {{ template "slimheader" }} - -
-

{{ . }}

-
- {{ template "footer" }} - - \ No newline at end of file diff --git a/templates/header.html b/templates/response.html similarity index 73% rename from templates/header.html rename to templates/response.html index 6b2bb2a..3f0f8a7 100644 --- a/templates/header.html +++ b/templates/response.html @@ -1,4 +1,5 @@ -{{ define "header" }} + + Short: the simple url shortener -{{ end }} + + {{ if eq .IsGhost true }} +
+
+

{{ .H1 }}

+

{{ .H3 }}

+
+
+
+
+
+
+
+
+
+
+ {{ if or (eq .H1 "404") (eq .H1 "400" ) }} +
+
+ {{ else if eq .H1 "500" }} +

X

+

X

+ {{ end }} + {{ if eq .H1 "404" }} +
+ {{ else if or ( eq .H1 "400") (eq .H1 "500") }} +
+ {{ end }} +
+
+
+
+ {{ end }} +
+ {{ if eq .IsLink true }} +

URL Shortened to {{ .Line1 }}

+

+ {{ else }} +

{{ .Line1 }} +
+ {{ .Line2 }}

+ {{ end }} + {{ if eq .HasForm true }} + + {{ end }} +
+ + + \ No newline at end of file diff --git a/templates/returnPage.html b/templates/returnPage.html deleted file mode 100644 index f1bcfc0..0000000 --- a/templates/returnPage.html +++ /dev/null @@ -1,10 +0,0 @@ - - - {{ template "slimheader" }} - -
-

URL Shortened to {{ . }}

-
- {{ template "footer" }} - - \ No newline at end of file diff --git a/templates/slimheader.html b/templates/slimheader.html deleted file mode 100644 index 21e877e..0000000 --- a/templates/slimheader.html +++ /dev/null @@ -1,72 +0,0 @@ -{{ define "slimheader" }} - - Short: the simple url shortener - - -{{ end }} \ No newline at end of file From 4223049ccc0d12e420a5d2b7be4df59942e40492 Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Thu, 19 Sep 2019 16:25:58 +0200 Subject: [PATCH 3/3] Bump version from 1.1.0 to 1.2.0 --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 06a1cb8..15ecfe0 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ const ( letterIdxMask = 1<