2022-09-14 08:47:49 +02:00
|
|
|
{ stdenv
|
|
|
|
, pkgs
|
2022-10-09 08:52:41 +02:00
|
|
|
, lib
|
2022-09-14 08:47:49 +02:00
|
|
|
}:
|
|
|
|
{ serviceName
|
2022-10-17 08:43:20 +02:00
|
|
|
, servers ? []
|
2022-10-17 09:02:54 +02:00
|
|
|
, httpcheck ? null
|
|
|
|
, balance ? null
|
2022-09-29 08:34:03 +02:00
|
|
|
, phpFastcgi ? false
|
|
|
|
, phpDocroot ? null
|
|
|
|
, phpIndex ? "index.php"
|
2022-10-09 08:52:41 +02:00
|
|
|
, extraUseBackendConditions ? {}
|
|
|
|
, extraFrontendOptions ? []
|
|
|
|
, extraBackendOptions ? []
|
2022-09-14 08:47:49 +02:00
|
|
|
}:
|
|
|
|
|
2022-10-17 08:43:20 +02:00
|
|
|
with lib;
|
|
|
|
with lib.lists;
|
|
|
|
with lib.attrsets;
|
2022-09-29 08:34:03 +02:00
|
|
|
let
|
2022-10-17 08:43:20 +02:00
|
|
|
indent = map (x: " " + x);
|
|
|
|
|
|
|
|
mkServer = i: s:
|
|
|
|
let
|
|
|
|
proto = optional phpFastcgi "proto fcgi";
|
|
|
|
in
|
2022-10-21 06:56:35 +02:00
|
|
|
concatStringsSep " " (
|
|
|
|
[
|
|
|
|
"server ${serviceName}${toString i} ${s.address}"
|
|
|
|
]
|
|
|
|
++ proto
|
|
|
|
++ (optional (hasAttr "check" s && s.check != null) (
|
|
|
|
concatStrings (["check"] ++ (map (k: if !hasAttr k s.check then "" else " ${k} ${getAttr k s.check}") ["inter" "downinter" "fall" "rise"]))
|
|
|
|
))
|
|
|
|
);
|
2022-10-09 08:52:41 +02:00
|
|
|
|
2022-10-17 08:43:20 +02:00
|
|
|
serverslines = imap1 mkServer servers;
|
2022-10-09 08:52:41 +02:00
|
|
|
|
2022-09-29 08:34:03 +02:00
|
|
|
backend =
|
2022-10-17 08:43:20 +02:00
|
|
|
(
|
|
|
|
concatStringsSep "\n" (
|
|
|
|
[
|
|
|
|
"backend ${serviceName}"
|
|
|
|
]
|
2022-10-17 09:02:54 +02:00
|
|
|
++ indent (
|
|
|
|
[
|
|
|
|
"mode http"
|
|
|
|
"option forwardfor"
|
|
|
|
]
|
|
|
|
++ extraBackendOptions
|
|
|
|
++ optional (balance != null) "balance ${balance}"
|
|
|
|
++ optional (httpcheck != null) "option httpchk ${httpcheck}"
|
|
|
|
++ optional phpFastcgi "use-fcgi-app ${serviceName}-php-fpm"
|
|
|
|
++ serverslines
|
|
|
|
)
|
2022-10-17 08:43:20 +02:00
|
|
|
++ [""]) # final newline
|
|
|
|
) +
|
|
|
|
(if !phpFastcgi then "" else ''
|
2022-09-29 08:34:03 +02:00
|
|
|
|
|
|
|
fcgi-app ${serviceName}-php-fpm
|
|
|
|
log-stderr global
|
|
|
|
docroot ${phpDocroot}
|
|
|
|
index ${phpIndex}
|
|
|
|
path-info ^(/.+\.php)(/.*)?$
|
2022-10-17 08:43:20 +02:00
|
|
|
'');
|
2022-10-09 08:52:41 +02:00
|
|
|
|
2022-10-17 08:43:20 +02:00
|
|
|
extraAclsCondition = concatStrings (mapAttrsToList (k: v: "\nacl acl_${serviceName}_${k} ${v}") extraUseBackendConditions);
|
2022-10-09 08:52:41 +02:00
|
|
|
|
2022-10-17 08:43:20 +02:00
|
|
|
extraAclsOr = concatStrings (mapAttrsToList (k: v: " OR acl_${serviceName}_${k}") extraUseBackendConditions);
|
2022-09-29 08:34:03 +02:00
|
|
|
in
|
2022-09-14 08:47:49 +02:00
|
|
|
{
|
2022-10-09 08:52:41 +02:00
|
|
|
frontend = ''
|
|
|
|
acl acl_${serviceName} hdr_beg(host) ${serviceName}.${extraAclsCondition}
|
|
|
|
''
|
2022-10-17 08:43:20 +02:00
|
|
|
+ concatMapStrings (x: x + "\n") extraFrontendOptions
|
2022-10-09 08:52:41 +02:00
|
|
|
+ ''
|
|
|
|
use_backend ${serviceName} if acl_${serviceName}${extraAclsOr}
|
2022-09-14 08:47:49 +02:00
|
|
|
'';
|
|
|
|
|
2022-09-29 08:34:03 +02:00
|
|
|
inherit backend;
|
2022-09-14 08:47:49 +02:00
|
|
|
}
|