-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
89 lines (76 loc) · 2.35 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
package main
import (
"flag"
"log"
"os"
"github.com/communitybridge/ledger/balance"
"github.com/communitybridge/ledger/cmd"
"github.com/communitybridge/ledger/gen/restapi"
"github.com/communitybridge/ledger/gen/restapi/operations"
"github.com/communitybridge/ledger/health"
"github.com/communitybridge/ledger/transaction"
"github.com/go-openapi/loads"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
// BuildStamp is a timestamp (injected by go) of the build time
BuildStamp = "None"
// GitHash is the tag for current hash the build represents
GitHash = "None"
)
func main() {
host, err := os.Hostname()
if err != nil {
logrus.Panicln("unable to get Hostname", err)
}
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.DebugLevel)
logrus.WithFields(logrus.Fields{
"BuildTime": BuildStamp,
"GitHash": GitHash,
"Host": host,
}).Info("Start Service")
// Configures Viper, the configuration management tool, and set some app defaults
viperConfig := viper.New()
viperConfig.AutomaticEnv()
viperConfig.SetEnvPrefix("LS") // this prefix is specific to the Ledger Service
defaults := map[string]interface{}{
"PORT": 8080,
"USE_MOCK": "False",
}
for key, value := range defaults {
viperConfig.SetDefault(key, value)
}
// DB setup
pDB, err := sqlx.Connect("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
logrus.Fatal("err", err)
}
pDB.SetMaxOpenConns(3)
// loads generated Swagger API specifications
swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
if err != nil {
log.Fatal("Invalid swagger file for initializing", err)
}
api := operations.NewLedgerAPI(swaggerSpec)
// Health setup
healthService := health.New()
health.Configure(api, healthService)
// Transactions package endpoints
transactionRepo := transaction.NewRepository(pDB)
transactionService := transaction.NewService(transactionRepo)
transaction.Configure(api, transactionService)
// Balance package endpoints
balanceRepo := balance.NewRepository(pDB)
balanceService := balance.NewService(balanceRepo)
balance.Configure(api, balanceService)
var portFlag = flag.Int("port", viperConfig.GetInt("PORT"), "Port to listen for web requests on")
flag.Parse()
logrus.Info("Start Service")
if err := cmd.Start(api, *portFlag); err != nil {
logrus.Panicln(err)
}
}