services:
  caddy:
    image: caddy:2-alpine
    container_name: caddy
    restart: unless-stopped
    dns:
      - 1.1.1.1
      - 8.8.8.8
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - /opt/data/caddy:/data
      - /opt/configs/caddy:/config
    entrypoint: /bin/sh
    command:
      - -c
      - |
        while [ ! -f /config/lego/certificates/ada-dev.ru.crt ] || [ ! -f /config/lego/certificates/ada-dev.ru.key ]; do
          sleep 10
        done
        exec caddy run --config /etc/caddy/Caddyfile --adapter caddyfile

  lego:
    image: git.ada-dev.ru/ada/lego:latest
    container_name: caddy-lego
    dns:
      - 1.1.1.1
      - 8.8.8.8
    env_file:
      - .env
    restart: unless-stopped
    volumes:
      - /opt/configs/caddy/lego:/lego-data
    entrypoint: /bin/sh
    command:
      - -c
      - |
        while true; do
          if [ -f /lego-data/certificates/ada-dev.ru.crt ]; then
            /lego renew \
              --email "$$LEGO_EMAIL" \
              --dns timewebcloud \
              --dns.resolvers 1.1.1.1:53 \
              --dns.resolvers 8.8.8.8:53 \
              --dns.propagation-timeout 5m \
              --domains ada-dev.ru \
              --domains '*.ada-dev.ru' \
              --path /lego-data \
              --days 30
            sleep 24h
          else
            /lego run \
              --email "$$LEGO_EMAIL" \
              --accept-tos \
              --dns timewebcloud \
              --dns.resolvers 1.1.1.1:53 \
              --dns.resolvers 8.8.8.8:53 \
              --dns.propagation-timeout 5m \
              --domains ada-dev.ru \
              --domains '*.ada-dev.ru' \
              --path /lego-data
            sleep 10m
          fi
        done
networks: {}
