forked from JotaEspig/sigma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
118 lines (95 loc) · 3.15 KB
/
router.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
package main
import (
"net/http"
"os"
"sigma/controllers"
"sigma/middlewares"
"github.com/gin-gonic/gin"
"github.com/newrelic/go-agent/v3/integrations/nrgin"
"github.com/newrelic/go-agent/v3/newrelic"
)
func setNewRelicMiddleware(router *gin.Engine) {
nrAppName := os.Getenv("NR_APP_NAME")
nrAPIKey := os.Getenv("NR_API_KEY")
app, err := newrelic.NewApplication(
newrelic.ConfigAppName(nrAppName),
newrelic.ConfigLicense(nrAPIKey),
newrelic.ConfigDistributedTracerEnabled(true),
)
if err != nil {
panic(err)
}
router.Use(nrgin.Middleware(app))
}
// Gets the type of router engine according to ginMode.
// ginMode should be an env variable
func getRouterEngine() *gin.Engine {
routerMode := os.Getenv("ROUTER_MODE")
if routerMode == "release" {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
// Don't use logs middleware
return router
}
if routerMode == "staging" {
router := gin.Default()
setNewRelicMiddleware(router)
return router
}
return gin.Default()
}
// Set the routes to a router
func setRoutes(router *gin.Engine) {
// Login
router.GET("/", controllers.LoginRedirect())
router.GET("/login", controllers.LoginGET())
router.POST("/login", controllers.LoginPOST())
router.GET("/login/validate", controllers.IsLogged())
// Cadastro
router.GET("/cadastro", controllers.SignupGET())
router.POST("/cadastro", controllers.SignupPOST())
// User group
user := router.Group("/usuario", middlewares.AuthMiddleware())
user.GET("", controllers.GetProfilePage())
user.GET("/get", controllers.GetAllUserInfo())
user.PUT("/update", controllers.UpdateUser())
// Public user group (everyone can access this)
publicUser := router.Group("/:username")
publicUser.GET("", controllers.GetUserPage())
publicUser.GET("/get", controllers.GetPublicUserInfo())
// Student group
student := router.Group("/aluno", middlewares.IsStudentMiddleware())
student.GET("", controllers.GetStudentPage())
student.GET("/get", controllers.GetStudentInfo())
// Teacher group
teacher := router.Group("/professor", middlewares.IsTeacherMiddleware())
teacher.GET("", controllers.GetTeacherPage())
teacher.GET("/get", controllers.GetTeacherInfo())
teacher.GET("/update", controllers.UpdateTeacher())
// Admin group
admin := router.Group("/admin", middlewares.IsAdminMiddleware())
admin.GET("", controllers.GetAdminPage())
admin.GET("/get", controllers.GetAdminInfo())
admin.PUT("/update", controllers.UpdateAdmin())
// Admin tools group
adminTools := admin.Group("/tools")
// Admin tools to manage others admins
adminToolsForAdmin := adminTools.Group("/admin/:target",
middlewares.IsSuperAdminMiddleware())
adminToolsForAdmin.PUT("/update", controllers.UpdateTargetAdmin())
adminToolsForAdmin.DELETE("/delete", controllers.DeleteTargetAdmin())
}
func createRouter() *gin.Engine {
router := getRouterEngine()
router.LoadHTMLGlob("static/html/*.html")
// Loads the img, css and js folders
router.Static("css/", "static/css/")
router.Static("js/", "static/js/")
router.Static("img/", "static/img/")
router.NoRoute(func(ctx *gin.Context) {
ctx.HTML(http.StatusNotFound, "404.html", nil)
})
setRoutes(router)
return router
}