-
Notifications
You must be signed in to change notification settings - Fork 0
/
appsrv.go
159 lines (131 loc) · 3.43 KB
/
appsrv.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"github.com/gabibotos/go-srv/log"
"github.com/gabibotos/go-srv/middleware"
"github.com/gabibotos/go-srv/srv"
"github.com/gorilla/mux"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.opencensus.io/zpages"
"net/http"
"github.com/heptiolabs/healthcheck"
)
/*
create git repo
create new repo using this appsrv
add viper, cobra and slog
create runtime.go and test it again
get more info about metrics and trace
checkout google wire, go project structure, some mock lib for tests
*/
type (
appsrv struct {
healthcheck.Handler
opts *options
lg log.Logger
server srv.Server
app *mux.Router
systemApp *mux.Router
}
)
func New(log log.Logger, opts ...Option) Server {
sysApp := mux.NewRouter()
health := healthcheck.NewHandler()
sysApp.Use(middleware.NoCache)
sysApp.PathPrefix("/debug/pprof/").Handler(middleware.Profiler())
sysApp.HandleFunc("/healthz", health.LiveEndpoint)
sysApp.HandleFunc("/readyz", health.ReadyEndpoint)
sysApp.HandleFunc("/version", VersionHandler(log, NewVersionInfo()))
app := mux.NewRouter()
app.Use(
middleware.ProxyHeaders,
middleware.Recover(log),
middleware.LogRequests(log),
)
s := appsrv{
lg: log,
app: app,
systemApp: sysApp,
Handler: health,
}
s.opts = newDefaultWithOptions(&s, opts...)
if s.opts.metrics != nil {
if pe, ok := s.opts.metrics.(http.Handler); ok {
s.systemApp.Handle("/metrics", pe)
}
err := view.Register(ochttp.ServerRequestCountView,
ochttp.ServerRequestBytesView,
ochttp.ServerResponseBytesView,
ochttp.ServerLatencyView,
ochttp.ServerRequestCountByMethod,
ochttp.ServerResponseCountByStatusCode)
if err != nil {
panic(err)
}
view.RegisterExporter(s.opts.metrics)
}
if s.opts.tracer != nil {
trace.RegisterExporter(s.opts.tracer)
s.app.Use(
func(next http.Handler) http.Handler {
return &ochttp.Handler{
Handler: next,
Propagation: &b3.HTTPFormat{},
IsPublicEndpoint: s.opts.isPublic,
}
},
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
if route != nil {
// Get the route path and pas it to OpenCensus HTTP handler
path, err := route.GetPathTemplate()
if err != nil {
http.NotFound(w, r)
return
}
ochttp.WithRouteTag(next, path).ServeHTTP(w, r)
} else {
// Handle case when no route is found (if needed)
http.NotFound(w, r)
}
})
},
)
muxx := http.NewServeMux()
zpages.Handle(muxx, "/")
sysApp.Handle("/rpcz", muxx)
sysApp.Handle("/tracez", muxx)
sysApp.Handle("/public/", muxx)
}
return &s
}
func (s *appsrv) App() *mux.Router {
return s.app
}
// Init the application and its modules with the config.
func (s *appsrv) Init() error {
srvOpts := s.opts.httpOpts[:]
srvOpts = append(srvOpts, s.opts.systemOpts...) // force admin config
srvOpts = append(srvOpts,
srv.HandlesRequestsWith(s.app), // force handler config
)
s.server = srv.New(srvOpts...)
return nil
}
// Start the application an its enabled modules
func (s *appsrv) Start() error {
if err := s.server.Listen(); err != nil {
return err
}
return s.server.Serve()
}
// Stop the application an its enabled modules
func (s *appsrv) Stop() error {
if err := s.server.Shutdown(); err != nil {
return err
}
return nil
}