Skip to content

Commit

Permalink
Added support for files mapping to reverse-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
rgaudin committed Aug 15, 2023
1 parent 709fb19 commit 4250903
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
13 changes: 7 additions & 6 deletions reverse-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ Caddy-based reverse-proxy to serve all our services

Configuration is done solely via environment variables

| Variable | Default | Usage |
| ------------ | ------------------ | ----------------------------------------------------------------- |
| `FQDN` | `generic.hotspot` | Hostname to serve at |
| `SERVICES` | | `,`-separated list of services to configure. Ex `kiwix,edupi` |
| `DEBUG` | | Set any value to enable Caddy debug output |
| Variable | Default | Usage |
| --------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `FQDN` | `generic.hotspot` | Hostname to serve at |
| `SERVICES` | | `,`-separated list of services to configure. Ex `kiwix,edupi` |
| `FILES_MAPPING` | | `,`-separated list of `{subdomain}:{subfolder}` mapping for files-related services (using a single files service). Ex `nomad:nomadeducation,download-zims:zims` |
| `DEBUG` | | Set any value to enable Caddy debug output |

Although served as a docker-service, `home` is not to be listed as it has no user-facing endpoint.

All services are expected to be reachable from reverse-proxy container at `{service}:80`.
All services are expected to be reachable from reverse-proxy container at `{service}:80`.
31 changes: 28 additions & 3 deletions reverse-proxy/gen-caddyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
import os
import sys
import traceback
from typing import List

from jinja2 import Template

services: List[str] = [
services: list[str] = [
srv.strip() for srv in os.getenv("SERVICES", "").split(",") if srv
]
files_map: dict[str, str] = {
entry.split(":", 1)[0]: entry.split(":", 1)[1]
for entry in os.getenv("FILES_MAPPING", "").split(",")
if ":" in entry
}
debug: bool = bool(os.getenv("DEBUG", False))
template: Template = Template(
"""
Expand Down Expand Up @@ -70,6 +74,26 @@
{% endfor %}
{% endif %}
{% if files_map %}# endpoint-based files_map
{% for subdomain, folder in files_map.items() %}{{subdomain}}.{$FQDN}:80, {{subdomain}}.{$FQDN}:443 {
tls internal
reverse_proxy files:80
rewrite * /{{folder}}/{path}?{query}
handle_errors {
respond "HTTP {http.error.status_code} Error ({http.error.message})"
}
}
{% endfor %}
{% endif %}
# fallback for unhandled names/IP arriving here
:80, :443 {
tls internal
respond "Not Found! Oops" 404
}
"""
)

Expand All @@ -82,14 +106,15 @@ def gen_caddyfile():
debug=debug,
services=services,
nb_services=len(services),
files_map=files_map,
)
)
except Exception as exc:
print("[CRITICAL] unable to gen Caddyfile, using default")
traceback.print_exception(exc)
return

print(f"Generated Caddyfile for: {services}")
print(f"Generated Caddyfile for: {services=} and {files_map}")


if __name__ == "__main__":
Expand Down

0 comments on commit 4250903

Please sign in to comment.