-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
176 lines (153 loc) · 4.39 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
package main
import (
"fmt"
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/logger"
"github.com/kataras/iris/v12/middleware/recover"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// User defines the user mongo object.
type User struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Age int `json:"age"`
Msisdn string `json:"msisdn"`
InsertedAt time.Time `json:"inserted_at" bson:"inserted_at"`
LastUpdate time.Time `json:"last_update" bson:"last_update"`
}
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// Optionally, add two built'n handlers
// that can recover from any http-relative panics
// and log the requests to the terminal.
app.Use(recover.New())
app.Use(logger.New())
session, err := mgo.Dial("127.0.0.1")
if nil != err {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("usergo").C("profiles")
// Index
index := mgo.Index{
Key: []string{"msisdn"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err = c.EnsureIndex(index)
if err != nil {
panic(err)
}
// Method: GET Default Endpoint
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Welcome User Micro Service"})
})
// Gets all users
// Method: GET
// Resource: this to get all all users
app.Get("/users", func(ctx iris.Context) {
results := []User{}
err := c.Find(nil).All(&results)
if err != nil {
// TODO: Do something about the error
fmt.Println(err.Error())
ctx.StatusCode(iris.StatusNoContent)
return
}
fmt.Println("Results All: ", results)
ctx.JSON(iris.Map{"response": results})
})
// Gets a single user
// Method: GET
// Resource: this to get all all users
app.Get("/users/{msisdn}", func(ctx iris.Context) {
msisdn := ctx.Params().Get("msisdn")
fmt.Println(msisdn)
result := User{}
err = c.Find(bson.M{"msisdn": msisdn}).One(&result)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
ctx.JSON(iris.Map{"response": result})
})
// Method: POST
// Resource: This is to create a new user
app.Post("/users", func(ctx iris.Context) {
params := &User{}
err := ctx.ReadJSON(params)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
params.LastUpdate = time.Now()
err = c.Insert(params)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
fmt.Println("Successfully inserted into database")
result := User{}
err = c.Find(bson.M{"msisdn": params.Msisdn}).One(&result)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
ctx.JSON(iris.Map{"response": "User succesfully created", "message": result})
})
// Method: PATCH
// Resource: This is to update a user record
app.Patch("/users/{msisdn}", func(ctx iris.Context) {
msisdn := ctx.Params().Get("msisdn")
fmt.Println(msisdn)
params := &User{}
err := ctx.ReadJSON(params)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
params.InsertedAt = time.Now()
query := bson.M{"msisdn": msisdn}
err = c.Update(query, params)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
result := User{}
err = c.Find(bson.M{"msisdn": params.Msisdn}).One(&result)
if err != nil {
ctx.JSON(iris.Map{"response": err.Error()})
return
}
ctx.JSON(iris.Map{"response": "user record successfully updated", "data": result})
})
// Method: DELETE
// Resource: This is to delete a user record
app.Delete("/users/{msisdn: string}", func(ctx iris.Context) {
msisdn := ctx.Params().Get("msisdn")
fmt.Println(msisdn)
params := &User{}
err := ctx.ReadJSON(params)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
params.InsertedAt = time.Now()
query := bson.M{"msisdn": msisdn}
err = c.Remove(query)
if err != nil {
ctx.StopWithJSON(iris.StatusBadRequest, iris.Map{"response": err.Error()})
return
}
ctx.JSON(iris.Map{"response": "user record successfully deleted"})
})
app.Listen(":8080")
}