-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.go
285 lines (241 loc) · 6.94 KB
/
api.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Due to golang plugin mechanism,
// the package of function handler must be main package
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/textproto"
"strconv"
"time"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
const (
URL_PARAMS_PREFIX = "X-Fission-Params"
)
var (
dbConn *gorm.DB
)
func init() {
if dbConn == nil {
dbUrl := "postgresql://root@cockroachdb.guestbook:26257/guestbook?sslmode=disable"
dbName := "guestbook"
dbConn = ConnectDB(dbUrl, dbName)
}
}
func GetDB() *gorm.DB {
return dbConn
}
// GetPathValue g, "id"et url path parameter from the request header.
// For example, if a RESTful API is exposed with path `/guestbook/{id}`
// The fission router will extract and attach the value of `id`
// to the request header with key `X-Fission-Params-Id`.
// Then, the function can retrieve the value of `id` from the header with key.
// ** Notice **:
// The case of first letter after each dash(-) will be transform to lowercase.
// For example: 'id' -> 'Id', 'fooBAR' -> 'Foobar', 'foo-BAR' -> 'Foo-Bar'.
func GetPathValue(r *http.Request, param string) string {
// transform text case
// For example: 'id' -> 'Id', 'fooBAR' -> 'Foobar', 'foo-BAR' -> 'Foo-Bar'.
param = textproto.CanonicalMIMEHeaderKey(param)
// generate header key for accessing request header value
key := fmt.Sprintf("%s-%s", URL_PARAMS_PREFIX, param)
// get header value
return r.Header.Get(key)
}
// GetQueryString get query value from the request query string.
// Unlike url path, the fission router leaves request query intact,
// which means that the case of query string will not change nor
// need to access it from request header.
// For example: If a request come with uri '/guestbook/123?start=123&end=456',
// the function will see `start=123&end=456`.
func GetQueryString(r *http.Request, query string) string {
return r.URL.Query().Get(query)
}
// getMessageId get message id from request
func getMessageId(r *http.Request) (uint, error) {
msgIdStr := GetPathValue(r, "id")
if len(msgIdStr) > 0 {
id, err := strconv.ParseUint(msgIdStr, 10, 64)
if err != nil {
return 0, err
}
return uint(id), nil
}
return 0, nil
}
// getDate get date from query string
func getDate(r *http.Request, query string) (*time.Time, error) {
str := GetQueryString(r, query)
if len(str) == 0 {
return nil, nil
}
second, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return nil, err
}
t := time.Unix(second, 0)
return &t, nil
}
func MessagePostHandler(w http.ResponseWriter, r *http.Request) {
// POST guestbook/messages
body, err := ioutil.ReadAll(r.Body)
if err != nil {
err = errors.Wrap(err, "Error reading request body")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
msg := Message{}
err = json.Unmarshal(body, &msg)
if err != nil {
err = errors.Wrap(err, "Error decoding json body")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
// Database will assign id to the id field automatically,
// remove user assigned value.
if msg.ID != 0 {
msg.ID = 0
}
msg.Timestamp = time.Now().Unix()
err = GetDB().Create(&msg).Error
if err != nil {
err = errors.Wrap(err, "Error inserting message to database")
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
}
func MessageGetHandler(w http.ResponseWriter, r *http.Request) {
// GET guestbook/messages
// GET guestbook/messages/{id}
// GET guestbook/messages/?start=ooo&end=xxx
msgId, err := getMessageId(r)
if err != nil {
err = errors.Wrap(err, "Error parsing message id")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
// extract 'start' and 'end' from query string `start=xxx&end=ooo`
start, err := getDate(r, "start")
if err != nil {
err = errors.Wrap(err, "Error parsing start value")
}
end, err := getDate(r, "end")
if err != nil {
err = errors.Wrap(err, "Error parsing end value")
}
var respObj interface{}
if msgId != 0 && start == nil && end == nil {
msg := Message{}
err := GetDB().First(&msg, msgId).Error
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Error finding message with message id %v", msgId))
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
respObj = msg
} else {
msgs := []Message{}
if start == nil && end == nil {
err = GetDB().Find(&msgs).Error
} else {
err = GetDB().Where("timestamp >= ? AND timestamp <= ?", start.Unix(), end.Unix()).
Find(&msgs).Error
}
if err != nil {
err = errors.Wrap(err, "Error retrieving messages")
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
respObj = msgs
}
body, err := json.Marshal(respObj)
if err != nil {
err = errors.Wrap(err, "Error encoding response body")
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(body)
}
func MessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
// PUT guestbook/messages/{id}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
err = errors.Wrap(err, "Error reading request body")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
msg := Message{}
err = json.Unmarshal(body, &msg)
if err != nil {
err = errors.Wrap(err, "Error decoding json body")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
// Database will assign id to the id field automatically,
// remove user assigned value.
if msg.ID != 0 {
msg.ID = 0
}
msgId, err := getMessageId(r)
if err != nil {
err = errors.Wrap(err, "Error parsing message id")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = GetDB().Model(&Message{}).Where("id = ?", msgId).Update("message", msg.Message).Error
if err != nil {
err = errors.Wrap(err, "Error updating message")
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
}
func MessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
// DELETE guestbook/messages/{id}
msgId, err := getMessageId(r)
if err != nil {
err = errors.Wrap(err, "Error parsing message id")
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
err = GetDB().Where("id = ?", msgId).Delete(&Message{}).Error
if err != nil {
err = errors.Wrap(err, "Error deleting message")
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
}