services:
  asciinema_backend:
    image: ${ASCIINEMA_IMAGE:-ghcr.io/asciinema/asciinema-server:latest}
    container_name: ${ASCIINEMA_COMPOSE_PROJECT_NAME}_backend
    restart: unless-stopped
    networks:
      - compose_network
    volumes:
      - asciinema_data:/var/opt/asciinema
      - ./custom.exs:/opt/app/etc/custom.exs:ro
    environment:
      SECRET_KEY_BASE: ${ASCIINEMA_SECRET_KEY}
      URL_HOST: ${ASCIINEMA_TRAEFIK_HOST}
      URL_SCHEME: https
      DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@${CORE_DB_HOST}/${ASCIINEMA_DB_NAME}?pool_size=10
      SMTP_HOST: ${EMAIL_SMTP_HOST}
      SMTP_USERNAME: ${EMAIL_SMTP_USER}
      SMTP_PASSWORD: ${EMAIL_SMTP_PASSWORD}
      SMTP_FROM_ADDRESS: ${EMAIL_FROM}
      SIGN_UP_DISABLED: ${ASCIINEMA_SIGN_UP_DISABLED:-false}
      DEFAULT_AVATAR: gravatar

  asciinema:
    image: nginx:alpine
    container_name: ${ASCIINEMA_COMPOSE_PROJECT_NAME}_app
    restart: unless-stopped
    networks:
      - compose_network
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./theme:/theme:ro
    depends_on:
      - asciinema_backend
    labels:
      - 'traefik.enable=${ASCIINEMA_TRAEFIK_ENABLED}'
      # Main web interface - HTTP to HTTPS redirect
      - 'traefik.http.middlewares.${ASCIINEMA_COMPOSE_PROJECT_NAME}-redirect-web-secure.redirectscheme.scheme=https'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web.middlewares=${ASCIINEMA_COMPOSE_PROJECT_NAME}-redirect-web-secure'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web.rule=Host(`${ASCIINEMA_TRAEFIK_HOST}`)'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web.entrypoints=web'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web.service=${ASCIINEMA_COMPOSE_PROJECT_NAME}'
      # Main web interface - HTTPS router
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web-secure.rule=Host(`${ASCIINEMA_TRAEFIK_HOST}`)'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web-secure.tls.certresolver=resolver'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web-secure.entrypoints=web-secure'
      - 'traefik.http.middlewares.${ASCIINEMA_COMPOSE_PROJECT_NAME}-compress.compress=true'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web-secure.middlewares=${ASCIINEMA_COMPOSE_PROJECT_NAME}-compress,security-headers@file'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-web-secure.service=${ASCIINEMA_COMPOSE_PROJECT_NAME}'
      - 'traefik.http.services.${ASCIINEMA_COMPOSE_PROJECT_NAME}.loadbalancer.server.port=8080'
      # Admin interface - HTTP to HTTPS redirect
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web.middlewares=${ASCIINEMA_COMPOSE_PROJECT_NAME}-redirect-web-secure'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web.rule=Host(`admin.${ASCIINEMA_TRAEFIK_HOST}`)'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web.entrypoints=web'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web.service=${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin'
      # Admin interface - HTTPS router with Basic Auth
      - 'traefik.http.middlewares.${ASCIINEMA_COMPOSE_PROJECT_NAME}-auth.basicauth.users=${AUTH_USERS}'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web-secure.rule=Host(`admin.${ASCIINEMA_TRAEFIK_HOST}`)'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web-secure.tls.certresolver=resolver'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web-secure.entrypoints=web-secure'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web-secure.middlewares=${ASCIINEMA_COMPOSE_PROJECT_NAME}-auth,${ASCIINEMA_COMPOSE_PROJECT_NAME}-compress,security-headers@file'
      - 'traefik.http.routers.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin-web-secure.service=${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin'
      - 'traefik.http.services.${ASCIINEMA_COMPOSE_PROJECT_NAME}-admin.loadbalancer.server.port=8080'
      # Network
      - 'traefik.docker.network=${NETWORK_NAME}'
      # Watchtower
      - 'com.centurylinklabs.watchtower.enable=${WATCHTOWER_LABEL_ENABLE}'

volumes:
  asciinema_data:
    name: ${ASCIINEMA_COMPOSE_PROJECT_NAME}_data

networks:
  compose_network:
    name: ${NETWORK_NAME}
    external: true
