/
main.go
64 lines (56 loc) · 1.15 KB
/
main.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
package main
import (
"context"
"log"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
"github.com/taral14/filmun/src/auth"
"github.com/taral14/filmun/src/film"
"github.com/taral14/filmun/src/person"
"github.com/taral14/filmun/src/server"
"github.com/taral14/filmun/src/user"
"go.uber.org/fx"
)
func init() {
viper.SetEnvPrefix("FILMS_")
viper.AddConfigPath("./config")
viper.SetConfigName("config")
err := viper.ReadInConfig()
if err != nil {
log.Panic(err)
}
}
func main() {
app := fx.New(
server.Module,
person.Module,
film.Module,
user.Module,
auth.Module,
fx.Provide(initDB),
fx.Invoke(register),
)
app.Run()
}
func register(lifecycle fx.Lifecycle, app *server.App) {
lifecycle.Append(
fx.Hook{
OnStart: func(ctx context.Context) error {
go app.Start(ctx)
return nil
},
OnStop: func(ctx context.Context) error {
log.Println("Shutting down server...")
return app.Shutdown(ctx)
},
},
)
}
func initDB() *sqlx.DB {
db, err := sqlx.Connect("mysql", viper.GetString("mysql.connection"))
if err != nil {
log.Panic("init DB: " + err.Error())
}
return db
}