-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (81 loc) · 2.19 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
package main
import (
"embed"
"reflect"
"sync"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/tyzbit/btcapi"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Watcher struct {
BTCAPI btcapi.Config
CancelWaitGroup *sync.WaitGroup
CancelSignals map[string]chan bool
DB *gorm.DB
LogConfig logger.Interface
Config
}
type Config struct {
BTCAPIEndpoint string `env:"BTC_RPC_API"`
CheckAllPubkeyTypes bool `env:"CHECK_ALL_PUBKEY_TYPES"`
Currency string `env:"CURRENCY"`
DBPath string `env:"DB_PATH"`
DiscordWebhook string `env:"DISCORD_WEBHOOK"`
SleepInterval int `env:"SLEEP_INTERVAL"`
LogLevel string `env:"LOG_LEVEL"`
Lookahead int `env:"LOOKAHEAD"`
PageSize int `env:"PAGE_SIZE"`
Port string `env:"PORT"`
}
type DiscordPayload struct {
Content string `json:"content"`
}
const (
DefaultApi string = "https://bitcoinexplorer.org"
DefaultDBPath string = "/db/addresses.sqlite"
DefaultLookahead int = 20
DefaultPageSize int = 100
DefaultSleepInterval int = 300
SatsPerBitcoin int = 100000000
)
var (
allSchemaTypes = []interface{}{
&AddressInfo{},
&PubkeyInfo{},
}
watcher Watcher
//go:embed web
web embed.FS
)
func init() {
watcher.InitLogging()
// Set defaults and throw errors if necessary values aren't set
watcher.FillDefaults()
}
func main() {
db, err := gorm.Open(sqlite.Open(watcher.Config.DBPath), &gorm.Config{Logger: watcher.LogConfig})
if err != nil {
log.Fatal("unable to open db: ", err)
}
watcher.DB = db
for _, schemaType := range allSchemaTypes {
if err := watcher.DB.AutoMigrate(schemaType); err != nil {
log.Fatal("unable to automigrate ", reflect.TypeOf(&schemaType).Elem().Name(), "err: ", err)
}
}
// Set up BTC-RPC
watcher.BTCAPI = btcapi.Config{
ExplorerURL: watcher.Config.BTCAPIEndpoint,
}
watcher.StartWatches()
r := gin.New()
r.Use(gin.LoggerWithFormatter(GinJSONFormatter))
InitFrontend(r)
InitBackend(r)
if err := r.Run(":"+watcher.Config.Port); err != nil {
log.Fatal("could not start: ", err)
}
}