job "traefik" {
  group "traefik" {
    network {
      port "http" {
        static = 80
      }

      port "api" {
        static = 8081
      }
    }

    service {
      name = "traefik"

      tags = [
        "traefik.enable=true",
        "traefik.http.routers.traefik.rule=Host(`traefik.othrayte.one`)",
        "traefik.http.routers.traefik.service=traefik",
        "traefik.http.services.traefik.loadbalancer.server.port=8081",
      ]

      check {
        name     = "alive"
        type     = "tcp"
        port     = "http"
        interval = "10s"
        timeout  = "2s"
      }
    }

    task "traefik" {
      driver = "docker"

      config {
        image        = "traefik:v3.3"
        network_mode = "host"

        volumes = [
          "local/traefik.yml:/etc/traefik/traefik.yml",
          "local/configs/:/etc/traefik/configs/",
        ]
      }

      template {
        data = <<EOF
entryPoints:
  http:
    address: ":80"
    http:
      middlewares:
        - auth
  traefik:
    address: ":8081"

api:
  dashboard: true
  insecure: true

providers:
  file:
    directory: "/etc/traefik/configs/"

  consulCatalog:
    prefix: "traefik"
    exposedByDefault: false
    defaultRule: {{"Host(`{{ .Name }}.othrayte.one`)"}}
    endpoint:
      address: "127.0.0.1:8500"
      scheme: "http"
EOF

        destination = "local/traefik.yml"
      }

      template {
        data = <<EOF
http:
  middlewares:
    auth:
      basicAuth:
        users:
          - "othrayte:$apr1$7PqVUfNm$Go/SNo6y331KYDnQdOLIt/"
 
  routers:
    nomadui:
      rule: "Host(`nomad.othrayte.one`)"
      service: nomadui
    consului:
      rule: "Host(`consul.othrayte.one`)"
      service: consului

  services:
    nomadui:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:4646"
    consului:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:8500"
EOF

        destination = "local/configs/nomad.yml"
      }

      resources {
        cpu    = 100
        memory = 128
      }
    }
  }
}
