Skip to content
Snippets Groups Projects
Commit 704df64d authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Introduced templating

parent 4952cd0d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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)
......
<html><a href="/upload/">Upload images</a></html>
\ No newline at end of file
html
a(href="/upload/").
Upload images
\ No newline at end of file
<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
html
form(action="/upload/", method="POST" enctype="multipart/form-data")
input(type="file", name="file")
input(type="submit")
\ No newline at end of file
<!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
<!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
<!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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment