version: "3.8"

services:
  jws-traefik-v1:
    container_name: jws-traefik-v1
    image: traefik:v2.10
    restart: always
    ports:
      - 4020:80
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik.yaml:/traefik.yaml
      - ./routes.yaml:/routes.yaml:rw
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.tf.rule=Host(`jws-treafik-v1.frappet.synology.me`)"
      - "traefik.http.routers.tf.middlewares=user-auth@file"
      - "traefik.http.routers.tf.service=api@internal"

    environment:
      - TZ=Asia/Bangkok
    networks:
      - jws

  jws-keycloak-v1:
    container_name: jws-keycloak-v1
    image: quay.io/keycloak/keycloak:latest
    restart: unless-stopped
    mem_limit: "1g"
    command:
      - start-dev
      - --proxy=edge
    environment:
      - KEYCLOAK_ADMIN=admin
      - KEYCLOAK_ADMIN_PASSWORD=1234
      - TZ=Asia/Bangkok
    ports:
      - "8080:8080"
    volumes:
      - keycloak-data:/opt/keycloak/data
      - ./keycloak-themes:/opt/keycloak/themes:rw
    networks:
      - jws

  jws-mariadb-v1:
    container_name: jws-mariadb-v1
    image: mariadb:lts
    restart: unless-stopped
    ports:
      - 3306:3306
    volumes:
      - mariadb-data:/var/lib/mysql
    environment:
      - TZ=Asia/Bangkok
      - MARIADB_ROOT_PASSWORD=1234
    networks:
      - jws

  jws-postgres-v1:
    container_name: jws-postgres-v1
    image: postgres:alpine
    restart: unless-stopped
    ports:
      - 5433:5432
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=1234
    networks:
      - jws

  jws-elasticsearch-v1:
    build:
      context: .
      dockerfile_inline: |
        FROM elasticsearch:8.12.0
        RUN /usr/share/elasticsearch/bin/elasticsearch-plugin install --batch analysis-icu
    image: elasticsearch-local:8.12.0
    deploy:
      resources:
        limits:
          memory: 1G
    restart: unless-stopped
    container_name: jws-elasticsearch-v1
    ports:
      - 9201:9200
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
    environment:
      - TZ=Asia/Bangkok
      - xpack.security.enabled=false
      - discovery.type=single-node
    networks:
      - jws

  jws-kibana-v1:
    container_name: jws-kibana-v1
    image: kibana:8.12.0
    deploy:
      resources:
        limits:
          memory: 1G
    restart: unless-stopped
    depends_on:
      - jws-elasticsearch-v1
    ports:
      - 5602:5601
    volumes:
      - kibana-data:/usr/share/kibana/data
    environment:
      - TZ=Asia/Bangkok
      - ELASTICSEARCH_HOSTS=http://jws-elasticsearch-v1:9200
    networks:
      - jws

  jws-rabbitmq-v1:
    container_name: jws-rabbitmq-v1
    image: rabbitmq:3-management-alpine
    restart: unless-stopped
    ports:
      - 5673:5672
      - 15673:15672
    volumes:
      - rabbitmq-data:/var/lib/rabbitmq
    environment:
      - TZ=Asia/Bangkok
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=1234
      - RABBITMQ_DEFAULT_VHOST=vhost
    networks:
      - jws

  jws-minio-v1:
    container_name: jws-minio-v1
    image: minio/minio:latest
    deploy:
      resources:
        limits:
          memory: 1G
    restart: unless-stopped
    command: server --console-address ":9001" /data
    ports:
      - 9002:9000
      - 9003:9001
    volumes:
      - minio-data:/data
    environment:
      - TZ=Asia/Bangkok
      - MINIO_ROOT_USER=admin
      - MINIO_ROOT_PASSWORD=12345678
    networks:
      - jws

  jws-n8n-v1:
    container_name: jws-n8n-v1
    image: docker.n8n.io/n8nio/n8n
    restart: unless-stopped
    ports:
      - 5678:5678
    # extra_hosts:
    #   - host.docker.internal:host-gateway
    volumes:
      - n8n-data:/home/node/.n8n
    environment:
      - GENERIC_TIMEZONE=Asia/Bangkok
    networks:
      - jws

  # Backend
  jws-backend-v1:
    container_name: jws-backend-v1
    image: docker.frappet.com/jws/jws-backend:0.0.1
    restart: unless-stopped
    depends_on:
      - jws-postgres-v1
    ports:
      - 20006:3000
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.jws-backend-v1.rule=Host(`jws-v1.frappet.synology.me`) && PathPrefix(`/api`)"
      - "traefik.http.routers.jws-backend-v1.entrypoints=unsecure"
    environment:
      - TZ=Asia/Bangkok
      - KC_URL=https://jws-keycloak-v1.frappet.synology.me
      - KC_REALM=dev
      - KC_SERVICE_ACCOUNT_CLIENT_ID=dev-service
      - KC_SERVICE_ACCOUNT_SECRET=cWYgHrp9za3757D02iOylTQBs18tipOh
      - APP_HOST=0.0.0.0
      - APP_PORT=3000
      - MINIO_HOST=jws-minio-v1
      - MINIO_PORT=9000
      - MINIO_ACCESS_KEY=9Z8b1fXQxQFJLfTXWzVg
      - MINIO_SECRET_KEY=qX1DypyyPRCUpfkBdNNDlhla3kjlj5ywJANbpCHI
      - MINIO_BUCKET=jws-dev
      - ELASTICSEARCH_PROTOCOL=http
      - ELASTICSEARCH_HOST=jws-elasticsearch-v1
      - ELASTICSEARCH_PORT=9200
      - ELASTICSEARCH_INDEX=jws-log-index
      - DATABASE_URL=postgresql://postgres:1234@jws-postgres-v1:5432/dev_1?schema=public
    # extra_hosts:
    #   - host.docker.internal:host-gateway
    networks:
      - jws

  # Frontend
  jws-frontend-v1:
    container_name: jws-frontend-v1
    image: docker.frappet.com/jws/jws-frontend:0.0.1
    restart: unless-stopped
    depends_on:
      - jws-backend-v1
    ports:
      - 20007:8080
    labels:
      - traefik.enable=true
      - traefik.http.routers.jws-frontend-v1.rule=Host(`jws-v1.frappet.synology.me`)
      - "traefik.http.routers.jws-frontend-v1.entrypoints=unsecure"
    environment:
      TZ: Asia/Bangkok
      API_BASE_URL: https://jws-v1.frappet.synology.me/api
      KC_URL: https://jws-keycloak-v1.frappet.synology.me
      KC_REALM: dev
      KC_CLIENT_ID: dev-service
    networks:
      - jws

volumes:
  keycloak-data:
  mariadb-data:
  postgres-data:
  elasticsearch-data:
  kibana-data:
  rabbitmq-data:
  minio-data:
  n8n-data:

networks:
  jws:
    external: true
