-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (57 loc) · 1.72 KB
/
main.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
package main
import (
"aaa311/utils"
"aaa311/views"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func main() {
initialize()
router := getEngine()
router.Run(fmt.Sprintf(":%s", viper.GetString("server.port")))
}
//getEngine sets all the backend endpoints and handlers
func getEngine() *gin.Engine {
router := gin.Default()
router.Use(gin.ErrorLoggerT(gin.ErrorTypePrivate))
router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
router.POST("/authenticate", views.Authenticate)
router.POST("/sign-up", views.CreateUser)
router.POST("/recovery/password", views.ForgotPassword)
router.POST("/recovery/change-password", views.ChangePassword)
v1 := router.Group("/api/aaa/v1", utils.CheckJWTToken())
{
v1.POST("/verify", views.Verify)
}
users := v1.Group("/users")
{
users.GET("/:username/roles", views.GetUserRoles)
users.POST("/:username/roles", views.AssociateRoles)
users.DELETE("/:username/roles/:role", views.DeleteRoles)
users.PUT("/:username", views.UpdateUser)
}
return router
}
func initialize() {
//read the config file and sets various variables used throughout the backend
viper.AddConfigPath("/run/secrets")
viper.AddConfigPath("/opt")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("toml")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
viper.BindEnv("database.dir", "DATABASE_DIR")
viper.BindEnv("mail.sender", "MAIL_SENDER")
viper.BindEnv("mail.smtp", "MAIL_SMTP")
log.SetFormatter(&log.TextFormatter{ForceColors: true})
log.SetLevel(log.DebugLevel)
//sets constants used by the token related functions
utils.InitiateTokenParams()
}