check / check (push) Waiting to run
Neither Server embeds *http.Server any more. Each holds it as a private field, is its handler, and has Serve, Shutdown, Close and ServeHTTP. sww's serve hook takes the new daemonServer interface of those first three, and fx provides each Server as it. Model: opus-5-5
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package sww
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/processlog"
|
|
"sneak.berlin/go/smallwebwaf/internal/rproxy"
|
|
)
|
|
|
|
// RunRProxy is `sww rproxy`: it reads rproxy's settings, then serves
|
|
// requests until ctx is done. Then it gives the requests in progress
|
|
// shutdownTimeout to finish, and the reports being sent
|
|
// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. It returns the process's exit status, 1
|
|
// when rproxy cannot start. Its process messages name the host's name as
|
|
// instance, and it writes those at info or more severe: rproxy has no
|
|
// setting for either.
|
|
func RunRProxy(ctx context.Context, params Params) int {
|
|
hostname, _ := os.Hostname() // "" when the host has no name to give
|
|
processLog := processlog.New(processlog.Params{
|
|
Output: params.Stdout,
|
|
InstanceName: hostname,
|
|
Level: slog.LevelInfo,
|
|
})
|
|
|
|
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
|
if err != nil {
|
|
return cannotStart(processLog, startError{"invalid setting", err})
|
|
}
|
|
|
|
var (
|
|
server *rproxy.Server
|
|
listener net.Listener
|
|
)
|
|
|
|
failed := make(servingFailed, 1)
|
|
|
|
app := fx.New(
|
|
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
|
|
fx.Provide(
|
|
// run waits for rproxy.Server's reports, and serve takes it as a
|
|
// daemonServer.
|
|
fx.Annotate(
|
|
newRProxyServer,
|
|
fx.As(fx.Self()),
|
|
fx.As(new(daemonServer)),
|
|
),
|
|
newRProxyListener,
|
|
),
|
|
fx.Invoke(serve),
|
|
fx.Populate(&server, &listener),
|
|
fx.WithLogger(fxEventLogger(processLog)),
|
|
)
|
|
|
|
err = app.Err()
|
|
if err != nil {
|
|
return cannotStart(processLog, err)
|
|
}
|
|
|
|
processLog.Info("starting",
|
|
"version", params.Version,
|
|
"address", listener.Addr().String(),
|
|
"settings", cfg)
|
|
|
|
return run(ctx, app, failed, processLog, func() error {
|
|
server.WaitForReports()
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// newRProxyServer returns the server rproxy runs.
|
|
func newRProxyServer(
|
|
cfg *config.RProxy, out requestLog, processLog *slog.Logger,
|
|
) *rproxy.Server {
|
|
return rproxy.New(rproxy.Params{
|
|
Config: cfg,
|
|
RequestLog: out,
|
|
ProcessLog: processLog,
|
|
})
|
|
}
|
|
|
|
// newRProxyListener listens on SWW_RPROXY_LISTEN_ADDR.
|
|
func newRProxyListener(cfg *config.RProxy) (net.Listener, error) {
|
|
return listen(cfg.ListenAddr, "SWW_RPROXY_LISTEN_ADDR")
|
|
}
|