# docker-compose.db.yml

version: "3.8"

services:
  fems-postgres:
    image: postgres:16
    container_name: fems-postgres
    restart: unless-stopped
    env_file:
      - .env.${NODE_ENV:-development}
    ports:
      - "${POSTGRES_PORT}:5432"
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - LANG=en_US.utf8
      - LC_ALL=en_US.utf8
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_HOST_AUTH_METHOD=scram-sha-256
      - POSTGRES_INITDB_ARGS=--auth-host=scram-sha-256 --auth-local=scram-sha-256
      - TZ=Asia/Seoul
    volumes:
      - fems_postgres:/var/lib/postgresql/data
      - ./backups/postgres:/backups
      - ./fems-postgres/init-scripts:/docker-entrypoint-initdb.d
      - ./fems-postgres/postgresql.conf:/etc/postgresql/postgresql.conf:ro
      - ./fems-postgres/pg_hba.conf:/etc/postgresql/pg_hba.conf:ro
    command:
      - "postgres"
      - "-c"
      - "config_file=/etc/postgresql/postgresql.conf"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5

  fems-timescaledb:
    build:
      context: ./fems-timescaledb
      dockerfile: Dockerfile
    container_name: fems-timescaledb
    restart: unless-stopped
    env_file:
      - .env.${NODE_ENV:-development}
    ports:
      - "${TIMESCALEDB_PORT}:5433"
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - LANG=en_US.utf8
      - LANGUAGE=en_US.utf8
      - LC_ALL=en_US.utf8
      - TZ=Asia/Seoul
      - POSTGRES_HOST_AUTH_METHOD=scram-sha-256
      - POSTGRES_INITDB_ARGS=--auth-host=scram-sha-256 --auth-local=scram-sha-256
      - POSTGRES_DB=${TIMESCALEDB_DB}
      - POSTGRES_USER=${TIMESCALEDB_USER}
      - POSTGRES_PASSWORD=${TIMESCALEDB_PASSWORD}
      # - TIMESCALEDB_DB=${TIMESCALEDB_DB}
      # - TIMESCALEDB_USER=${TIMESCALEDB_USER}
      # - TIMESCALEDB_PASSWORD=${TIMESCALEDB_PASSWORD}
    volumes:
      - fems_timescaledb:/var/lib/postgresql/data
      - ./fems-timescaledb/postgresql.conf:/etc/postgresql/postgresql.conf:ro
      - ./fems-timescaledb/pg_hba.conf:/etc/postgresql/pg_hba.conf:ro
      - ./fems-timescaledb/init-scripts:/docker-entrypoint-initdb.d/:ro
    command: ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  fems-redis:
    image: redis:alpine
    container_name: fems-redis
    restart: unless-stopped
    env_file:
      - .env.${NODE_ENV:-development}
    # ports:
    #   - "${REDIS_PORT}:6379"
    command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}"]
    volumes:
      - fems_redis:/data
      - ./backups/redis:/backups
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - REDIS_PASSWORD=${REDIS_PASSWORD}
    depends_on:
      - fems-postgres
      - fems-timescaledb
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3

  fems-mqtt:
    build:
      context: ./fems-mqtt
      dockerfile: Dockerfile
      args:
        - MQTT_USERNAME=${MQTT_USERNAME}
        - MQTT_PASSWORD=${MQTT_PASSWORD}
        - NODE_RED_USERNAME=${NODE_RED_USERNAME}
        - NODE_RED_PASSWORD=${NODE_RED_PASSWORD}
    container_name: fems-mqtt
    restart: unless-stopped
    env_file:
      - .env.${NODE_ENV:-development}
    ports:
      - "${MQTT_PORT}:1883"
      - "${MQTT_WSS_PORT}:8883"
    environment:
      - MQTT_USERNAME=${MQTT_USERNAME}
      - MQTT_PASSWORD=${MQTT_PASSWORD}
      - NODE_RED_USERNAME=${NODE_RED_USERNAME}
      - NODE_RED_PASSWORD=${NODE_RED_PASSWORD}
    volumes:
      - ./fems-mqtt/data:/mosquitto/data
      - ./fems-mqtt/config:/mosquitto/config
      - ./fems-mqtt/log:/mosquitto/log
      - ./fems-mqtt/certs:/mosquitto/certs
    # entrypoint: ["/docker-entrypoint.sh"]
    # command: ["/usr/sbin/mosquitto", "-c", "/mosquitto/config/mosquitto.conf"]
    user: mosquitto
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "mosquitto_sub -h localhost -t '$$SYS/#' -C 1 -u ${MQTT_USERNAME} -P ${MQTT_PASSWORD} || exit 1",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
    depends_on:
      - fems-postgres
      - fems-timescaledb

volumes:
  fems_postgres:
  fems_redis:
  fems_timescaledb:

networks:
  default:
    driver: bridge
  internal:
    driver: bridge