-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpserver.go
108 lines (90 loc) · 2.73 KB
/
rpserver.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
package server
import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"gopkg.in/reportportal/commons-go.v1/commons"
"gopkg.in/reportportal/commons-go.v1/conf"
"gopkg.in/reportportal/commons-go.v1/registry"
"log"
"net/http"
"strconv"
)
//RpServer represents ReportPortal micro-service instance
type RpServer struct {
mux *chi.Mux
cfg *conf.RpConfig
Sd registry.ServiceDiscovery
buildInfo *commons.BuildInfo
hChecks []HealthCheck
}
//New creates new instance of RpServer struct
func New(cfg *conf.RpConfig, buildInfo *commons.BuildInfo) *RpServer {
var sd registry.ServiceDiscovery
switch cfg.Registry {
case conf.Consul:
cfg.Consul.Tags = append(cfg.Consul.Tags, "statusPageUrlPath=/info", "healthCheckUrlPath=/health")
sd = registry.NewConsul(cfg)
}
srv := &RpServer{
mux: chi.NewMux(),
cfg: cfg,
Sd: sd,
hChecks: []HealthCheck{},
buildInfo: buildInfo,
}
srv.mux.Use(middleware.Recoverer)
return srv
}
//WithRouter gives access to Chi router to add route and perform other modifications
func (srv *RpServer) WithRouter(f func(router *chi.Mux)) {
f(srv.mux)
}
//AddHandler is preferred way to add handler to the server.
//Allows to return error which is representation of HTTP Response
func (srv *RpServer) AddHandler(method, pattern string, f func(w http.ResponseWriter, r *http.Request) error) {
srv.mux.Method(method, pattern, Handler{f})
}
//AddHealthCheck adds health check
func (srv *RpServer) AddHealthCheck(h HealthCheck) {
srv.hChecks = append(srv.hChecks, h)
}
//AddHealthCheckFunc adds health check function
func (srv *RpServer) AddHealthCheckFunc(f func() error) {
srv.hChecks = append(srv.hChecks, HealthCheckFunc(f))
}
//StartServer starts HTTP server
func (srv *RpServer) StartServer() {
srv.initDefaultRoutes()
if nil != srv.Sd {
registry.Register(srv.Sd)
}
// listen and server on mentioned port
log.Printf("Starting on port %d", srv.cfg.Server.Port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(srv.cfg.Server.Port), srv.mux))
}
//initDefaultRoutes initializes default routes
func (srv *RpServer) initDefaultRoutes() {
srv.mux.Get("/health", func(w http.ResponseWriter, rq *http.Request) {
//collect status results
var errs []string
for _, hc := range srv.hChecks {
if err := hc.Check(); nil != err {
errs = append(errs, err.Error())
}
}
rs := map[string]interface{}{}
status := http.StatusOK
if len(errs) > 0 {
rs["status"] = "DOWN"
rs["errors"] = errs
status = http.StatusBadRequest
} else {
rs["status"] = "UP"
}
WriteJSON(status, rs, w)
})
bi := map[string]interface{}{"build": srv.buildInfo}
srv.mux.Get("/info", func(w http.ResponseWriter, rq *http.Request) {
WriteJSON(http.StatusOK, bi, w)
})
}