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
package main import ( "database/sql" "fmt" "log" "net/http" "git.j3s.sh/feeds.gay/sqlite" ) type Site struct { db *sql.DB } // New returns a fully populated & ready for action Site func New() *Site { s := Site{ db: sqlite.SetupAndOpen("feeds.gay.db"), } return &s } func (s *Site) Start(addr string, mux *http.ServeMux) { log.Fatal(http.ListenAndServe(addr, mux)) } func (s *Site) rootHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET") { return } // The "/" pattern matches everything, so we need to check // that we're at the root here. if r.URL.Path != "/" { http.NotFound(w, r) return } fmt.Fprintf(w, "feeds.gay is dope & you should like it\n") } func (s *Site) loginHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "GET", "POST") { return } if r.Method == "GET" { // if logged out: fmt.Fprintf(w, "display login forms\n") // if logged in: fmt.Fprintf(w, "you are already logged in :D\n") } if r.Method == "POST" { fmt.Fprintf(w, "cmon POST\n") } } func (s *Site) logoutHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "POST") { return } // TODO: delete session cookie http.Redirect(w, r, "/", http.StatusSeeOther) } func (s *Site) registerHandler(w http.ResponseWriter, r *http.Request) { if !methodAllowed(w, r, "POST") { return } // TODO: create user in database // TODO: add session cookie http.Redirect(w, r, "/", http.StatusSeeOther) }