-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.go
48 lines (38 loc) · 988 Bytes
/
http_server.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
package serverutils
import (
"context"
"net/http"
)
// HttpServer represents an instance
// of *http.Server, which gracefully shutdowns
// and has a status
type HttpServer struct {
status Status
server *http.Server
}
// NewHttpServer creates a new instance
// of *HttpServer
func NewHttpServer(server *http.Server) *HttpServer {
return &HttpServer{server: server}
}
// Make sure struct implements interfaces.
var _ Server = &HttpServer{}
var _ serverOperations = &HttpServer{}
func (s *HttpServer) Run(ctx context.Context, req RunRequest) error {
return startServer(ctx, s, req)
}
func (s *HttpServer) Status() Status {
return s.status
}
func (s *HttpServer) gracefullyShutdown(ctx context.Context) error {
// Update server status to Closed
s.status = Stopped
// Shutdown server
return s.server.Shutdown(ctx)
}
func (s *HttpServer) serve(port int32) error {
// Update server status to Started
s.status = Running
// Start server
return s.server.ListenAndServe()
}