]>
Commit | Line | Data |
---|---|---|
7a9e5112 | 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 { | |
85545aba | 24 | api.Config |
7a9e5112 | 25 | } |
26 | ||
27 | type 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 | ||
40 | func (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 | ||
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 | ||
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 | |
39183537 | 68 | if C.Mode == "prod" { |
36942af3 | 69 | gin.DisableConsoleColor() |
7a9e5112 | 70 | gin.SetMode(gin.ReleaseMode) |
71 | } | |
72 | ||
73 | log.Infof("CONFIG:") | |
74 | log.Infof("LISTEN: %s", strings.Join([]string{C.Address, C.Port}, ":")) | |
75 | log.Infof("PUBLIC_DIR: %s", C.App.PublicDir) | |
76 | } | |
77 | ||
78 | func SetApiRoute(router *gin.RouterGroup, route api.Route) { | |
79 | switch route.Method { | |
80 | case "GET": | |
81 | router.GET(route.Path, route.Handlers...) | |
82 | case "POST": | |
83 | router.POST(route.Path, route.Handlers...) | |
84 | case "OPTIONS": | |
85 | router.OPTIONS(route.Path, route.Handlers...) | |
86 | default: | |
87 | panic(fmt.Errorf("%s method not handled", route.Method)) | |
88 | } | |
89 | } | |
90 | ||
91 | func SetGroup(router *gin.RouterGroup, group api.Group) { | |
92 | var r *gin.RouterGroup | |
93 | if group.Root == "" { | |
94 | r = router | |
95 | } else { | |
96 | r = router.Group(group.Root) | |
97 | } | |
98 | ||
99 | if group.Middlewares != nil { | |
100 | for _, middleware := range group.Middlewares { | |
101 | r.Use(api.M(middleware)) | |
102 | } | |
103 | } | |
104 | ||
105 | for _, route := range group.Routes { | |
106 | SetApiRoute(r, route) | |
107 | } | |
108 | } | |
109 | ||
110 | func main() { | |
111 | engine := gin.New() | |
112 | ||
113 | apiGroup := engine.Group("/api") | |
114 | appGroup := engine | |
115 | ||
116 | engine.Use(gin.Recovery()) | |
117 | ||
118 | if C.Mode == "production" { | |
119 | engine.Use(api.Logger()) | |
120 | apiGroup.Use(api.Logger()) | |
121 | } else { | |
122 | engine.Use(gin.Logger()) | |
123 | apiGroup.Use(gin.Logger()) | |
124 | } | |
125 | ||
126 | apiGroup.Use(cors.New(cors.Config{ | |
127 | AllowOrigins: []string{fmt.Sprintf("https://%s", C.Api.Domain)}, | |
128 | AllowMethods: []string{"POST", "GET", "OPTIONS"}, | |
129 | AllowHeaders: []string{"Authorization"}, | |
130 | ExposeHeaders: []string{"Authorization"}, | |
131 | AllowCredentials: true, | |
132 | MaxAge: 12 * time.Hour, | |
133 | })) | |
134 | ||
135 | for _, group := range api.Groups { | |
136 | SetGroup(apiGroup, group) | |
137 | } | |
138 | ||
139 | appGroup.Static("/public", C.App.PublicDir) | |
140 | availableRoutes := []string{ | |
141 | "/", | |
142 | "/signup", | |
143 | "/signin", | |
2da5b12c | 144 | "/confirm", |
85545aba | 145 | "/reset-password", |
146 | "/change-password", | |
7a9e5112 | 147 | "/signout", |
148 | "/me", | |
2e4885d9 | 149 | "/admin", |
16e43cc7 | 150 | "/account", |
7a9e5112 | 151 | "/otp/enroll", |
152 | "/otp/validate", | |
adf936f6 | 153 | "/not_confirmed", |
7a9e5112 | 154 | } |
155 | ||
156 | for _, route := range availableRoutes { | |
157 | appGroup.StaticFile(route, path.Join(C.App.PublicDir, "/index.html")) | |
158 | } | |
159 | ||
160 | engine.Run(strings.Join([]string{C.Address, C.Port}, ":")) | |
161 | } |