-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
364 lines (305 loc) · 9.88 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// gocouch project main.go
// register main handlers for the Go CouchDB web server
package main
import (
"encoding/json"
"io"
"net/http"
"regexp"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"GConfig"
"Internals"
"JSONStructs"
)
var (
SlashRSP *JSONStructs.SlashResponse
ValidDB_Name *regexp.Regexp
e *echo.Echo
//DBList map[string]Internals.BoltDB
)
func slashHandler(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Server", Internals.ServerMsg)
c.JSON(http.StatusOK, SlashRSP)
return nil
}
func uuidHandler(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Pragma", "no-cache")
c.Response().Header().Set("ETag", "buru-buru")
c.Response().Header().Set("Server", Internals.ServerMsg)
defUUIDS := []string{Internals.GetUUID(),
Internals.GetUUID(),
Internals.GetUUID(),
Internals.GetUUID(),
Internals.GetUUID()}
UUIDRSP := &JSONStructs.UUIDSResponse{
UUIDS: defUUIDS,
}
c.JSON(http.StatusOK, UUIDRSP)
return nil
}
func dbHeadHandler(c echo.Context) error {
db := c.Param("db")
if Contains(ALL_DBS, db) {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Server", Internals.ServerMsg)
} else {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Server", Internals.ServerMsg)
return echo.NewHTTPError(http.StatusNotFound)
}
//json.NewEncoder(c.Response()).Encode(c.Param("db"))
c.Response().Flush()
//c.JSON(http.StatusOK, rr)
return nil
}
func dbPutHandler(c echo.Context) error {
db := c.Param("db")
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Server", Internals.ServerMsg)
if !ValidDB_Name.MatchString(db) {
//the DB name is not valid -> 400
InvalidDBName := &JSONStructs.DBErrorResponse{
ErrorMsg: "illegal_database_name",
Reason: "Name: '" + db + "'. Only lowercase characters (a-z), " +
"digits (0-9), and any of the characters _, $, " +
"(, ), +, -, and / are allowed. Must begin with " +
" a letter.",
}
c.JSON(http.StatusBadRequest, InvalidDBName)
return nil
}
if Contains(ALL_DBS, db) {
//the DB already exists -> 412
DBExists := &JSONStructs.DBErrorResponse{
ErrorMsg: "file_exists",
Reason: "The database could not be created, the file already exists.",
}
c.JSON(http.StatusPreconditionFailed, DBExists)
return nil
}
//TODO implement database security so that 401 may be checked also
//It should be OK - create the DB file and add it to ALL_DBS
//Send 201 response
newDB := Internals.NewBoltDB("", db+".bd")
defer newDB.Close()
ALL_DBS = append(ALL_DBS, db)
c.Response().Header().Set("Location", "http://get.server.address.or.host.name"+":"+
GConfig.GoCouchCFG.HTTPd.Port+"/"+db)
DBCreatedOK := &JSONStructs.OKResponse{
OK: true,
}
c.JSON(http.StatusCreated, DBCreatedOK)
return nil
}
func dbPostHandler(c echo.Context) error {
db := c.Param("db")
//check db
//400 or 404 possible error
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Server", Internals.ServerMsg)
if !ValidDB_Name.MatchString(db) {
//the DB name is not valid -> 400
InvalidDBName := &JSONStructs.DBErrorResponse{
ErrorMsg: "illegal_database_name",
Reason: "Name: '" + db + "'. Only lowercase characters (a-z), " +
"digits (0-9), and any of the characters _, $, " +
"(, ), +, -, and / are allowed. Must begin with " +
" a letter.",
}
c.JSON(http.StatusBadRequest, InvalidDBName)
return nil
}
if !Contains(ALL_DBS, db) {
//the DB does not exist -> 404
DBExists := &JSONStructs.DBErrorResponse{
ErrorMsg: "not_found",
Reason: "no_db_file",
}
c.JSON(http.StatusNotFound, DBExists)
return nil
}
//TODO implement database security so that 401 may be checked also
//implement batch transactions and return 202 Accepted
//Get JSON payload and decode it
dec := json.NewDecoder(c.Request().Body)
var payload map[string]interface{}
for {
if err := dec.Decode(&payload); err == io.EOF {
break
} else if err != nil {
e.Logger.Error("%s", err)
}
//e.Logger().Info("%s", payload["_id"])
}
if payload["_id"] != nil {
//check to see if there is already a document with this id -> 409
tmp_db := Internals.NewBoltDB("", db+".bd")
defer tmp_db.Close()
if tmp_db.ExistsDoc([]byte(payload["_id"].(string))) {
DOCExists := &JSONStructs.DBErrorResponse{
ErrorMsg: "conflict",
Reason: "Document update conflict.",
}
c.JSON(http.StatusConflict, DOCExists)
return nil
} else {
//create new doc with specified id
doc, _ := json.Marshal(payload)
payload["_rev"] = "1-" + Internals.GetMD5Hash(doc)
doc, _ = json.Marshal(payload)
//e.Logger().Info("%s", doc)
tmp_db.UpdateDB([]byte(payload["_id"].(string)),
[]byte(payload["_rev"].(string)),
[]byte(doc))
}
} else {
//create a new doc with random id
tmp_db := Internals.NewBoltDB("", db+".bd")
defer tmp_db.Close()
payload["_id"] = Internals.GetUUID()
doc, _ := json.Marshal(payload)
payload["_rev"] = "1-" + Internals.GetMD5Hash(doc)
doc, _ = json.Marshal(payload)
//e.Logger().Info("%s", doc)
tmp_db.UpdateDB([]byte(payload["_id"].(string)),
[]byte(payload["_rev"].(string)),
[]byte(doc))
}
DOCCreated := &JSONStructs.DocOKResponse{
OK: true,
ID: payload["_id"].(string),
REV: payload["_rev"].(string),
}
c.Response().Header().Set("ETag", payload["_rev"].(string))
c.Response().Header().Set("Location", "http://get.server.address.or.host.name"+":"+
GConfig.GoCouchCFG.HTTPd.Port+"/"+db+"/"+
payload["_id"].(string))
c.JSON(http.StatusCreated, DOCCreated)
return nil
}
func dbDeleteHandler(c echo.Context) error {
db := c.Param("db")
rev := c.QueryParam("rev")
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Server", Internals.ServerMsg)
//Check for possible errors 400 and 404
if !Contains(ALL_DBS, db) {
//the DB does not exist -> 404
DBExists := &JSONStructs.DBErrorResponse{
ErrorMsg: "not_found",
Reason: "missing",
}
c.JSON(http.StatusNotFound, DBExists)
return nil
}
if len(rev) > 0 || !ValidDB_Name.MatchString(db) {
//the DB name is not valid -> 400
InvalidDBName := &JSONStructs.DBErrorResponse{
ErrorMsg: "bad_reques",
Reason: "You tried to DELETE a database with a ?rev= parameter. " +
"Did you mean to DELETE a document instead?",
}
c.JSON(http.StatusBadRequest, InvalidDBName)
return nil
}
//TODO implement database security so that 401 may be checked also
err := Internals.DeleteDB("", db+".bd")
if err != nil {
e.Logger.Error("%s", err)
}
//Remove also from ALL_DBS
all_dbs_init("")
DeleteOKResponse := &JSONStructs.OKResponse{
OK: true,
}
c.JSON(http.StatusOK, DeleteOKResponse)
return nil
}
func dbBackup(c echo.Context) error {
db := c.Param("db")
//Check the existence of the database -> 404
c.Response().Header().Set("Server", Internals.ServerMsg)
//Check for possible errors 400 and 404
if !Contains(ALL_DBS, db) {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
//the DB does not exist -> 404
DBExists := &JSONStructs.DBErrorResponse{
ErrorMsg: "not_found",
Reason: "Missing database file!",
}
c.JSON(http.StatusNotFound, DBExists)
return nil
}
tmp_db := Internals.ROBoltDB("", db+".bd")
defer tmp_db.Close()
err := tmp_db.ExportFile("", db+".bd", c)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
c.Response().Flush()
return nil
}
func dbGetHandler(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Pragma", "no-cache")
c.Response().Header().Set("ETag", "buru-buru")
c.Response().Header().Set("Server", Internals.ServerMsg)
json.NewEncoder(c.Response()).Encode(c.Param("db"))
c.Response().Flush()
//c.JSON(http.StatusOK, rr)
return nil
}
func all_dbsHandler(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().Header().Set("Cache-Control", "must-revalidate")
c.Response().Header().Set("Pragma", "no-cache")
c.Response().Header().Set("Server", Internals.ServerMsg)
c.JSON(http.StatusOK, ALL_DBS)
return nil
}
func init() {
ValidDB_Name = regexp.MustCompile(`^[a-z][a-z0-9_$()+/-]*$`)
GConfig.CheckCFGFile()
GConfig.CheckReplicatorFile()
all_dbs_init("")
SlashRSP = &JSONStructs.SlashResponse{
CouchDB: "Welcome to GO CouchDB",
UUID: GConfig.GoCouchCFG.GoCouch.UUID,
Version: "0.0.1",
Vendor: map[string]string{"version": "0.0.1", "vendor": "Free Time Software"},
}
}
func main() {
e = echo.New()
// Middleware
e.Use(mw.Logger())
e.Use(mw.Recover())
//Register routes
e.GET("/", slashHandler)
e.GET("/_uuids", uuidHandler)
e.GET("/_all_dbs", all_dbsHandler)
//Database operations
e.HEAD("/:db", dbHeadHandler)
e.GET("/:db", dbGetHandler)
e.PUT("/:db", dbPutHandler)
e.POST("/:db", dbPostHandler)
e.DELETE("/:db", dbDeleteHandler)
//_backup
e.GET("/_backup/:db", dbBackup)
e.Logger.Info("%s", Internals.WelcomeMsg)
//Start server
e.Logger.Fatal(e.Start(GConfig.GoCouchCFG.HTTPd.Bind_Address +
":" + GConfig.GoCouchCFG.HTTPd.Port))
}