This repository was archived by the owner on Jul 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
106 lines (91 loc) · 2.25 KB
/
server.go
File metadata and controls
106 lines (91 loc) · 2.25 KB
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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"time"
"go.uber.org/fx"
"go.uber.org/zap"
)
type ServerIn struct {
fx.In
Logger *zap.Logger
CLI CLI
ListenConfig *net.ListenConfig
KeyHandler *KeyHandler
KeysHandler *KeysHandler
IssueHandler *IssueHandler
SignHandler *SignHandler
SwaggerHandler http.Handler `name:"swaggerHandler"`
Lifecycle fx.Lifecycle
Shutdowner fx.Shutdowner
}
func NewServer(in ServerIn) (s *http.Server, err error) {
s = &http.Server{
Addr: in.CLI.Address,
ReadHeaderTimeout: 2 * time.Second,
}
mux := http.NewServeMux()
mux.Handle("GET /keys", in.KeysHandler)
mux.Handle("GET /key", in.KeyHandler)
mux.Handle("GET /key/{kid}", in.KeyHandler)
mux.Handle("GET /issue", in.IssueHandler)
mux.Handle("PUT /sign", in.SignHandler)
mux.Handle("GET /swagger/", in.SwaggerHandler)
s.Handler = mux
in.Lifecycle.Append(
fx.StartStopHook(
func(ctx context.Context) (err error) {
var l net.Listener
l, err = in.ListenConfig.Listen(ctx, in.CLI.Network, in.CLI.Address)
if err == nil {
s.Addr = l.Addr().String()
go func() {
defer in.Shutdowner.Shutdown()
in.Logger.Info(
"starting server",
zap.String("address", s.Addr),
zap.Any(
"endpoints",
map[string]string{
"key": fmt.Sprintf("http://%s/key", s.Addr),
"keys": fmt.Sprintf("http://%s/keys", s.Addr),
"issue": fmt.Sprintf("http://%s/issue", s.Addr),
"sign": fmt.Sprintf("http://%s/sign", s.Addr),
},
),
)
serveErr := s.Serve(l)
if serveErr != nil && !errors.Is(serveErr, http.ErrServerClosed) {
in.Logger.Error("unable to start server", zap.Error(serveErr))
}
}()
}
if err != nil {
in.Logger.Error("unable to start listener", zap.Error(err))
}
return
},
s.Shutdown,
),
)
return
}
func ProvideServer() fx.Option {
return fx.Options(
fx.Provide(
func() *net.ListenConfig {
return new(net.ListenConfig)
},
NewServer,
),
fx.Invoke(
// force the server to start
func(*http.Server) {},
),
)
}