/
glog.go
72 lines (64 loc) · 1.58 KB
/
glog.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
package glog
import (
"appengine"
"appengine/user"
"fmt"
"html/template"
"net/http"
)
var templates = template.Must(template.ParseGlob("static/tmpl/*"))
type AdminPage struct {
Admin_email string
LogoutUrl string
}
func init() {
http.HandleFunc("/", Index)
http.HandleFunc("/admin", Admin)
}
func Index(w http.ResponseWriter, r *http.Request) {
// err := templates.ExecuteTemplate(w, "indexPage", nil)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
w.Header().Set("Content-type", "text/html; charset=utf-8")
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, _ := user.LoginURL(c, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
url, _ := user.LogoutURL(c, "/")
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
}
func Admin(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
an := AdminPage{"", ""}
var templ = ""
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
c.Debugf("User present %v", u)
an.Admin_email = u.Email
templ = "adminIndex"
url, err := user.LogoutURL(c, "/admin")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
an.LogoutUrl = url
err = templates.ExecuteTemplate(w, templ, an)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}