services:
  traefik:
    image: traefik:latest
    container_name: traefik
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "${TRAEFIK_WEB_PORT}:80"
      - "${TRAEFIK_API_PORT}:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - proxy
      - interstack

  gitea:
    image: gitea/gitea:latest
    container_name: gitea
    environment:
      DISABLE_REGISTRATION: "true"
      ROOT_URL: "https://${GITEA_DOMAIN}"
    ports:
      - "${GITEA_SSH_PORT}:22"
    volumes:
      - ./data/gitea:/data
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.gitea.rule=Host(`${GITEA_DOMAIN}`) || Host(`${GITEA_TECHNICAL_DOMAIN}`)"
      - "traefik.http.routers.gitea.entrypoints=web"
      - "traefik.http.routers.gitea.middlewares=gitea-cors"
      - "traefik.http.services.gitea.loadbalancer.server.port=3000"
      - "traefik.docker.network=proxy"
      - "traefik.http.middlewares.gitea-cors.headers.accesscontrolalloworiginlist=https://${WEBAPP_DOMAIN}"
      - "traefik.http.middlewares.gitea-cors.headers.accesscontrolallowmethods=GET,POST,PUT,DELETE,OPTIONS"
      - "traefik.http.middlewares.gitea-cors.headers.accesscontrolallowheaders=Content-Type,Authorization,Accept,X-Requested-With"
      - "traefik.http.middlewares.gitea-cors.headers.accesscontrolmaxage=3600"
      - "traefik.http.middlewares.gitea-cors.headers.addvaryheader=true"
    networks:
      - proxy
      - interstack

  woodpecker-server:
    image: woodpeckerci/woodpecker-server:next-2a445a6663-alpine
    container_name: woodpecker-server
    environment:
      WOODPECKER_HOST: "https://${WOODPECKER_DOMAIN}"
      WOODPECKER_OPEN: "true"
      WOODPECKER_GITEA: "true"
      WOODPECKER_GITEA_URL: "https://${GITEA_DOMAIN}"
      WOODPECKER_GITEA_CLIENT: "6de1c095-dd30-497b-811e-278759f71eba"
      WOODPECKER_GITEA_SECRET: ${GITEA_CLIENT_SECRET}
      WOODPECKER_SERVER_ADDR: ":8000"
      WOODPECKER_AGENT_SECRET: ${WOODPECKER_AGENT_SECRET}
    volumes:
      - ./data/woodpecker:/var/lib/woodpecker
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.woodpecker.rule=Host(`${WOODPECKER_DOMAIN}`) || Host(`${WOODPECKER_TECHNICAL_DOMAIN}`)"
      - "traefik.http.routers.woodpecker.entrypoints=web"
      - "traefik.http.services.woodpecker.loadbalancer.server.port=8000"
      - "traefik.docker.network=proxy"
    networks:
      - proxy
      - interstack

  woodpecker-agent:
    image: woodpeckerci/woodpecker-agent:next-2a445a6663-alpine
    container_name: woodpecker-agent
    environment:
      WOODPECKER_SERVER: "woodpecker-server:9000"
      WOODPECKER_AGENT_SECRET: ${WOODPECKER_AGENT_SECRET}
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - softwares:/softwares
    networks:
      - proxy
      - interstack

  mysql:
    image: mysql:8
    container_name: mysql
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: softwares
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./data/db_backup:/db_backup
    networks:
      - proxy
      - interstack

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: phpmyadmin
    environment:
      PMA_HOST: mysql
    depends_on:
      mysql:
        condition: service_healthy
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.phpmyadmin.rule=Host(`${PHPMYADMIN_DOMAIN}`)"
      - "traefik.http.routers.phpmyadmin.entrypoints=web"
      - "traefik.http.services.phpmyadmin.loadbalancer.server.port=80"
      - "traefik.docker.network=proxy"
    volumes:
      - ./pma-config.user.inc.php:/etc/phpmyadmin/config.user.inc.php
    networks:
      - proxy
      - interstack

  frontend:
    build:
      context: ./apps/frontend
      dockerfile: Dockerfile.development
    container_name: frontend
    environment:
      - NODE_ENV=development
      - HOST=0.0.0.0
      - WEBAPP_GITEA_TOKEN=${WEBAPP_GITEA_TOKEN}
      - WEBAPP_WIKIJS_TOKEN=${WEBAPP_WIKIJS_TOKEN}
      - DISCORD_INVITE_LINK=${DISCORD_INVITE_LINK}
      - YOUTUBE_API_KEY=${YOUTUBE_API_KEY}
      - YOUTUBE_CHANNEL_ID=${YOUTUBE_CHANNEL_ID}
    volumes:
      - ./apps/frontend:/app
      - /app/node_modules
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.frontend.rule=Host(`${WEBAPP_DOMAIN}`)"
      - "traefik.http.routers.frontend.entrypoints=web"
      - "traefik.http.routers.frontend.priority=1"
      - "traefik.http.services.frontend.loadbalancer.server.port=5173"
      - "traefik.docker.network=proxy"
    networks:
      - proxy
      - interstack

  api:
    build:
      context: ./apps/api
      dockerfile: Dockerfile.development
    container_name: api
    environment:
      - RAILS_ENV=development
      - DB_USER=root
      - DB_PASSWORD=${DB_PASSWORD}
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=softwares
      - UPDATE_SECRET=${UPDATE_SECRET}
      - FILE_CONTAINER_PATH=/softwares
      - IMAGE_CONTAINER_PATH=/images
      - SECRET_KEY_BASE=${SECRET_KEY_BASE:-changeme_in_production_use_long_random_string}
      - ADMIN_EMAIL=${ADMIN_EMAIL:-admin@teletype.hu}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD:-password123}
    depends_on:
      mysql:
        condition: service_healthy
    volumes:
      - ./data/softwares:/softwares
      - ./data/images:/images
      - ./apps/api:/app
      - api_bundle:/usr/local/bundle
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.api-cors.headers.accesscontrolalloworiginlist=https://${WEBAPP_DOMAIN}"
      - "traefik.http.middlewares.api-cors.headers.accesscontrolallowmethods=GET,POST,PUT,DELETE,OPTIONS"
      - "traefik.http.middlewares.api-cors.headers.accesscontrolallowheaders=Content-Type,Authorization,Accept,X-Requested-With"
      - "traefik.http.middlewares.api-cors.headers.accesscontrolmaxage=3600"
      - "traefik.http.middlewares.api-cors.headers.addvaryheader=true"
      - "traefik.http.routers.api.rule=Host(`${WEBAPP_DOMAIN}`) && (PathPrefix(`/api`) || PathPrefix(`/file`) || PathPrefix(`/update`) || PathPrefix(`/admin`))"
      - "traefik.http.routers.api.entrypoints=web"
      - "traefik.http.routers.api.priority=10"
      - "traefik.http.routers.api.middlewares=api-cors"
      - "traefik.http.services.api.loadbalancer.server.port=3000"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.api-games.rule=Host(`${GAMES_DOMAIN:-games.teletype.hu}`)"
      - "traefik.http.routers.api-games.entrypoints=web"
      - "traefik.http.routers.api-games.middlewares=api-cors"
      - "traefik.http.routers.api-games.service=api"
    networks:
      - proxy
      - interstack

  droparea:
    image: linuxserver/openssh-server
    container_name: droparea
    environment:
      PUID: 1
      PGID: 1
      TZ: Europe/Budapest
      SUDO_ACCESS: "false"
      PASSWORD_ACCESS: "true"
      USER_NAME: drop
      USER_PASSWORD: ${DROP_PASSWORD}
    volumes:
      - ./data/softwares:/home/drop
    ports:
      - "${DROPAREA_SSH_PORT}:2222"
    networks:
      - proxy
      - interstack

volumes:
  gitea:
  woodpecker:
  mysql-data:
  softwares:
  api_bundle:

networks:
  proxy:
    name: proxy
  interstack:
    name: interstack
    external: true

