-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdyp.go
163 lines (144 loc) · 3.36 KB
/
hdyp.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/user"
)
var t *template.Template
var staticService = http.FileServer(http.Dir("www"))
func init() {
t = template.Must(template.ParseGlob("template/*.html"))
http.HandleFunc("/", handle)
http.HandleFunc("/login", handleLogin)
http.HandleFunc("/logout", handleLogout)
http.HandleFunc("/_ah/start", serverStart)
}
func main() {
appengine.Main()
}
func serverStart(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
if appengine.IsDevAppServer() {
if !GetSettingBool(c, "alreadyInit") {
if err := loadSomeData(c); err != nil {
log.Printf(err.Error())
} else {
SetSettingBool(c, "alreadyInit", true)
fmt.Fprint(w, "OK")
return
}
}
}
fmt.Fprint(w, "OK")
return
}
func notFound(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
t.ExecuteTemplate(w, "404.html", map[string]interface{}{
"user": u,
})
}
func handle(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if r.URL.Path == "/" {
t.ExecuteTemplate(w, "index.html", map[string]interface{}{
"user": u,
})
return
}
if strings.Contains(r.URL.Path, ".") {
stat, err := os.Stat("www" + r.URL.Path)
if err != nil || stat.IsDir() || os.IsNotExist(err) {
// FIXME: probably handle that err not so blindly...
notFound(w, r)
return
}
staticService.ServeHTTP(w, r)
return
}
if n := strings.Count(r.URL.Path, "/"); n > 1 {
notFound(w, r)
return
}
reqWord := strings.TrimLeft(r.URL.Path, "/")
if reqWord != strings.ToLower(reqWord) {
// it's not all lowercase, so redirect to the canonical path, which is all lowercase
http.Redirect(w, r, "/"+strings.ToLower(reqWord), http.StatusFound)
return
}
word, err := GetWord(c, reqWord)
if err != nil {
if err == datastore.ErrNoSuchEntity {
t.ExecuteTemplate(w, "new.html", map[string]interface{}{
"user": u,
"word": Word{
Word: reqWord,
},
})
return
}
notFound(w, r)
return
}
if word != nil {
t.ExecuteTemplate(w, "word.html", map[string]interface{}{
"user": u,
"word": word,
"noautofocus": true,
})
return
}
notFound(w, r)
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
redir := r.URL.Query().Get("r")
if redir == "" {
redir = r.Referer()
if redir == "" {
redir = "/"
}
}
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
loginURL, err := user.LoginURL(c, redir)
if err != nil {
// something bad happened? FIXME: handle this error some other way
fmt.Fprint(w, err.Error())
return
}
http.Redirect(w, r, loginURL, http.StatusSeeOther)
return
}
http.Redirect(w, r, redir, http.StatusSeeOther)
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
redir := r.URL.Query().Get("r")
if redir == "" {
redir = r.Referer()
if redir == "" {
redir = "/"
}
}
c := appengine.NewContext(r)
u := user.Current(c)
if u != nil {
logoutURL, err := user.LogoutURL(c, redir)
if err != nil {
// something bad happened? FIXME: handle this error some other way
fmt.Fprint(w, err.Error())
return
}
http.Redirect(w, r, logoutURL, http.StatusSeeOther)
return
}
http.Redirect(w, r, redir, http.StatusSeeOther)
}