refactor(reverse-proxy): use route registry

This commit is contained in:
2026-08-23 23:44:22 -04:00
parent c267a2ec5c
commit 7f6d3b295e
9 changed files with 186 additions and 235 deletions

View File

@@ -1,10 +1,33 @@
{ config, lib, my, ... }:
{
config,
lib,
my,
...
}@args:
let
cfg = config.modules.reverse-proxy;
inherit (my) reverseProxyRoutes;
registry = args.registry or config.registry;
enabledRoutes = lib.filterAttrs (_: route: route.enable) cfg.routes;
routesByHost = lib.groupBy (route: route.host) (lib.attrValues enabledRoutes);
routeWithBackend = hostName: routeName: route:
let
facts = my.registry.lookup registry hostName routeName;
in
if route.backend != null then
route
else if facts ? address && route.port != null then
route // { backend = "${route.scheme}://${facts.address}:${toString route.port}"; }
else
route;
allRoutes = lib.concatLists (
lib.mapAttrsToList (
hostName: routes:
lib.mapAttrsToList (routeName: route: routeWithBackend hostName routeName route) routes
) registry.route
);
enabledRoutes = builtins.filter (route: route.enable) allRoutes;
routesByHost = lib.groupBy (route: route.host) enabledRoutes;
rootRoutesByHost = lib.mapAttrs (
host: routes:
@@ -46,27 +69,22 @@ in
options.modules.reverse-proxy = {
enable = lib.mkEnableOption "the Nginx reverse proxy route realizer";
routes = lib.mkOption {
type = lib.types.attrsOf reverseProxyRoutes.routeType;
default = { };
description = "Reverse-proxy route declarations keyed by stable route name.";
};
};
config = lib.mkIf cfg.enable {
assertions =
lib.mapAttrsToList (host: rootRoute: {
assertion = rootRoute != null;
message = "modules.reverse-proxy.routes for ${host} must include a primary / route.";
message = "registry.route entries for ${host} must include a primary / route.";
}) rootRoutesByHost
++ lib.mapAttrsToList (host: routes: {
assertion = !hasDuplicatePaths routes;
message = "modules.reverse-proxy.routes for ${host} must not declare the same path more than once.";
message = "registry.route entries for ${host} must not declare the same path more than once.";
}) routesByHost
++ map (route: {
assertion = !hasUnresolvedBackend route;
message = "modules.reverse-proxy.routes for ${route.host}${route.path} must resolve to a backend URL before actualization.";
}) (builtins.filter hasUnresolvedBackend (lib.attrValues enabledRoutes));
message = "registry.route entry for ${route.host}${route.path} must resolve to a backend URL before actualization.";
}) (builtins.filter hasUnresolvedBackend enabledRoutes);
networking.firewall.allowedTCPPorts = [
80