90 lines
1.7 KiB
Markdown
90 lines
1.7 KiB
Markdown
# apiserver
|
||
|
||
`apiserver/` 提供微信小程序用的远端渲染接口:
|
||
- 小程序只上传参数(字体 ID、文字、字号、颜色等)
|
||
- 服务端读取 `fonts.json` + `fonts/`,生成 SVG 后返回
|
||
|
||
## 1. 启动
|
||
|
||
在仓库根目录执行:
|
||
|
||
```bash
|
||
python3 apiserver/server.py \
|
||
--host 0.0.0.0 \
|
||
--port 9300 \
|
||
--static-root /home/gavin/font2svg
|
||
```
|
||
|
||
其中 `--static-root` 目录必须包含:
|
||
|
||
- `fonts.json`
|
||
- `fonts/`(字体文件目录)
|
||
|
||
如果不传 `--manifest`,默认读取 `<static-root>/fonts.json`。
|
||
|
||
## 2. API
|
||
|
||
### GET `/healthz`
|
||
|
||
返回服务健康状态和已加载字体数量。
|
||
|
||
### POST `/api/render-svg`
|
||
|
||
请求示例:
|
||
|
||
```json
|
||
{
|
||
"fontId": "其他字体/AlimamaDaoLiTi",
|
||
"text": "星程字体转换",
|
||
"fontSize": 120,
|
||
"fillColor": "#000000",
|
||
"letterSpacing": 0,
|
||
"maxCharsPerLine": 45
|
||
}
|
||
```
|
||
|
||
成功响应:
|
||
|
||
```json
|
||
{
|
||
"ok": true,
|
||
"data": {
|
||
"fontId": "其他字体/AlimamaDaoLiTi",
|
||
"fontName": "AlimamaDaoLiTi",
|
||
"width": 956.2,
|
||
"height": 144.3,
|
||
"svg": "<?xml ...>"
|
||
}
|
||
}
|
||
```
|
||
|
||
## 3. Nginx 反向代理
|
||
|
||
在 `fonts.biboer.cn` 的 server 块中增加:
|
||
|
||
```nginx
|
||
location /api/ {
|
||
proxy_pass http://127.0.0.1:9300;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
proxy_set_header X-Forwarded-Proto $scheme;
|
||
proxy_connect_timeout 5s;
|
||
proxy_send_timeout 60s;
|
||
proxy_read_timeout 60s;
|
||
}
|
||
```
|
||
|
||
然后执行:
|
||
|
||
```bash
|
||
sudo nginx -t && sudo systemctl reload nginx
|
||
```
|
||
|
||
## 4. 约束
|
||
|
||
- 字体解析完全基于 `fonts.json`,`fontId` 必须存在。
|
||
- 服务端启用 CORS,允许小程序访问。
|
||
- 不依赖 Flask/FastAPI,使用 Python 标准库 HTTP 服务。
|