/
main.go
133 lines (109 loc) · 3.02 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
package main
import (
"encoding/json"
"log"
"net/http"
rdb "github.com/dancannon/gorethink"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
)
var (
session = newDBConn()
tableName = "People"
c = newCORSOptions()
)
// Person is the model for our user.
type Person struct {
ID string `json:"id" gorethink:"id,omitempty"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
CoolnessFactor int `json:"coolnessFactor"`
}
func newDBConn() *rdb.Session {
session, err := rdb.Connect(rdb.ConnectOpts{
Address: "localhost:28015",
})
if err != nil {
log.Fatalln("Error: %s", err)
return nil
}
resp, _ := rdb.TableCreate(tableName).RunWrite(session)
log.Println("Tables created: ", resp.TablesCreated)
return session
}
func newCORSOptions() *cors.Cors {
c := cors.New(cors.Options{
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "HEAD"},
})
return c
}
// List all users in the DB.
func List(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var people []Person
rows, err := rdb.Table(tableName).Run(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = rows.All(&people)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(people)
}
// Get all users in the DB.
func Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p Person
row, err := rdb.Table(tableName).Get(ps.ByName("id")).Run(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
row.One(&p)
json.NewEncoder(w).Encode(p)
}
// Update a specific user
func Update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var p Person
json.NewDecoder(r.Body).Decode(&p)
resp, err := rdb.Table(tableName).Get(ps.ByName("id")).Update(p).RunWrite(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(resp.GeneratedKeys)
}
// Delete a specific user from the DB>
func Delete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
resp, err := rdb.Table(tableName).Get(ps.ByName("id")).Delete().RunWrite(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(resp)
}
// Add a new user
func Add(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var p Person
json.NewDecoder(r.Body).Decode(&p)
row, err := rdb.Table(tableName).Insert(p).RunWrite(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id := row.GeneratedKeys
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{
"id": id[0],
})
}
func main() {
router := httprouter.New()
router.GET("/api/people", List)
router.GET("/api/people/:id", Get)
router.PUT("/api/people/:id", Update)
router.DELETE("/api/people/:id", Delete)
router.POST("/api/people", Add)
http.ListenAndServe(":8000", c.Handler(router))
}