Repository navigation
Expand file tree
/
Copy pathserve_runtime.go
More file actions
321 lines (303 loc) · 7.61 KB
/
Copy pathserve_runtime.go
File metadata and controls
321 lines (303 loc) · 7.61 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"
"go.kenn.io/agentsview/internal/apiclient"
"go.kenn.io/agentsview/internal/config"
"go.kenn.io/agentsview/internal/server"
)
type serveRuntimeOptions struct {
Mode string
BasePath string
RequestedPort int
OnCaddyStarted func(int)
}
type serveRuntime struct {
Cfg config.Config
LocalURL string
PublicURL string
ServeErrCh <-chan error
Caddy *managedCaddy
}
func prepareRunServeRuntimeConfig(ctx context.Context,
cfg config.Config,
restartPort int,
onCaddyStarted func(int),
) (config.Config, serveRuntimeOptions, error) {
cfg, requestedPort := applyServeRestartPort(cfg, restartPort)
opts := serveRuntimeOptions{
Mode: "serve",
RequestedPort: requestedPort,
OnCaddyStarted: onCaddyStarted,
}
prepared, err := prepareServeRuntimeConfig(ctx, cfg, opts)
return prepared, opts, err
}
func prepareServeRuntimeConfig(ctx context.Context,
cfg config.Config,
opts serveRuntimeOptions,
) (config.Config, error) {
requestedPort := opts.RequestedPort
if requestedPort == 0 {
requestedPort = cfg.Port
}
port, err := server.FindAvailablePort(ctx, cfg.Host, cfg.Port)
if err != nil {
return cfg, err
}
if cfg.PortExplicit && cfg.Port != 0 && port != cfg.Port {
return cfg, fmt.Errorf(
"requested port %d on %s is unavailable; choose another --port or use --port 0",
cfg.Port, cfg.Host,
)
}
if port != cfg.Port {
if cfg.Port == 0 {
fmt.Printf("Using available port %d\n", port)
} else {
fmt.Printf("Port %d in use, using %d\n", cfg.Port, port)
}
}
cfg.Port = port
if cfg.Proxy.Mode == "" && cfg.PublicURL != "" {
updatedURL, updatedOrigins, changed, err := rewriteConfiguredPublicURLPort(
cfg.PublicURL,
cfg.PublicOrigins,
requestedPort,
cfg.Port,
)
if err != nil {
return cfg, fmt.Errorf("invalid public url: %w", err)
}
if changed {
cfg.PublicURL = updatedURL
cfg.PublicOrigins = updatedOrigins
}
}
return cfg, nil
}
func startServerWithOptionalCaddy(
ctx context.Context,
cfg config.Config,
srv *server.Server,
opts serveRuntimeOptions,
) (*serveRuntime, error) {
serveErrCh := make(chan error, 1)
go func() {
serveErrCh <- srv.ListenAndServe()
}()
if err := waitForBackendReady(
ctx, cfg, srv, opts.BasePath, 5*time.Second, serveErrCh,
); err != nil {
shutdownCtx, cancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
if errors.Is(err, context.Canceled) {
return nil, err
}
return nil, fmt.Errorf("server failed to start: %w", err)
}
var caddy *managedCaddy
if cfg.Proxy.Mode == "caddy" {
var err error
caddy, err = startManagedCaddy(
ctx, cfg, opts.Mode, opts.OnCaddyStarted,
)
if err != nil {
shutdownCtx, cancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
return nil, fmt.Errorf("managed caddy error: %w", err)
}
publicPort, err := publicURLPort(cfg.PublicURL)
if err != nil {
shutdownCtx, cancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer cancel()
caddy.Stop()
_ = srv.Shutdown(shutdownCtx)
return nil, fmt.Errorf("invalid public url: %w", err)
}
if err := waitForLocalPort(
ctx,
cfg.Proxy.BindHost,
publicPort,
5*time.Second,
caddy.Err(),
); err != nil {
shutdownCtx, cancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer cancel()
caddy.Stop()
_ = srv.Shutdown(shutdownCtx)
if errors.Is(err, context.Canceled) {
return nil, err
}
return nil, fmt.Errorf("managed caddy error: %w", err)
}
}
return &serveRuntime{
Cfg: cfg,
LocalURL: fmt.Sprintf("http://%s:%d", cfg.Host, cfg.Port),
PublicURL: strings.TrimRight(browserURL(cfg), "/") + strings.TrimRight(opts.BasePath, "/"),
ServeErrCh: serveErrCh,
Caddy: caddy,
}, nil
}
// waitForBackendReady waits until the started HTTP server proves that the
// readiness request reached this Server instance. The proof uses a temporary
// server-held secret, so a colliding listener never receives the persistent
// bearer token.
func waitForBackendReady(
ctx context.Context,
cfg config.Config,
srv *server.Server,
basePath string,
timeout time.Duration,
errCh <-chan error,
) error {
if err := srv.EnableStartupProbe(); err != nil {
return err
}
defer srv.DisableStartupProbe()
deadline := time.Now().Add(timeout)
address := net.JoinHostPort(
probeHostForDial(cfg.Host), strconv.Itoa(cfg.Port),
)
transport := &http.Transport{}
defer transport.CloseIdleConnections()
client := &http.Client{
Transport: transport,
Timeout: 200 * time.Millisecond,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
api, err := apiclient.NewHTTPClient("http://"+address+strings.TrimRight(basePath, "/"), "", client)
if err != nil {
return err
}
var lastErr error
for time.Now().Before(deadline) {
if ctx.Err() != nil {
return ctx.Err()
}
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errCh:
if ctx.Err() != nil {
return ctx.Err()
}
if err == nil {
return fmt.Errorf(
"service exited before becoming ready on %s",
address,
)
}
return err
default:
}
challenge, expectedProof, err := srv.StartupProbeChallenge()
if err != nil {
return err
}
response, err := api.GetStartupProbeWithResponse(ctx, &apiclient.GetStartupProbeRequestOptions{
Header: &apiclient.GetStartupProbeHeaders{XAgentsViewStartupChallenge: challenge},
})
if response != nil {
resp := response.HTTPResponse
if resp.StatusCode != http.StatusNoContent {
err = fmt.Errorf(
"startup probe on %s returned status %d",
address, resp.StatusCode,
)
} else if !server.ValidStartupProbeResponse(resp, expectedProof) {
err = fmt.Errorf("startup probe on %s returned an invalid proof", address)
} else {
return nil
}
}
lastErr = err
timer := time.NewTimer(50 * time.Millisecond)
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
}
if lastErr == nil {
lastErr = fmt.Errorf("timed out waiting for %s", address)
}
return lastErr
}
func waitForServerRuntime(
ctx context.Context,
srv *server.Server,
rt *serveRuntime,
) error {
var caddyErrCh <-chan error
if rt.Caddy != nil {
caddyErrCh = rt.Caddy.Err()
}
select {
case err := <-rt.ServeErrCh:
if err != nil && !errors.Is(err, http.ErrServerClosed) {
if rt.Caddy != nil {
rt.Caddy.Stop()
}
return fmt.Errorf("server error: %w", err)
}
if rt.Caddy != nil {
rt.Caddy.Stop()
}
return nil
case err := <-caddyErrCh:
shutdownCtx, cancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
if ctx.Err() != nil {
if serveErr := <-rt.ServeErrCh; serveErr != nil &&
!errors.Is(serveErr, http.ErrServerClosed) {
return fmt.Errorf("server error: %w", serveErr)
}
return nil
}
if err != nil {
return fmt.Errorf("managed caddy error: %w", err)
}
return errors.New("managed caddy exited unexpectedly")
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(
context.Background(), 5*time.Second,
)
defer cancel()
if rt.Caddy != nil {
rt.Caddy.Stop()
}
if err := srv.Shutdown(shutdownCtx); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("server shutdown error: %w", err)
}
if err := <-rt.ServeErrCh; err != nil &&
!errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("server error: %w", err)
}
return nil
}
}