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
·111 lines (95 loc) · 2.78 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package mem
import (
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"html/template"
"net/http"
)
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) {
ctx := appengine.NewContext(req)
cookie, err := getCookie(res, req)
if err != nil {
// problem retrieving cookie
log.Errorf(ctx, "ERROR index getCookie: %s", err)
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
id := cookie.Value
if req.Method == "POST" {
src, _, err := req.FormFile("data")
if err != nil {
log.Errorf(ctx, "ERROR index req.FormFile: %s", err)
// TODO: create error page to show user
http.Redirect(res, req, "/", http.StatusSeeOther)
return
}
err = uploadPhoto(src, id, req)
if err != nil {
log.Errorf(ctx, "ERROR index uploadPhoto: %s", err)
// expired cookie may exist on client
http.Redirect(res, req, "/logout", http.StatusSeeOther)
return
}
}
m, err := retrieveMemc(id, req)
if err != nil {
log.Errorf(ctx, "ERROR index retrieveMemc: %s", err)
// expired cookie may exist on client
http.Redirect(res, req, "/logout", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(res, "index.html", m)
}
func logout(res http.ResponseWriter, req *http.Request) {
cookie, err := newVisitor(req)
if err != nil {
ctx := appengine.NewContext(req)
log.Errorf(ctx, "ERROR logout getCookie: %s", err)
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
http.SetCookie(res, cookie)
http.Redirect(res, req, "/", http.StatusSeeOther)
}
func login(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
cookie, err := getCookie(res, req)
if err != nil {
log.Errorf(ctx, "ERROR login getCookie: %s", err)
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
id := cookie.Value
if req.Method == "POST" && req.FormValue("password") == "secret" {
m, err := retrieveMemc(id, req)
if err != nil {
log.Errorf(ctx, "ERROR index retrieveMemc: %s", err)
// expired cookie may exist on client
http.Redirect(res, req, "/logout", http.StatusSeeOther)
return
}
m.State = true
m.Name = req.FormValue("name")
cookie, err := currentVisitor(m, id, req)
if err != nil {
log.Errorf(ctx, "ERROR login currentVisitor: %s", err)
http.Redirect(res, req, "/", http.StatusSeeOther)
return
}
http.SetCookie(res, cookie)
http.Redirect(res, req, "/", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(res, "login.html", nil)
}