Wire process identity cancellation and shared HTTP server lifecycle
This commit is contained in:
76
internal/adapter/cli/server.go
Normal file
76
internal/adapter/cli/server.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
httpadapter "gitea.maximumdirect.net/eric/scriptorium/internal/adapter/http"
|
||||
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
"go.maximumdirect.net/ckconfig"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func serveHTTP(invocation commandkit.Invocation, input serveInput) commandkit.Outcome {
|
||||
return serveWithListener(invocation, input, (&net.ListenConfig{}).Listen)
|
||||
}
|
||||
|
||||
func serveWithListener(invocation commandkit.Invocation, input serveInput, listen func(context.Context, string, string) (net.Listener, error)) commandkit.Outcome {
|
||||
result, err := loadApplication(invocation.Context, input.Selection, input.Application, struct{}{})
|
||||
var sources []configkit.SourceInfo
|
||||
var records []configkit.Diagnostic
|
||||
if result != nil {
|
||||
sources, records = result.Sources(), result.Diagnostics()
|
||||
}
|
||||
var native *configkit.LoadError
|
||||
if errors.As(err, &native) && native != nil {
|
||||
sources, records = native.Sources(), native.Diagnostics()
|
||||
}
|
||||
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
||||
queueDiagnostics(invocation.Reporter(), diagnostics)
|
||||
if err != nil {
|
||||
return requestFailure(err, explanation)
|
||||
}
|
||||
settings := appconfig.Settings(result.Config)
|
||||
reader, err := httpadapter.NewRestrictedArtifactReader(settings.ArtifactRoot, settings.MaxArtifactBytes)
|
||||
if err != nil {
|
||||
return commandkit.Failure("Could not initialize the HTTP artifact reader.").WithCause(err)
|
||||
}
|
||||
engine, err := newEngine(settings, promptkit.WithArtifactReader(reader))
|
||||
if err != nil {
|
||||
return frameworkFailure(invocation.Reporter(), err, explanation)
|
||||
}
|
||||
inspection, err := engine.InspectConfiguration()
|
||||
if err != nil {
|
||||
return requestFailure(err, explanation)
|
||||
}
|
||||
diagnostics, explanation = ckconfig.AdaptDiagnostics("PromptKit configuration:\n", inspection.Sources(), inspection.Diagnostics())
|
||||
queueDiagnostics(invocation.Reporter(), diagnostics)
|
||||
server := &http.Server{
|
||||
Addr: settings.ServerAddr,
|
||||
Handler: httpadapter.NewHandlerWithOptions(engine, httpadapter.HandlerOptions{MaxRequestBytes: settings.MaxRequestBytes, MaxResponseBytes: settings.MaxResponseBytes}),
|
||||
ReadHeaderTimeout: defaults.HTTPReadHeaderTimeoutDefault,
|
||||
}
|
||||
listener, err := listen(invocation.Context, "tcp", settings.ServerAddr)
|
||||
if err != nil {
|
||||
return commandkit.Failure("Could not listen for HTTP requests.").WithCause(err)
|
||||
}
|
||||
_ = invocation.Reporter().Info(fmt.Sprintf("Serving HTTP on %s.", listener.Addr()))
|
||||
closed := make(chan struct{})
|
||||
var cancellationCloseErr error
|
||||
stop := context.AfterFunc(invocation.Context, func() { cancellationCloseErr = server.Close(); close(closed) })
|
||||
serveErr := server.Serve(listener)
|
||||
if !stop() {
|
||||
<-closed
|
||||
}
|
||||
closeErr := errors.Join(cancellationCloseErr, server.Close())
|
||||
if errors.Is(serveErr, http.ErrServerClosed) && invocation.Context.Err() != nil {
|
||||
return commandkit.Failure("HTTP server stopped after cancellation.").WithCause(errors.Join(invocation.Context.Err(), closeErr))
|
||||
}
|
||||
return commandkit.Failure("HTTP server stopped unexpectedly.").WithCause(errors.Join(serveErr, closeErr))
|
||||
}
|
||||
Reference in New Issue
Block a user