-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (115 loc) · 2.5 KB
/
main.go
File metadata and controls
128 lines (115 loc) · 2.5 KB
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
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(),
},
),
)
}
// 테스트 데이터로 임시 대체
testMembers := []MeetupMember{
{
Member: struct {
ID int "json:\"id\""
Name string "json:\"name\""
Photo struct {
ID int "json:\"id\""
PhotoLink string "json:\"photo_link\""
} "json:\"photo\""
}{
ID: 1,
Name: "홍길동",
},
Response: "yes",
},
{
Member: struct {
ID int "json:\"id\""
Name string "json:\"name\""
Photo struct {
ID int "json:\"id\""
PhotoLink string "json:\"photo_link\""
} "json:\"photo\""
}{
ID: 2,
Name: "김철수",
},
Response: "yes",
},
{
Member: struct {
ID int "json:\"id\""
Name string "json:\"name\""
Photo struct {
ID int "json:\"id\""
PhotoLink string "json:\"photo_link\""
} "json:\"photo\""
}{
ID: 3,
Name: "이영희",
},
Response: "yes",
},
}
err := json.NewEncoder(w).Encode(testMembers)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
writeErrAsJSON(w, err)
return
}
// 원래 코드 (주석 처리)
// 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)
}
}