-
Notifications
You must be signed in to change notification settings - Fork 149
/
main.go
135 lines (117 loc) · 3.39 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/moov-io/base/admin"
moovhttp "github.com/moov-io/base/http"
"github.com/moov-io/base/http/bind"
"github.com/moov-io/base/strx"
"github.com/moov-io/ach"
"github.com/gorilla/mux"
)
var (
httpAddr = flag.String("http.addr", bind.HTTP("ACH"), "HTTP listen address")
adminAddr = flag.String("admin.addr", bind.Admin("ACH"), "Admin HTTP listen address")
flagBasePath = flag.String("base-path", "/", "Base path to serve HTTP routes and webui from")
)
func main() {
flag.Parse()
log.Printf("Starting moov-io/ach webui version %s", ach.Version)
// Channel for errors
errs := make(chan error)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
// Start Admin server (with Prometheus metrics)
adminServer, err := admin.New(admin.Opts{
Addr: *adminAddr,
})
if err != nil {
errs <- fmt.Errorf("problem running admin server: %v", err)
os.Exit(1)
}
adminServer.AddVersionHandler(ach.Version) // Setup 'GET /version'
go func() {
log.Printf("listening on %s", adminServer.BindAddr())
if err := adminServer.Listen(); err != nil {
err = fmt.Errorf("problem starting admin http: %v", err)
log.Print(err)
errs <- err
}
}()
defer adminServer.Shutdown()
// Setup business HTTP routes
router := mux.NewRouter().PathPrefix(*flagBasePath).Subrouter()
addPingRoute(router)
// Register our assets route
assetsPath := strx.Or(os.Getenv("ASSETS_PATH"), filepath.Join("cmd", "webui", "assets"))
log.Printf("serving assets from %s", assetsPath)
err = addAssetsPath(router, assetsPath)
if err != nil {
log.Print(err)
errs <- err
}
serve := &http.Server{
Addr: *httpAddr,
Handler: router,
TLSConfig: &tls.Config{
InsecureSkipVerify: false,
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
},
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 10 * time.Second,
}
shutdownServer := func() {
if err := serve.Shutdown(context.TODO()); err != nil {
log.Print(err)
}
}
// Start business logic HTTP server
go func() {
if certFile, keyFile := os.Getenv("HTTPS_CERT_FILE"), os.Getenv("HTTPS_KEY_FILE"); certFile != "" && keyFile != "" {
log.Printf("binding to %s for secure HTTP server", *httpAddr)
if err := serve.ListenAndServeTLS(certFile, keyFile); err != nil {
log.Print(err)
}
} else {
log.Printf("binding to %s for HTTP server", *httpAddr)
if err := serve.ListenAndServe(); err != nil {
log.Print(err)
}
}
}()
// Block/Wait for an error
if err := <-errs; err != nil {
shutdownServer()
log.Print(err)
}
}
func addPingRoute(r *mux.Router) {
r.Methods("GET").Path("/ping").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
moovhttp.SetAccessControlAllowHeaders(w, r.Header.Get("Origin"))
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("PONG"))
})
}
func addAssetsPath(r *mux.Router, assetPath string) error {
if _, err := os.Stat(assetPath); err != nil {
return fmt.Errorf("ERROR: unable to stat %s: %w", assetPath, err)
}
r.Methods("GET").PathPrefix("/").Handler(http.StripPrefix(*flagBasePath, http.FileServer(http.Dir(assetPath))))
return nil
}