-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
135 lines (125 loc) · 4.14 KB
/
serve.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
package serve
import (
"fmt"
"net/http"
"net/url"
"path"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/shantanu-hashcash/go/amount"
"github.com/shantanu-hashcash/go/clients/auroraclient"
"github.com/shantanu-hashcash/go/keypair"
"github.com/shantanu-hashcash/go/services/regulated-assets-approval-server/internal/db"
"github.com/shantanu-hashcash/go/services/regulated-assets-approval-server/internal/serve/kycstatus"
"github.com/shantanu-hashcash/go/support/errors"
supporthttp "github.com/shantanu-hashcash/go/support/http"
"github.com/shantanu-hashcash/go/support/log"
"github.com/shantanu-hashcash/go/support/render/health"
)
type Options struct {
AssetCode string
BaseURL string
DatabaseURL string
FriendbotPaymentAmount int
AuroraURL string
IssuerAccountSecret string
KYCRequiredPaymentAmountThreshold string
NetworkPassphrase string
Port int
}
func Serve(opts Options) {
listenAddr := fmt.Sprintf(":%d", opts.Port)
serverConfig := supporthttp.Config{
ListenAddr: listenAddr,
Handler: handleHTTP(opts),
TCPKeepAlive: time.Minute * 3,
ShutdownGracePeriod: time.Second * 50,
ReadTimeout: time.Second * 5,
WriteTimeout: time.Second * 35,
IdleTimeout: time.Minute * 2,
OnStarting: func() {
log.Info("Starting SEP-8 Approval Server")
log.Infof("Listening on %s", listenAddr)
},
OnStopping: func() {
log.Info("Stopping SEP-8 Approval Server")
},
}
supporthttp.Run(serverConfig)
}
func handleHTTP(opts Options) http.Handler {
issuerKP, err := keypair.ParseFull(opts.IssuerAccountSecret)
if err != nil {
log.Fatal(errors.Wrap(err, "parsing secret"))
}
parsedKYCRequiredPaymentThreshold, err := amount.ParseInt64(opts.KYCRequiredPaymentAmountThreshold)
if err != nil {
log.Fatal(errors.Wrapf(err, "%s cannot be parsed as a Hcnet amount", opts.KYCRequiredPaymentAmountThreshold))
}
db, err := db.Open(opts.DatabaseURL)
if err != nil {
log.Fatal(errors.Wrap(err, "error parsing database url"))
}
db.SetMaxOpenConns(20)
err = db.Ping()
if err != nil {
log.Warn("Error pinging to Database: ", err)
}
mux := chi.NewMux()
mux.Use(middleware.RequestID)
mux.Use(middleware.RealIP)
mux.Use(supporthttp.LoggingMiddleware)
mux.Use(corsHandler)
mux.Get("/health", health.PassHandler{}.ServeHTTP)
mux.Get("/.well-known/hcnet.toml", hcnetTOMLHandler{
assetCode: opts.AssetCode,
issuerAddress: issuerKP.Address(),
networkPassphrase: opts.NetworkPassphrase,
approvalServer: buildURLString(opts.BaseURL, "tx-approve"),
kycThreshold: parsedKYCRequiredPaymentThreshold,
}.ServeHTTP)
mux.Get("/friendbot", friendbotHandler{
assetCode: opts.AssetCode,
issuerAccountSecret: opts.IssuerAccountSecret,
auroraClient: opts.auroraClient(),
auroraURL: opts.AuroraURL,
networkPassphrase: opts.NetworkPassphrase,
paymentAmount: opts.FriendbotPaymentAmount,
}.ServeHTTP)
mux.Post("/tx-approve", txApproveHandler{
assetCode: opts.AssetCode,
issuerKP: issuerKP,
auroraClient: opts.auroraClient(),
networkPassphrase: opts.NetworkPassphrase,
db: db,
kycThreshold: parsedKYCRequiredPaymentThreshold,
baseURL: opts.BaseURL,
}.ServeHTTP)
mux.Route("/kyc-status", func(mux chi.Router) {
mux.Post("/{callback_id}", kycstatus.PostHandler{
DB: db,
}.ServeHTTP)
mux.Get("/{hcnet_address_or_callback_id}", kycstatus.GetDetailHandler{
DB: db,
}.ServeHTTP)
mux.Delete("/{hcnet_address}", kycstatus.DeleteHandler{
DB: db,
}.ServeHTTP)
})
return mux
}
func (opts Options) auroraClient() auroraclient.ClientInterface {
return &auroraclient.Client{
AuroraURL: opts.AuroraURL,
HTTP: &http.Client{Timeout: 30 * time.Second},
}
}
func buildURLString(baseURL, endpoint string) string {
URL, err := url.Parse(baseURL)
if err != nil {
log.Fatal(errors.Wrapf(err, "Unable to parse URL: %s", baseURL))
}
URL.Path = path.Join(URL.Path, endpoint)
return URL.String()
}