-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
64 lines (57 loc) · 1.38 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"
"github.com/axengine/ethevent/pkg/chainindex"
"github.com/axengine/ethevent/pkg/database"
"github.com/axengine/ethevent/pkg/http"
"github.com/axengine/ethevent/pkg/svc"
"github.com/axengine/utils/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
)
// @title eth-events API
// @version 0.1.0
// @description
// @host
// @BasePath /
func main() {
dbo := database.New(filepath.Join(viper.GetString("datadir"), "events.db"), log.Logger)
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
ci := chainindex.New(log.Logger, dbo)
if err := ci.Init(); err != nil {
log.Logger.Panic("Init", zap.Error(err))
}
wg.Add(1)
go ci.Start(ctx, &wg)
httpServer := http.New(svc.New(log.Logger, dbo))
wg.Add(1)
go httpServer.Start(ctx, &wg, true, viper.GetInt("http.port"))
exit := make(chan os.Signal, 1)
signal.Notify(exit, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
select {
case <-exit:
cancel()
if err := httpServer.Stop(ctx); err != nil {
log.Logger.Warn("http.Stop", zap.Error(err))
}
case <-ctx.Done():
wg.Wait()
log.Logger.Info("main exit")
os.Exit(0)
}
}
}
func init() {
pflag.Int("http.port", 8080, "server http port")
pflag.String("datadir", ".", "db path")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}