/
main.go
58 lines (52 loc) · 1.5 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
package web
import (
"html/template"
"net/http"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/user"
)
func init() {
tmpl, err := template.ParseGlob("templates/*.tmpl.html")
if err != nil {
http.Handle("/", errHandler(err.Error(), http.StatusInternalServerError))
return
}
http.Handle("/questions/", templateHandler(tmpl, "question"))
http.Handle("/", templateHandler(tmpl, "index"))
}
func templateHandler(tmpl *template.Template, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
if ok := ensureUser(ctx, w, r); !ok {
return
}
err := tmpl.ExecuteTemplate(w, name, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}
// errHandler gets an http.Handler that reports the specified
// error.
func errHandler(err string, code int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, err, code)
})
}
// ensureUser makes sure a user is logged in, or redirects the the
// login page.
// Returns true if a user is logged in.
func ensureUser(ctx context.Context, w http.ResponseWriter, r *http.Request) bool {
me := user.Current(ctx)
if me == nil {
loginURL, err := user.LoginURL(ctx, r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return false
}
http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect)
return false
}
return true
}