forked from raystack/salt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
82 lines (70 loc) · 2.25 KB
/
http.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
package server
import (
"context"
"fmt"
"net/http"
"strings"
)
// HTTPServer is an server to serve http requests
//
// Deprecated: Prefer `mux` package instead of this.
type HTTPServer struct {
config Config
httpServer *http.Server
// httpMux is used for allowing addition of custom handlers to the http server
httpMux *http.ServeMux
}
// HTTPOption sets configs, properties or other parameters for the server.HTTPServer
type HTTPOption func(*httpOptions)
type httpOptions struct {
httpServer *http.Server
}
// WithHTTPServer sets http.Server instance for server.HTTPServer
func WithHTTPServer(httpServer *http.Server) HTTPOption {
return func(hos *httpOptions) {
hos.httpServer = httpServer
}
}
// NewHTTP creates a new server.HTTPServer instance with given config and server.HTTPOption
//
// Deprecated: Prefer `mux` package instead of this.
func NewHTTP(config Config, options ...HTTPOption) (*HTTPServer, error) {
hos := &httpOptions{}
for _, opt := range options {
opt(hos)
}
server := &HTTPServer{config: config}
if hos.httpServer != nil {
server.httpServer = hos.httpServer
} else {
server.httpServer = &http.Server{}
}
server.httpServer.Addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
server.httpMux = http.NewServeMux()
return server, nil
}
// Serve starts the configured http server to serve requests
func (s *HTTPServer) Serve() error {
s.httpServer.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.httpMux.ServeHTTP(w, r)
})
return s.httpServer.ListenAndServe()
}
// RegisterHandler registers provided pattern and handler on the http server
func (s *HTTPServer) RegisterHandler(pattern string, handler http.Handler) {
s.httpMux.Handle(pattern, handler)
}
// SetGateway sets a server.GRPCGateway instance on the http server to be proxy requests to a grpc service
func (s *HTTPServer) SetGateway(patternPrefix string, gw *GRPCGateway) {
prefix := strings.TrimSuffix(patternPrefix, "/")
pattern := prefix + "/"
s.httpMux.Handle(pattern, http.StripPrefix(prefix, gw.gwmux))
}
// Shutdown gracefully stops the server, and kills the server when passed context is cancelled
func (s *HTTPServer) Shutdown(ctx context.Context) {
s.httpServer.Shutdown(ctx)
go func() {
<-ctx.Done()
s.httpServer.Close()
}()
}