version: "3.9"

services:
  webui:
    build:
      context: .
      args:
        SERVICE: webui
    env_file:
      ./services/webui/.env
    restart: unless-stopped

  request:
    build:
      context: .
      args:
        SERVICE: request
    depends_on:
      - "database"
      - "torrent"
      - "webui"
    env_file:
      - ./services/request/.env
    environment:
      DATABASE: "autoplex"
    links:
      - "database"
      - "torrent"
    ports:
      - 3200:3200
    restart: unless-stopped
    secrets:
      - app_key
      - discord_bot_key
      - mysql_root_password
      - plex_token
      - tmdb_key
      - tvdb_key
      - tvdb_pin
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  plex:
    build:
      context: .
      args:
        SERVICE: plex
    env_file:
      - ./services/plex/.env
    environment:
      DATABASE: "autoplex"
    restart: unless-stopped
    secrets:
      - mysql_root_password
      - plex_token
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  search:
    build:
      context: .
      args:
        SERVICE: search
    env_file:
      - ./services/search/.env
    restart: unless-stopped
    secrets:
      - tmdb_key
      - tvdb_key
      - tvdb_pin
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  seeker:
    build:
      context: .
      args:
        SERVICE: seeker
    depends_on:
      - "database"
      - "torrent"
    environment:
      DATABASE: "autoplex"
    links:
      - "database"
      - "torrent"
    restart: unless-stopped
    secrets:
      - mysql_root_password
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  torrent_rest:
    build:
      context: .
      args:
        SERVICE: torrent-rest
    depends_on:
      - "torrent"
    env_file:
      - ./services/torrent-rest/.env
    restart: unless-stopped
    ports:
      - 3300:3300
    secrets:
      - app_key
      - mysql_root_password
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  torrent_search:
    build:
      context: .
      args:
        SERVICE: torrent-search
    restart: unless-stopped
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  torrent:
    build:
      context: .
      args:
        BASE: slim
        SERVICE: torrent
    depends_on:
      - "database"
      - "vpn"
    environment:
      DATABASE: "autoplex_torrent"
    healthcheck:
      test: ["CMD", "ping", "-q", "-c1", "google.com"]
      interval: 5m
      timeout: 10s
      retries: 3
      start_period: 5m
    network_mode: service:vpn
    restart: unless-stopped
    secrets:
      - mysql_root_password
    user: ${USER_ID}:${GROUP_ID}
    volumes:
      - var:/var/autoplex

  database:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD_FILE: /run/secrets/mysql_root_password
    ports:
      - 33061:3306
    restart: unless-stopped
    security_opt:
      - seccomp=unconfined
    secrets:
      - mysql_root_password
    volumes:
      - ./bootstrap/database/init:/docker-entrypoint-initdb.d
      - mysql:/var/lib/mysql

  vpn:
    build:
      context: ./utils/vpn
    cap_add:
      - net_admin
    command:
      - '-f ""'
    environment:
      TZ: "CST6CDT"
    links:
      - "database"
    networks:
      - default
    read_only: true
    tmpfs:
      - /run
      - /tmp
    restart: unless-stopped
    secrets:
      - vpn_auth
    security_opt:
      - label:disable
    stdin_open: true
    tty: true
    volumes:
      - /dev/net:/dev/net:z
      - ./.volumes/vpn:/vpn

secrets:
  app_key:
    file: ./.secrets/app_key
  discord_bot_key:
    file: ./.secrets/discord_bot_key
  mysql_root_password:
    file: ./.secrets/mysql_root_password
  plex_token:
    file: ./.secrets/plex_token
  tmdb_key:
    file: ./.secrets/tmdb_key
  tvdb_key:
    file: ./.secrets/tvdb_key
  tvdb_pin:
    file: ./.secrets/tvdb_pin
  vpn_auth:
    file: ./.secrets/vpn_auth

volumes:
  mysql:
  var:
