# yaml-language-server: $schema=https://schema.zeabur.app/template.json
apiVersion: zeabur.com/v1
kind: Template
metadata:
    name: Mix Space
spec:
    description: An Alternative Personal Space.
    coverImage: https://mx-space.js.org/logo.png
    icon: https://mx-space.js.org/corelogo.png
    variables:
        - key: ALLOWED_ORIGINS
          type: STRING
          name: 被允许的域名
          description: 【后端】填写被允许访问的域名，通常是前端的域名，如果允许多个域名访问，用英文逗号，分隔。
        - key: JWT_SECRET
          type: PASSWORD
          name: JWT Secret
          description: 【后端】填写长度不小于 16 个字符，不大于 32 个字符的字符串，用于加密用户的 JWT，务必保存好自己的密钥，不要泄露给他人。服务设置完成后，不要修改此变量，否则会导致异常。
        - key: ENCRYPT_ENABLE
          type: STRING
          name: 开启加密
          description: 【后端】【选填】如果你确定要开启加密，输入 true，开启加密后，你需要在下方填写加密密钥。
        - key: ENCRYPT_KEY
          type: STRING
          name: 加密密钥
          description: 【后端】【选填】加密密钥，如果你不知道你在做什么，那么建议留空，具体内容可参考 https://mx-space.js.org/usage/security.html 。若开启加密，则需注意密钥长度必须为 64 位且只有小写字母和数字，不然会在初始化时报错。注意这是不可逆的，务必保存自己的秘钥。所以并不是非常推荐使用，除非你真的需要加密 API Key.
        - key: NEXT_PUBLIC_API_URL
          type: STRING
          name: 后端 API 地址
          description: 【前端】Shiro 前端所要连接到的API地址（即后端地址），格式为 https://domain.tld/api/v2，后续需要在服务管理页面绑定此域名。如果你计划在其它地方部署前端，此处可以留空并在目标配置，并删除 shiro 服务。
        - key: NEXT_PUBLIC_GATEWAY_URL
          type: STRING
          name: 后端网关地址
          description: 【前端】Shiro 前端所要连接到的网关地址（即后端地址），格式为 https://domain.tld/，同上。如果你计划在其它地方部署前端，此处可以留空并在目标配置，并删除 shiro 服务。
    tags:
        - CMS
        - Website
        - Blog
    readme: |
        一个使用 Zeabur 快速搭建 Mix Space 后端 + Shiro 前端的模板，可以作为博客使用。

        [项目文档](https://mx-space.js.org)

        [手动部署教程](https://lab.gb0.dev/post/mxspace-on-zeabur/)

        部署完成之后，请在 **Nginx** 服务处添加一个域名。
    services:
        - name: mongodb
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/mongodb.svg
          template: PREBUILT_V2
          spec:
            id: mongodb
            source:
                image: mongo:7
                command:
                    - sh
                args:
                    - -c
                    - sed -i '10,23d' /usr/local/bin/docker-entrypoint.sh && exec docker-entrypoint.sh mongod
            ports:
                - id: database
                  port: 27017
                  type: TCP
            volumes:
                - id: data
                  dir: /data/db
            instructions:
                - title: Command to connect to your MongoDB
                  content: mongosh "mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${PORT_FORWARDED_HOSTNAME}:${DATABASE_PORT_FORWARDED_PORT}"
                - title: MongoDB connection string
                  content: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${PORT_FORWARDED_HOSTNAME}:${DATABASE_PORT_FORWARDED_PORT}
                - title: MongoDB username
                  content: ${MONGO_USERNAME}
                - title: MongoDB password
                  content: ${MONGO_PASSWORD}
                - title: MongoDB host
                  content: ${PORT_FORWARDED_HOSTNAME}
                - title: MongoDB port
                  content: ${DATABASE_PORT_FORWARDED_PORT}
            env:
                MONGO_CONNECTION_STRING:
                    default: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}
                    expose: true
                MONGO_HOST:
                    default: ${CONTAINER_HOSTNAME}
                    expose: true
                MONGO_INITDB_ROOT_PASSWORD:
                    default: ${PASSWORD}
                MONGO_INITDB_ROOT_USERNAME:
                    default: mongo
                MONGO_PASSWORD:
                    default: ${MONGO_INITDB_ROOT_PASSWORD}
                    expose: true
                MONGO_PORT:
                    default: ${DATABASE_PORT}
                    expose: true
                MONGO_URI:
                    default: ${MONGO_CONNECTION_STRING}
                    expose: true
                MONGO_USERNAME:
                    default: ${MONGO_INITDB_ROOT_USERNAME}
                    expose: true
        - name: redis
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/marketplace/redis.svg
          template: PREBUILT_V2
          spec:
            id: redis
            source:
                image: redis/redis-stack-server:latest
            ports:
                - id: database
                  port: 6379
                  type: TCP
            volumes:
                - id: data
                  dir: /data
            instructions:
                - title: Command to connect to your Redis
                  content: redis-cli -h ${PORT_FORWARDED_HOSTNAME} -p ${DATABASE_PORT_FORWARDED_PORT} -a ${REDIS_PASSWORD}
                - title: Redis Connection String
                  content: redis://:${REDIS_PASSWORD}@${PORT_FORWARDED_HOSTNAME}:${DATABASE_PORT_FORWARDED_PORT}
                - title: Redis password
                  content: ${REDIS_PASSWORD}
                - title: Redis host
                  content: ${PORT_FORWARDED_HOSTNAME}
                - title: Redis port
                  content: ${DATABASE_PORT_FORWARDED_PORT}
            env:
                REDIS_ARGS:
                    default: --requirepass ${REDIS_PASSWORD}
                REDIS_CONNECTION_STRING:
                    default: redis://:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}
                    expose: true
                REDIS_HOST:
                    default: ${CONTAINER_HOSTNAME}
                    expose: true
                REDIS_PASSWORD:
                    default: ${PASSWORD}
                    expose: true
                REDIS_PORT:
                    default: ${DATABASE_PORT}
                    expose: true
                REDIS_URI:
                    default: ${REDIS_CONNECTION_STRING}
                    expose: true
        - name: mx-backend
          icon: https://mx-space.js.org/logo.png
          dependencies:
            - redis
            - mongodb
          template: PREBUILT_V2
          spec:
            id: mx-backend
            source:
                image: innei/mx-server:latest
            ports:
                - id: web
                  port: 2333
                  type: HTTP
            volumes:
                - id: data
                  dir: /root/.mx-space
            env:
                DB_HOST:
                    default: ${MONGO_HOST}
                    expose: true
                DB_PASSWORD:
                    default: ${MONGO_PASSWORD}
                    expose: true
                DB_PORT:
                    default: ${MONGO_PORT}
                    expose: true
                DB_USERNAME:
                    default: ${MONGO_USERNAME}
                    expose: true
                NODE_ENV:
                    default: production
                TZ:
                    default: Asia/Shanghai
        - name: shiro
          template: PREBUILT_V2
          spec:
            id: shiro
            source:
                image: innei/shiro:latest
            ports:
                - id: web
                  port: 2323
                  type: HTTP
            env:
                NEXT_SHARP_PATH:
                    default: /usr/local/lib/node_modules/sharp
                NODE_ENV:
                    default: production
        - name: Nginx
          icon: https://www.svgrepo.com/show/373924/nginx.svg
          template: PREBUILT_V2
          spec:
            id: nginx
            source:
                image: nginx:1.25
            ports:
                - id: web
                  port: 80
                  type: HTTP
            configs:
                - path: /etc/nginx/nginx.conf
                  template: |
                    worker_processes  5; error_log  stderr; worker_rlimit_nofile 8192;
                    events {}
                    http {
                        default_type application/octet-stream;
                        log_format   main '$remote_addr - $remote_user [$time_local]  $status '
                        '"$request" $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
                        access_log   /dev/stdout  main;
                        sendfile     on;
                        tcp_nopush   on;
                        server_names_hash_bucket_size 128; # this seems to be required for some vhosts

                    server {
                        ## 反向代理开始
                        ## WebSocket 地址
                        location /socket.io {
                            proxy_set_header Upgrade $http_upgrade;
                            proxy_set_header Connection "Upgrade";
                            proxy_buffering off;
                            proxy_set_header Host $host;
                            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                            proxy_set_header X-Forwarded-Proto $scheme;
                            proxy_pass http://mx-backend.zeabur.internal:2333/socket.io;
                        }
                        ## API 地址
                        location /api/v2 {
                            proxy_pass http://mx-backend.zeabur.internal:2333/api/v2;
                        }
                        ## 简读 render 地址
                        location /render {
                            proxy_pass http://mx-backend.zeabur.internal:2333/render;
                        }
                        ## Shiro 地址
                        location / {
                            proxy_pass http://shiro.zeabur.internal:2323;
                        }
                        ## 后台地址
                        location /proxy {
                            proxy_pass http://mx-backend.zeabur.internal:2333/proxy;
                        }
                        location /qaqdmin {
                            proxy_pass http://mx-backend.zeabur.internal:2333/proxy/qaqdmin;
                        }
                        ## RSS 地址
                        location ~* \/(feed|sitemap|atom.xml) {
                            proxy_pass http://mx-backend.zeabur.internal:2333/$1;
                        }
                        ## 反向代理结束
                        }
                    }
                  permission: null
                  envsubst: null
