81 lines
1.8 KiB
Markdown
81 lines
1.8 KiB
Markdown
# Platform(go-zero)骨架
|
||
|
||
Publish(校验蓝图 → 分配 schema → 生成/执行 DDL → 注册元数据)+ 动态 CRUD + JWT。
|
||
|
||
## 目录
|
||
|
||
```text
|
||
platform/
|
||
platform.go
|
||
docker-compose.yml # 本地 Postgres
|
||
etc/platform.yaml
|
||
internal/
|
||
authx/ # JWT 签发/校验 + Dev 头兜底
|
||
blueprint/
|
||
schema/ # Postgres DDL(数值 PK 用 IDENTITY)
|
||
meta/
|
||
crud/ # MemoryEngine + PostgresEngine
|
||
logic/applogic/
|
||
handler/
|
||
```
|
||
|
||
## 运行(内存 + JWT)
|
||
|
||
```bash
|
||
cd platform
|
||
go test ./...
|
||
go run . -f etc/platform.yaml
|
||
# 另开终端
|
||
go run ./scripts/smoke.go
|
||
```
|
||
|
||
冒烟会:签发 JWT → publish → create → list → 非法 token 应 401。
|
||
|
||
## 运行(Postgres 行级 Engine)
|
||
|
||
```bash
|
||
docker compose up -d
|
||
```
|
||
|
||
改 `etc/platform.yaml`:
|
||
|
||
```yaml
|
||
DataSource: "postgres://platform:platform@127.0.0.1:5432/platform?sslmode=disable"
|
||
DevAuth: false
|
||
```
|
||
|
||
再启动服务。此时:
|
||
- publish **真实建 schema/表**
|
||
- CRUD 走 `PostgresEngine`(参数化 SQL + `tenant_id` 强制条件)
|
||
|
||
## JWT
|
||
|
||
```http
|
||
POST /api/v1/auth/token
|
||
{"tenant_id":1,"user_id":1,"secret":"dev-only-change-me"}
|
||
|
||
→ {"access_token":"...","token_type":"Bearer","expires_at":...}
|
||
```
|
||
|
||
业务请求:
|
||
|
||
```http
|
||
Authorization: Bearer <access_token>
|
||
```
|
||
|
||
| 配置 | 行为 |
|
||
|------|------|
|
||
| `DevAuth: false` | 必须带合法 JWT |
|
||
| `DevAuth: true` | 无 JWT 时可用 `X-Tenant-Id` / `X-User-Id`(仅本地) |
|
||
| 带了错误 Bearer | 一律 401(即使 DevAuth=true) |
|
||
|
||
生产请更换 `Auth.AccessSecret` / `IssueSecret`,并关掉 `DevAuth`。
|
||
|
||
## 元数据落库
|
||
|
||
启用 DataSource 后自动迁移并使用 `platform_meta.tenant_apps`:
|
||
|
||
- 存 blueprint / ddl / endpoints / status
|
||
- 进程重启后仍可按 slug 解析动态 CRUD
|
||
- 无库时回退内存 `MemoryStore`
|