-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (96 loc) · 2.64 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
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
package main
import (
"bitmoi/api"
"bitmoi/app"
db "bitmoi/db/sqlc"
"bitmoi/gapi"
"bitmoi/mail"
"bitmoi/utilities"
"bitmoi/utilities/common"
"bitmoi/worker"
"database/sql"
"fmt"
"os"
"github.com/hibiken/asynq"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var bApp = app.NewApp()
func init() {
bApp.Commands = []*cli.Command{
app.StoreCommand,
app.PruneCommand,
}
bApp.Flags = []cli.Flag{
app.DatadirFlag,
app.GRPCFlag,
app.HTTPFlag,
app.LogLevelFlag,
}
bApp.Action = bitmoi
}
// @title Bitmoi API
// @version 1.0
// @description API for Bitmoi service
// @contact.name API Support
// @contact.email [email protected]
// @license.name CC BY-NC-SA 4.0
// @license.url https://creativecommons.org/licenses/by-nc-sa/4.0/
// @host api.bitmoi.co.kr
// @BasePath /
func main() {
if err := bApp.Run(os.Args); err != nil {
log.Panic().Err(err).Msg("Bitmoi API closed. Error: %s")
os.Exit(1)
}
}
func bitmoi(ctx *cli.Context) error {
config := utilities.GetConfig("./")
if path := ctx.String(app.DatadirFlag.Name); path != "" {
config.SetDataDir(path)
}
config.SetLogLevel(int8(ctx.Int(app.LogLevelFlag.Name)))
conn, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
return fmt.Errorf("cannot connect db %w", err)
}
dbStore := db.NewStore(conn)
errCh := make(chan error)
if isGrpcRun := ctx.Bool(app.GRPCFlag.Name); isGrpcRun {
server, err := gapi.NewServer(config, dbStore)
if err != nil {
log.Panic().Err(err).Msg("cannot create gRPC server")
}
go server.ListenGRPC(errCh)
go server.ListenGRPCGateWay(errCh)
}
if isHTTPRun := ctx.Bool(app.HTTPFlag.Name); isHTTPRun {
if config.Environment == common.EnvProduction {
go runTaskProcessor(config, dbStore)
}
go runHttpServer(config, dbStore, errCh)
}
return <-errCh
}
func runHttpServer(config *utilities.Config, store db.Store, errCh chan error) {
redisOpt := asynq.RedisClientOpt{
Addr: config.RedisAddress,
}
var taskDistributor worker.TaskDistributor
if config.Environment == common.EnvProduction {
taskDistributor = worker.NewRedisTaskDistributor(redisOpt)
}
server, err := api.NewServer(config, store, taskDistributor)
if err != nil {
log.Panic().Err(err).Msg("cannot create server")
}
errCh <- server.Listen()
}
func runTaskProcessor(config *utilities.Config, store db.Store) {
gmailSender := mail.NewGmailSender(config)
taskProcessor := worker.NewRedisTaskProcessor(asynq.RedisClientOpt{Addr: config.RedisAddress}, store, gmailSender, config.AccessTokenDuration)
log.Info().Msg("start task processor")
if err := taskProcessor.Start(); err != nil {
log.Panic().Err(err).Msg("failed to start task processor")
}
}