From 704df64d6f17bbd4aadda8c7d512711548fc7a1b Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Sat, 17 Mar 2018 02:52:25 +0100
Subject: [PATCH] Introduced templating

---
 Dockerfile               |  2 +-
 main.go                  | 93 +++++++++++++++++++++++++++++++++++++---
 static/index.html        |  1 -
 static/index.pug         |  3 --
 static/upload/index.html |  1 -
 static/upload/index.pug  |  4 --
 templates/index          | 10 +++++
 templates/me_images      | 14 ++++++
 templates/upload         | 13 ++++++
 9 files changed, 125 insertions(+), 16 deletions(-)
 delete mode 100644 static/index.html
 delete mode 100644 static/index.pug
 delete mode 100644 static/upload/index.html
 delete mode 100644 static/upload/index.pug
 create mode 100644 templates/index
 create mode 100644 templates/me_images
 create mode 100644 templates/upload

diff --git a/Dockerfile b/Dockerfile
index e5fb44d..5bf0da9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -11,5 +11,5 @@ RUN CGO_ENABLED=false go build -a app .
 FROM alpine:3.7
 WORKDIR /
 COPY --from=builder /go/src/app/app /app
-COPY --from=builder /go/src/app/static /static
+COPY --from=builder /go/src/app/templates /templates
 CMD ["/app"]
\ No newline at end of file
diff --git a/main.go b/main.go
index bdb85ae..d89ea0f 100644
--- a/main.go
+++ b/main.go
@@ -12,6 +12,8 @@ import (
 	"crypto/rand"
 	"time"
 	"database/sql"
+	_ "github.com/lib/pq"
+	"html/template"
 )
 
 func writeBody(reader io.ReadCloser, path string) error {
@@ -95,6 +97,20 @@ func printHeaders(r *http.Request) {
 	}
 }
 
+type UserInfo struct {
+	Id    string
+	Name  string
+	Email string
+}
+
+func parseUser(r *http.Request) UserInfo {
+	return UserInfo{
+		r.Header.Get("X-Auth-Subject"),
+		r.Header.Get("X-Auth-Username"),
+		r.Header.Get("X-Auth-Email"),
+	}
+}
+
 func main() {
 	config := NewConfigFromEnv()
 
@@ -108,12 +124,12 @@ func main() {
 		panic(err)
 	}
 
-	staticServer := http.FileServer(http.Dir("static/"))
 	imageServer := http.FileServer(http.Dir(config.TargetFolder))
 
 	http.HandleFunc("/upload/", func(w http.ResponseWriter, r *http.Request) {
-		user := r.Header.Get("X-Auth-Id")
 		if r.Method == "POST" {
+			user := parseUser(r)
+
 			r.ParseMultipartForm(32 << 20)
 			file, _, err := r.FormFile("file")
 			image, err := createImage(&config, file)
@@ -126,7 +142,7 @@ func main() {
 				return
 			}
 
-			_, err = db.Exec("INSERT INTO images (id, owner) VALUES ($1, $2)", image.Id, user)
+			_, err = db.Exec("INSERT INTO images (id, owner) VALUES ($1, $2)", image.Id, user.Id)
 			if err != nil {
 				panic(err)
 			}
@@ -181,14 +197,79 @@ func main() {
 				}
 			}
 		} else {
-			staticServer.ServeHTTP(w, r)
+			user := parseUser(r)
+
+			type UploadData struct {
+				User UserInfo
+			}
+
+			tmpl, err := template.New("upload").ParseFiles("templates/upload")
+			if err != nil {
+				panic(err)
+			}
+			err = tmpl.Execute(w, UploadData{
+				user,
+			})
+			if err != nil {
+				panic(err)
+			}
+		}
+	})
+
+	http.HandleFunc("/me/images/", func(w http.ResponseWriter, r *http.Request) {
+		user := parseUser(r)
+
+		type ImageListData struct {
+			User   UserInfo
+			Images []string
+		}
+
+		result, err := db.Query("SELECT id FROM images WHERE owner = $1", user.Id)
+		if err != nil {
+			panic(err)
+		}
+
+		var images []string
+		for result.Next() {
+			var id string
+			err := result.Scan(&id)
+			if err != nil {
+				panic(err)
+			}
+			images = append(images, id)
+		}
+
+		tmpl, err := template.New("me_images").ParseFiles("templates/me_images")
+		if err != nil {
+			panic(err)
+		}
+		err = tmpl.Execute(w, ImageListData{
+			user,
+			images,
+		})
+		if err != nil {
+			panic(err)
 		}
 	})
 
 	http.Handle("/i/", http.StripPrefix("/i/", imageServer))
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-		printHeaders(r)
-		staticServer.ServeHTTP(w, r)
+		user := parseUser(r)
+
+		type IndexData struct {
+			User UserInfo
+		}
+
+		tmpl, err := template.New("index").ParseFiles("templates/index")
+		if err != nil {
+			panic(err)
+		}
+		err = tmpl.Execute(w, IndexData{
+			user,
+		})
+		if err != nil {
+			panic(err)
+		}
 	})
 
 	err = http.ListenAndServe(":8080", nil)
diff --git a/static/index.html b/static/index.html
deleted file mode 100644
index 8025f96..0000000
--- a/static/index.html
+++ /dev/null
@@ -1 +0,0 @@
-<html><a href="/upload/">Upload images</a></html>
\ No newline at end of file
diff --git a/static/index.pug b/static/index.pug
deleted file mode 100644
index a9efe59..0000000
--- a/static/index.pug
+++ /dev/null
@@ -1,3 +0,0 @@
-html
-    a(href="/upload/").
-        Upload images
\ No newline at end of file
diff --git a/static/upload/index.html b/static/upload/index.html
deleted file mode 100644
index 01cb744..0000000
--- a/static/upload/index.html
+++ /dev/null
@@ -1 +0,0 @@
-<html><form action="/upload/" method="POST" enctype="multipart/form-data"><input type="file" name="file"/><input type="submit"/></form></html>
\ No newline at end of file
diff --git a/static/upload/index.pug b/static/upload/index.pug
deleted file mode 100644
index 00c790b..0000000
--- a/static/upload/index.pug
+++ /dev/null
@@ -1,4 +0,0 @@
-html
-    form(action="/upload/", method="POST" enctype="multipart/form-data")
-        input(type="file", name="file")
-        input(type="submit")
\ No newline at end of file
diff --git a/templates/index b/templates/index
new file mode 100644
index 0000000..8585807
--- /dev/null
+++ b/templates/index
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+
+{{ if .User.Id }}
+    <p>Welcome, {{ .User.Name }}</p>
+    <p>
+        <a href="/me/images">My Images</a> |
+        <a href="/me/albums">My Albums</a> |
+        <a href="/upload">Upload</a>
+    </p>
+{{ end }}
\ No newline at end of file
diff --git a/templates/me_images b/templates/me_images
new file mode 100644
index 0000000..53061dd
--- /dev/null
+++ b/templates/me_images
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+
+<p>Welcome, {{ .User.Name }}</p>
+<p>
+    <a href="/me/images">My Images</a> |
+    <a href="/me/albums">My Albums</a> |
+    <a href="/upload">Upload</a>
+</p>
+
+{{ range .Images }}
+    <a href="/i/{{ . }}">
+        <img src="/i/{{ . }}t" >
+    </a>
+{{ end }}
\ No newline at end of file
diff --git a/templates/upload b/templates/upload
new file mode 100644
index 0000000..7a4f1d0
--- /dev/null
+++ b/templates/upload
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+
+<p>Welcome, {{ .User.Name }}</p>
+<p>
+    <a href="/me/images">My Images</a> |
+    <a href="/me/albums">My Albums</a> |
+    <a href="/upload">Upload</a>
+</p>
+
+<form action="/upload/" method="POST" enctype="multipart/form-data">
+    <input type="file" name="file"/>
+    <input type="submit"/>
+</form>
\ No newline at end of file
-- 
GitLab