-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.go
55 lines (49 loc) · 980 Bytes
/
home.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
package main
import (
"html/template"
"net/http"
)
func home(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
cookies := r.Cookies()
var tokenCookie *http.Cookie
for _, cookie := range cookies {
if cookie.Name == "token" {
tokenCookie = cookie
break
}
}
if tokenCookie == nil {
http.Redirect(w, r, "/register", http.StatusSeeOther)
return
}
buf, err := pages.ReadFile("pages/home.html")
if err != nil {
logError(err)
giveError(w, err)
return
}
var body string = string(buf)
tmpl, err := template.New("home").Parse(body)
if err != nil {
logError(err)
giveError(w, err)
return
}
user, err := userFromToken(tokenCookie.Value, permUser)
if err != nil {
logError(err)
giveError(w, err)
return
}
if err := tmpl.Execute(w, user); err != nil {
logError(err)
giveError(w, err)
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
}