refactor(reverse-proxy): use route registry
This commit is contained in:
@@ -1,10 +1,33 @@
|
||||
{ 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.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
|
||||
) cfg.routes
|
||||
);
|
||||
|
||||
enabledRoutes = builtins.filter (route: route.enable) allRoutes;
|
||||
routesByHost = lib.groupBy (route: route.host) enabledRoutes;
|
||||
|
||||
rootRoutesByHost = lib.mapAttrs (
|
||||
host: routes:
|
||||
@@ -47,9 +70,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 my.registry.definitions.route.type);
|
||||
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 +89,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
|
||||
|
||||
Reference in New Issue
Block a user