-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.go
120 lines (95 loc) · 2.48 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package main
import (
"errors"
"log/slog"
"net/http"
"github.com/spf13/pflag"
"github.com/cilium/hive"
"github.com/cilium/hive/cell"
)
var serverCell = cell.Module(
"http-server",
"Simple HTTP Server",
cell.Config(defaultServerConfig),
cell.Provide(newServer),
)
//
// Server API
//
type Server interface {
// ListenAddress returns the address at which the server is listening,
// e.g. ":8888".
ListenAddress() string
}
// HTTPHandler specifies an HTTP handler for a specific path.
type HTTPHandler struct {
Path string // Path to serve at, e.g. /hello
Handler http.HandlerFunc // The handler to call for this path
}
// HTTPHandlerOut is a convenience struct for cells that implement
// only a single handler.
type HTTPHandlerOut struct {
cell.Out
HTTPHandler HTTPHandler `group:"http-handlers"`
}
//
// Server configuration
//
type serverConfig struct {
ServerAddress string // Server listen address
}
func (def serverConfig) Flags(flags *pflag.FlagSet) {
flags.String("server-address", def.ServerAddress, "HTTP server listen address")
}
// defaultServerConfig is the default server configuration.
var defaultServerConfig = serverConfig{
ServerAddress: ":8888",
}
//
// Implementation
//
type serverParams struct {
cell.In
Config serverConfig
Log *slog.Logger
Lifecycle cell.Lifecycle
Shutdowner hive.Shutdowner
Handlers []HTTPHandler `group:"http-handlers"`
}
type simpleServer struct {
params serverParams
server http.Server
}
func (s *simpleServer) listenAndServe() {
s.params.Log.Info("Listening", "server-address", s.params.Config.ServerAddress)
err := s.server.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
// An unexpected error happened (e.g. failed to listen),
// shut down the application.
s.params.Shutdowner.Shutdown(hive.ShutdownWithError(err))
}
}
func (s *simpleServer) ListenAddress() string {
return s.server.Addr
}
func (s *simpleServer) Start(ctx cell.HookContext) error {
go s.listenAndServe()
return nil
}
func (s *simpleServer) Stop(ctx cell.HookContext) error {
// Stop the server. Waits for clients to finish.
return s.server.Shutdown(ctx)
}
func newServer(params serverParams) Server {
mux := http.NewServeMux()
s := &simpleServer{params: params}
s.server.Addr = params.Config.ServerAddress
s.server.Handler = mux
for _, h := range params.Handlers {
mux.HandleFunc(h.Path, h.Handler)
}
params.Lifecycle.Append(s)
return s
}