# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

# This docker-compose file contains optional Postgres and Postgres-writer services
# for Magistrala platform. Since these are optional, this file is dependent of docker-compose file
# from <project_root>/docker. In order to run these services, execute command:
# docker compose -f docker/docker-compose.yaml -f docker/addons/postgres-writer/docker-compose.yaml up
# from project root. PostgreSQL default port (5432) is exposed, so you can use various tools for database
# inspection and data visualization.

networks:
  magistrala-base-net:
    external: true

volumes:
  magistrala-postgres-writer-volume:

services:
  postgres:
    image: postgres:16.2-alpine
    container_name: magistrala-postgres
    restart: on-failure
    environment:
      POSTGRES_USER: ${MG_POSTGRES_USER}
      POSTGRES_PASSWORD: ${MG_POSTGRES_PASS}
      POSTGRES_DB: ${MG_POSTGRES_NAME}
    ports:
      - 5434:5432
    networks:
      - magistrala-base-net
    volumes:
      - magistrala-postgres-writer-volume:/var/lib/postgresql/data

  postgres-writer:
    image: ghcr.io/absmach/magistrala/postgres-writer:${MG_RELEASE_TAG}
    container_name: magistrala-postgres-writer
    depends_on:
      - postgres
    restart: on-failure
    environment:
      MG_POSTGRES_WRITER_LOG_LEVEL: ${MG_POSTGRES_WRITER_LOG_LEVEL}
      MG_POSTGRES_WRITER_CONFIG_PATH: ${MG_POSTGRES_WRITER_CONFIG_PATH}
      MG_POSTGRES_WRITER_HTTP_HOST: ${MG_POSTGRES_WRITER_HTTP_HOST}
      MG_POSTGRES_WRITER_HTTP_PORT: ${MG_POSTGRES_WRITER_HTTP_PORT}
      MG_POSTGRES_WRITER_HTTP_SERVER_CERT: ${MG_POSTGRES_WRITER_HTTP_SERVER_CERT}
      MG_POSTGRES_WRITER_HTTP_SERVER_KEY: ${MG_POSTGRES_WRITER_HTTP_SERVER_KEY}
      MG_POSTGRES_HOST: ${MG_POSTGRES_HOST}
      MG_POSTGRES_PORT: ${MG_POSTGRES_PORT}
      MG_POSTGRES_USER: ${MG_POSTGRES_USER}
      MG_POSTGRES_PASS: ${MG_POSTGRES_PASS}
      MG_POSTGRES_NAME: ${MG_POSTGRES_NAME}
      MG_POSTGRES_SSL_MODE: ${MG_POSTGRES_SSL_MODE}
      MG_POSTGRES_SSL_CERT: ${MG_POSTGRES_SSL_CERT}
      MG_POSTGRES_SSL_KEY: ${MG_POSTGRES_SSL_KEY}
      MG_POSTGRES_SSL_ROOT_CERT: ${MG_POSTGRES_SSL_ROOT_CERT}
      MG_MESSAGE_BROKER_URL: ${MG_MESSAGE_BROKER_URL}
      MG_JAEGER_URL: ${MG_JAEGER_URL}
      MG_JAEGER_TRACE_RATIO: ${MG_JAEGER_TRACE_RATIO}
      MG_SEND_TELEMETRY: ${MG_SEND_TELEMETRY}
      MG_POSTGRES_WRITER_INSTANCE_ID: ${MG_POSTGRES_WRITER_INSTANCE_ID}
    ports:
      - ${MG_POSTGRES_WRITER_HTTP_PORT}:${MG_POSTGRES_WRITER_HTTP_PORT}
    networks:
      - magistrala-base-net
    volumes:
      - ./config.toml:/config.toml
