-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathroutes.go
133 lines (119 loc) · 4.3 KB
/
routes.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
package master
import (
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
c "github.com/sdslabs/gasper/services/master/controllers"
m "github.com/sdslabs/gasper/services/master/middlewares"
"github.com/sdslabs/gasper/types"
)
// ServiceName is the name of the current microservice
const ServiceName = types.Master
// NewService returns a new instance of the current microservice
func NewService() http.Handler {
// router is the main routes handler for the current microservice package
router := gin.Default()
corsConfig := cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization", "Cookie", "Authorization-Type"},
AllowCredentials: false,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
}
router.Use(cors.New(corsConfig))
router.NoRoute(c.Handle404)
// Bind frontend generated from https://github.com/sdslabs/SWS
router.GET("", func(ctx *gin.Context) {
ctx.Data(200, frontendBinder["index.html"].responseHeader, frontendBinder["index.html"].content)
})
for file, box := range frontendBinder {
// A deep copy of the filename and the box is made because as the loop iterator traverses
// all of the handler functions point to the last element of the map which is not correct
// This is due to the iterator occupying a single instance of heap memory and all handler
// functions pointing to that single heap memory instance
// Making a deep copy makes separate clones of heap memory instances thereby preventing
// the undesired override
fileDeepCopy := file
boxDeepCopy := box
router.GET("/"+fileDeepCopy, func(ctx *gin.Context) {
ctx.Data(200, boxDeepCopy.responseHeader, boxDeepCopy.content)
})
}
auth := router.Group("/auth")
{
auth.POST("/login", m.LoginHandler)
auth.POST("/register", m.ValidateRegistration, c.Register)
auth.GET("/refresh", m.RefreshHandler)
auth.PUT("/revoke", c.RevokeToken)
}
router.GET("/instances", m.AuthRequired(), c.FetchAllInstancesByUser)
router.POST("/gctllogin", m.JWTGctl.MiddlewareFunc(), c.GctlLogin)
github := router.Group("/github")
{
github.POST("", m.AuthRequired(), c.CreateRepository)
github.POST("/token", m.AuthRequired(), c.FetchPAT)
github.POST("/delete", m.AuthRequired(), c.DeleteRepository)
}
app := router.Group("/apps")
app.Use(m.AuthRequired())
{
app.POST("/:language", m.ValidateApplicationRequest, c.CreateApp)
app.GET("", c.FetchAppsByUser)
app.GET("/:app", m.IsAppOwner, c.GetApplicationInfo)
app.PUT("/:app", m.IsAppOwner, c.UpdateAppByName)
app.DELETE("/:app", m.IsAppOwner, c.DeleteApp)
app.GET("/:app/logs", m.IsAppOwner, c.FetchAppLogs)
app.PATCH("/:app/rebuild", m.IsAppOwner, c.RebuildApp)
app.PATCH("/:app/transfer/:user", m.IsAppOwner, c.TransferApplicationOwnership)
app.GET("/:app/term", m.IsAppOwner, c.DeployWebTerminal)
app.GET("/:app/metrics", m.IsAppOwner, c.FetchMetrics)
app.GET("/:app/remote", m.IsAppOwner, c.FetchAppRemote)
}
db := router.Group("/dbs")
db.Use(m.AuthRequired())
{
db.POST("/:database", m.ValidateDatabaseRequest, c.CreateDatabase)
db.GET("", c.FetchDatabasesByUser)
db.GET("/:db", m.IsDatabaseOwner, c.GetDatabaseInfo)
db.DELETE("/:db", m.IsDatabaseOwner, c.DeleteDatabase)
db.PATCH("/:db/transfer/:user", m.IsDatabaseOwner, c.TransferDatabaseOwnership)
}
user := router.Group("/user")
user.Use(m.AuthRequired())
{
user.GET("", c.GetLoggedInUserInfo)
user.PUT("/password", c.UpdatePassword)
user.DELETE("", c.DeleteUser)
}
admin := router.Group("/admin")
admin.Use(m.AuthRequired(), m.VerifyAdmin)
{
apps := admin.Group("/apps")
{
apps.GET("", c.GetAllApplications)
apps.GET("/:app", c.GetApplicationInfo)
apps.DELETE("/:app", c.DeleteApp)
}
dbs := admin.Group("/dbs")
{
dbs.GET("", c.GetAllDatabases)
dbs.GET("/:db", c.GetDatabaseInfo)
dbs.DELETE("/:db", c.DeleteDatabase)
}
users := admin.Group("/users")
{
users.GET("", c.GetAllUsers)
users.GET("/:user", c.GetUserInfo)
users.DELETE("/:user", c.DeleteUserByAdmin)
users.PATCH("/:user/grant", c.GrantSuperuserPrivilege)
users.PATCH("/:user/revoke", c.RevokeSuperuserPrivilege)
}
nodes := admin.Group("/nodes")
{
nodes.GET("", c.GetAllNodes)
nodes.GET("/:type", c.GetNodesByName)
}
}
return router
}