1
0
Fork 0
selfhostblocks/haproxy/siteconfig.nix

89 lines
2.2 KiB
Nix
Raw Normal View History

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
, servers ? []
, 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 ? []
, debugHeaders ? false
2022-09-14 08:47:49 +02:00
}:
with lib;
with lib.lists;
with lib.attrsets;
2022-09-29 08:34:03 +02:00
let
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
serverslines = imap1 mkServer servers;
2022-10-09 08:52:41 +02:00
2022-09-29 08:34:03 +02:00
backend =
(
concatStringsSep "\n" (
[
"backend ${serviceName}"
]
++ 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
)
++ [""]) # 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-09 08:52:41 +02:00
extraAclsCondition = concatStrings (mapAttrsToList (k: v: "\nacl acl_${serviceName}_${k} ${v}") extraUseBackendConditions);
2022-10-09 08:52:41 +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}
''
+ concatMapStrings (x: x + "\n") extraFrontendOptions
+ concatMapStrings (x: x + "\n") (optionals debugHeaders [
"option httplog"
"http-request capture req.hdrs len 512 if acl_${serviceName}${extraAclsOr}"
''log-format "%ci:%cp [%tr] %ft [[%hr]] %hs %{+Q}r"''
])
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
}