-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
113 lines (94 loc) · 2.51 KB
/
http.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
package main
import (
"fmt"
"net/http"
"github.com/labstack/echo"
)
func badRequest(c *echo.Context, description string, err error) {
c.String(http.StatusBadRequest, fmt.Sprintf("%s: %s", description, err))
}
func ok(c *echo.Context) {
c.String(http.StatusOK, "Success\n")
}
func okWithBody(c *echo.Context, body []byte) error {
c.Response.Header().Set(echo.HeaderContentType, echo.MIMEJSON+"; charset=utf-8")
c.Response.WriteHeader(http.StatusOK)
_, err := c.Response.Write(body)
return err
}
func Welcome(c *echo.Context) {
c.String(http.StatusOK, "Welcome to RTD v0.1")
}
func Create(c *echo.Context) {
if _, err := getDb(c.Param("db")); err != nil {
badRequest(c, "Error creating your database", err)
} else {
ok(c)
}
}
func Delete(c *echo.Context) {
if err := deleteDb(c.Param("db")); err != nil {
badRequest(c, "Error deleting your database", err)
} else {
ok(c)
}
}
func Query(c *echo.Context) {
docs, err := query(c.Param("db"), c.Param("collection"), c.Request.Body)
if err != nil {
badRequest(c, "Error querying collection", err)
} else {
okWithBody(c, docs)
}
}
func UpdateQuery(c *echo.Context) {
docs, err := updateQuery(c.Param("db"), c.Param("collection"), c.Request.Body)
if err != nil {
badRequest(c, "Error updating collection", err)
} else {
okWithBody(c, docs)
}
}
func InsertDoc(c *echo.Context) {
insertedDoc, err := insertDoc(c.Param("db"), c.Param("collection"), c.Request.Body)
if err != nil {
badRequest(c, "Error inserting document", err)
} else {
okWithBody(c, insertedDoc.Bytes())
}
}
func FindDoc(c *echo.Context) {
doc, err := findDoc(c.Param("db"), c.Param("collection"), c.Param("id"))
if err != nil {
badRequest(c, "Error finding document", err)
} else {
okWithBody(c, doc)
}
}
func UpdateDoc(c *echo.Context) {
doc, err := updateDoc(c.Param("db"), c.Param("collection"), c.Param("id"), c.Request.Body)
if err != nil {
badRequest(c, "Error updating document", err)
} else {
okWithBody(c, doc)
}
}
func DeleteDoc(c *echo.Context) {
c.String(http.StatusOK, fmt.Sprintf("DeleteDoc %s:%s:%s", c.Param("db"), c.Param("collection"), c.Param("id")))
}
func StartHttp(bind string) {
e := echo.New()
// root
e.Get("/", Welcome)
// DB
e.Post("/:db", Create)
e.Delete("/:db", Delete)
// Documents
e.Get("/:db/:collection", Query)
e.Put("/:db/:collection", UpdateQuery)
e.Post("/:db/:collection", InsertDoc)
e.Get("/:db/:collection/:id", FindDoc)
e.Put("/:db/:collection/:id", UpdateDoc)
e.Delete("/:db/:collection/:id", DeleteDoc)
e.Run(bind)
}