/
main.go
executable file
·36 lines (31 loc) · 937 Bytes
/
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
package main
import (
"fmt"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/user"
)
func init() {
http.HandleFunc("/", index)
http.HandleFunc("/admin/", admin)
}
func index(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
u := user.Current(ctx)
url, _ := user.LogoutURL(ctx, "/")
res.Header().Set("Content-Type", "text/html")
fmt.Fprintf(res, `Welcome, %s! <br>`, u.Email)
fmt.Fprintf(res, `You are admin: %v <br>`, u.Admin)
if u.Admin {
fmt.Fprint(res, `(<a href="/admin">go to admin</a>) <br>`)
}
fmt.Fprintf(res, `(<a href="%s">sign out</a>)`, url)
}
func admin(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
u := user.Current(ctx)
url, _ := user.LogoutURL(ctx, "/")
res.Header().Set("Content-Type", "text/html")
fmt.Fprintf(res, `Welcome ADMIN, %s! <br>`, u.Email)
fmt.Fprintf(res, `(<a href="%s">sign out</a>)`, url)
}