1
0
Fork 0
selfhostblocks/haproxy/siteconfig.nix

62 lines
1.6 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
2022-10-09 08:52:41 +02:00
, serviceAddress ? null
, serviceSocket ? 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-09 08:52:41 +02:00
assert lib.assertMsg (
(serviceAddress == null && serviceSocket != null)
|| (serviceAddress != null && serviceSocket == null)
) "set either serviceAddress or serviceSocket";
2022-09-29 08:34:03 +02:00
let
2022-10-09 08:52:41 +02:00
backendOptions = lib.concatMapStrings (x : "\n " + x) extraBackendOptions;
serviceBind = if serviceAddress != null then serviceAddress else serviceSocket;
2022-09-29 08:34:03 +02:00
backend =
if !phpFastcgi
then ''
backend ${serviceName}
mode http
2022-10-09 08:52:41 +02:00
option forwardfor${backendOptions}
server ${serviceName}1 ${serviceBind}
2022-09-29 08:34:03 +02:00
'' else ''
backend ${serviceName}
mode http
2022-10-09 08:52:41 +02:00
option forwardfor${backendOptions}
2022-09-29 08:34:03 +02:00
use-fcgi-app ${serviceName}-php-fpm
2022-10-09 08:52:41 +02:00
server ${serviceName}1 ${serviceBind} proto fcgi
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 = lib.concatStrings (lib.attrsets.mapAttrsToList (k: v: "\nacl acl_${serviceName}_${k} ${v}") extraUseBackendConditions);
extraAclsOr = lib.concatStrings (lib.attrsets.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}
''
+ lib.concatMapStrings (x: x + "\n") extraFrontendOptions
+ ''
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
}