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