forked from baesy0/kalena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_restapi.go
184 lines (178 loc) · 4.71 KB
/
http_restapi.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"encoding/json"
"net/http"
"strconv"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// handleAPISchedule 함수는 Schedule을 POST 한다.
func handleAPISchedule(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
s := Schedule{}
r.ParseForm()
collection := r.FormValue("collection")
if collection == "" {
http.Error(w, "collection을 설정해 주세요", http.StatusBadRequest)
return
}
s.Collection = collection
title := r.FormValue("title")
if title == "" {
http.Error(w, "title을 설정해 주세요", http.StatusBadRequest)
return
}
s.Title = title
start := r.FormValue("start")
if start == "" {
http.Error(w, "start를 설정해 주세요", http.StatusBadRequest)
return
}
if !regexRFC3339Time.MatchString(start) {
http.Error(w, "시간 형식이 아닙니다", http.StatusBadRequest)
return
}
s.Start = start
end := r.FormValue("end")
if end == "" {
http.Error(w, "end를 설정해 주세요", http.StatusBadRequest)
return
}
if !regexRFC3339Time.MatchString(end) {
http.Error(w, "시간 형식이 아닙니다", http.StatusBadRequest)
return
}
s.End = end
color := r.FormValue("color")
if color == "" {
http.Error(w, "color를 설정해 주세요", http.StatusBadRequest)
return
}
if !regexWebColor.MatchString(color) {
http.Error(w, "#FFFFFF 형식이 아닙니다", http.StatusBadRequest)
return
}
s.Color = color
layer := r.FormValue("layer")
if layer == "" {
http.Error(w, "layer를 설정해 주세요", http.StatusBadRequest)
return
}
s.Layer = layer
err := s.CheckError()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
c := session.DB(*flagDBName).C(s.Collection + ".layers")
// 레이어설정 컬렉션에서 이름이 일치하는 레이어가 있는지 검사한다.
layerNum, err := c.Find(bson.M{"name": s.Layer}).Count()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// 이름이 일치하는 레이어가 없으면 에러처리
if layerNum == 0 {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// 스케쥴 추가
err = AddSchedule(session, s)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(s)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
} else {
http.Error(w, "Not Supported Method", http.StatusMethodNotAllowed)
return
}
}
// handleAPILayer 핸들러는 Layer를 POST 한다.
func handleAPILayer(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
l := Layer{}
r.ParseForm()
collection := r.FormValue("collection")
if collection == "" {
http.Error(w, "collection을 설정해 주세요", http.StatusBadRequest)
return
}
name := r.FormValue("name")
if name == "" {
http.Error(w, "name 설정해 주세요", http.StatusBadRequest)
return
}
l.Name = name
order := r.FormValue("order")
if order == "" {
http.Error(w, "order를 설정해 주세요", http.StatusBadRequest)
return
}
o, err := strconv.Atoi(order)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
l.Order = o
color := r.FormValue("color")
if color == "" {
http.Error(w, "color를 설정해 주세요", http.StatusBadRequest)
return
}
l.Color = color
hidden := r.FormValue("hidden")
if hidden == "" {
http.Error(w, "hidden을 설정해 주세요", http.StatusBadRequest)
return
}
b, err := strconv.ParseBool(hidden)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
l.Hidden = b
err = l.CheckError()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
session, err := mgo.Dial(*flagDBIP)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer session.Close()
err = AddLayer(session, collection, l)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// json 으로 결과 전송
data, err := json.Marshal(l)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
} else {
http.Error(w, "Not Supported Method", http.StatusMethodNotAllowed)
return
}
}