services:
  db:
    image: mysql
    container_name: mysql
    restart: unless-stopped
    volumes:
      - ./mysql/data:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      - TZ=${TZ}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 30s
      timeout: 10s
      retries: 5
    networks:
      - backend
  mqtt:
    image: eclipse-mosquitto:openssl
    container_name: mosquitto
    restart: unless-stopped
    volumes:
      - ./mosquitto/config:/mosquitto/config
      - ./letsencrypt:/mosquitto/config/letsencrypt
      - ./mosquitto/data:/mosquitto/data
      - ./mosquitto/log:/mosquitto/log
    environment:
      - TZ=${TZ}
    healthcheck:
      test: ["CMD", "mosquitto_ctrl", "status"]
      interval: 30s
      timeout: 10s
      retries: 5
    labels:
      - "traefik.enable=true"
      - "traefik.tcp.routers.mqtt.rule=HostSNI(`*`)"
      - "traefik.tcp.routers.mqtt.entrypoints=mqtt"
      - "traefik.tcp.services.mqtt.loadbalancer.server.port=1883"
      - "traefik.tcp.routers.mqtts.rule=HostSNI(`*`)"
      - "traefik.tcp.routers.mqtts.entrypoints=mqtts"
      - "traefik.tcp.services.mqtts.loadbalancer.server.port=8883"
    networks:
      - traefik
      - backend
  apache:
    image: webdevops/php-apache:8.4
    volumes:
      - ./www:/app
    deploy:
      mode: replicated
      replicas: 1
    depends_on:
      - db
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.map-http.rule=Host(`map.domain.tld`)"
      - "traefik.http.routers.map-http.entrypoints=web"
      - "traefik.http.routers.map-http.middlewares=map-redirectscheme"
      - "traefik.http.routers.map-https.rule=Host(`map.domain.tld`)"
      - "traefik.http.routers.map-https.entrypoints=websecure"
      - "traefik.http.routers.map-https.tls=true"
      - "traefik.http.routers.map-https.tls.certresolver=letsencrypt"
      - "traefik.http.services.map-service.loadbalancer.server.port=80"
      - "traefik.http.middlewares.map-redirectscheme.redirectscheme.scheme=https"
      - "traefik.http.middlewares.map-redirectscheme.redirectscheme.permanent=true"
    environment:
      - WEB_DOCUMENT_ROOT=${WEB_DOCUMENT_ROOT}
      - PHP_MEMORY_LIMIT=${PHP_MEMORY_LIMIT}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/status.php"]
      interval: 30s
      timeout: 10s
      retries: 5
    networks:
      - traefik
      - backend
  recorder:
    image: owntracks-mqtt-mysql
    depends_on:
      - db
      - mqtt
    environment:
      - MQTT_BROKER=${MQTT_BROKER}
      - MQTT_PORT=${MQTT_PORT}
      - MQTT_USERNAME=${MQTT_USERNAME}
      - MQTT_PASSWORD=${MQTT_PASSWORD}
      - MYSQL_HOST=${MYSQL_HOST}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}

networks:
  traefik:
    external: true
  backend:
    driver: bridge