]>
Commit | Line | Data |
---|---|---|
1 | package main | |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "path" | |
6 | "strings" | |
7 | "time" | |
8 | ||
9 | "git.immae.eu/Cryptoportfolio/Front.git/api" | |
10 | "git.immae.eu/Cryptoportfolio/Front.git/db" | |
11 | ||
12 | "github.com/gin-contrib/cors" | |
13 | "github.com/gin-gonic/gin" | |
14 | "github.com/jloup/utils" | |
15 | "github.com/wercker/journalhook" | |
16 | ) | |
17 | ||
18 | var log = utils.StandardL().WithField("module", "api") | |
19 | ||
20 | type AppConfig struct { | |
21 | PublicDir string `toml:"public_dir"` | |
22 | } | |
23 | ||
24 | type ApiConfig struct { | |
25 | api.Config | |
26 | } | |
27 | ||
28 | type Config struct { | |
29 | App AppConfig | |
30 | Api ApiConfig | |
31 | Mail api.MailConfig | |
32 | Db db.DBConfig | |
33 | Redis db.RedisConfig | |
34 | ||
35 | utils.LogConfiguration | |
36 | Address string | |
37 | Port string | |
38 | Mode string | |
39 | } | |
40 | ||
41 | func (c *Config) SetToDefaults() { | |
42 | *c = Config{ | |
43 | Address: "localhost", | |
44 | Port: "8000", | |
45 | Mode: "dev", | |
46 | App: AppConfig{ | |
47 | PublicDir: "./public", | |
48 | }, | |
49 | } | |
50 | ||
51 | c.LogConfiguration.SetToDefaults() | |
52 | } | |
53 | ||
54 | var C Config | |
55 | ||
56 | func init() { | |
57 | utils.MustParseStdConfigFile(&C) | |
58 | ||
59 | err := utils.ConfigureStdLogger(C.LogConfiguration) | |
60 | if err != nil { | |
61 | panic(err) | |
62 | } | |
63 | ||
64 | if C.Mode == "prod" { | |
65 | gin.DisableConsoleColor() | |
66 | gin.SetMode(gin.ReleaseMode) | |
67 | journalhook.Enable() | |
68 | } | |
69 | ||
70 | api.SetConfig(C.Api.Config) | |
71 | api.SetMailConfig(C.Mail) | |
72 | ||
73 | db.Init(C.Db, C.Redis) | |
74 | ||
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 | ||
80 | func 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 | ||
93 | func 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 | ||
112 | func main() { | |
113 | engine := gin.New() | |
114 | ||
115 | apiGroup := engine.Group("/api") | |
116 | appGroup := engine | |
117 | ||
118 | engine.Use(gin.Recovery()) | |
119 | ||
120 | if C.Mode == "prod" { | |
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", | |
146 | "/confirm", | |
147 | "/reset-password", | |
148 | "/change-password", | |
149 | "/signout", | |
150 | "/me", | |
151 | "/admin", | |
152 | "/account", | |
153 | "/otp/enroll", | |
154 | "/otp/validate", | |
155 | "/not_confirmed", | |
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 | } |