-
Notifications
You must be signed in to change notification settings - Fork 126
/
server.go
109 lines (90 loc) · 3 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 service
import (
"github.com/callistaenterprise/goblog/accountservice/cmd"
"github.com/callistaenterprise/goblog/common/monitoring"
"github.com/callistaenterprise/goblog/common/tracing"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
gqlhandler "github.com/graphql-go/graphql-go-handler"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"net/http"
"strconv"
"time"
)
type Server struct {
cfg *cmd.Config
r *chi.Mux
h *Handler
}
func NewServer(cfg *cmd.Config, h *Handler, qlResolvers GraphQLResolvers) *Server {
initGraphQL(qlResolvers)
return &Server{cfg: cfg, h: h}
}
func (s *Server) Close() {
}
func (s *Server) Start() {
logrus.Infof("Starting HTTP server on %v", ":"+s.cfg.Port)
err := http.ListenAndServe(":"+s.cfg.Port, s.r)
if err != nil {
logrus.WithError(err).Fatal("error starting HTTP server")
}
}
func (s *Server) SetupRoutes() {
s.r = chi.NewRouter()
s.r.Use(middleware.RequestID)
s.r.Use(middleware.RealIP)
s.r.Use(middleware.Logger)
s.r.Use(middleware.Recoverer)
s.r.Use(middleware.Timeout(time.Minute))
// Sub-routers with monitoring
s.r.Route("/accounts", func(r chi.Router) {
r.With(Trace("Get_Account")).
With(Monitor(s.cfg.Name, "GetAccount", "GET /accounts/{accountId}")).
Get("/{accountId}", s.h.GetAccount)
r.With(Trace("StoreAccount")).
With(Monitor(s.cfg.Name, "StoreAccount", "POST /accounts")).
Post("/", s.h.StoreAccount)
r.With(Trace("UpdateAccount")).
With(Monitor(s.cfg.Name, "UpdateAccount", "PUT /accounts")).
Put("/", s.h.UpdateAccount)
})
s.r.With(Trace("GraphQL")).
With(Monitor(s.cfg.Name, "GraphQL", "POST /graphql")).
Post("/graphql", gqlhandler.New(&gqlhandler.Config{
Schema: &schema,
Pretty: false,
}).ServeHTTP)
s.r.Get("/health", s.h.HealthCheck)
s.r.Get("/metrics", promhttp.Handler().ServeHTTP)
logrus.Info("Successfully set up chi routes")
}
func Monitor(serviceName, routeName, signature string) func(http.Handler) http.Handler {
summaryVec := monitoring.BuildSummaryVec(serviceName, routeName, signature)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
start := time.Now()
next.ServeHTTP(rw, req)
duration := time.Since(start)
// Store duration of request
summaryVec.WithLabelValues("duration").Observe(duration.Seconds())
// Store size of response, if possible.
size, err := strconv.Atoi(rw.Header().Get("Content-Length"))
if err == nil {
summaryVec.WithLabelValues("size").Observe(float64(size))
}
})
}
}
func Trace(opName string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
logrus.Infof("starting span for %v", opName)
span := tracing.StartHTTPTrace(req, opName)
ctx := tracing.UpdateContext(req.Context(), span)
next.ServeHTTP(rw, req.WithContext(ctx))
span.Finish()
logrus.Infof("finished span for %v", opName)
})
}
}