-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
61 lines (52 loc) · 1.53 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
package main
import (
"crypto/tls"
"net/http"
"time"
log "github.com/sirupsen/logrus"
cfg "github.com/vulpemventures/nigiri-chopsticks/config"
"github.com/vulpemventures/nigiri-chopsticks/router"
"golang.org/x/crypto/acme/autocert"
)
func makeHTTPServer(handler http.Handler, addr string) *http.Server {
return &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second,
Addr: addr,
Handler: handler,
}
}
func main() {
config, err := cfg.NewConfigFromFlags()
if err != nil {
log.WithError(err).Fatal("Failed to parse flags")
}
log.WithFields(log.Fields{
"tls_enabled": config.IsTLSEnabled(),
"faucet_enabled": config.IsFaucetEnabled(),
"mining_enabled": config.IsMiningEnabled(),
"logger_enabled": config.IsLoggerEnabled(),
"listen_url": config.ListenURL(),
"electrs_url": config.ElectrsURL(),
"rpc_server_url": config.RPCServerURL(),
"chain": config.Chain(),
}).Info("Starting server with configuration:")
r := router.NewRouter(config)
s := makeHTTPServer(r, config.ListenURL())
if !config.IsTLSEnabled() {
if err = s.ListenAndServe(); err != nil {
log.WithError(err).Fatal("HTTP server exited with error")
}
} else {
dataDir := "."
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(dataDir),
}
s.TLSConfig = &tls.Config{GetCertificate: m.GetCertificate}
if err = s.ListenAndServeTLS("", ""); err != nil {
log.WithError(err).Fatal("HTTPS server exited with error")
}
}
}