services:
  redis:
    image: redis:alpine
    restart: always
    networks:
      - fj_internal  # MUST match the network your Forgejo container is on
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
  forgejo:
    image: codeberg.org/forgejo/forgejo:9.0
    container_name: fj-node${FJ_NODE_ID}
    restart: always
    environment:
      - FORGEJO__database__DB_TYPE=mysql
      - FORGEJO__database__HOST=172.16.201.150
      - FORGEJO__database__PORT=3306
      - FORGEJO__database__NAME=${FJ_DB_NAME}
      - FORGEJO__database__USER=${FJ_DB_USER}
      - FORGEJO__database__PASSWD=${FJ_DB_PASS}
      - FORGEJO__server__DOMAIN=git.${TRAEFIK_DNS_SUFFIX}
      - FORGEJO__server__ROOT_URL=https://git.${TRAEFIK_DNS_SUFFIX}/
      - FORGEJO__server__SSH_PORT=2222
      - GITEA__queue__TYPE=redis
      - GITEA__queue__CONN_STR=redis://redis:6379/0
      - GITEA__session__PROVIDER=redis
      - GITEA__session__PROVIDER_CONFIG=redis://redis:6379/1
      - GITEA__cache__ADAPTER=redis
      - GITEA__cache__HOST=redis://redis:6379/2
    volumes:
      - /shared/git:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3022:3000"
      - "2222:22"
    networks:
      - fj_internal
      - fj_external
    labels:
      - "traefik.enable=${TRAEFIK_ENABLE}"
      - "traefik.docker.network=fj_external"

      # --- SHARED HA ROUTER (git.domain.com) ---
      - "traefik.http.routers.forgejo-shared-${FJ_NODE_ID}.rule=Host(`git.${TRAEFIK_DNS_SUFFIX}`)"
      - "traefik.http.routers.forgejo-shared-${FJ_NODE_ID}.entrypoints=${TRAEFIK_ENTRY_POINTS}"
      - "traefik.http.routers.forgejo-shared-${FJ_NODE_ID}.tls.certresolver=desec"
      - "traefik.http.routers.forgejo-shared-${FJ_NODE_ID}.service=forgejo-common-svc"

      # --- NODE-SPECIFIC ADMIN ROUTER (git1... git2...) ---
      - "traefik.http.routers.forgejo-admin-${FJ_NODE_ID}.rule=Host(`git${FJ_NODE_ID}.${TRAEFIK_DNS_SUFFIX}`)"
      - "traefik.http.routers.forgejo-admin-${FJ_NODE_ID}.entrypoints=${TRAEFIK_ENTRY_POINTS}"
      - "traefik.http.routers.forgejo-admin-${FJ_NODE_ID}.tls.certresolver=desec"
      - "traefik.http.routers.forgejo-admin-${FJ_NODE_ID}.service=forgejo-common-svc"

      # --- SHARED SERVICE ---
      - "traefik.http.services.forgejo-common-svc.loadbalancer.server.port=3000"
      # Forgejo benefits from sticky sessions to keep the web-git terminal stable
      - "traefik.http.services.forgejo-common-svc.loadbalancer.sticky.cookie=true"
networks:
  fj_external:
    external: true
  fj_internal:
    internal: true