This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (105 loc) · 3.21 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
115
116
117
118
119
120
121
122
123
124
package main
import (
"io/ioutil"
"os"
"os/signal"
"syscall"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/database"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/parameter"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
AppVersion = "0.1.0"
config = viper.New()
db database.Database
log *logger.Logger
)
func init() {
flag.StringSlice("nodes", []string{"main.manapotion.io:5556"}, "the nodes from which to collect spent addresses from")
flag.String("database", "spent_addresses", "the name of the database folder")
flag.String("importFile", "spent_addresses.bin", "the name of the import file containing the initial spent addresses")
flag.Int("cache.size", 100000, "the size of the cache containing spent addresses")
flag.Int("cache.batchEvictionSize", 10000, "the amount of addresses to evict together from the cache")
flag.String("http.bindAddress", "0.0.0.0:8081", "the bind address for the HTTP calls")
flag.Int("http.maxAddressesInRequest", 1000, "the maximum amount of spent addresses within a single HTTP call")
flag.String("http.secretKey", "1337", "the secret key used to authenticate for certain HTTP calls")
}
func main() {
var err error
// init config
must(parameter.LoadConfigFile(config, ".", "config", true, false))
// init common
must(logger.InitGlobalLogger(config))
log = logger.NewLogger("App")
initCache()
// check whether we're running with a clean database and if so, require a spent-addresses import file
importFileName := config.GetString("importFile")
isFirstRun := firstRun()
if isFirstRun {
if _, err := os.Stat(importFileName); err != nil {
if os.IsNotExist(err) {
log.Fatalf("you must supply an import file '%s', if you're running the program for the first time", importFileName)
}
log.Fatalf("unable to check existence of import file '%s': %s", importFileName, err)
}
}
dbDir := config.GetString("database")
badgerDB, err := database.CreateDB(dbDir, getDatabaseOpts(dbDir))
if err != nil {
log.Fatalf("unable to instantiate database: %s", err)
}
// init database
db, err = database.Get(0, badgerDB)
must(err)
// make sure to close the database when the program exits
defer func() {
log.Info("closing database...")
if err := badgerDB.Close(); err != nil {
log.Error(err)
}
log.Info("closing database...done")
}()
spawnBadgerGC()
// do import routine
if isFirstRun {
importSpentAddresses(importFileName)
}
// register shutdown handler
registerShutdownCleanupHook()
// spawn collectors
for _, node := range config.GetStringSlice("nodes") {
spawnCollector(node)
}
// spawn http server
spawnHTTPServer()
// execute
daemon.Start()
shutdownSignal := make(chan os.Signal)
signal.Notify(shutdownSignal, syscall.SIGTERM)
signal.Notify(shutdownSignal, syscall.SIGINT)
<-shutdownSignal
// shutdown
daemon.ShutdownAndWait()
}
func firstRun() bool {
fileInfos, err := ioutil.ReadDir(config.GetString("database"))
if err != nil {
// panic on other errors, for example permission related
if !os.IsNotExist(err) {
panic(err)
}
return true
}
if len(fileInfos) == 0 {
return true
}
return false
}
func must(err error) {
if err != nil {
panic(err)
}
}