# docker-compose.yml + Caddyfile generation by gluing service fragments. # # Fragments live in templates/compose/. A few carry @TOKENS@ that depend on the # user's choices (published ports, depends_on for embedded backing services); # those are substituted here. External backing services contribute no fragment. _frag() { cat "${BOOTSTRAP_DIR}/templates/compose/$1"; } generate_compose() { local nl=$'\n' local api_ports="" api_depends="" worker_depends="" webui_ports="" caddy_ports="" local dep="" # depends_on: only wait on backing services we actually run, and only when # they are healthy (never start the backend over an unready dependency). [[ "$CFG_DB_MODE" == "embedded" ]] && dep+=" db:${nl} condition: service_healthy${nl}" [[ "$CFG_REDIS_MODE" == "embedded" ]] && dep+=" redis:${nl} condition: service_healthy${nl}" if [[ -n "$dep" ]]; then api_depends=" depends_on:${nl}${dep%"$nl"}" worker_depends="$api_depends" fi # API port: published whenever there is no bundled proxy (backend-only, or # webui with the operator's own proxy). Behind Caddy the API stays in-network. if [[ "$CFG_PROXY" == "no" ]]; then api_ports=" ports:${nl} - \"\${API_PORT}:8000\"" fi # Webui port: published only when the webui runs without the bundled proxy. if [[ "$CFG_WEBUI" == "yes" && "$CFG_PROXY" == "no" ]]; then webui_ports=" ports:${nl} - \"\${WEBUI_PORT}:80\"" fi # Caddy publish ports: 80/443 with a domain (auto-HTTPS), else plain HTTP. if [[ "$CFG_HTTPS" == "yes" ]]; then caddy_ports=" - \"80:80\"${nl} - \"443:443\"" else caddy_ports=" - \"\${HTTP_PORT}:80\"" fi # -- assemble --------------------------------------------------------- local backend webui caddy backend="$(_frag backend.yml)" backend="${backend//@API_PORTS@/$api_ports}" backend="${backend//@API_DEPENDS@/$api_depends}" backend="${backend//@WORKER_DEPENDS@/$worker_depends}" { _frag _base.yml printf '%s\n' "$backend" if [[ "$CFG_WEBUI" == "yes" ]]; then webui="$(_frag webui.yml)" webui="${webui//@WEBUI_PORTS@/$webui_ports}" printf '%s\n' "$webui" fi [[ "$CFG_DB_MODE" == "embedded" ]] && _frag postgres.yml [[ "$CFG_REDIS_MODE" == "embedded" ]] && _frag redis.yml [[ "$CFG_STORAGE" == "s3" && "$CFG_S3_MODE" == "embedded" ]] && _frag minio.yml if [[ "$CFG_PROXY" == "yes" ]]; then caddy="$(_frag caddy.yml)" caddy="${caddy//@CADDY_PORTS@/$caddy_ports}" printf '%s\n' "$caddy" fi # -- named volumes (only those actually referenced) --------------- echo "" echo "volumes:" echo " transcode_cache:" [[ "$CFG_DB_MODE" == "embedded" ]] && echo " pgdata:" [[ "$CFG_REDIS_MODE" == "embedded" ]] && echo " redisdata:" [[ "$CFG_STORAGE" == "s3" && "$CFG_S3_MODE" == "embedded" ]] && echo " miniodata:" if [[ "$CFG_PROXY" == "yes" ]]; then echo " caddy_data:" echo " caddy_config:" fi } >"$COMPOSE_FILE" [[ "$CFG_PROXY" == "yes" ]] && generate_caddyfile } # generate_caddyfile — concrete Caddyfile (no env indirection). Routes API # paths to the backend and everything else to the static webui. generate_caddyfile() { local site if [[ "$CFG_HTTPS" == "yes" ]]; then site="$CFG_DOMAIN"; else site=":80"; fi { if [[ "$CFG_HTTPS" == "yes" && -n "${CFG_ACME_EMAIL:-}" ]]; then echo "{" echo " email ${CFG_ACME_EMAIL}" echo "}" echo "" fi echo "${site} {" echo " @api path /api/* /health* /rest/*" echo " reverse_proxy @api api:8000" echo " reverse_proxy webui:80" echo "}" } >"$CADDYFILE" }