mirror of
https://github.com/dali99/nixos-matrix-modules.git
synced 2025-09-14 04:52:48 +02:00
nginx: refactor upstream generation
This commit is contained in:
60
lib.nix
60
lib.nix
@@ -17,4 +17,64 @@ rec {
|
||||
l = firstListenerOfType r w.settings.worker_listeners;
|
||||
in connectionInfo l;
|
||||
|
||||
mapWorkersToUpstreamsByType = workerInstances:
|
||||
lib.pipe workerInstances [
|
||||
lib.attrValues
|
||||
|
||||
# Index by worker type
|
||||
(lib.foldl (acc: worker: acc // {
|
||||
${worker.type} = (acc.${worker.type} or [ ]) ++ [ worker ];
|
||||
}) { })
|
||||
|
||||
# Subindex by resource names, listener types, and convert to upstreams
|
||||
(lib.mapAttrs (_: workers: lib.pipe workers [
|
||||
(lib.concatMap (worker: worker.settings.worker_listeners))
|
||||
lib.flatten
|
||||
mapListenersToUpstreamsByType
|
||||
]))
|
||||
];
|
||||
|
||||
mapListenersToUpstreamsByType = listenerInstances:
|
||||
lib.pipe listenerInstances [
|
||||
# Index by resource names
|
||||
(lib.concatMap (listener: lib.pipe listener [
|
||||
(listener: let
|
||||
allResourceNames = lib.pipe listener.resources [
|
||||
(map (resource: resource.names))
|
||||
lib.flatten
|
||||
lib.unique
|
||||
];
|
||||
in if allResourceNames == [ ]
|
||||
then { "empty" = listener; }
|
||||
else lib.genAttrs allResourceNames (_: listener))
|
||||
lib.attrsToList
|
||||
]))
|
||||
|
||||
(lib.foldl (acc: listener: acc // {
|
||||
${listener.name} = (acc.${listener.name} or [ ]) ++ [ listener.value ];
|
||||
}) { })
|
||||
|
||||
# Index by listener type
|
||||
(lib.mapAttrs (_:
|
||||
(lib.foldl (acc: listener: acc // {
|
||||
${listener.type} = (acc.${listener.type} or [ ]) ++ [ listener ];
|
||||
}) { })
|
||||
))
|
||||
|
||||
# Convert listeners to upstream URIs
|
||||
(lib.mapAttrs (_:
|
||||
(lib.mapAttrs (_: listeners:
|
||||
lib.pipe listeners [
|
||||
(lib.concatMap (listener:
|
||||
if listener.path != null
|
||||
then [ "unix:${listener.path}" ]
|
||||
else (map (addr: "${addr}:${toString listener.port}") listener.bind_addresses)
|
||||
))
|
||||
# NOTE: Adding ` = { }` to every upstream might seem unnecessary in isolation,
|
||||
# but it makes it easier to set upstreams in the nginx module.
|
||||
(uris: lib.genAttrs uris (_: { }))
|
||||
]
|
||||
))
|
||||
))
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user