-
Notifications
You must be signed in to change notification settings - Fork 65
/
server.go
48 lines (41 loc) · 1.06 KB
/
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 server
import (
"context"
"net/http"
"time"
"github.com/go-acme/lego/log"
"github.com/gophish/hub/config"
)
type Server struct {
config *config.ServerConfig
http *http.Server
repositories []string
}
type ServerOption func(*Server) error
func NewServer(serverConfig *config.ServerConfig, opts ...ServerOption) (*Server, error) {
defaultServer := &http.Server{
ReadTimeout: 10 * time.Second,
Addr: serverConfig.ListenAddress,
}
server := &Server{
config: serverConfig,
http: defaultServer,
}
for _, opt := range opts {
if err := opt(server); err != nil {
return server, err
}
}
err := server.registerRoutes()
return server, err
}
func (server *Server) Start() error {
log.Infof("Starting healthcheck server at http://%s", server.config.ListenAddress)
return server.http.ListenAndServe()
}
// Shutdown attempts to gracefully shutdown the server.
func (server *Server) Shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
return server.http.Shutdown(ctx)
}