services:
  gitea-postgres:
    image: docker.io/library/postgres:15
    container_name: ${SERVICE_NAME}-postgres
    environment:
      - POSTGRES_USER=${GITEA_DB_USERNAME}
      - POSTGRES_PASSWORD=${GITEA_DB_PASSWORD}
      - POSTGRES_DB=${SERVICE_NAME}
    volumes:
      - /docker/management/gitea-postgres/postgres:/var/lib/postgresql/data
    restart: unless-stopped
    networks:
      - internal_net

  gitea:
    image: gitea/gitea:latest
    container_name: ${SERVICE_NAME}
    restart: unless-stopped
    depends_on:
      - ${SERVICE_NAME}-postgres
    networks:
      - internal_net
      - external_net
    volumes:
      - /docker/management/${SERVICE_NAME}:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    environment:
      - TZ=${TZ}
      - GITEA__actions__ENABLED=true
      - GITEA__database__DB_TYPE=postgres
      - GITEA__database__HOST=${SERVICE_NAME}-postgres:5432
      - GITEA__database__NAME=${SERVICE_NAME}
      - GITEA__server__ROOT_URL=${GITEA_PROTOCOL}://${GITEA_FQDN}
      - GITEA__database__USER=${GITEA_DB_USERNAME}
      - GITEA__database__PASSWD=${GITEA_DB_PASSWORD}
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.${SERVICE_NAME}.rule=Host(`${GITEA_FQDN}`)"
      - "traefik.http.routers.${SERVICE_NAME}.entrypoints=${TRAEFIK_ENTRYPOINT}"
      - "traefik.http.routers.${SERVICE_NAME}.tls.certresolver=${TRAEFIK_RESOLVER}"
      - "traefik.http.services.${SERVICE_NAME}.loadbalancer.server.port=${GITEA_INTERNAL_PORT}"
      - "traefik.docker.network=gitea_external"

networks:
  internal_net:
    name: ${GITEA_INTERNAL_NETWORK_NAME}
    driver: bridge
  external_net:
    name: ${GITEA_EXTERNAL_NETWORK_NAME}
    driver: bridge