-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
91 lines (75 loc) · 2.42 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
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
package api
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// Prefixes.
const (
Prefix = "/elemental"
PrefixV1 = "/v1"
)
type Server struct {
context context.Context
port uint
k8sClient client.Client
httpServer *http.Server
logger logr.Logger
}
func NewServer(ctx context.Context, k8sClient client.Client, port uint) *Server {
return &Server{
context: ctx,
port: port,
k8sClient: k8sClient,
logger: log.FromContext(ctx),
}
}
func (s *Server) NewRouter() *mux.Router {
router := mux.NewRouter()
elementalV1 := router.PathPrefix(fmt.Sprintf("%s%s", Prefix, PrefixV1)).Subrouter()
elementalV1.Handle("/namespaces/{namespace}/registrations/{registrationName}",
NewGetElementalRegistrationHandler(s.logger, s.k8sClient)).
Methods(http.MethodGet)
elementalV1.Handle("/namespaces/{namespace}/registrations/{registrationName}/hosts",
NewPostElementalHostHandler(s.logger, s.k8sClient)).
Methods(http.MethodPost)
elementalV1.Handle("/namespaces/{namespace}/registrations/{registrationName}/hosts/{hostName}",
NewDeleteElementalHostHandler(s.logger, s.k8sClient)).
Methods(http.MethodDelete)
elementalV1.Handle("/namespaces/{namespace}/registrations/{registrationName}/hosts/{hostName}",
NewPatchElementalHostHandler(s.logger, s.k8sClient)).
Methods(http.MethodPatch)
elementalV1.Handle("/namespaces/{namespace}/registrations/{registrationName}/hosts/{hostName}/bootstrap",
NewGetElementalHostBootstrapHandler(s.logger, s.k8sClient)).
Methods(http.MethodGet)
return router
}
func (s *Server) Start(ctx context.Context) error {
s.logger.Info("Starting Elemental API V1 Server")
s.httpServer = &http.Server{
Handler: s.NewRouter(),
Addr: fmt.Sprintf(":%d", s.port),
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
}
go func() {
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
s.logger.Error(err, "FATAL: listening for TCP incoming connections")
os.Exit(1)
}
}()
<-ctx.Done()
s.logger.Info("Shutting down Elemental API V1 Server")
shutdownCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
if err := s.httpServer.Shutdown(shutdownCtx); err != nil {
s.logger.Error(err, "shutting down http server")
}
return nil
}