check / check (push) Successful in 3m54s
go.uber.org/fx builds the parts of waf and rproxy in internal/sww, one provider per part. Each background loop is a lifecycle hook added where its part is built. waf.Params takes every part built, and waf.New builds none. GeoJS calls back into waf, so waf exports AddLookup and GeoJSFailed, and clientGroup and banNetblock take the settings. newServer, loadStateFiles, the inBackground list and serveRProxy are gone. serve is the one hook that serves both daemons, and reports a failure to run on a channel of its own; fx's signal handling is not used. Once fx has stopped, run takes each daemon's last step: waf writes the state files, and rproxy waits for its reports. Model: opus-5-5
88 lines
2.1 KiB
Go
88 lines
2.1 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 (
|
|
server *rproxy.Server
|
|
listener net.Listener
|
|
)
|
|
|
|
failed := make(servingFailed, 1)
|
|
|
|
app := fx.New(
|
|
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
|
|
fx.Provide(
|
|
newRProxyServer,
|
|
func(server *rproxy.Server) *http.Server { return server.Server },
|
|
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")
|
|
}
|