-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
185 lines (147 loc) · 3.92 KB
/
main.go
File metadata and controls
185 lines (147 loc) · 3.92 KB
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
package main
import (
"GoRestAPI/config"
"GoRestAPI/model"
"fmt"
"strconv"
"time"
"github.com/kataras/iris"
jwt "github.com/dgrijalva/jwt-go"
"github.com/iris-contrib/middleware/cors"
jwtmiddleware "github.com/iris-contrib/middleware/jwt"
)
func main() {
config.Init()
port := "3000"
app := makeNew()
app.Run(iris.Addr(":" + port))
}
func makeNew() *iris.Application {
app := iris.New()
// make jwtAuth
jwtHandler := jwtmiddleware.New(jwtmiddleware.Config{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return []byte("secret"), nil
},
SigningMethod: jwt.SigningMethodHS256,
})
// make cors
corsHandler := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
})
v1 := app.Party("/api/v1", corsHandler).AllowMethods(iris.MethodOptions)
{
v1.Use(jwtHandler.Serve)
// Gets all users
// Method: GET
// Resource: this to get all all users
v1.Get("/users", func(ctx iris.Context) {
results, err := model.GetUsers()
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusNotFound)
} else {
ctx.StatusCode(iris.StatusOK)
ctx.JSON(results)
}
})
// Gets a single user
// Method: GET
// Resource: this to get all all users
v1.Get("/users/{id: string}", func(ctx iris.Context) {
msisdn := ctx.Params().Get("id")
if msisdn == "" {
ctx.StatusCode(iris.StatusBadRequest)
}
result, err := model.GetUser(msisdn)
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
}
ctx.JSON(result)
})
// Method: PUT
// Resource: This is to update a user record
v1.Put("/users/{id: string}", func(ctx iris.Context) {
msisdn := ctx.Params().Get("id")
params := &model.User{}
params.ID, _ = strconv.Atoi(msisdn)
err := ctx.ReadJSON(params)
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
}
if msisdn == "" {
ctx.StatusCode(iris.StatusBadRequest)
} else {
err := model.UpdateUser(params)
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
} else {
ctx.StatusCode(iris.StatusOK)
}
}
})
}
api := app.Party("/api", corsHandler).AllowMethods(iris.MethodOptions)
{
// Method: POST
// Resource: This is to sign up a new user
api.Post("/user", func(ctx iris.Context) {
params := &model.User{}
err := ctx.ReadJSON(params)
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
} else {
err := model.CreateUser(params)
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
} else {
ctx.StatusCode(iris.StatusOK)
}
}
})
// Method : POST
// Resource : This is to login
api.Post("/login", func(ctx iris.Context) {
auth := new(model.Auth)
err := ctx.ReadJSON(auth)
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
return
}
list, err := model.GetUsers()
if err != nil {
fmt.Println(err)
ctx.StatusCode(iris.StatusBadRequest)
return
}
for _, user := range list.Users {
if auth.UserId == strconv.Itoa(user.ID) && auth.Password == user.Password {
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["name"] = user.Firstname + user.Lastname
claims["admin"] = true
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
t, err := token.SignedString([]byte("secret"))
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
}
ctx.JSON(map[string]interface{}{
"token": t,
"expire": claims["exp"],
})
ctx.StatusCode(iris.StatusOK)
}
}
ctx.StatusCode(iris.StatusUnauthorized)
})
}
return app
}