-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (48 loc) · 1.03 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
package main
import (
"context"
"net/http"
"cloud.google.com/go/firestore"
"github.com/gorilla/mux"
"github.com/mager/caffy-beans-example/config"
"github.com/mager/caffy-beans-example/database"
"github.com/mager/caffy-beans-example/logger"
handler "github.com/mager/caffy-beans-example/route_handler"
"github.com/mager/caffy-beans-example/router"
"go.uber.org/fx"
"go.uber.org/zap"
)
func main() {
fx.New(
fx.Provide(
config.Options,
database.Options,
logger.Options,
router.Options,
),
fx.Invoke(Register),
).Run()
}
func Register(
lc fx.Lifecycle,
cfg *config.Config,
database *firestore.Client,
logger *zap.SugaredLogger,
router *mux.Router,
) {
lc.Append(
fx.Hook{
OnStart: func(context.Context) error {
logger.Info("Listening on ", cfg.Application.Address)
go http.ListenAndServe(cfg.Application.Address, router)
return nil
},
OnStop: func(context.Context) error {
defer logger.Sync()
defer database.Close()
return nil
},
},
)
handler.New(logger, router, database)
}