services:
  mysql:
    image: mysql:8.0
    container_name: winstudent-mysql
    restart: unless-stopped
    env_file:
      - .env
    command:
      - --innodb_buffer_pool_size=32M
      - --innodb_buffer_pool_instances=1
      - --innodb_log_buffer_size=4M
      - --innodb_redo_log_capacity=32M
      - --key_buffer_size=8M
      - --max_connections=20
      - --tmp_table_size=8M
      - --max_heap_table_size=8M
      - --performance_schema=OFF
      - --thread_stack=196608
      - --sort_buffer_size=512K
      - --read_buffer_size=256K
      - --read_rnd_buffer_size=512K
      - --join_buffer_size=256K
    mem_limit: 256m
    ports:
      - "3309:3306"
    volumes:
      - win_mysql_data:/var/lib/mysql
      - ./db/docker-init:/docker-entrypoint-initdb.d:ro
      - ./db/Objects:/db-objects:ro
    healthcheck:
      # Uses TCP (-h 127.0.0.1) so it only passes after init scripts complete
      # and MySQL starts listening on the network.
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"]
      interval: 10s
      timeout: 5s
      retries: 15
      start_period: 30s
    networks:
      - backend

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    container_name: winstudent-api
    restart: unless-stopped
    env_file:
      - .env
    environment:
      - ASPNETCORE_URLS=http://+:8005
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - backend
      - web
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.winstudent-api.rule=Host(`winapi.opelly.me`)"
      - "traefik.http.routers.winstudent-api.entrypoints=websecure"
      - "traefik.http.routers.winstudent-api.tls.certresolver=letsencrypt"
      - "traefik.http.services.winstudent-api.loadbalancer.server.port=8005"
      - "traefik.http.routers.winstudent-api.middlewares=gzip@file,security-headers@file"

  ui:
    build:
      context: ./ui/winstudentgoaltracker
      dockerfile: Dockerfile
    container_name: winstudent-ui
    restart: unless-stopped
    depends_on:
      - api
    networks:
      - web
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.winstudent-ui.rule=Host(`win.opelly.me`)"
      - "traefik.http.routers.winstudent-ui.entrypoints=websecure"
      - "traefik.http.routers.winstudent-ui.tls.certresolver=letsencrypt"
      - "traefik.http.services.winstudent-ui.loadbalancer.server.port=8006"
      - "traefik.http.routers.winstudent-ui.middlewares=gzip@file,security-headers@file"

networks:
  backend:
  web:
    external: true

volumes:
  win_mysql_data:
