/
baseRouter.go
65 lines (52 loc) · 1.58 KB
/
baseRouter.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
package server
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"github.com/go-chi/chi/middleware"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
// BaseRouterCfg stores configs for base router
type BaseRouterCfg struct {
ServiceVersion string
ProfilerPath string
APIDefinition io.Reader
APIDefinitionPath string
}
// NewBaseRouter will return new router with already registered standard endpoints
// /version.json - to serve service version
// {cfg.ApiDefinition} - to serve api schema
// {ProfilerPath} - to serve http profiler
func NewBaseRouter(h BaseHTTPHandler, cfg BaseRouterCfg) (*mux.Router, error) {
srv := mux.NewRouter()
// API definition
APIDefinition, err := ioutil.ReadAll(cfg.APIDefinition)
if err != nil {
return nil, errors.Wrap(err, "failed to read api definition")
}
// Register swagger handler
srv.HandleFunc(cfg.APIDefinitionPath, func(w http.ResponseWriter, r *http.Request) {
l := h.NewLoggingResponseWriter(w, r)
_, err := io.Copy(w, bytes.NewReader(APIDefinition))
if err != nil {
l.WriteHeaderWithErr(http.StatusInternalServerError, err)
}
})
// VERSION
versionResp := []byte(`{"version":"` + cfg.ServiceVersion + `"}`)
srv.HandleFunc("/version.json", func(w http.ResponseWriter, r *http.Request) {
l := h.NewLoggingResponseWriter(w, r)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write(versionResp)
if err != nil {
l.WriteHeaderWithErr(http.StatusInternalServerError, err)
}
})
// HTTP PROFILER
if len(cfg.ProfilerPath) != 0 {
srv.Handle(cfg.ProfilerPath, middleware.Profiler())
}
return srv, nil
}