|
@@ -17,26 +17,39 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
|
17
|
17
|
return
|
18
|
18
|
}
|
19
|
19
|
|
20
|
|
- songs, err := loadDb()
|
21
|
|
- if err != nil {
|
22
|
|
- http.Error(w, err.Error(), http.StatusInternalServerError)
|
23
|
|
- return
|
24
|
|
- }
|
25
|
20
|
alsoEmptySongs := SongsFile{
|
26
|
21
|
Songs: make([]*SongEntry, 52),
|
27
|
22
|
}
|
28
|
23
|
for week := 1; week <= 52; week++ {
|
29
|
24
|
alsoEmptySongs.Songs[week-1] = &SongEntry{Week: week}
|
30
|
25
|
}
|
31
|
|
- for _, song := range songs.Songs {
|
32
|
|
- alsoEmptySongs.Songs[song.Week-1] = song
|
|
26
|
+ username := ""
|
|
27
|
+ session, err := getSession(r)
|
|
28
|
+ if session != nil {
|
|
29
|
+ songs, err := loadDb()
|
|
30
|
+ if err != nil {
|
|
31
|
+ http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
32
|
+ return
|
|
33
|
+ }
|
|
34
|
+ for _, song := range songs.Songs {
|
|
35
|
+ alsoEmptySongs.Songs[song.Week-1] = song
|
|
36
|
+ }
|
|
37
|
+ username = session.username
|
33
|
38
|
}
|
34
|
39
|
|
35
|
40
|
var templates = cachedTemplates
|
36
|
41
|
if true {
|
37
|
42
|
templates = template.Must(template.ParseFiles("web/index.html"))
|
38
|
43
|
}
|
39
|
|
- err = templates.ExecuteTemplate(w, "index.html", alsoEmptySongs)
|
|
44
|
+
|
|
45
|
+ data := struct {
|
|
46
|
+ Username string
|
|
47
|
+ Songs []*SongEntry
|
|
48
|
+ }{
|
|
49
|
+ username,
|
|
50
|
+ alsoEmptySongs.Songs,
|
|
51
|
+ }
|
|
52
|
+ err = templates.ExecuteTemplate(w, "index.html", data)
|
40
|
53
|
if err != nil {
|
41
|
54
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
42
|
55
|
}
|
|
@@ -47,6 +60,7 @@ func updateHandler(w http.ResponseWriter, r *http.Request) {
|
47
|
60
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
48
|
61
|
return
|
49
|
62
|
}
|
|
63
|
+
|
50
|
64
|
err := r.ParseForm()
|
51
|
65
|
if err != nil {
|
52
|
66
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
@@ -100,16 +114,41 @@ func updateHandler(w http.ResponseWriter, r *http.Request) {
|
100
|
114
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
101
|
115
|
}
|
102
|
116
|
|
|
117
|
+func loginHandler(w http.ResponseWriter, r *http.Request) {
|
|
118
|
+ err := r.ParseForm()
|
|
119
|
+ if err != nil {
|
|
120
|
+ http.Error(w, err.Error(), http.StatusBadRequest)
|
|
121
|
+ return
|
|
122
|
+ }
|
|
123
|
+ username := r.Form.Get("username")
|
|
124
|
+ password := r.Form.Get("password")
|
|
125
|
+ remember := r.Form.Get("remember")
|
|
126
|
+ longer := remember == "remember-me"
|
|
127
|
+
|
|
128
|
+ cookie, err := tryLogin(username, password, longer)
|
|
129
|
+
|
|
130
|
+ if err != nil {
|
|
131
|
+ http.Error(w, err.Error(), http.StatusForbidden)
|
|
132
|
+ return
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ http.SetCookie(w, &cookie)
|
|
136
|
+ http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
137
|
+}
|
|
138
|
+
|
103
|
139
|
func webStart(modifiedSongChan chan *SongEntry) {
|
104
|
140
|
songsChan = modifiedSongChan
|
105
|
141
|
|
|
142
|
+ mux := http.NewServeMux()
|
|
143
|
+
|
106
|
144
|
fs := http.FileServer(http.Dir("web"))
|
107
|
|
- http.Handle("/css/", fs)
|
108
|
|
- http.Handle("/fonts/", fs)
|
109
|
|
- http.Handle("/js/", fs)
|
110
|
|
- http.Handle("/favicon.ico", fs)
|
111
|
|
- http.HandleFunc("/", indexHandler)
|
112
|
|
- http.HandleFunc("/update", updateHandler)
|
113
|
|
-
|
114
|
|
- http.ListenAndServe("localhost:8080", nil)
|
|
145
|
+ mux.Handle("/css/", fs)
|
|
146
|
+ mux.Handle("/fonts/", fs)
|
|
147
|
+ mux.Handle("/js/", fs)
|
|
148
|
+ mux.Handle("/favicon.ico", fs)
|
|
149
|
+ mux.HandleFunc("/", indexHandler)
|
|
150
|
+ mux.HandleFunc("/update", updateHandler)
|
|
151
|
+ mux.HandleFunc("/login", loginHandler)
|
|
152
|
+
|
|
153
|
+ http.ListenAndServe("localhost:8080", mux)
|
115
|
154
|
}
|