services:
  forgejo:
    image: codeberg.org/forgejo/forgejo:latest
    container_name: fj-node${FJ_NODE_ID}
    restart: always
    environment:
      - FORGEJO__database__DB_TYPE=${FJ_DB_TYPE}
      - FORGEJO__database__HOST=${FJ_DB_HOST}
      - FORGEJO__database__PORT=${FJ_DB_PORT}
      - FORGEJO__database__NAME=${FJ_DB_NAME}
      - FORGEJO__database__USER=${FJ_DB_USER}
      - FORGEJO__database__PASSWD=${FJ_DB_PASS}
      - FORGEJO__storage__TYPE=${FJ_STORE_TYPE}
      - FORGEJO__storage__MINIO_ENDPOINT=${FJ_STORE_ENDPOINT}
      - FORGEJO__storage__MINIO_ACCESS_KEY_ID=${FJ_STORE_KEY}
      - FORGEJO__storage__MINIO_SECRET_ACCESS_KEY=${FJ_STORE_SECRET}
      - FORGEJO__storage__MINIO_BUCKET=${FJ_STORE_BUCKET}
      - FORGEJO__storage__MINIO_USE_SSL=false
    volumes:
      - /docker/core/forgejo/data:/data
    ports:
      - "3000:3000"
      - "222:22"
    networks:
      - fj_internal
      - fj_external
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=${TRAEFIK_NETWORK}"

      # --- 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=${TRAEFIK_RESOLVER}"
      - "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=${TRAEFIK_RESOLVER}"
      - "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_internal:
    external: true
  fj_external:
    driver: bridge