-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
83 lines (69 loc) · 2.42 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/hashicorp/go-hclog"
"github.com/maticnetwork/ethstats-backend/ethstats"
)
var (
defaultDBEndpoint = "postgres://postgres:postgrespassword@postgres:5432/postgres?sslmode=disable"
persistDataDuration int
)
func main() {
config := ðstats.Config{}
var logLevel string
dbEndpoint := os.Getenv("DB_ENDPOINT")
if dbEndpoint == "" {
dbEndpoint = defaultDBEndpoint
}
serverCMD := flag.NewFlagSet("server", flag.ExitOnError)
serverCMD.StringVar(&config.Endpoint, "db-endpoint", dbEndpoint, "database endpoint")
serverCMD.StringVar(&config.CollectorAddr, "collector.addr", "0.0.0.0:8000", "ws service address for collector")
serverCMD.StringVar(&config.CollectorSecret, "collector.secret", os.Getenv("COLLECTOR_SECRET"), "collector secret")
serverCMD.StringVar(&logLevel, "log-level", "info", "log level")
serverCMD.StringVar(&config.FrontendAddr, "frontend.addr", os.Getenv("FRONTEND_ADDR"), "frontend address")
serverCMD.StringVar(&config.FrontendSecret, "frontend.secret", os.Getenv("FRONTEND_SECRET"), "frontend secret")
serverCMD.BoolVar(&config.ShouldSaveBlockTxs, "save-block-txs", true, "should block txs be written to db")
purgeCMD := flag.NewFlagSet("purge", flag.ExitOnError)
purgeCMD.IntVar(&persistDataDuration, "persist-days", 0, "Data older than this days will be deleted")
switch os.Args[1] {
case "server":
serverCMD.Parse(os.Args[2:])
case "purge":
purgeCMD.Parse(os.Args[2:])
if persistDataDuration > 0 {
state, err := ethstats.NewState(config.Endpoint)
if err != nil {
fmt.Printf("[ERROR]: %v", err)
os.Exit(0)
}
persistDataDuration = persistDataDuration * 24 * 60 * 60
//send persistData Duration in seconds
err = state.DeleteOlderData(persistDataDuration)
if err != nil {
fmt.Printf("[ERROR]: %v", err)
os.Exit(0)
}
fmt.Printf("[INFO]: Data older than %d days deleted\n", persistDataDuration)
} else {
fmt.Println("[ERROR]: persist-days must be greater than 0")
}
os.Exit(0)
default:
fmt.Println("expected 'server' or 'purge' subcommands")
os.Exit(1)
}
logger := hclog.New(&hclog.LoggerOptions{Level: hclog.LevelFromString(logLevel)})
srv, err := ethstats.NewServer(logger, config)
if err != nil {
fmt.Printf("[ERROR]: %v", err)
os.Exit(0)
}
signalCh := make(chan os.Signal, 4)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
<-signalCh
srv.Close()
}