version: '3'

services:

  nginx-main:
    image: nginx:latest
    container_name: nginx-main
    restart: always
    volumes:
      - ./nginx-main.conf:/etc/nginx/nginx.conf:ro
      # nise.moe certificates (by Cloudflare)
      - ./nise-data/certificate.pem:/etc/ssl/certs/nisemoe/certificate.pem:ro
      - ./nise-data/private.key:/etc/ssl/certs/nisemoe/private.key:ro
    ports:
      - "443:443"
      - "80:80"

  # Shared services which are used by others
  postgres:
    image: groonga/pgroonga:3.1.6-alpine-15
    container_name: postgres
    restart: always
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
    volumes:
      - postgres-data:/var/lib/postgresql/data
    command: >
      -c shared_buffers=6GB
      -c effective_cache_size=12GB
      -c work_mem=64MB
      -c maintenance_work_mem=2GB
      -c checkpoint_completion_target=0.9
      -c checkpoint_timeout=15min
      -c max_wal_size=2GB
      -c wal_buffers=16MB
      -c max_connections=100
      -c max_worker_processes=8
      -c max_parallel_workers_per_gather=4
      -c max_parallel_workers=8
      -c effective_io_concurrency=40
    shm_size: '128mb'

  redis:
    image: redis:alpine
    container_name: redis
    restart: always

  # ------------------------------------------------------------------

  gitea:
    image: gitea/gitea
    container_name: gitea
    restart: always
    environment:
      USER_UID: 1336
      USER_GID: 1336
      GITEA__database__DB_TYPE: postgres
      GITEA__database__HOST: ${DB_HOST}:5432
      GITEA__database__NAME: gitea
      GITEA__database__USER: ${DB_USER}
      GITEA__database__PASSWD: ${DB_PASS}
    depends_on:
      - postgres
      - redis
    volumes:
      - ./gitea-data/app.ini:/data/gitea/conf/app.ini
      - gitea-data:/data

 # ------------------------------------------------------------------

  nise-nginx:
    image: nginx:latest
    container_name: nise-nginx
    restart: always
    volumes:
      - ./nise-data/nginx.conf:/etc/nginx/nginx.conf:ro

  nise-backend:
    image: git.nise.moe/nuff/nise-backend:latest
    container_name: nise-backend
    environment:
      SPRING_PROFILES_ACTIVE: postgres,discord,import:scores,import:users,fix:scores
      # App configuration
      OLD_SCORES_PAGE_SIZE: 1000
      # Postgres
      POSTGRES_HOST: ${DB_HOST}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASS: ${DB_PASS}
      POSTGRES_DB: nise
      # redis
      REDIS_DB: 4
      # Discord
      WEBHOOK_URL: ${WEBHOOK_URL}
      SCORES_WEBHOOK_URL: ${SCORES_WEBHOOK_URL}
      # osu!api
      OSU_API_KEY: ${OSU_API_KEY}
      OSU_CLIENT_ID: ${OSU_CLIENT_ID}
      OSU_CLIENT_SECRET: ${OSU_CLIENT_SECRET}
      OSU_CALLBACK: "https://nise.moe/api/login/oauth2/code/osu"
      # Metabase
      METABASE_API_KEY: ${METABASE_API_KEY}
      # Internal API
      CIRCLEGUARD_API_URL: http://nise-circleguard:5000
      # Auth
      ORIGIN: "https://nise.moe"
      REPLAY_ORIGIN: "https://replay.nise.moe"
      COOKIE_SECURE: false
      BEATMAPS_PATH: "/app/dbs"
    restart: always
    volumes:
        - ./nise-data/beatmaps:/app/dbs
    depends_on:
      - postgres
      - redis

  nise-circleguard:
    image: git.nise.moe/nuff/nise-circleguard:latest
    container_name: nise-circleguard
    environment:
      OSU_API_KEY: ${OSU_API_KEY}
    restart: always
    volumes:
      - ./nise-data/beatmaps:/app/dbs

  nise-frontend2:
    image: git.nise.moe/nuff/nise-frontend:latest
    container_name: nise-frontend2
    restart: always

  nise-replay-viewer:
    image: git.nise.moe/nuff/nise-replay-viewer:latest
    container_name: nise-replay-viewer
    restart: always

  nise-discord:
    image: git.nise.moe/nuff/nise-discord:latest
    container_name: nise-discord
    environment:
      DISCORD_TOKEN: ${DISCORD_TOKEN}
      REACTION_CHANNEL_ID: ${REACTION_CHANNEL_ID}
      REACTION_EMOJI_ID: ${REACTION_EMOJI_ID}
    restart: always

  nise-metabase:
    image: metabase/metabase:latest
    container_name: nise-metabase
    volumes:
    - /dev/urandom:/dev/random:ro
    environment:
      MB_DB_TYPE: postgres
      MB_DB_DBNAME: metabase
      MB_DB_PORT: 5432
      MB_DB_USER: ${DB_METABASE_USER}
      MB_DB_PASS: ${DB_METABASE_PASS}
      MB_DB_HOST: postgres
    healthcheck:
      test: curl --fail -I http://localhost:3000/api/health || exit 1
      interval: 15s
      timeout: 5s
      retries: 5

volumes:
  postgres-data:
  gitea-data:
