### CRIMSON --- A simple PHP framework.
### Copyright © 2024 Freya Murphy <contact@freyacat.org>
###
### This file is part of CRIMSON.
###
### CRIMSON is free software; you can redistribute it and/or modify it
### under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 3 of the License, or (at
### your option) any later version.
###
### CRIMSON is distributed in the hope that it will be useful, but
### WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with CRIMSON. If not, see <http://www.gnu.org/licenses/>.

services:
  # `docker-compose-api.yml`
  # This compose file creates the `rest` container running postgrest. This is
  # the program that crimson uses for the default API functionality.
  #
  # This service stack is only enabled when API_ENABLED=true.
  #
  # WARNING: The postgres container must also be enabled `POSTGRES_ENABLED=true`
  # otherwise the docker stack will be invalid.

  rest:
    # Postgrest uses postgres to store the api schema. To work properly, the
    # database must be working (healthy) and all the api schema must be loaded.
    # It is db-init's job to load the api schema, thus this container depends
    # on both postgres and db-init.
    build: ${CRIMSON_ROOT}/build/postgrest
    restart: unless-stopped
    env_file: DOCKER_ENV_FILES
    healthcheck:
      test: curl -I "http://localhost:3000/"
      interval: 10s
      timeout: 3s
      retries: 10
      start_period: 3s
    depends_on:
      postgres:
        condition: service_healthy
      db-init:
        condition: service_completed_successfully

  web:
    # Nginx proxies requests to the API making it an added dependency.
    depends_on:
      rest:
        condition: service_healthy