-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
109 lines (88 loc) · 2.66 KB
/
server.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
package apiserver
import (
"context"
"net/http"
"time"
"code.cloudfoundry.org/lager"
"github.com/alphagov/paas-billing/apiserver/auth"
"github.com/alphagov/paas-billing/eventio"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type Config struct {
// Authenticator sets the auth mechanism (required)
Authenticator auth.Authenticator
// Store sets the Store used for querying events (required)
Store eventio.EventStore
// Logger sets the request logger
Logger lager.Logger
// EnablePanic will cause the server to crash on panic if set to true
EnablePanic bool
}
// New creates a base new server. Use ListenAndServe to start accepting connections.
// It will only serve the status page
func NewBaseServer(cfg Config) *echo.Echo {
e := echo.New()
e.HTTPErrorHandler = errorHandler
if !cfg.EnablePanic {
e.Use(middleware.Recover())
}
if cfg.Logger != nil {
echoCompatibleLogger := NewLogger(cfg.Logger)
e.Logger = echoCompatibleLogger
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Output: echoCompatibleLogger,
}))
}
e.GET("/", status(cfg.Store))
return e
}
// New creates a new server. Use ListenAndServe to start accepting connections.
// Serves api functions
func New(cfg Config) *echo.Echo {
e := NewBaseServer(cfg)
e.GET("/vat_rates", VATRatesHandler(cfg.Store))
e.GET("/currency_rates", CurrencyRatesHandler(cfg.Store))
e.GET("/pricing_plans", PricingPlansHandler(cfg.Store))
e.GET("/forecast_events", ForecastEventsHandler(cfg.Store))
e.GET("/usage_events", UsageEventsHandler(cfg.Store, cfg.Authenticator))
e.GET("/billable_events", BillableEventsHandler(cfg.Store, cfg.Store, cfg.Authenticator))
e.GET("/totals", TotalCostHandler(cfg.Store))
return e
}
func status(store eventio.EventStore) echo.HandlerFunc {
return func(c echo.Context) error {
success := true
status := http.StatusOK
if err := store.Ping(); err != nil {
success = false
status = http.StatusInternalServerError
}
return c.JSONPretty(status, map[string]bool{
"ok": success,
}, " ")
}
}
func ListenAndServe(ctx context.Context, logger lager.Logger, e *echo.Echo, addr string) error {
ctx, shutdown := context.WithCancel(ctx)
go func() {
defer shutdown()
logger.Info("started", lager.Data{
"addr": addr,
})
if err := e.Start(addr); err != nil {
select {
case <-ctx.Done():
return
default:
e.Logger.Error("listen-and-serve-error", err)
}
}
}()
// Wait for parent context to get cancelled then drain with a 10s timeout
<-ctx.Done()
e.Logger.Info("stopping")
drainCtx, cancelDrain := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelDrain()
return e.Shutdown(drainCtx)
}