check / check (push) Successful in 4m20s
go.uber.org/fx builds each daemon's parts in internal/sww from one small provider per part, and runs each background loop as one lifecycle hook. This replaces newServer, loadStateFiles, the inBackground list and both serve functions. One serve hook and one run function serve both daemons. fx's own events go to the process log at debug. Start-up errors keep their messages through startError. RunWAF and RunRProxy use fx's Start and Stop, not its signal handling. The last state write is registered before the loops that feed it, so that it runs after they end. Model: opus-5-5
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package sww
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
"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 := requestlog.NewProcessLogger(
|
|
params.Stdout,
|
|
hostname,
|
|
slog.LevelInfo,
|
|
)
|
|
|
|
cfg, err := config.RProxyFromEnvironment(params.Viper)
|
|
if err != nil {
|
|
return cannotStart(processLog, startError{"invalid setting", err})
|
|
}
|
|
|
|
var listener net.Listener
|
|
|
|
app := fx.New(
|
|
fx.Supply(cfg, processLog, requestLog{params.Stdout}),
|
|
fx.Provide(
|
|
newRProxyServer,
|
|
func(server *rproxy.Server) *http.Server { return server.Server },
|
|
newRProxyListener,
|
|
),
|
|
fx.Invoke(serve),
|
|
fx.Populate(&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, processLog)
|
|
}
|
|
|
|
// newRProxyServer returns the server rproxy runs. Once it has stopped
|
|
// serving, the stop waits for the reports being sent.
|
|
func newRProxyServer(
|
|
lc fx.Lifecycle, cfg *config.RProxy, out requestLog,
|
|
processLog *slog.Logger,
|
|
) *rproxy.Server {
|
|
server := rproxy.New(rproxy.Params{
|
|
Config: cfg,
|
|
RequestLog: out,
|
|
ProcessLog: processLog,
|
|
})
|
|
|
|
// fx stops in the reverse order, so this follows the stop of serving.
|
|
lc.Append(fx.StopHook(server.WaitForReports))
|
|
|
|
return server
|
|
}
|
|
|
|
// newRProxyListener listens on SWW_RPROXY_LISTEN_ADDR.
|
|
func newRProxyListener(cfg *config.RProxy) (net.Listener, error) {
|
|
return listen(cfg.ListenAddr, "SWW_RPROXY_LISTEN_ADDR")
|
|
}
|