-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
66 lines (58 loc) · 2.07 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
package main
import (
"context"
"log"
"github.com/skynetlabs/pinner/api"
"github.com/skynetlabs/pinner/build"
"github.com/skynetlabs/pinner/conf"
"github.com/skynetlabs/pinner/database"
"github.com/skynetlabs/pinner/logger"
"github.com/skynetlabs/pinner/scanner"
"github.com/skynetlabs/pinner/skyd"
"github.com/skynetlabs/pinner/sweeper"
"gitlab.com/NebulousLabs/errors"
)
func main() {
// Load the configuration from the environment and the local .env file.
cfg, err := conf.LoadConfig()
if err != nil {
log.Fatal(err)
}
// Initialise the global context and logger. These will be used throughout
// the service. Once the context is closed, any background threads will
// wind themselves down.
ctx := context.Background()
logger, err := logger.New(cfg.LogLevel, cfg.LogFile)
if err != nil {
log.Fatal(errors.AddContext(err, "failed to initialise logger"))
}
defer func() {
if err := logger.Close(); err != nil {
log.Println(errors.AddContext(err, "failed to close logger"))
}
}()
// Initialised the database connection.
db, err := database.New(ctx, cfg.DBCredentials, logger)
if err != nil {
logger.Fatal(errors.AddContext(err, database.ErrCtxFailedToConnect))
}
// Start the background scanner.
skydClient := skyd.NewClient(cfg.SiaAPIHost, cfg.SiaAPIPort, cfg.SiaAPIPassword, skyd.NewCache(), logger)
scanner := scanner.New(db, logger, cfg.MinPinners, cfg.ScannerThreads, cfg.ServerName, cfg.SleepBetweenScans, skydClient)
err = scanner.Start()
if err != nil {
logger.Fatal(errors.AddContext(err, "failed to start Scanner"))
}
swpr := sweeper.New(db, skydClient, cfg.ServerName, logger)
// Schedule a regular sweep..
swpr.UpdateSchedule(sweeper.SweepInterval)
// Initialise the server.
server, err := api.New(cfg.ServerName, db, logger, skydClient, scanner, swpr)
if err != nil {
logger.Fatal(errors.AddContext(err, "failed to build the api"))
}
logger.Print("Starting Pinner service")
logger.Printf("GitRevision: %v (built %v)", build.GitRevision, build.BuildTime)
err = server.ListenAndServe(4000)
logger.Fatal(errors.Compose(err, scanner.Close()))
}