-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaltair.go
333 lines (281 loc) · 8.33 KB
/
altair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
_ "github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/subosito/gotenv"
"github.com/kodefluence/altair/cfg"
"github.com/kodefluence/altair/core"
"github.com/kodefluence/altair/module/apierror"
"github.com/kodefluence/altair/module/app"
"github.com/kodefluence/altair/module/controller"
"github.com/kodefluence/altair/module/healthcheck"
"github.com/kodefluence/altair/module/projectgenerator"
"github.com/kodefluence/altair/module/router"
"github.com/kodefluence/altair/plugin"
"github.com/kodefluence/monorepo/db"
"github.com/spf13/cobra"
)
var (
dbConfigs map[string]core.DatabaseConfig = map[string]core.DatabaseConfig{}
databases map[string]db.DB = map[string]db.DB{}
appConfig core.AppConfig
pluginBearer core.PluginBearer
)
func main() {
_ = gotenv.Load()
loadConfig()
executeCommand()
}
func loadConfig() {
dbConfigs, _ = cfg.Database().Compile("config/database.yml")
appConfig, _ = cfg.App().Compile("config/app.yml")
pluginBearer, _ = cfg.Plugin().Compile("config/plugin/")
}
func executeCommand() {
rootCmd := &cobra.Command{
Use: "altair",
Short: "Light Weight and Robust API Gateway.",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
}
runCmd := &cobra.Command{
Use: "run",
Short: "Run API gateway services.",
Run: func(cmd *cobra.Command, args []string) {
if appConfig == nil {
fmt.Println("App config is not loaded, only run command in altair working directory")
return
}
defer closeConnection()
if err := fabricateConnection(); err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error running altair")
return
}
if err := runAPI(); err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error running altair API")
}
},
}
configCmd := &cobra.Command{
Use: "config",
Short: "See list of configs",
Example: "altair config app",
Run: func(cmd *cobra.Command, args []string) {
if appConfig == nil {
fmt.Println("App config is not loaded, only run command in altair working directory")
return
}
if len(args) < 1 {
fmt.Println("Invalid number of arguments, expected 1. Example `altair config [config_name]`.")
fmt.Println("Available option:")
fmt.Println("- all")
fmt.Println("- app")
fmt.Println("- db")
return
}
app := func() {
fmt.Println("app config:")
fmt.Println("====================")
fmt.Print(appConfig.Dump())
fmt.Println("--------------------")
}
db := func() {
fmt.Println("db config:")
fmt.Println("====================")
for key, config := range dbConfigs {
fmt.Printf("instance: %s\n", key)
fmt.Printf("driver: %s\n", config.Driver())
fmt.Println("--------------------")
fmt.Print(config.Dump())
}
fmt.Println("--------------------")
}
switch args[0] {
case "all":
app()
fmt.Println()
db()
case "app":
app()
case "db":
db()
default:
fmt.Println("Invalid argument. Available: [app, db, all]")
return
}
},
}
appController := controller.Provide(nil, nil, rootCmd)
appModule := app.Provide(appController)
projectgenerator.Load(appModule)
pluginCmd := &cobra.Command{
Use: "plugin",
Short: "List of plugin commands",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
if appConfig == nil {
fmt.Println("App config is not loaded, only run command in altair working directory")
return
}
defer closeConnection()
if err := fabricateConnection(); err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error running altair")
return
}
apiError := apierror.Provide()
pluginController := controller.Provide(nil, apiError, cmd)
pluginModule := app.Provide(pluginController)
appBearer := cfg.AppBearer(nil, appConfig)
dbBearer := cfg.DatabaseBearer(databases, dbConfigs)
if err := plugin.LoadCommand(appBearer, pluginBearer, dbBearer, apiError, pluginModule); err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error generating plugins")
}
childCmd, _, err := cmd.Find(args)
if err != nil || childCmd.Use == cmd.Use {
_ = cmd.Help()
} else {
_ = childCmd.Execute()
}
},
}
rootCmd.AddCommand(runCmd, pluginCmd, configCmd)
_ = rootCmd.Execute()
}
func dbConnectionFabricator(instanceName string, dbConfig core.DatabaseConfig) (db.DB, error) {
port, err := dbConfig.DBPort()
if err != nil {
return nil, err
}
maxConnLifetime, err := dbConfig.DBConnectionMaxLifetime()
if err != nil {
return nil, err
}
maxIdleConn, err := dbConfig.DBMaxIddleConn()
if err != nil {
return nil, err
}
maxOpenConn, err := dbConfig.DBMaxOpenConn()
if err != nil {
return nil, err
}
sqldb, err := db.FabricateMySQL(instanceName, db.Config{
Username: dbConfig.DBUsername(),
Password: dbConfig.DBPassword(),
Host: dbConfig.DBHost(),
Port: fmt.Sprintf("%d", port),
Name: dbConfig.DBDatabase(),
}, db.WithConnMaxLifetime(maxConnLifetime), db.WithMaxIdleConn(maxIdleConn), db.WithMaxOpenConn(maxOpenConn))
if err != nil {
return nil, err
}
log.Info().Msg(fmt.Sprintf("Complete fabricating mysql writer connection: %s:%s@tcp(%s:%d)/%s?", dbConfig.DBUsername(), "***********", dbConfig.DBHost(), port, dbConfig.DBDatabase()))
return sqldb, nil
}
func fabricateConnection() error {
for key, config := range dbConfigs {
sqlDB, err := dbConnectionFabricator(key, config)
if err != nil {
return err
}
databases[key] = sqlDB
}
return nil
}
func closeConnection() {
excs := db.CloseAll()
for _, exc := range excs {
log.Error().
Err(exc).
Stack().
Str("detail", exc.Detail()).
Array("tags", zerolog.Arr().Str("altair").Str("main").Str(exc.Title())).
Msg("Error closing mysql writer")
}
}
func runAPI() error {
gin.SetMode(gin.ReleaseMode)
apiEngine := gin.New()
apiError := apierror.Provide()
baseController := controller.Provide(apiEngine.Handle, apiError, nil)
baseModule := app.Provide(baseController)
healthcheck.Load(baseModule)
pluginEngine := apiEngine.Group("/_plugins/", gin.BasicAuth(gin.Accounts{
appConfig.BasicAuthUsername(): appConfig.BasicAuthPassword(),
}))
pluginController := controller.Provide(pluginEngine.Handle, apiError, nil)
pluginModule := app.Provide(pluginController)
appBearer := cfg.AppBearer(pluginEngine, appConfig)
dbBearer := cfg.DatabaseBearer(databases, dbConfigs)
if err := plugin.Load(appBearer, pluginBearer, dbBearer, apiError, pluginModule); err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error generating plugins")
return err
}
compiler, forwarder := router.Provide(pluginModule.Controller().ListDownstream(), pluginModule.Controller().ListMetric())
routeObjects, err := compiler.Compile("./routes")
if err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error compiling routes")
return err
}
err = forwarder.Generate(apiEngine, routeObjects)
if err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error generating routes")
return err
}
gracefulSignal := make(chan os.Signal, 1)
signal.Notify(gracefulSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", appConfig.Port()),
Handler: apiEngine,
}
log.Info().Msg(fmt.Sprintf("Running Altair in: http://127.0.0.1:%d", appConfig.Port()))
if err := srv.ListenAndServe(); err != nil {
log.Error().
Err(err).
Stack().
Array("tags", zerolog.Arr().Str("altair").Str("main")).
Msg("Error running api engine")
}
}()
closeSignal := <-gracefulSignal
log.Info().Array("tags", zerolog.Arr().Str("altair").Str("main")).Msg(fmt.Sprintf("Receiving %s signal.... Cleaning up processes.", closeSignal.String()))
return nil
}