# yaml-language-server: $schema=https://schema.zeabur.app/template.json
apiVersion: zeabur.com/v1
kind: Template
metadata:
    name: VoAPI
spec:
    description: VoAPI stack for Zeabur with MySQL and Redis.
    icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/docker.svg
    tags:
        - AI
        - API
        - Gateway
    readme: |-
        VoAPI on Zeabur

        This template deploys:

        - db-voapi (MySQL 8.0.43)
        - redis-voapi (Redis latest)
        - voapi-app (voapi/voapi:latest)

        After deployment, bind your domain to `voapi-app`.
    services:
        - name: db-voapi
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/mysql.svg
          template: PREBUILT
          spec:
            id: db-voapi
            source:
                image: mysql:8.0.43
                command:
                    - --max_connections=5000
                    - --character-set-server=utf8mb4
                    - --collation-server=utf8mb4_unicode_ci
            ports:
                - id: database
                  port: 3306
                  type: TCP
            volumes:
                - id: data
                  dir: /var/lib/mysql
            instructions:
                - title: MySQL Private Host
                  content: ${MYSQL_HOST}
                - title: MySQL Port
                  content: ${MYSQL_PORT}
            env:
                MYSQL_ALLOW_EMPTY_PASSWORD:
                    default: "yes"
                MYSQL_DATABASE:
                    default: voapi
                MYSQL_HOST:
                    default: ${CONTAINER_HOSTNAME}
                    expose: true
                MYSQL_PASSWORD:
                    default: voapi123
                    expose: true
                MYSQL_PORT:
                    default: ${DATABASE_PORT}
                    expose: true
                MYSQL_USER:
                    default: voapi
                    expose: true
                TZ:
                    default: Asia/Shanghai
            configs:
                - path: /docker-entrypoint-initdb.d/init-db.sql
                  template: |-
                    CREATE DATABASE IF NOT EXISTS `voapi` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
                    CREATE DATABASE IF NOT EXISTS `voapi-log` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
                    CREATE DATABASE IF NOT EXISTS `voapi-body-log` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

                    GRANT ALL PRIVILEGES ON `voapi`.* TO 'voapi'@'%';
                    GRANT ALL PRIVILEGES ON `voapi-log`.* TO 'voapi'@'%';
                    GRANT ALL PRIVILEGES ON `voapi-body-log`.* TO 'voapi'@'%';

                    FLUSH PRIVILEGES;
                  permission: null
                  envsubst: null
        - name: redis-voapi
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/redis.svg
          template: PREBUILT
          spec:
            id: redis-voapi
            source:
                image: redis:latest
            ports:
                - id: database
                  port: 6379
                  type: TCP
            volumes:
                - id: data
                  dir: /data
            instructions:
                - title: Redis Private Host
                  content: ${REDIS_HOST}
                - title: Redis Port
                  content: ${REDIS_PORT}
            env:
                REDIS_HOST:
                    default: ${CONTAINER_HOSTNAME}
                    expose: true
                REDIS_PORT:
                    default: ${DATABASE_PORT}
                    expose: true
                REDIS_URI:
                    default: redis://${REDIS_HOST}:${REDIS_PORT}/0
                    expose: true
        - name: voapi-app
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/docker.svg
          template: PREBUILT
          spec:
            id: voapi-app
            source:
                image: voapi/voapi:latest
            ports:
                - id: web
                  port: 6800
                  type: HTTP
            volumes:
                - id: file
                  dir: /file
                - id: public
                  dir: /public
            env:
                MYSQL_DSN:
                    default: voapi:voapi123@tcp(db-voapi.zeabur.internal:3306)/voapi
                MYSQL_LOG_BODY_DSN:
                    default: voapi:voapi123@tcp(db-voapi.zeabur.internal:3306)/voapi-body-log
                MYSQL_LOG_DSN:
                    default: voapi:voapi123@tcp(db-voapi.zeabur.internal:3306)/voapi-log
                REDIS_URI:
                    default: redis://redis-voapi.zeabur.internal:6379/0
                TZ:
                    default: Asia/Shanghai
            configs:
                - path: /config.yml
                  template: |-
                    app:
                      port: 6800

                    db:
                      dirver: mysql
                      log-dirver: mysql

                    mysql:
                      dsn: ${MYSQL_DSN}
                      log-dsn: ${MYSQL_LOG_DSN}
                      log-body-dsn: ${MYSQL_LOG_BODY_DSN}
                      log-sharding:
                        enable: false
                        mode: y

                    redis:
                      dsn: ${REDIS_URI}
                      pool-size: 0
                  permission: null
                  envsubst: true
