1
0
Fork 0
selfhostblocks/haproxy/siteconfig.nix

70 lines
1.5 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 ? []
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
}:
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
concatStringsSep " " ([
"server ${serviceName}${toString i} ${s.address}"
] ++ proto ++ s.extra);
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"
]
++ indent extraBackendOptions
++ optional phpFastcgi " use-fcgi-app ${serviceName}-php-fpm"
++ indent 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
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
}