refactor(reverse-proxy): use route registry

This commit is contained in:
2026-08-23 23:44:22 -04:00
parent c267a2ec5c
commit ed018806d8
8 changed files with 89 additions and 106 deletions

View File

@@ -1,10 +1,34 @@
{ config, lib, my, ... }:
{
config,
lib,
my,
registry ? config.registry,
...
}:
let
cfg = config.modules.reverse-proxy;
inherit (my) reverseProxyRoutes;
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.at 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
) cfg.routes
);
enabledRoutes = builtins.filter (route: route.enable) allRoutes;
routesByHost = lib.groupBy (route: route.host) enabledRoutes;
rootRoutesByHost = lib.mapAttrs (
host: routes:
@@ -47,9 +71,9 @@ in
enable = lib.mkEnableOption "the Nginx reverse proxy route realizer";
routes = lib.mkOption {
type = lib.types.attrsOf reverseProxyRoutes.routeType;
type = lib.types.attrsOf (lib.types.attrsOf reverseProxyRoutes.routeType);
default = { };
description = "Reverse-proxy route declarations keyed by stable route name.";
description = "Reverse-proxy route declarations keyed by source host and stable route name.";
};
};
@@ -66,7 +90,7 @@ in
++ 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));
}) (builtins.filter hasUnresolvedBackend enabledRoutes);
networking.firewall.allowedTCPPorts = [
80