This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·69 lines (54 loc) · 1.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package mem
import (
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"html/template"
"net/http"
"strings"
)
var tpl *template.Template
func init() {
tpl, _ = template.ParseGlob("templates/*.html")
http.HandleFunc("/", index)
http.HandleFunc("/login", login)
http.HandleFunc("/logout", logout)
http.Handle("/favicon.ico", http.NotFoundHandler())
fs := http.FileServer(http.Dir("assets"))
http.Handle("/imgs/", fs)
http.ListenAndServe(":8080", nil)
}
func index(res http.ResponseWriter, req *http.Request) {
cookie := genCookie(res, req)
if req.Method == "POST" {
src, hdr, err := req.FormFile("data")
if err != nil {
ctx := appengine.NewContext(req)
log.Errorf(ctx, "error uploading photo: %s", err)
// TODO: create error page to show user
}
cookie = uploadPhoto(src, hdr, cookie, req)
http.SetCookie(res, cookie)
}
m := Model(cookie, req)
tpl.ExecuteTemplate(res, "index.html", m)
}
func logout(res http.ResponseWriter, req *http.Request) {
cookie := newVisitor(req)
http.SetCookie(res, cookie)
http.Redirect(res, req, "/", 302)
}
func login(res http.ResponseWriter, req *http.Request) {
cookie := genCookie(res, req)
if req.Method == "POST" && req.FormValue("password") == "secret" {
m := Model(cookie, req)
m.State = true
m.Name = req.FormValue("name")
xs := strings.Split(cookie.Value, "|")
id := xs[0]
cookie := currentVisitor(m, id, req)
http.SetCookie(res, cookie)
http.Redirect(res, req, "/", 302)
return
}
tpl.ExecuteTemplate(res, "login.html", nil)
}