]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - cmd/app/main.go
Mails.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / app / main.go
CommitLineData
7a9e5112 1package main
2
3import (
4 "fmt"
5 "path"
6 "strings"
7 "time"
8
9 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/api"
10 "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
11
12 "github.com/gin-contrib/cors"
13 "github.com/gin-gonic/gin"
14 "github.com/jloup/utils"
15)
16
17var log = utils.StandardL().WithField("module", "api")
18
19type AppConfig struct {
20 PublicDir string `toml:"public_dir"`
21}
22
23type ApiConfig struct {
85545aba 24 api.Config
7a9e5112 25}
26
27type Config struct {
323b7f40 28 App AppConfig
29 Api ApiConfig
2da5b12c 30 Mail api.MailConfig
323b7f40 31 Db db.DBConfig
32 Redis db.RedisConfig
7a9e5112 33
34 utils.LogConfiguration
35 Address string
36 Port string
37 Mode string
38}
39
40func (c *Config) SetToDefaults() {
41 *c = Config{
42 Address: "localhost",
43 Port: "8000",
44 Mode: "dev",
45 App: AppConfig{
46 PublicDir: "./public",
47 },
7a9e5112 48 }
49
50 c.LogConfiguration.SetToDefaults()
51}
52
53var C Config
54
55func init() {
56 utils.MustParseStdConfigFile(&C)
57
58 err := utils.ConfigureStdLogger(C.LogConfiguration)
59 if err != nil {
60 panic(err)
61 }
62
85545aba 63 api.SetConfig(C.Api.Config)
2da5b12c 64 api.SetMailConfig(C.Mail)
7a9e5112 65
323b7f40 66 db.Init(C.Db, C.Redis)
7a9e5112 67
68 if C.Mode == "production" {
69 gin.SetMode(gin.ReleaseMode)
70 }
71
72 log.Infof("CONFIG:")
73 log.Infof("LISTEN: %s", strings.Join([]string{C.Address, C.Port}, ":"))
74 log.Infof("PUBLIC_DIR: %s", C.App.PublicDir)
75}
76
77func SetApiRoute(router *gin.RouterGroup, route api.Route) {
78 switch route.Method {
79 case "GET":
80 router.GET(route.Path, route.Handlers...)
81 case "POST":
82 router.POST(route.Path, route.Handlers...)
83 case "OPTIONS":
84 router.OPTIONS(route.Path, route.Handlers...)
85 default:
86 panic(fmt.Errorf("%s method not handled", route.Method))
87 }
88}
89
90func SetGroup(router *gin.RouterGroup, group api.Group) {
91 var r *gin.RouterGroup
92 if group.Root == "" {
93 r = router
94 } else {
95 r = router.Group(group.Root)
96 }
97
98 if group.Middlewares != nil {
99 for _, middleware := range group.Middlewares {
100 r.Use(api.M(middleware))
101 }
102 }
103
104 for _, route := range group.Routes {
105 SetApiRoute(r, route)
106 }
107}
108
109func main() {
110 engine := gin.New()
111
112 apiGroup := engine.Group("/api")
113 appGroup := engine
114
115 engine.Use(gin.Recovery())
116
117 if C.Mode == "production" {
118 engine.Use(api.Logger())
119 apiGroup.Use(api.Logger())
120 } else {
121 engine.Use(gin.Logger())
122 apiGroup.Use(gin.Logger())
123 }
124
125 apiGroup.Use(cors.New(cors.Config{
126 AllowOrigins: []string{fmt.Sprintf("https://%s", C.Api.Domain)},
127 AllowMethods: []string{"POST", "GET", "OPTIONS"},
128 AllowHeaders: []string{"Authorization"},
129 ExposeHeaders: []string{"Authorization"},
130 AllowCredentials: true,
131 MaxAge: 12 * time.Hour,
132 }))
133
134 for _, group := range api.Groups {
135 SetGroup(apiGroup, group)
136 }
137
138 appGroup.Static("/public", C.App.PublicDir)
139 availableRoutes := []string{
140 "/",
141 "/signup",
142 "/signin",
2da5b12c 143 "/confirm",
85545aba 144 "/reset-password",
145 "/change-password",
7a9e5112 146 "/signout",
147 "/me",
16e43cc7 148 "/account",
7a9e5112 149 "/otp/enroll",
150 "/otp/validate",
adf936f6 151 "/not_confirmed",
7a9e5112 152 }
153
154 for _, route := range availableRoutes {
155 appGroup.StaticFile(route, path.Join(C.App.PublicDir, "/index.html"))
156 }
157
158 engine.Run(strings.Join([]string{C.Address, C.Port}, ":"))
159}