-
Notifications
You must be signed in to change notification settings - Fork 48
/
api.go
144 lines (126 loc) · 2.97 KB
/
api.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
// Application
//
// Application description
//
// Schemes: http
// Host: localhost:8080
// BasePath: /
// Version: 0.0.1
//
// Consumes:
// - application/json
// - application/xml
//
// Produces:
// - application/json
// - application/xml
//
// swagger:meta
package route
import (
"go-api-boilerplate/controller"
"go-api-boilerplate/dic"
_ "go-api-boilerplate/route/description" // For Swagger
"github.com/getsentry/raven-go"
"github.com/gin-contrib/sentry"
"github.com/gin-gonic/gin"
"github.com/mnvx/di"
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
"net/http"
)
var db = make(map[string]string)
func Setup(builder *di.Builder) *gin.Engine {
r := gin.Default()
client := dic.Container.Get(dic.RavenClient).(*raven.Client)
if client != nil {
r.Use(sentry.Recovery(client, false))
}
// Display Swagger documentation
r.StaticFile("doc/swagger.json", "doc/swagger.json")
config := &ginSwagger.Config{
URL: "/doc/swagger.json", //The url pointing to API definition
}
// use ginSwagger middleware to
r.GET("/swagger/*any", ginSwagger.CustomWrapHandler(config, swaggerFiles.Handler))
userController := dic.Container.Get(dic.UserController).(*controller.UserController)
// swagger:route GET /ping common getPing
//
// Ping
//
// Get Ping and reply Pong
//
// Responses:
// 200:
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
// swagger:route GET /users/:id user getUser
//
// User
//
// Get user data
//
// Responses:
// 200: UserResponse
r.GET("/users/:id", userController.Get)
// swagger:route GET /users user GetUsers
//
// Users list
//
// Get users list data
//
// Responses:
// 200: UsersResponse
r.GET("/users", userController.List)
// swagger:route POST /users user CreateUser
//
// New user
//
// Create new user
//
// Responses:
// 200: UserResponse
r.POST("/users", userController.Create)
// swagger:route PUT /users/:id user UpdateUser
//
// Update user
//
// Update existing user
//
// Responses:
// 200: UserResponse
r.PUT("/users/:id", userController.Update)
// swagger:route DELETE /users/:id user DeleteUser
//
// Delete user
//
// Delete existing user
//
// Responses:
// 200:
r.DELETE("/users/:id", userController.Delete)
// Authorized group (uses gin.BasicAuth() middleware)
// Same than:
// authorized := r.Group("/")
// authorized.Use(gin.BasicAuth(gin.Credentials{
// "foo": "bar",
// "manu": "123",
//}))
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
"foo": "bar", // user:foo password:bar
"manu": "123", // user:manu password:123
}))
authorized.POST("admin", func(c *gin.Context) {
user := c.MustGet(gin.AuthUserKey).(string)
// Parse JSON
var json struct {
Value string `json:"value" binding:"required"`
}
if c.Bind(&json) == nil {
db[user] = json.Value
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
})
return r
}