-
Notifications
You must be signed in to change notification settings - Fork 426
/
main.go
101 lines (80 loc) · 3 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
package main
import (
"flag"
"fmt"
"net/http"
"sync"
"github.com/gobitfly/eth2-beaconchain-explorer/db"
"github.com/gobitfly/eth2-beaconchain-explorer/metrics"
"github.com/gobitfly/eth2-beaconchain-explorer/types"
"github.com/gobitfly/eth2-beaconchain-explorer/userService"
"github.com/gobitfly/eth2-beaconchain-explorer/utils"
"github.com/gobitfly/eth2-beaconchain-explorer/version"
"github.com/sirupsen/logrus"
_ "net/http/pprof"
_ "github.com/gobitfly/eth2-beaconchain-explorer/docs"
_ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
configPath := flag.String("config", "", "Path to the config file, if empty string defaults will be used")
flag.Parse()
cfg := &types.Config{}
err := utils.ReadConfig(cfg, *configPath)
if err != nil {
logrus.Fatalf("error reading config file: %v", err)
}
utils.Config = cfg
logrus.WithFields(logrus.Fields{
"config": *configPath,
"version": version.Version,
"chainName": utils.Config.Chain.ClConfig.ConfigName}).Printf("starting")
if utils.Config.Chain.ClConfig.SlotsPerEpoch == 0 || utils.Config.Chain.ClConfig.SecondsPerSlot == 0 {
utils.LogFatal(err, "invalid chain configuration specified, you must specify the slots per epoch, seconds per slot and genesis timestamp in the config file", 0)
}
if utils.Config.Metrics.Enabled {
go func(addr string) {
logrus.Infof("serving metrics on %v", addr)
if err := metrics.Serve(addr); err != nil {
logrus.WithError(err).Fatal("Error serving metrics")
}
}(utils.Config.Metrics.Address)
}
if utils.Config.Pprof.Enabled {
go func() {
logrus.Infof("starting pprof http server on port %s", utils.Config.Pprof.Port)
logrus.Info(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", utils.Config.Pprof.Port), nil))
}()
}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
db.MustInitFrontendDB(&types.DatabaseConfig{
Username: cfg.Frontend.WriterDatabase.Username,
Password: cfg.Frontend.WriterDatabase.Password,
Name: cfg.Frontend.WriterDatabase.Name,
Host: cfg.Frontend.WriterDatabase.Host,
Port: cfg.Frontend.WriterDatabase.Port,
MaxOpenConns: cfg.Frontend.WriterDatabase.MaxOpenConns,
MaxIdleConns: cfg.Frontend.WriterDatabase.MaxIdleConns,
SSL: cfg.Frontend.WriterDatabase.SSL,
}, &types.DatabaseConfig{
Username: cfg.Frontend.ReaderDatabase.Username,
Password: cfg.Frontend.ReaderDatabase.Password,
Name: cfg.Frontend.ReaderDatabase.Name,
Host: cfg.Frontend.ReaderDatabase.Host,
Port: cfg.Frontend.ReaderDatabase.Port,
MaxOpenConns: cfg.Frontend.ReaderDatabase.MaxOpenConns,
MaxIdleConns: cfg.Frontend.ReaderDatabase.MaxIdleConns,
SSL: cfg.Frontend.ReaderDatabase.SSL,
}, "pgx", "postgres")
}()
// if needed, init the database, cache or bigtable
wg.Wait()
defer db.FrontendReaderDB.Close()
defer db.FrontendWriterDB.Close()
logrus.Infof("database connection established")
userService.Init()
utils.WaitForCtrlC()
logrus.Println("exiting...")
}