caddy: fix indentation for static site

Fix by moving the inline HTML to a separate file and serve it using
`file_server` directive.
This commit is contained in:
Tigor Hutasuhut 2024-10-13 16:01:52 +07:00
parent da88afbb51
commit ed60032f75

View file

@ -1,4 +1,4 @@
{ config, lib, ... }:
{ config, lib, unstable, ... }:
let
cfg = config.profile.services.caddy;
inherit (lib) mkIf attrsets strings lists;
@ -7,6 +7,7 @@ in
config = mkIf cfg.enable {
services.caddy = {
enable = true;
package = unstable.caddy;
};
services.caddy.globalConfig = /*caddy*/ ''
@ -15,36 +16,44 @@ in
}
'';
environment.etc."caddy/static/tigor.web.id/index.html" = {
text =
let
domains = attrsets.mapAttrsToList (name: _: strings.removePrefix "https://" name) config.services.caddy.virtualHosts;
sortedDomains = lists.sort (a: b: a < b) domains;
list = map
(domain: /*html*/ ''
<div class="col-12 col-sm-6 col-md-4 col-lg-3 text-center align-middle">
<a href="https://${domain}">${domain}</a>
</div>
'')
sortedDomains;
items = strings.concatStringsSep "\n" list;
in
/*html*/ ''
<!DOCTYPE html>
<html>
<head>
<title>Hosted Sites</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body class="container">
<h1 class="text-center">Hosted Sites</h1>
<div class="row g-4">
${items}
</div>
</body>
</html>
'';
user = "caddy";
group = "caddy";
};
services.caddy.virtualHosts =
let
domains = attrsets.mapAttrsToList (name: _: strings.removePrefix "https://" name) config.services.caddy.virtualHosts;
sortedDomains = lists.sort (a: b: a < b) domains;
list = map
(domain: /*html*/ ''
<div class="col-12 col-sm-6 col-md-4 col-lg-3 text-center align-middle">
<a href="https://${domain}">${domain}</a>
</div>'')
sortedDomains;
items = strings.concatStringsSep "\n" list;
html = /*html*/
''<!DOCTYPE html>
<html>
<head>
<title>Hosted Sites</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body class="container">
<h1 class="text-center">Hosted Sites</h1>
<div class="row g-4">
${items}
</div>
</body>
</html>'';
in
{
"router.tigor.web.id".extraConfig = ''
@denied not remote_ip private_ranges
@ -55,17 +64,13 @@ in
'';
"tigor.web.id".extraConfig =
''
header Content-Type text/html
respond <<EOF
${html}
EOF 200
root * /etc/caddy/static/tigor.web.id
file_server
'';
"crowfx.web.id".extraConfig =
''
header Content-Type text/html
respond <<EOF
${html}
EOF 200
root * /etc/caddy/static/tigor.web.id
file_server
'';
};