forked from flow-hydraulics/flow-pds
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
131 lines (104 loc) · 2.61 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
125
126
127
128
129
130
131
package main
import (
"flag"
"fmt"
"os"
"github.com/flow-hydraulics/flow-pds/service/app"
"github.com/flow-hydraulics/flow-pds/service/common"
"github.com/flow-hydraulics/flow-pds/service/config"
"github.com/flow-hydraulics/flow-pds/service/http"
"github.com/flow-hydraulics/flow-pds/service/transactions"
flowGrpc "github.com/onflow/flow-go-sdk/access/grpc"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const version = "0.7.0"
var (
sha1ver string // sha1 revision used to build the program
buildTime string // when the executable was built
)
func init() {
lvl, ok := os.LookupEnv("FLOW_PDS_LOG_LEVEL")
if !ok {
// LOG_LEVEL not set, default to info
lvl = "info"
}
ll, err := log.ParseLevel(lvl)
if err != nil {
ll = log.DebugLevel
}
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
log.SetLevel(ll)
}
func main() {
var (
printVersion bool
envFilePath string
)
// If we should just print the version number and exit
flag.BoolVar(&printVersion, "version", false, "if true, print version and exit")
// Allow configuration of envfile path
// If not set, ParseConfig will not try to load variables to environment from a file
flag.StringVar(&envFilePath, "envfile", "", "envfile path")
flag.Parse()
if printVersion {
fmt.Printf("v%s build on %s from sha1 %s\n", version, buildTime, sha1ver)
os.Exit(0)
}
opts := &config.ConfigOptions{EnvFilePath: envFilePath}
cfg, err := config.ParseConfig(opts)
if err != nil {
panic(err)
}
if err := runServer(cfg); err != nil {
panic(err)
}
os.Exit(0)
}
func runServer(cfg *config.Config) error {
if cfg == nil {
return fmt.Errorf("config not provided")
}
log.Infof("Starting server (v%s)...", version)
// Flow client
// TODO: WithInsecure()?
maxSize := 1024 * 1024 * 64
flowClient, err := flowGrpc.NewBaseClient(cfg.AccessAPIHost, grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxSize)),
)
if err != nil {
return err
}
defer func() {
if err := flowClient.Close(); err != nil {
log.Error(err)
}
}()
// Database
db, err := common.NewGormDB(cfg)
if err != nil {
return err
}
defer common.CloseGormDB(db)
// Migrate app database
if err := app.Migrate(db); err != nil {
return err
}
if err := transactions.Migrate(db); err != nil {
return err
}
// Application
app, err := app.New(cfg, db, flowClient, true)
if err != nil {
return err
}
defer app.Close()
// HTTP server
server := http.NewServer(cfg, app)
server.ListenAndServe()
return nil
}