-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
74 lines (63 loc) · 1.38 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
)
var (
port string
)
func init() {
flag.StringVar(&port, "-p", "8000", "listen port")
}
func main() {
flag.Parse()
http.HandleFunc("/", IndexView)
http.HandleFunc("/event", GetEventHandler)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fmt.Printf("Listening on %s.\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// IndexView render the index template
func IndexView(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/html; charset=utf-8")
f, err := os.Open("index.html")
chk(err)
defer f.Close()
io.Copy(w, f)
}
// GetEventHandler return the latest event and attendance info as json
func GetEventHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
writeErrAsJSON := func(w io.Writer, err error) {
chk(
json.NewEncoder(w).Encode(
map[string]string{
"message": err.Error(),
},
),
)
}
members, err := MeetupResvMembersOfLastEvent()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
writeErrAsJSON(w, err)
return
}
err = json.NewEncoder(w).Encode(members)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
writeErrAsJSON(w, err)
return
}
}
func chk(err error) {
if err != nil {
panic(err)
}
}