-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
71 lines (59 loc) · 1.46 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
package main
import (
"log"
"net/http"
"os"
"strconv"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
http.HandleFunc("/invite", invite)
http.HandleFunc("/assets/", handleAssets)
http.HandleFunc("/", handleStatic)
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func invite(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
err := r.ParseForm()
if err != nil {
badRequest(w, r, err)
return
}
em := r.Form.Get("email")
okstr := r.Form.Get("ok")
// ok references whether the individual identifies as a woman
// or a gender minority.
ok, err := strconv.ParseBool(okstr)
if err != nil {
badRequest(w, r, err)
return
}
if !ok {
http.Redirect(w, r, "http://www.womenwhogo.org/failure.html", http.StatusFound)
return
}
err = inviteUser(r, em)
if err != nil {
interalServerError(w, r, err)
return
}
http.Redirect(w, r, "http://www.womenwhogo.org/success.html", http.StatusFound)
}
func badRequest(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("Error bad request: %v", err)
http.Error(w, "Bad request.", http.StatusBadRequest)
}
func interalServerError(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("Internal server error: %v", err)
http.Error(w, "Internal server error.", http.StatusInternalServerError)
}