Soft fork for a caddy reverse template for railway

Merge pull request #1 from railwayapp-templates/extra-config-via-env

Support waking a service via the private network

authored by Brody Over and committed by GitHub 19c363cf 45a3688a

Changed files
+85 -15
+57 -12
Caddyfile
··· 13 13 } 14 14 } 15 15 16 + (lb_settings) { 17 + lb_policy round_robin 18 + lb_retries 100 19 + lb_try_duration 10s 20 + lb_try_interval 250ms 21 + } 22 + 23 + (passive_health_checks) { 24 + fail_duration 60s 25 + max_fails 300 26 + unhealthy_latency 5s 27 + unhealthy_request_count 200 28 + } 29 + 16 30 # site block, listens on the $PORT environment variable, automatically assigned by railway 17 31 :{$PORT} { 18 32 # access logs ··· 20 34 format json # set access log format to json mode 21 35 } 22 36 23 - reverse_proxy {$FRONTEND_HOST} # proxy all requests for /* to the frontend, configure this variable in the service settings 37 + # proxy all requests for /* to the frontend, configure these variables in the service settings 38 + reverse_proxy { 39 + # for private networking replicas are exposed as multiple dns results, use those dns results as the upstreams 40 + dynamic a { 41 + name {$FRONTEND_DOMAIN} 42 + port {$FRONTEND_PORT} 43 + refresh 1s 44 + dial_timeout 30s 45 + versions ipv4 ipv6 46 + } 47 + 48 + # configure load balancing settings 49 + import lb_settings 50 + 51 + # configure passive health checks 52 + import passive_health_checks 53 + 54 + # sets the Host header to the header to the dynamic name and port options 55 + header_up Host {upstream_hostport} 56 + } 24 57 25 58 # the handle_path directive WILL strip /api/ from the path before proxying 59 + # use `handle` instead of `handle_path` if you dont want to strip the /api/ path 26 60 # this is needed if your backend's api routes don't start with /api/ 27 61 # change paths as needed 28 - handle_path /api/* { 62 + handle_path {$BACKEND_PATH:/api}/* { 29 63 # the /api/ prefix WILL be stripped from the uri sent to the proxy host 30 - reverse_proxy {$BACKEND_HOST} # proxy all requests for /api/* to the backend, configure this variable in the service settings 31 - } 64 + # 65 + # proxy all requests for /api/* to the backend, configure this variable in the service settings 66 + reverse_proxy { 67 + # for private networking replicas are exposed as multiple dns results, use those dns results as the upstreams 68 + dynamic a { 69 + name {$BACKEND_DOMAIN} 70 + port {$BACKEND_PORT} 71 + refresh 1s 72 + dial_timeout 30s 73 + versions ipv4 ipv6 74 + } 32 75 33 - # this handle directive will NOT strip /api/ from the path before proxying 34 - # if your backend's api routes do start with /api/ then you wouldn't want to strip the path prefix 35 - # if so, comment out the above handle_path and reverse_proxy directives, and uncomment these handle and reverse_proxy directives 36 - # change paths as needed 37 - # handle /api/* { 38 - # the /api/ prefix will NOT be stripped from the uri sent to the proxy host 39 - # reverse_proxy {$BACKEND_HOST} # proxy all requests for /api/* to the backend, configure this variable in the service settings 40 - # } 76 + # configure load balancing settings 77 + import lb_settings 78 + 79 + # configure passive health checks 80 + import passive_health_checks 81 + 82 + # sets the Host header to the header to the dynamic name and port options 83 + header_up Host {upstream_hostport} 84 + } 85 + } 41 86 }
+9 -3
Dockerfile
··· 1 1 FROM caddy:latest 2 2 3 - COPY Caddyfile /etc/caddy/Caddyfile 3 + WORKDIR /app 4 4 5 - RUN caddy fmt --overwrite /etc/caddy/Caddyfile 5 + COPY Caddyfile ./ 6 6 7 - CMD exec caddy run --config /etc/caddy/Caddyfile --adapter caddyfile 2>&1 7 + COPY --chmod=755 entrypoint.sh ./ 8 + 9 + RUN caddy fmt --overwrite Caddyfile 10 + 11 + ENTRYPOINT ["/bin/sh"] 12 + 13 + CMD ["entrypoint.sh"]
+19
entrypoint.sh
··· 1 + #!/bin/sh 2 + 3 + set -euo pipefail 4 + 5 + # for backwards compatibility, seperates host and port from url 6 + export FRONTEND_DOMAIN=${FRONTEND_DOMAIN:-${FRONTEND_HOST%:*}} 7 + export FRONTEND_PORT=${FRONTEND_PORT:-${FRONTEND_HOST##*:}} 8 + 9 + export BACKEND_DOMAIN=${BACKEND_DOMAIN:-${BACKEND_HOST%:*}} 10 + export BACKEND_PORT=${BACKEND_PORT:-${BACKEND_HOST##*:}} 11 + 12 + # strip https:// or https:// from domain if necessary 13 + FRONTEND_DOMAIN=${FRONTEND_DOMAIN##*://} 14 + BACKEND_DOMAIN=${BACKEND_DOMAIN##*://} 15 + 16 + echo using frontend: ${FRONTEND_DOMAIN} with port: ${FRONTEND_PORT} 17 + echo using backend: ${BACKEND_DOMAIN} with port: ${BACKEND_PORT} 18 + 19 + exec caddy run --config Caddyfile --adapter caddyfile 2>&1