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