forked from AlexisJasso/space-z
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (77 loc) · 1.96 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
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
package main
import (
"fmt"
"net/http"
"github.com/gorilla/sessions"
"strings"
)
var store = sessions.NewCookieStore([]byte("something-very-secret"))
var things []string
func getSession(w http.ResponseWriter, r *http.Request) (*sessions.Session) {
session, err := store.Get(r, "session-name")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
return session
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("view handler!")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
for _, thing := range things {
fmt.Fprintf(w, "%v<hr /><br />", thing)
}
form := `
<hr />
<form action="/say" method="POST">
Say What?
<input type="text" name="say" value="???"><br>
<input type="submit" value="Submit">
</form>
`
fmt.Fprint(w, form)
}
func sayHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("say handler!")
session := getSession(w, r)
if session == nil {
return
}
fmt.Println(fmt.Printf("%#v", r))
r.ParseForm()
fmt.Println(fmt.Printf("%#v", r.Form))
name := session.Values["name"]
if name == "" || name == nil {
name = "Anon"
}
thingSaid := r.Form["say"][0]
if thingSaid != "" {
things = append(things, fmt.Sprintf("%v: %v", name, thingSaid))
}
http.Redirect(w, r, "/", 303)
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("login handler!")
session := getSession(w, r)
parts := strings.Split(r.URL.Path, "/")
name := parts[2]
session.Values["name"] = name
session.Save(r, w)
fmt.Fprintf(w, "Logged you in, %v!", name)
}
func baseHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
base := parts[1]
fmt.Printf("%v -> %v\n", r.URL.Path, base)
if strings.ToLower(base) == "login" {
loginHandler(w, r)
} else if strings.ToLower(base) == "say" {
sayHandler(w, r)
} else {
viewHandler(w, r)
}
}
func main() {
http.HandleFunc("/", baseHandler)
http.ListenAndServe(":8080", nil)
}