job "traefik-consul-kv" {
  datacenters = ["dc1"]
  type = "service"

  group "traefik" {
    count = 1

    constraint {
      attribute = "${node.unique.name}"
      value     = "hcp1"
    }

    network {
      mode = "host"
      port "http" {
        static = 80
        host_network = "tailscale0"
      }
      port "traefik" {
        static = 8080
        host_network = "tailscale0"
      }
    }

    task "traefik" {
      driver = "exec"
      
      config {
        command = "/usr/local/bin/traefik"
        args = [
          "--configfile=/local/traefik.yml"
        ]
      }

      template {
        data = <<EOF
api:
  dashboard: true
  insecure: true

entryPoints:
  web:
    address: "0.0.0.0:80"
  traefik:
    address: "0.0.0.0:8080"

providers:
  consulCatalog:
    endpoint:
      address: "warden.tailnet-68f9.ts.net:8500"
      scheme: "http"
    watch: true
  file:
    filename: /local/dynamic.yml
    watch: true

metrics:
  prometheus:
    addEntryPointsLabels: true
    addServicesLabels: true
    addRoutersLabels: true

log:
  level: INFO
EOF
        destination = "local/traefik.yml"
      }

      template {
        data = <<EOF
http:
  middlewares:
    consul-stripprefix:
      stripPrefix:
        prefixes:
          - "/consul"
    
    traefik-stripprefix:
      stripPrefix:
        prefixes:
          - "/traefik"

    nomad-stripprefix:
      stripPrefix:
        prefixes:
          - "/nomad"

    consul-redirect:
      redirectRegex:
        regex: "^/consul/?$"
        replacement: "/consul/ui/"
        permanent: false

    nomad-redirect:
      redirectRegex:
        regex: "^/nomad/?$"
        replacement: "/nomad/ui/"
        permanent: false

    traefik-redirect:
      redirectRegex:
        regex: "^/traefik/?$"
        replacement: "/traefik/dashboard/"
        permanent: false

  services:
    consul-cluster:
      loadBalancer:
        servers:
          - url: "http://warden.tailnet-68f9.ts.net:8500"  # 北京，优先
          - url: "http://ch4.tailnet-68f9.ts.net:8500"     # 韩国，备用
          - url: "http://ash3c.tailnet-68f9.ts.net:8500"   # 美国，备用
        healthCheck:
          path: "/v1/status/leader"
          interval: "30s"
          timeout: "15s"

    nomad-cluster:
      loadBalancer:
        servers:
          - url: "http://ch2.tailnet-68f9.ts.net:4646"     # Nomad server leader
        healthCheck:
          path: "/v1/status/leader"
          interval: "30s"
          timeout: "15s"

  routers:
    consul-redirect:
      rule: "Path(`/consul`) || Path(`/consul/`)"
      service: consul-cluster
      middlewares:
        - consul-redirect
      entryPoints:
        - web
      priority: 100

    consul-ui:
      rule: "PathPrefix(`/consul/ui`)"
      service: consul-cluster
      middlewares:
        - consul-stripprefix
      entryPoints:
        - web
      priority: 5

    consul-api:
      rule: "PathPrefix(`/consul/v1`)"
      service: consul-cluster
      middlewares:
        - consul-stripprefix
      entryPoints:
        - web
      priority: 5
    
    traefik-api:
      rule: "PathPrefix(`/traefik/api`)"
      service: api@internal
      middlewares:
        - traefik-stripprefix
      entryPoints:
        - web
      priority: 6

    traefik-dashboard:
      rule: "PathPrefix(`/traefik/dashboard`)"
      service: dashboard@internal
      middlewares:
        - traefik-stripprefix
      entryPoints:
        - web
      priority: 5

    traefik-redirect:
      rule: "Path(`/traefik`) || Path(`/traefik/`)"
      middlewares:
        - "traefik-redirect"
      entryPoints:
        - web
      priority: 100

    nomad-redirect:
      rule: "Path(`/nomad`) || Path(`/nomad/`)"
      service: nomad-cluster
      middlewares:
        - nomad-redirect
      entryPoints:
        - web
      priority: 100

    nomad-ui:
      rule: "PathPrefix(`/nomad/ui`)"
      service: nomad-cluster
      middlewares:
        - nomad-stripprefix
      entryPoints:
        - web
      priority: 5

    nomad-api:
      rule: "PathPrefix(`/nomad/v1`)"
      service: nomad-cluster
      middlewares:
        - nomad-stripprefix
      entryPoints:
        - web
      priority: 5
EOF
        destination = "local/dynamic.yml"
      }

      resources {
        cpu = 500
        memory = 512
      }
    }
  }
}
