healthcheck.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/sh
  2. # Use the value of the corresponding env var (if present),
  3. # or a default value otherwise.
  4. : "${DATA_FOLDER:="data"}"
  5. : "${ROCKET_PORT:="80"}"
  6. CONFIG_FILE="${DATA_FOLDER}"/config.json
  7. # Given a config key, return the corresponding config value from the
  8. # config file. If the key doesn't exist, return an empty string.
  9. get_config_val() {
  10. local key="$1"
  11. # Extract a line of the form:
  12. # "domain": "https://bw.example.com/path",
  13. grep "\"${key}\":" "${CONFIG_FILE}" |
  14. # To extract just the value (https://bw.example.com/path), delete:
  15. # (1) everything up to and including the first ':',
  16. # (2) whitespace and '"' from the front,
  17. # (3) ',' and '"' from the back.
  18. sed -e 's/[^:]\+://' -e 's/^[ "]\+//' -e 's/[,"]\+$//'
  19. }
  20. # Extract the base path from a domain URL. For example:
  21. # - `` -> ``
  22. # - `https://bw.example.com` -> ``
  23. # - `https://bw.example.com/` -> ``
  24. # - `https://bw.example.com/path` -> `/path`
  25. # - `https://bw.example.com/multi/path` -> `/multi/path`
  26. get_base_path() {
  27. echo "$1" |
  28. # Delete:
  29. # (1) everything up to and including '://',
  30. # (2) everything up to '/',
  31. # (3) trailing '/' from the back.
  32. sed -e 's|.*://||' -e 's|[^/]\+||' -e 's|/*$||'
  33. }
  34. # Read domain URL from config.json, if present.
  35. if [ -r "${CONFIG_FILE}" ]; then
  36. domain="$(get_config_val 'domain')"
  37. if [ -n "${domain}" ]; then
  38. # config.json 'domain' overrides the DOMAIN env var.
  39. DOMAIN="${domain}"
  40. fi
  41. fi
  42. addr="${ROCKET_ADDRESS}"
  43. if [ -z "${addr}" ] || [ "${addr}" = '0.0.0.0' ] || [ "${addr}" = '::' ]; then
  44. addr='localhost'
  45. fi
  46. base_path="$(get_base_path "${DOMAIN}")"
  47. if [ -n "${ROCKET_TLS}" ]; then
  48. s='s'
  49. fi
  50. curl --insecure --fail --silent --show-error \
  51. "http${s}://${addr}:${ROCKET_PORT}${base_path}/alive" || exit 1