/
ginrouter.go
145 lines (121 loc) · 4.85 KB
/
ginrouter.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
package route
import (
"log"
"github.com/gin-gonic/gin"
"github.com/bsinou/vitrnx-goback/auth"
"github.com/bsinou/vitrnx-goback/handler"
"github.com/bsinou/vitrnx-goback/model"
)
func StartRouter() {
r := gin.Default()
r.Use(loggingHandler(), cors(), checkCredentials(), connect())
declareRoutes(r)
log.Fatal(r.Run(":8888"))
// debug router without auth
// x := gin.Default()
// x.Use(loggingHandler(), cors(), Connect())
// declareRoutes(x)
// log.Fatal(x.Run(":9999"))
}
func declareRoutes(r *gin.Engine) {
// Authentication
authG := r.Group(model.ApiPrefix + "auth")
{
// authG.Use()
authG.OPTIONS("login", handler.DoNothing) // POST
authG.POST("login", auth.PostLogin)
}
// Users
user := r.Group(model.ApiPrefix + "users")
{
// Configure wrappers for this group
// user.Use(loggingHandler(), cors(), checkCredentials(), Connect(), addUserMeta())
user.Use(addUserMeta())
// Enable fetch with js and CORS
user.OPTIONS("", handler.DoNothing)
user.OPTIONS(":id", handler.DoNothing)
user.OPTIONS(":id/roles", handler.DoNothing)
// REST
user.GET("", handler.GetUsers) // query with params
user.GET(":"+model.KeyUserID, handler.GetUser) // get one
user.POST("", applyUserCreationPolicies(), handler.CreateUser) // CREATION
user.PATCH(":id", applyUserUpdatePolicies(), handler.PatchUser)
user.DELETE(":"+model.KeyUserID, applyUserDeletePolicies(), handler.DeleteUser)
user.PATCH(":id/roles", applyUserRolesUpdatePolicies(), handler.PatchUserRoles)
}
// UserMeta
meta := r.Group(model.ApiPrefix + "usermeta")
presence := r.Group(model.ApiPrefix + "presence")
da := r.Group(model.ApiPrefix + "dreamAddresses")
{
meta.Use(addUserMeta())
meta.OPTIONS("", handler.DoNothing)
meta.OPTIONS(":"+model.KeyUserID, handler.DoNothing)
presence.OPTIONS("/guestsByDay", handler.DoNothing)
presence.OPTIONS("/guestNb", handler.DoNothing)
da.OPTIONS("", handler.DoNothing)
presence.GET("/guestsByDay", handler.ListGuestsByDay)
presence.GET("/guestNb", handler.GuestTotal)
// REST
meta.GET(":"+model.KeyUserID, handler.ReadPresence)
meta.POST(":"+model.KeyUserID, applyUserMetaPolicies(), handler.PutPresence)
da.GET("", handler.GetDreamAddresses)
}
// Roles
roles := r.Group(model.ApiPrefix + "roles")
{
roles.OPTIONS("", handler.DoNothing) // POST
roles.OPTIONS(":"+model.KeyMgoID, handler.DoNothing) // PUT, DELETE
roles.GET("", handler.GetRoles) // query with params
}
// Groups
groups := r.Group(model.ApiPrefix + "groups")
{
groups.OPTIONS("", handler.DoNothing) // POST
groups.GET("", handler.GetGroups) // query with params
}
// Posts
posts := r.Group(model.ApiPrefix + "posts")
{
// Configure wrappers for this group
// posts.Use(loggingHandler(), cors(), checkCredentials(), Connect(), addUserMeta(), unmarshallPost(), applyPostPolicies())
posts.Use(addUserMeta(), unmarshallPost(), applyPostPolicies())
// Enable fetch with js and CORS
posts.OPTIONS("", handler.DoNothing) // POST
posts.OPTIONS(":"+model.KeyPath, handler.DoNothing) // PUT, DELETE
posts.OPTIONS(":"+model.KeyPath+"/comments", handler.DoNothing) // PUT, DELETE
// REST
posts.GET("", handler.ListPosts) // query with params
posts.GET(":"+model.KeyPath, handler.ReadPost) // get one
posts.GET(":"+model.KeyPath+"/comments", handler.ListPostComments) // get post comments
posts.POST("", handler.PutPost) // new post
posts.POST(":"+model.KeyPath, handler.PutPost) // update post
posts.DELETE(":"+model.KeyPath, handler.DeletePost) // delete post
}
// Comments
comments := r.Group(model.ApiPrefix + "comments")
{
// comments.Use(loggingHandler(), cors(), checkCredentials(), Connect(), addUserMeta(), unmarshallComment(), applyCommentPolicies())
comments.Use(addUserMeta(), unmarshallComment(), applyCommentPolicies())
comments.OPTIONS("", handler.DoNothing)
comments.OPTIONS(":"+model.KeyMgoID, handler.DoNothing)
// REST
comments.GET("", handler.ListComments) // query with params
comments.POST("", handler.PutComment) // Create or update
comments.DELETE(":"+model.KeyMgoID, handler.DeleteComment) // delete comment
}
// Tasks
tasks := r.Group(model.ApiPrefix + "tasks")
{
tasks.Use(addUserMeta(), unmarshallTask(), applyTaskPolicies())
tasks.OPTIONS("", handler.DoNothing)
// tasks.OPTIONS(":"+model.KeyCategoryID, handler.DoNothing)
tasks.OPTIONS(":"+model.KeyMgoID, handler.DoNothing)
// REST
tasks.GET("", handler.ListTasks)
tasks.GET(":"+model.KeyCategoryID, handler.ListTasks)
tasks.POST("", handler.PutTask)
tasks.POST(":"+model.KeyMgoID, handler.PutTask)
tasks.DELETE(":"+model.KeyMgoID, handler.DeleteTask)
}
}