diff --git a/Dockerfile b/Dockerfile index 4d55b14..623fc26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.12.4 as builder +FROM golang:1.12.9 as builder ENV CGO_ENABLED=0 diff --git a/main.go b/main.go index 54e1933..98b310f 100644 --- a/main.go +++ b/main.go @@ -22,34 +22,48 @@ import ( "github.com/patrickmn/go-cache" ) -const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Base strings for RandStringBytesMaskImprSrc +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Base strings for randStringBytesMaskImprSrc const ( letterIdxBits = 6 // 6 bits to represent a letter index letterIdxMask = 1<= 0; { @@ -130,9 +145,10 @@ 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") log.Println(err) w.WriteHeader(http.StatusInternalServerError) - internalErrorTmpl.Execute(w, msg+err.Error()) + t.Execute(w, msg+err.Error()) } // itemsCount returns the number of kv pairs on the in meomry database @@ -161,6 +177,7 @@ 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) @@ -168,12 +185,13 @@ func itemsFromFile(w http.ResponseWriter, r *http.Request, dumpFile string) { internalError(w, "Cannot open file "+dumpFile+": ", err) } else { pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj) - okTmpl.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB") + t.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB") } } // 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) @@ -181,12 +199,13 @@ func itemsFromPost(w http.ResponseWriter, r *http.Request) { internalError(w, "Cannot parse JSON: ", err) } else { pool = cache.NewFrom(240*time.Hour, 1*time.Hour, dumpObj) - okTmpl.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB") + t.Execute(w, "Imported "+strconv.Itoa(len(dumpObj))+" items to the DB") } } // 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(), ) @@ -194,7 +213,7 @@ func itemsDumpToFile(w http.ResponseWriter, r *http.Request, dumpFile string) { if err != nil { internalError(w, "Failed to open json file: ", err) } else { - okTmpl.Execute(w, "Dump writen to: "+dumpFile) + t.Execute(w, "Dump writen to: "+dumpFile) } } @@ -218,7 +237,7 @@ func main() { } if *port > 65535 || *port < 1 { - + log.Fatalln("Invalid port number") } if *path != "" && !strings.HasSuffix(*path, "/") { *path = *path + "/" diff --git a/templates/400.html b/templates/400.html index 3837895..0184d44 100644 --- a/templates/400.html +++ b/templates/400.html @@ -1,293 +1,40 @@ - - Short: the simple url shortener - - - -
-
-

400

-

Bad Request

-
-
-
-
-
-
-
+ {{ 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

+
-
-
-
-

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

- -
- - \ No newline at end of file + {{ template "footer" }} + + \ No newline at end of file diff --git a/templates/404.html b/templates/404.html index a1b8efd..be7cd44 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,293 +1,41 @@ - - Short: the simple url shortener - - - -
-
-

404

-

page not found

-
-
-
-
-
-
-
+ +
+
+

404

+

page not found

-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

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

+
-
-
-
-

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

- -
- - \ No newline at end of file + {{ template "footer" }} + + \ No newline at end of file diff --git a/templates/500.html b/templates/500.html index 615282d..df07b3a 100644 --- a/templates/500.html +++ b/templates/500.html @@ -1,287 +1,35 @@ - - Short: the simple url shortener - - - -
-
-

500

-

Internal server error

-
-
-
-
-
-
-
+ +
+
+

500

+

Internal server error

-
-
-

X

-

X

-
+
+
+
+
+
+
+
+
+

X

+

X

+
+
+
+
+
+
+

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

-
-
-
-

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

-
- - \ No newline at end of file + {{ template "footer" }} + + \ No newline at end of file diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..9e1b755 --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,5 @@ +{{ define "footer" }} + +{{ end }} \ No newline at end of file diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..6b2bb2a --- /dev/null +++ b/templates/header.html @@ -0,0 +1,282 @@ +{{ define "header" }} + + Short: the simple url shortener + + +{{ end }} diff --git a/templates/index.html b/templates/index.html index 866a0bb..b4e2f7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,87 +1,18 @@ - - Short: the simple url shortener - - - -
-

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

- -
- + {{ 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 index 15ba75b..ef328cc 100644 --- a/templates/ok.html +++ b/templates/ok.html @@ -1,33 +1,10 @@ - - Short: the simple url shortner - - - -
-

{{ . }}

-
- + {{ template "slimheader" }} + +
+

{{ . }}

+
+ {{ template "footer" }} + \ No newline at end of file diff --git a/templates/returnPage.html b/templates/returnPage.html index 748825a..f1bcfc0 100644 --- a/templates/returnPage.html +++ b/templates/returnPage.html @@ -1,33 +1,10 @@ - - Short: the simple url shortner - - - -
-

URL Shortened to {{ . }}

-
- + {{ template "slimheader" }} + +
+

URL Shortened to {{ . }}

+
+ {{ template "footer" }} + \ No newline at end of file diff --git a/templates/slimheader.html b/templates/slimheader.html new file mode 100644 index 0000000..21e877e --- /dev/null +++ b/templates/slimheader.html @@ -0,0 +1,72 @@ +{{ define "slimheader" }} + + Short: the simple url shortener + + +{{ end }} \ No newline at end of file