]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - cmd/app/main.go
Mails.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / app / main.go
1 package main
2
3 import (
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
17 var log = utils.StandardL().WithField("module", "api")
18
19 type AppConfig struct {
20 PublicDir string `toml:"public_dir"`
21 }
22
23 type ApiConfig struct {
24 api.Config
25 }
26
27 type Config struct {
28 App AppConfig
29 Api ApiConfig
30 Mail api.MailConfig
31 Db db.DBConfig
32 Redis db.RedisConfig
33
34 utils.LogConfiguration
35 Address string
36 Port string
37 Mode string
38 }
39
40 func (c *Config) SetToDefaults() {
41 *c = Config{
42 Address: "localhost",
43 Port: "8000",
44 Mode: "dev",
45 App: AppConfig{
46 PublicDir: "./public",
47 },
48 }
49
50 c.LogConfiguration.SetToDefaults()
51 }
52
53 var C Config
54
55 func init() {
56 utils.MustParseStdConfigFile(&C)
57
58 err := utils.ConfigureStdLogger(C.LogConfiguration)
59 if err != nil {
60 panic(err)
61 }
62
63 api.SetConfig(C.Api.Config)
64 api.SetMailConfig(C.Mail)
65
66 db.Init(C.Db, C.Redis)
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
77 func 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
90 func 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
109 func 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",
143 "/confirm",
144 "/reset-password",
145 "/change-password",
146 "/signout",
147 "/me",
148 "/account",
149 "/otp/enroll",
150 "/otp/validate",
151 "/not_confirmed",
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 }