locals {
  SUBDOMAIN = "git." // End with dot or leave blamk for root domain
  DOMAIN = "example.local"
  TRAEFIK_DOMAIN = "${local.SUBDOMAIN}${local.DOMAIN}"
}

job "gitea" {
  datacenters = ["dc1"]
  type = "service"

    group "application" {
    count = 1

    network {
      mode = "bridge"
      port "ingress" {
        to = 3000
      }
    }

    volume "gitea-data" {
      type      = "host"
      source    = "gitea-data"
    }

    service {
      name = "gitea"
      port = "ingress"
      
      connect {
        sidecar_service {
          proxy {
            upstreams {
              destination_name   = "postgres"
              local_bind_address = "127.0.0.1"
              local_bind_port    = 5432
            }
          } 
          tags = ["traefik.enable=false"] # Hide envoy from traefik
        }
      }

      tags = [
        "traefik.enable=true", 
        "traefik.http.routers.gitea.tls=true",
        "traefik.http.routers.gitea.entrypoints=websecure",
        "traefik.http.routers.gitea.rule=Host(`${local.TRAEFIK_DOMAIN}`)"
      ] 

      check {
        type = "http"
        path = "/"
        interval = "10s"
        timeout = "2s"
      }
    }

    task "gitea" {
      driver = "docker"

      config {
        image = "gitea/gitea:1.21.7"
        ports = ["ingress"]
      }

      volume_mount {
        volume      = "gitea-data"
        destination = "/data"
      }
    }
  }
}