forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (178 loc) · 5.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"time"
log "github.com/sirupsen/logrus"
"github.com/facebookgo/inject"
"github.com/goji/httpauth"
"github.com/spf13/cobra"
"github.com/stellar/go/clients/federation"
"github.com/stellar/go/clients/stellartoml"
"github.com/stellar/go/services/compliance/internal/config"
"github.com/stellar/go/services/compliance/internal/crypto"
"github.com/stellar/go/services/compliance/internal/db"
"github.com/stellar/go/services/compliance/internal/handlers"
supportConfig "github.com/stellar/go/support/config"
"github.com/stellar/go/support/db/schema"
"github.com/stellar/go/support/errors"
supportHttp "github.com/stellar/go/support/http"
)
var app *App
var rootCmd *cobra.Command
var migrateFlag bool
var configFile string
var versionFlag bool
var version = "N/A"
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
rootCmd.Execute()
}
func init() {
rootCmd = &cobra.Command{
Use: "compliance",
Short: "stellar compliance server",
Long: `stellar compliance server`,
Run: run,
}
rootCmd.Flags().BoolVarP(&migrateFlag, "migrate-db", "", false, "migrate DB to the newest schema version")
rootCmd.Flags().StringVarP(&configFile, "config", "c", "compliance.cfg", "path to config file")
rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "displays compliance server version")
}
func run(cmd *cobra.Command, args []string) {
var cfg config.Config
err := supportConfig.Read(configFile, &cfg)
if err != nil {
switch cause := errors.Cause(err).(type) {
case *supportConfig.InvalidConfigError:
log.Error("config file: ", cause)
default:
log.Error(err)
}
os.Exit(-1)
}
err = cfg.Validate()
if err != nil {
log.Fatal(err.Error())
return
}
if cfg.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
}
app, err = NewApp(cfg, migrateFlag, versionFlag, version)
if err != nil {
log.Fatal(err.Error())
return
}
app.Serve()
}
// App is the application object
type App struct {
config config.Config
requestHandler handlers.RequestHandler
}
// NewApp constructs an new App instance from the provided config.
func NewApp(config config.Config, migrateFlag bool, versionFlag bool, version string) (app *App, err error) {
var g inject.Graph
var database db.PostgresDatabase
if config.Database.URL != "" {
err = database.Open(config.Database.URL)
if err != nil {
err = fmt.Errorf("Cannot connect to a DB: %s", err)
return
}
}
if migrateFlag {
var migrationsApplied int
migrationsApplied, err = schema.Migrate(
database.GetDB(),
db.Migrations,
schema.MigrateUp,
0,
)
if err != nil {
return
}
log.Info("Applied migrations: ", migrationsApplied)
os.Exit(0)
return
}
if versionFlag {
fmt.Printf("Compliance Server Version: %s \n", version)
os.Exit(0)
return
}
requestHandler := handlers.RequestHandler{}
httpClientWithTimeout := http.Client{
Timeout: 10 * time.Second,
}
stellartomlClient := stellartoml.Client{
HTTP: &httpClientWithTimeout,
}
federationClient := federation.Client{
HTTP: &httpClientWithTimeout,
StellarTOML: &stellartomlClient,
}
err = g.Provide(
&inject.Object{Value: &requestHandler},
&inject.Object{Value: &config},
&inject.Object{Value: &database},
&inject.Object{Value: &crypto.SignerVerifier{}},
&inject.Object{Value: &stellartomlClient},
&inject.Object{Value: &federationClient},
&inject.Object{Value: &httpClientWithTimeout},
&inject.Object{Value: &handlers.NonceGenerator{}},
)
if err != nil {
log.Fatal("Injector: ", err)
}
if err := g.Populate(); err != nil {
log.Fatal("Injector: ", err)
}
app = &App{
config: config,
requestHandler: requestHandler,
}
return
}
// Serve starts the server
func (a *App) Serve() {
// External endpoints
external := supportHttp.NewAPIMux(false)
// Middlewares
headers := http.Header{}
headers.Set("Content-Type", "application/json")
external.Use(supportHttp.StripTrailingSlashMiddleware())
external.Use(supportHttp.HeadersMiddleware(headers))
external.Post("/", a.requestHandler.HandlerAuth)
if a.config.TxStatusAuth != nil {
external.Method("GET", "/tx_status", httpauth.SimpleBasicAuth(a.config.TxStatusAuth.Username, a.config.TxStatusAuth.Password)(http.HandlerFunc(a.requestHandler.HandlerTxStatus)))
}
go func() {
supportHttp.Run(supportHttp.Config{
ListenAddr: fmt.Sprintf(":%d", *a.config.ExternalPort),
Handler: external,
TLS: a.config.TLS,
OnStarting: func() {
log.Infof("External server listening on %d", *a.config.ExternalPort)
},
})
}()
// Internal endpoints
internal := supportHttp.NewAPIMux(false)
internal.Use(supportHttp.StripTrailingSlashMiddleware("/admin"))
internal.Use(supportHttp.HeadersMiddleware(headers, "/admin/"))
internal.Post("/send", a.requestHandler.HandlerSend)
internal.Post("/receive", a.requestHandler.HandlerReceive)
internal.Post("/allow_access", a.requestHandler.HandlerAllowAccess)
internal.Post("/remove_access", a.requestHandler.HandlerRemoveAccess)
supportHttp.Run(supportHttp.Config{
ListenAddr: fmt.Sprintf(":%d", *a.config.InternalPort),
Handler: internal,
OnStarting: func() {
log.Infof("Internal server listening on %d", *a.config.InternalPort)
},
})
}