-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
56 lines (47 loc) · 1.42 KB
/
rest.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
package server
import (
"fmt"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// RegisterEndpointFunc defines function contract to register endpoint.
type RegisterEndpointFunc func(server *runtime.ServeMux) error
// Rest is responsible to act as HTTP/1.1 REST server.
// It composes grpc-gateway runtime.ServeMux.
type Rest struct {
*runtime.ServeMux
port string
}
// NewRest creates an instance of Rest.
func NewRest(port string) *Rest {
return &Rest{
ServeMux: runtime.NewServeMux(),
port: port,
}
}
// EnablePrometheus enables prometheus endpoint.
// It can be accessed via /metrics.
func (r *Rest) EnablePrometheus() error {
return r.ServeMux.HandlePath(http.MethodGet, "/metrics", prometheusHandler())
}
// RegisterEndpoints registers HTTP/1.1 REST endpoints.
// If there are some errors, it returns the first error it encounter and stop the iteration.
func (r *Rest) RegisterEndpoints(fns ...RegisterEndpointFunc) error {
for _, fn := range fns {
if err := fn(r.ServeMux); err != nil {
return err
}
}
return nil
}
// Run runs HTTP/1.1 runtime.ServeMux.
// It is blocking.
func (r *Rest) Run() error {
return http.ListenAndServe(fmt.Sprintf(":%s", r.port), r.ServeMux)
}
func prometheusHandler() runtime.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, _ map[string]string) {
promhttp.Handler().ServeHTTP(w, r)
}
}