-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
170 lines (147 loc) · 3.66 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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
"net/http"
)
var (
session *mgo.Session
collection *mgo.Collection
)
type User struct {
Id bson.ObjectId `bson:"_id" json:"id"`
FirstName string `bson:"firstName" json:"firstName"`
LastName string `bson:"lastName" json:"lastName"`
Age int `bson:"age" json:"age"`
}
type UserResource struct {
User User `json:"user"`
}
type UsersResource struct {
Users []User `json:"users"`
}
type Status struct {
Code int `json:"code"`
IsValid bool `json:"isValid"`
}
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
panic(err)
}
// get a new id
user.Id = bson.NewObjectId()
//insert into document collection
err = collection.Insert(&user)
if err != nil {
panic(err)
}
j, err := json.Marshal(UserResource{User: user})
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func UsersHandler(w http.ResponseWriter, r *http.Request) {
var users []User
iter := collection.Find(nil).Iter()
result := User{}
for iter.Next(&result) {
users = append(users, result)
}
w.Header().Set("Content-Type", "application/json")
j, err := json.Marshal(UsersResource{Users: users})
if err != nil {
panic(err)
}
w.Write(j)
}
func UpdateUserHandler(w http.ResponseWriter, r *http.Request) {
var err error
// Get id from url
vars := mux.Vars(r)
id := bson.ObjectIdHex(vars["id"])
// Decode the incoming user json
var user User
err = json.NewDecoder(r.Body).Decode(&user)
if err != nil {
panic(err)
}
// update on MogoDB
// M -> map[string]interface{}
err = collection.Update(bson.M{"_id": id},
bson.M{"$set": bson.M{"firstName": user.FirstName,
"lastName": user.LastName,
"age": user.Age,
}})
if err == nil {
log.Printf("Updated User: %s", id, user.FirstName)
} else {
panic(err)
}
j, err := json.Marshal(UserResource{User: user})
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
var err error
// Get id from url
vars := mux.Vars(r)
id := bson.ObjectIdHex(vars["id"])
var user User
err = collection.Find(bson.M{"_id": id}).One(&user)
if err != nil {
panic(err)
}
j, err := json.Marshal(UserResource{User: user})
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
id := vars["id"]
// Remove from database
err = collection.Remove(bson.M{"_id": bson.ObjectIdHex(id)})
if err != nil {
log.Printf("Could not find User %s to delete", id)
}
j, err := json.Marshal(Status{200, true})
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func init() {
log.Println("Starting mongodb session")
var err error
session, err = mgo.Dial("localhost")
if err != nil {
log.Fatalf("Error connecting to MongoDB")
}
collection = session.DB("usersDB2").C("users")
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/api/users", UsersHandler).Methods("GET")
router.HandleFunc("/api/users", CreateUserHandler).Methods("POST")
router.HandleFunc("/api/users/{id}", GetUserHandler).Methods("GET")
router.HandleFunc("/api/users/{id}", UpdateUserHandler).Methods("PUT")
router.HandleFunc("/api/users/{id}", DeleteUserHandler).Methods("DELETE")
http.Handle("/api/", router)
defer session.Close()
log.Println("Listening on 8080")
http.ListenAndServe(":8080", nil)
}