site.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main import ( "fmt" "net/http" "strings" "git.j3s.sh/vore/lib" "git.j3s.sh/vore/reaper" "git.j3s.sh/vore/sqlite" "golang.org/x/crypto/bcrypt" ) type Site struct { // title of the website title string // contains every single feed reaper *reaper.Reaper // site database handle db *sqlite.DB } // New returns a fully populated & ready for action Site func New() *Site { title := "vore" db := sqlite.New(title + ".db") s := Site{ title: title, reaper: reaper.Summon(db), db: db, } go s.reaper.Start() return &s } func (s *Site) indexHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET") { return } if s.loggedIn(r) { username := s.username(r) fmt.Fprintf(w, `<!DOCTYPE html> <title>%s</title> <p> { %s <a href=/logout>logout</a> } <p> <a href="/%s">view your feed</a> <p> <a href="/feeds">edit your feed</a>`, s.title, username, username) } else { fmt.Fprintf(w, `<!DOCTYPE html> <title>%s</title> <a href="/login">login</a>`, s.title) } } func (s *Site) loginHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET", "POST") { return } if r.Method == "GET" { if s.loggedIn(r) { fmt.Fprintf(w, "you are already logged in :3\n") } else { fmt.Fprintf(w, `<!DOCTYPE html> <h3>login</h3> <form method="POST" action="/login"> <label for="username">username:</label> <input type="text" name="username" required><br> <label for="password">password:</label> <input type="password" name="password" required><br> <input type="submit" value="login"> </form>`) fmt.Fprintf(w, `<h3>register</h3> <form method="POST" action="/register"> <label for="username">username:</label> <input type="text" name="username" required><br> <label for="password">password:</label> <input type="password" name="password" required><br> <input type="submit" value="register"> </form>`) } } if r.Method == "POST" { username := r.FormValue("username") password := r.FormValue("password") err := s.login(w, username, password) if err != nil { fmt.Fprintf(w, `<!DOCTYPE html> <p>π unauthorized: %s`, err) return } http.Redirect(w, r, "/", http.StatusSeeOther) } } // TODO: make this take a POST only in accordance w/ some spec func (s *Site) logoutHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET", "POST") { return } http.SetCookie(w, &http.Cookie{ Name: "session_token", Value: "", }) http.Redirect(w, r, "/", http.StatusSeeOther) } func (s *Site) registerHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "POST") { return } username := r.FormValue("username") password := r.FormValue("password") err := s.register(username, password) if err != nil { internalServerError(w, "failed to register user") return } err = s.login(w, username, password) if err != nil { internalServerError(w, "extremely weird login error") return } http.Redirect(w, r, "/", http.StatusSeeOther) } func (s *Site) userHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET") { return } fmt.Fprintf(w, "<!DOCTYPE html>") username := strings.TrimPrefix(r.URL.Path, "/") feeds := s.reaper.GetUserFeeds(username) if len(feeds) == 0 { fmt.Fprintf(w, "%s has no feeds π", username) return } sortedItems := s.reaper.SortFeedItems(feeds) for i := range sortedItems { fmt.Fprintf(w, `<p><a href="%s">%s</a>`, sortedItems[i].Link, sortedItems[i].Title) } } // [ ] GET /feeds // // > if no feeds, /discover for ideas // pretty-print your feeds // <text box with pre-populated list of your feed urls, one per line> // button: validate // POST /feeds/validate // logged out: unauthorized. click here to login. func (s *Site) feedsHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET") { return } if !s.loggedIn(r) { fmt.Fprintf(w, `<!DOCTYPE html> <p>β οΈ you are not logged inβ οΈ <p>please click the skull: <a href="/login">π</a>`) return } username := s.username(r) fmt.Fprintf(w, `<!DOCTYPE html> <title>%s</title> <p>%s's feeds:`, s.title, username) feeds := s.reaper.GetUserFeeds(username) for _, feed := range feeds { fmt.Fprintf(w, "<p>%s", feed.Title) fmt.Fprintf(w, "<p>%s", feed.Link) } // TODO: textbox with feed.URL // TODO: validate button } // username fetches a client's username based // on the sessionToken that user has set. username // will return "" if there is no sessionToken. func (s *Site) username(r *http.Request) string { sessionToken, err := r.Cookie("session_token") if err != nil { return "" } username := s.db.GetUsernameBySessionToken(sessionToken.Value) return username } func (s *Site) loggedIn(r *http.Request) bool { if s.username(r) == "" { return false } return true } // login compares the sqlite password field against the user supplied password and // sets a session token against the supplied writer. func (s *Site) login(w http.ResponseWriter, username string, password string) error { if username == "" { return fmt.Errorf("username cannot be nil") } if password == "" { return fmt.Errorf("password cannot be nil") } if !s.db.UserExists(username) { return fmt.Errorf("user does not exist") } storedPassword := s.db.GetPassword(username) err := bcrypt.CompareHashAndPassword([]byte(storedPassword), []byte(password)) if err != nil { return fmt.Errorf("invalid password") } sessionToken := lib.GenerateSessionToken() s.db.SetSessionToken(username, sessionToken) http.SetCookie(w, &http.Cookie{ Name: "session_token", Value: sessionToken, }) return nil } func (s *Site) register(username string, password string) error { hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return err } s.db.AddUser(username, string(hashedPassword)) return nil }