Compare commits
17
Commits
v1.0.2
..
ext-v1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec9718466d | ||
|
|
c5bb08022a | ||
|
|
b710745e88 | ||
|
|
5cf84fe9b7 | ||
|
|
27991a7822 | ||
|
|
d1e2f0ced0 | ||
|
|
e848306187 | ||
|
|
2ee9d7e66d | ||
|
|
e31585842d | ||
|
|
f068b4ab75 | ||
|
|
26e29409a9 | ||
|
|
4923129d10 | ||
|
|
8ab55e2193 | ||
|
|
9bc7ef3b5a | ||
|
|
9431ad835c | ||
|
|
2984d7d4f8 | ||
|
|
6324036e41 |
No files matched your search
@@ -1,16 +1,14 @@
|
||||
---
|
||||
name: marqdo-release
|
||||
description: >-
|
||||
Run a full Marqdo product release: detect latest version and ask the user for
|
||||
the next tag, update CHANGELOG/README/skills/public docs, bump Cargo versions,
|
||||
sync VS Code extension on branch vscode-extension when needed, build or
|
||||
trigger install assets (Windows CLI + **required Linux native/CLI zips**,
|
||||
stdlib, ext, public zip, VSIX), push via origin or
|
||||
https://proxy.cflmy.top/github.com/cflmy/marqdo.git, publish GitHub Release
|
||||
with detailed notes, and recover from network failures via that proxy or HK
|
||||
SSH jump (scripts/push-via-hk-jump.py). Use when the user asks to release,
|
||||
cut a version, publish vX.Y.Z, ship a GitHub release, 发版, 发布新版本, or tag
|
||||
marqdo.
|
||||
Run a Marqdo product release: detect latest version and ask the user for the
|
||||
next tag (or confirm mode), update CHANGELOG/README/skills/public docs, bump
|
||||
Cargo and/or ext/VERSION, sync VS Code extension when needed, publish GitHub
|
||||
Release assets, upload extension packs to Cloudflare R2 (ext.marqdo.com) and
|
||||
GitHub, ship Ubuntu CLI via PPA (scripts/ppa-ship.sh), and print manual
|
||||
commands when sudo/GPG/network steps need the user. Use when the user asks to
|
||||
release, cut a version, publish vX.Y.Z, ext-only pack, PPA, 发版, 发布新版本,
|
||||
or tag marqdo.
|
||||
---
|
||||
|
||||
# Marqdo release
|
||||
@@ -19,166 +17,193 @@ Canonical release playbook for **cflmy/marqdo**. Read this skill **before** tagg
|
||||
|
||||
## Hard rules
|
||||
|
||||
1. **Never invent the next version.** Detect current versions → **stop and ask the user** for the next SemVer (`X.Y.Z`). Do not proceed until they confirm.
|
||||
1. **Never invent the next version.** Detect current versions → **stop and ask the user** for the next SemVer (`X.Y.Z`) and **release mode** if unclear. Do not proceed until they confirm (unless they already stated both in the same message).
|
||||
2. **Never force-push** `main` / tags. **Never** `--no-verify` unless the user explicitly orders it.
|
||||
3. **Never commit `vscode-extension/` on `main`** (gitignored). Extension source lives only on branch **`vscode-extension`**. See `doc/design/vscode-extension-commit.md`.
|
||||
4. **Do not release from a dirty tree** (except intentional release commits you create in this flow).
|
||||
5. **Tag format** is always `vX.Y.Z` matching root `Cargo.toml` `version = "X.Y.Z"`.
|
||||
6. Prefer **tag push → GitHub Actions** (`.github/workflows/release.yml`) for **both** Windows and **Linux** install assets. Local packaging is fallback / verification.
|
||||
7. After network errors: apply [reference.md § Proxy](reference.md). **First try** `https://proxy.cflmy.top/github.com/cflmy/marqdo.git` (this clone’s usual `origin`). Retry; do not silently skip uploads. If the reverse-proxy still drops long pushes, use **HK jump** (`hk.cflmy.de`) via `scripts/push-via-hk-jump.py` — see reference § **Proxy** / **HK SSH jump**.
|
||||
8. **Do not ship a GitHub Release without the Linux extension/native zip.** Job `linux` must attach `marqdo-VER-native-x86_64-unknown-linux-gnu.zip` (and the Linux CLI bundle). Missing Linux packages = release incomplete.
|
||||
5. **Tag format:** CLI/full → `vX.Y.Z` matching root `Cargo.toml`. Ext-only → `ext-vX.Y.Z` matching `ext/VERSION` (Cargo may stay unchanged).
|
||||
6. Prefer **tag push → GitHub Actions** (`.github/workflows/release.yml`) for Windows/Linux install assets. Local packaging is fallback / verification.
|
||||
7. After network errors: apply [reference.md § Proxy](reference.md). **First try** `https://proxy.cflmy.top/github.com/cflmy/marqdo.git`. Retry; do not silently skip uploads. If needed: **HK jump** (`scripts/push-via-hk-jump.py`).
|
||||
8. **Do not ship a full GitHub Release without the Linux extension/native zip** when the mode includes ext packs. Missing Linux packages = incomplete for that mode.
|
||||
9. **Extension packs always publish to both** Cloudflare R2 (`https://ext.marqdo.com` via `scripts/upload-ext-r2.py`) **and** GitHub (Release assets / `ext-v*` tag). Credentials only in `~/.marqdo/r2.env` / CI — **never** commit. See [ext-cdn.md](../../../doc/design/ext-cdn.md).
|
||||
10. **CLI Ubuntu package = PPA** (`ppa:cflmy/marqdo`) via `./scripts/ppa-ship.sh` / `ppa-build-source.sh`. GitHub zips remain for Windows and non-apt Linux.
|
||||
11. **When a step needs the user** (sudo password, GPG passphrase, Launchpad UI, interactive `dput`): **stop, state what failed, and give the exact commands** to run in their terminal. Do not pretend the step succeeded.
|
||||
|
||||
## Defaults (after version is confirmed)
|
||||
## Release modes
|
||||
|
||||
Unless the user overrides, apply these automatically — **do not re-ask**:
|
||||
Ask (or take from user message) which mode:
|
||||
|
||||
| Mode | Bumps | Git tag | GitHub assets | R2 CDN | Ubuntu PPA |
|
||||
|------|-------|---------|---------------|--------|------------|
|
||||
| **full** (default) | `Cargo.toml` + `ext/VERSION` (same SemVer) | `vX.Y.Z` | CLI + ext + native + VSIX + … | yes | yes |
|
||||
| **cli** | `Cargo.toml` only | `vX.Y.Z` | CLI (+ optional attach prior ext) | no (unless user asks) | yes |
|
||||
| **ext** | `ext/VERSION` only | `ext-vX.Y.Z` (or attach to latest CLI release) | ext.zip + native-*.zip | **required** | no |
|
||||
|
||||
Defaults after version+mode confirmed — **do not re-ask**:
|
||||
|
||||
| Setting | Default |
|
||||
|---------|---------|
|
||||
| Sync / bump `vscode-extension` to same `VER` | **yes** |
|
||||
| Push `main`, create annotated tag, push tag immediately (trigger CI) | **yes** |
|
||||
| Release notes language | **中英双语** (Chinese first, then English section, or paired ZH/EN under each heading) |
|
||||
| Skip VSIX | **no** |
|
||||
| Docs-only release | **no** (full asset pipeline via CI) |
|
||||
| Sync / bump `vscode-extension` to same `VER` | **yes** for full/cli; **no** for ext-only |
|
||||
| Push `main`, create annotated tag, push tag (trigger CI) | **yes** for full/cli |
|
||||
| Ext packs → R2 **and** GitHub | **yes** whenever ext ships |
|
||||
| Ubuntu PPA upload after CLI tag | **yes** for full/cli |
|
||||
| Release notes language | **中英双语** |
|
||||
| Deploy gh-pages after release | **no** (only if user asks) |
|
||||
|
||||
Phase 0 asks **only** for the next version (and optional overrides). Example ask:
|
||||
Phase 0 ask example:
|
||||
|
||||
> 当前最新是 **vA.B.C**。请指定下一个版本号(如 `0.3.2`)。其余按默认:同步扩展、立刻打 tag 发布、中英双语说明。
|
||||
> 当前最新 CLI **vA.B.C**,扩展包 **E.F.G**。请指定下一个版本号与模式(`full` / `cli` / `ext`,如 `1.0.3 full`)。
|
||||
|
||||
## Phase 0 — Detect versions (mandatory stop)
|
||||
|
||||
Run in repo root (parallel OK):
|
||||
|
||||
```bash
|
||||
python3 .cursor/skills/marqdo-release/scripts/detect_version.py
|
||||
# or: rg / git tag / gh release list — see reference.md
|
||||
```
|
||||
|
||||
Report briefly, then **ask only for the next SemVer** (defaults above). Wait for confirmation. Normalize to `VER=X.Y.Z` and `TAG=vX.Y.Z`.
|
||||
Report briefly, then **ask for SemVer + mode** (unless already given). Normalize `VER` / `TAG`.
|
||||
|
||||
## Phase 1 — Preflight
|
||||
|
||||
Copy and track:
|
||||
|
||||
```
|
||||
Release progress:
|
||||
- [ ] Phase 0: version confirmed by user (TAG=…)
|
||||
- [ ] Phase 0: version + mode confirmed
|
||||
- [ ] Phase 1: preflight green
|
||||
- [ ] Phase 2: docs + version bumps committed
|
||||
- [ ] Phase 3: extension branch (if needed)
|
||||
- [ ] Phase 3: extension branch (full/cli)
|
||||
- [ ] Phase 4: main pushed; tag created & pushed
|
||||
- [ ] Phase 5: CI / assets / release notes verified
|
||||
- [ ] Phase 6: post-release checks
|
||||
- [ ] Phase 5: CI / GitHub assets / notes
|
||||
- [ ] Phase 6: R2 upload (if ext ships)
|
||||
- [ ] Phase 7: Ubuntu PPA (full/cli)
|
||||
- [ ] Phase 8: post-release checks
|
||||
```
|
||||
|
||||
Checks:
|
||||
|
||||
1. `git status` clean or only release WIP you control.
|
||||
2. `git checkout main && git pull --ff-only origin main`.
|
||||
3. Quick gate: `cargo test --test gold structure_hello` (and any release-critical tests the user named). Full gold suite if time allows.
|
||||
4. `CHANGELOG.md` has meaningful **Unreleased** notes; if empty, draft from `git log vLAST..HEAD --oneline` and confirm with user.
|
||||
5. Confirm no secrets in the commit (`*.env`, credentials).
|
||||
Checks: clean tree; `main` ff-only; smoke tests; CHANGELOG Unreleased; no secrets.
|
||||
|
||||
## Phase 2 — Docs & version bumps (main)
|
||||
|
||||
Bump **in lockstep**:
|
||||
|
||||
| File | Change |
|
||||
|------|--------|
|
||||
| `Cargo.toml` | `version = "VER"` |
|
||||
| `crates/marqdo-wasm/Cargo.toml` | same `VER` |
|
||||
| `CHANGELOG.md` | Move `## Unreleased` → `## vVER — YYYY-MM-DD`; leave fresh empty `## Unreleased` |
|
||||
| `README.md` | 「现状 / 如何使用最新」version strings, checkout tag, release URL |
|
||||
| `.cursor/skills/marqdo/SKILL.md` | Version-sensitive bullets if any (WASM/web status); keep rules accurate |
|
||||
| `public/**` | User-facing tutorials if features shipped (welcome, features/*, extensions). Prefer small factual updates over rewrites |
|
||||
| `doc/roadmap/*.md` | Mark shipped waves done if this release closes them |
|
||||
| `.github/workflows/release.yml` | Update **Highlights** body template to this release (CI still uses hardcoded blurb—keep in sync) |
|
||||
|
||||
Public HTML: regenerate before packaging / pages deploy:
|
||||
| File | full | cli | ext |
|
||||
|------|------|-----|-----|
|
||||
| `Cargo.toml` + `crates/marqdo-wasm/Cargo.toml` | bump | bump | — |
|
||||
| `ext/VERSION` | bump | — | bump |
|
||||
| `CHANGELOG.md` | `## vVER` | `## vVER` | `## Ext pack vVER` |
|
||||
| `README.md` / `public/**` install + download table | update | update | update ext notes |
|
||||
| `.cursor/skills/marqdo/SKILL.md` | if surface changed | same | rare |
|
||||
| `.github/workflows/release.yml` Highlights stub | yes | yes | — |
|
||||
|
||||
```bash
|
||||
./scripts/build-public.sh
|
||||
# outputs under public/ (gitignored HTML); used by release zip / gh-pages
|
||||
./scripts/build-public.sh # when public/ sources changed
|
||||
```
|
||||
|
||||
Commit on `main` (HEREDOC message), e.g.:
|
||||
|
||||
```text
|
||||
release: vVER — <one-line highlight>
|
||||
|
||||
EOF
|
||||
```
|
||||
|
||||
Push `main` (this repo’s `origin` is usually the reverse proxy):
|
||||
Commit + push `main` (proxy origin). On push failure → reference § Proxy; if still blocked, **print** for the user:
|
||||
|
||||
```bash
|
||||
# origin typically: https://proxy.cflmy.top/github.com/cflmy/marqdo.git
|
||||
git push origin main
|
||||
# one-shot if origin still points at github.com:
|
||||
git push https://proxy.cflmy.top/github.com/cflmy/marqdo.git HEAD:main
|
||||
# or: python3 scripts/push-via-hk-jump.py
|
||||
```
|
||||
|
||||
If that times out: [reference § HK SSH jump](reference.md).
|
||||
## Phase 3 — VS Code / Cursor extension (full/cli only)
|
||||
|
||||
## Phase 3 — VS Code / Cursor extension
|
||||
Default: bump `vscode-extension` package.json to `VER`, compile, push branch `vscode-extension`, return to `main`.
|
||||
|
||||
**Default: always** bump extension to `VER` on branch `vscode-extension` (skip only if user said so).
|
||||
|
||||
1. `git fetch origin vscode-extension`
|
||||
2. Work on `vscode-extension` branch (not main):
|
||||
- Bump `vscode-extension/package.json` `version` to `VER`.
|
||||
- Update `engines` / CLI version hints in extension README if present.
|
||||
- `npm ci && npm run compile`
|
||||
3. Commit & `git push origin vscode-extension`.
|
||||
4. Return to `main`. **Do not** add `vscode-extension/` to main.
|
||||
|
||||
## Phase 4 — Tag & trigger CI
|
||||
## Phase 4 — Tag & trigger CI (full/cli)
|
||||
|
||||
```bash
|
||||
git tag -a "TAG" -m "TAG"
|
||||
git push origin "TAG"
|
||||
git tag -a "vVER" -m "vVER"
|
||||
git push origin "vVER"
|
||||
```
|
||||
|
||||
This runs `.github/workflows/release.yml`:
|
||||
Ext-only:
|
||||
|
||||
- **windows** — CLI exe/zip, stdlib zip, ext zip, source zip, public zip, VSIX, Windows native zip
|
||||
- **linux** (`needs: windows`) — **required** Linux native plugins zip + Linux CLI bundle (`ext/` + `native/*.so`)
|
||||
```bash
|
||||
git tag -a "ext-vVER" -m "ext-vVER"
|
||||
git push origin "ext-vVER"
|
||||
```
|
||||
|
||||
If tag push fails: [reference.md § Proxy](reference.md) (`proxy.cflmy.top` first), retry once; then HK jump.
|
||||
(or build zips locally / via workflow and `gh release upload`).
|
||||
|
||||
## Phase 5 — Release notes & assets
|
||||
If tag push needs the user:
|
||||
|
||||
1. Wait for Actions success: `gh run list --workflow=Release --limit 3` / open the run URL. **Both** `windows` and `linux` jobs must be green.
|
||||
2. Ensure GitHub Release for `TAG` exists with assets (workflow uses `softprops/action-gh-release`).
|
||||
3. Confirm Linux assets exist: `marqdo-VER-native-x86_64-unknown-linux-gnu.zip` and `marqdo-VER-x86_64-unknown-linux-gnu.zip`. If missing, do **not** call the release done — re-run / upload.
|
||||
4. **Rewrite release body** (workflow body is a stub) with **中英双语** — see [reference.md § Notes template](reference.md). `gh release edit TAG --notes-file …`.
|
||||
4. Include: Highlights, Breaking, Install (`ext add`, plugins build), Downloads table, WASM (`marqdo wasm build`), links to CHANGELOG/README, extension branch note.
|
||||
5. Optional Linux local extras (not required if CI green): `marqdo wasm build` smoke; document in notes that wasm is built from source via CLI.
|
||||
```bash
|
||||
git push https://proxy.cflmy.top/github.com/cflmy/marqdo.git vVER
|
||||
```
|
||||
|
||||
Local Windows fallback: `scripts/release-full.ps1 -Tag TAG -Upload` (see script header).
|
||||
## Phase 5 — GitHub Release notes & assets
|
||||
|
||||
## Phase 6 — Post-release
|
||||
1. Wait for Actions (full/cli): both `windows` and `linux` green when ext natives required.
|
||||
2. Rewrite release body 中英双语 — [reference.md § Notes](reference.md).
|
||||
3. Confirm Linux `marqdo-VER-native-x86_64-unknown-linux-gnu.zip` when mode ships ext.
|
||||
|
||||
1. `gh release view TAG` — confirm assets + notes.
|
||||
2. `git status` clean; `main` synced.
|
||||
3. Tell user: release URL `https://github.com/cflmy/marqdo/releases/tag/TAG`.
|
||||
4. Optional: `./scripts/deploy-public.ps1` / pages workflow if user wants user-site refresh.
|
||||
5. Open fresh `## Unreleased` already done in Phase 2.
|
||||
## Phase 6 — Extension CDN (R2) — required when ext ships
|
||||
|
||||
```bash
|
||||
# credentials: ~/.marqdo/r2.env (never commit)
|
||||
python3 scripts/upload-ext-r2.py --version VER --from-dir dist/
|
||||
curl -fsS https://ext.marqdo.com/latest/VERSION
|
||||
```
|
||||
|
||||
Also ensure the **same** zips are on GitHub (`vVER` or `ext-vVER`). If R2 credentials missing, **tell the user**:
|
||||
|
||||
```bash
|
||||
# put keys in ~/.marqdo/r2.env then:
|
||||
python3 scripts/upload-ext-r2.py --version VER --from-dir dist/
|
||||
```
|
||||
|
||||
## Phase 7 — Ubuntu PPA (full/cli)
|
||||
|
||||
Linux CLI for apt users. After `Cargo.toml` is at `VER` and `debian/changelog` has a matching `VER-1ppaN` stanza:
|
||||
|
||||
```bash
|
||||
./scripts/ppa-ship.sh noble
|
||||
```
|
||||
|
||||
Packaging notes / noble Cargo.lock v4: [ubuntu-ppa.md](../../../doc/design/ubuntu-ppa.md).
|
||||
|
||||
**If the agent cannot sudo / sign / dput**, print exactly:
|
||||
|
||||
```bash
|
||||
cd ~/work/marqdo
|
||||
./scripts/ppa-ship.sh noble
|
||||
# or build then upload:
|
||||
./scripts/ppa-build-source.sh noble
|
||||
dput ppa:cflmy/marqdo ../build-area/marqdo_*~noble_source.changes
|
||||
```
|
||||
|
||||
Watch: https://launchpad.net/~cflmy/+archive/ubuntu/marqdo/+builds
|
||||
On Launchpad failure: read the build log, fix `debian/` or bundle script, bump `1ppaN`, re-upload. Tell the user the new `dput` command.
|
||||
|
||||
## Phase 8 — Post-release
|
||||
|
||||
1. `gh release view TAG` — assets + notes.
|
||||
2. CDN `https://ext.marqdo.com/vVER/` (if ext shipped).
|
||||
3. PPA build green (if cli shipped).
|
||||
4. Tell user URLs: GitHub release, CDN, PPA install one-liner.
|
||||
5. Optional gh-pages only if asked.
|
||||
|
||||
## Extension-only release (mode=ext)
|
||||
|
||||
1. User confirms **ext** SemVer (may differ from CLI).
|
||||
2. Bump `ext/VERSION`; CHANGELOG → `## Ext pack vVER`.
|
||||
3. Build/package `marqdo-VER-ext.zip` + `marqdo-VER-native-*.zip` (CI or local).
|
||||
4. Upload to **R2 and GitHub** (required both).
|
||||
5. Do **not** retag CLI / do **not** PPA unless user also asked for cli.
|
||||
|
||||
## Failure matrix (short)
|
||||
|
||||
| Failure | Action |
|
||||
|---------|--------|
|
||||
| `gh` / git TLS or Clash `7890` refused | Unset bad proxy; `required_permissions: ["all"]`; see [reference § Proxy](reference.md) |
|
||||
| GitHub blocked | **`origin` via proxy.cflmy.top** (see below), then HK jump |
|
||||
| Tag exists | Stop; ask user to bump or delete tag (no force on shared tags without explicit order) |
|
||||
| CI red | Fix on main, move tag only if user explicitly allows delete+re-push tag |
|
||||
| VSIX missing | Fetch `vscode-extension`, build locally, `gh release upload TAG dist/*.vsix` |
|
||||
| Linux native/CLI zip missing | Incomplete release — fix `linux` job or `gh release upload`; do not skip |
|
||||
| `gh` / git TLS or Clash `7890` refused | Unset bad proxy; see [reference § Proxy](reference.md) |
|
||||
| GitHub blocked | proxy.cflmy.top, then HK jump; **print commands** if agent cannot push |
|
||||
| Tag exists | Stop; ask user |
|
||||
| CI red | Fix on main; retag only if user allows |
|
||||
| R2 upload fails | Print `upload-ext-r2.py` command; do not skip silently |
|
||||
| PPA / sudo / GPG | Print `./scripts/ppa-ship.sh noble` (or `dput …`); do not skip silently |
|
||||
| Launchpad `Cargo.lock` v4 / old cargo | Ensure bundled `third_party/rust` via current `ppa-build-source.sh`; bump `1ppaN` |
|
||||
| Linux native zip missing (full/ext) | Incomplete — fix linux job or upload |
|
||||
|
||||
## Do not
|
||||
|
||||
- Ship without user-confirmed version.
|
||||
- Hardcode release highlights only in Actions YAML and forget CHANGELOG.
|
||||
- Commit `target/`, `dist/`, `*.wasm`, or `public/**/*.html` unless project policy changes.
|
||||
- Ship without user-confirmed version (and mode when ambiguous).
|
||||
- Ship ext packs to only one of R2 / GitHub.
|
||||
- Call a full/cli release done without attempting PPA **or** giving the user the PPA commands.
|
||||
- Commit `target/`, `dist/`, `*.wasm`, `third_party/rust/`, or `public/**/*.html` unless policy changes.
|
||||
- Merge extension tree into `main`.
|
||||
@@ -19,7 +19,7 @@ Canonical design (repo): [`markdown-mapping-v0.3.md`](../../doc/design/markdown-
|
||||
|
||||
**Authoring / library craft** (GFM-first, no json glue, how to develop Marqdo): use **[marqdo-dev](../marqdo-dev/SKILL.md)** whenever writing or refactoring `.mq.md`, `lib/*`, or `ext/*`.
|
||||
|
||||
**Product release** (tag, CHANGELOG, README, public docs, VSIX, GitHub assets): use [marqdo-release](../marqdo-release/SKILL.md) — detect latest version, **ask the user for the next SemVer**, then follow that playbook. Do not invent a version number.
|
||||
**Product release** (tag, CHANGELOG, README, public docs, VSIX, GitHub assets, **ext → R2+GitHub**, **CLI → Ubuntu PPA**): use [marqdo-release](../marqdo-release/SKILL.md) — detect versions, **ask for SemVer + mode (`full` / `cli` / `ext`)**, then follow that playbook. Ext-only releases are allowed. If sudo/GPG/PPA steps need the user, print the exact commands.
|
||||
|
||||
## Hard rules (do not violate)
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Ext CDN / R2 (Cloudflare) — copy to ~/.marqdo/r2.env and chmod 600
|
||||
# Never commit real keys. Public downloads: https://ext.marqdo.com
|
||||
|
||||
R2_ACCOUNT_ID=
|
||||
R2_BUCKET=marqdo
|
||||
R2_ENDPOINT=https://<accountid>.r2.cloudflarestorage.com
|
||||
R2_ACCESS_KEY_ID=
|
||||
R2_SECRET_ACCESS_KEY=
|
||||
R2_PUBLIC_BASE=https://ext.marqdo.com
|
||||
@@ -124,8 +124,8 @@ jobs:
|
||||
## Marqdo ${{ github.ref_name }}
|
||||
|
||||
### 亮点 / Highlights
|
||||
- **v1.0.2**:表驱动页面引言 `compose_intro` / `引言装配`;web-site Demo 与后台门禁加固
|
||||
- **v1.0.2**: table-bound page intro (`compose_intro`); web-site demo + desk auth polish
|
||||
- **v1.0.3**:Ubuntu PPA + 扩展 CDN(ext.marqdo.com);CLI/扩展可分发;noble 捆绑 Rust 工具链
|
||||
- **v1.0.3**: Ubuntu PPA + extension CDN; independent CLI/ext packs; bundled Rust for noble
|
||||
- **v1.0.1**:修复 Go `libweb` 动态路由 `{param}` 查询代入、详情 Markdown、CSS 数字误报
|
||||
- **Linux 包必出**:`*-native-x86_64-unknown-linux-gnu.zip` + Linux CLI bundle
|
||||
- Details: [CHANGELOG](https://github.com/cflmy/marqdo/blob/main/CHANGELOG.md) · [mapping v0.3](https://github.com/cflmy/marqdo/blob/main/doc/design/markdown-mapping-v0.3.md)
|
||||
|
||||
+6
-1
@@ -24,7 +24,8 @@
|
||||
.marqdo/
|
||||
**/.marqdo/
|
||||
.marqdo-ext/
|
||||
.env
|
||||
/vendor/
|
||||
/.cargo/config.toml
|
||||
|
||||
# Generated HTML beside tracked public/*.mq.md (sources stay in git)
|
||||
/public/index.html
|
||||
@@ -77,5 +78,9 @@ plugins/*/tmp/
|
||||
# Go web plugin c-shared build output (./scripts/build-web-plugin.sh)
|
||||
/plugins/web/build/
|
||||
|
||||
# PPA build: bundled Rust toolchain (packed into .orig by ppa-build-source.sh only)
|
||||
/third_party/rust/
|
||||
/third_party/
|
||||
|
||||
# ephemeral sqlite / test artifacts
|
||||
tests/ext/:memory:
|
||||
@@ -8,6 +8,59 @@
|
||||
|
||||
### Changed
|
||||
|
||||
## Ext pack v1.0.4 — 2026-09-17
|
||||
|
||||
### Highlights
|
||||
|
||||
**ext/web 能力面扩展(CLI 仍为 v1.0.3)**:可配置 RBAC、租户作用域、WS `on_message`/presence、原生 SPA 内存路由、协同记事本 MVP。安装:
|
||||
|
||||
```bash
|
||||
marqdo ext add web # CDN latest → 1.0.4
|
||||
```
|
||||
|
||||
### Added
|
||||
- **RBAC**:五表(`web_permissions` / `web_roles` / `web_role_permissions` / `web_users` / `web_user_roles`);`app.enable_rbac`、`app.gate permissions=`、`rbac.can`、注册页、`/_rbac/*` 管理 API/desk。
|
||||
- **Tenant**:`app.tenant`(path/header/subdomain)+ `tenant_scope` 自动过滤/盖戳。
|
||||
- **WebSocket G-WS2**:`route_ws` 支持 `on_message` 钩子、动态 room、`presence` 通道。
|
||||
- **Native SPA**:内存 `store_set` / `spa_goto` 与全页 WASM SPA 样站。
|
||||
- **Collab MVP**:LWW 文档协议(`DOC\t`)双端协同记事本样站。
|
||||
|
||||
### Changed
|
||||
- 扩展包 SemVer **1.0.4**(`ext/VERSION`);与 CLI **v1.0.3** 可独立安装。
|
||||
|
||||
## v1.0.3 — 2026-09-17
|
||||
|
||||
### Highlights
|
||||
|
||||
**分发渠道齐备**:Ubuntu PPA 装 CLI;扩展包走 CDN(`ext.marqdo.com`)与 GitHub;CLI 与扩展可分开发版。本版 CLI 与扩展包 **同步为 1.0.3**。
|
||||
|
||||
**语法**:未绑定的 GFM 表格是可丢弃的集合值,不再报 `unrecognized statement`(文档页可直接写安装表)。
|
||||
|
||||
```bash
|
||||
# Ubuntu
|
||||
sudo add-apt-repository ppa:cflmy/marqdo
|
||||
sudo apt update && sudo apt install marqdo
|
||||
marqdo ext add web
|
||||
|
||||
# 或源码 / Releases
|
||||
git checkout v1.0.3 && cargo build --release
|
||||
```
|
||||
|
||||
### Added
|
||||
- Ubuntu PPA packaging: `debian/` + `scripts/ppa-ship.sh` (一键装工具/打包/dput) + `ppa-build-source.sh` + [ubuntu-ppa.md](doc/design/ubuntu-ppa.md).
|
||||
- **Extension CDN** (`https://ext.marqdo.com` / Cloudflare R2):`ext add` 下载顺序改为 CDN → GitHub → proxy;独立包版本见 `ext/VERSION` 与 CDN `latest/VERSION`;上传脚本 `scripts/upload-ext-r2.py`(凭据 `~/.marqdo/r2.env`)。设计见 [ext-cdn.md](doc/design/ext-cdn.md)。
|
||||
- `ext add` 下载:GitHub 失败后自动回退 `https://proxy.cflmy.top/github.com/…`;可用 `MARQDO_EXT_DOWNLOAD_BASE` 指定首选镜像;加长读写超时。
|
||||
- 用户文档(README / `public/00-欢迎` / `features/05-扩展`)标明安装渠道:Ubuntu PPA、GitHub Releases、源码;扩展走 CDN。
|
||||
|
||||
### Fixed
|
||||
- `ext/` 导入默认搜索 `~/.marqdo/ext`(与 `marqdo ext add` 安装根一致),无需再手动 `export MARQDO_EXT`。
|
||||
- PPA/noble:捆绑 `third_party/rust`(现代 cargo)以构建 Cargo.lock v4;修复首次 Launchpad 构建失败(distro cargo 1.75)。
|
||||
- 未绑定的 GFM 表格不再报 `unrecognized statement`:作为集合值求值后丢弃(文档中可写表)。
|
||||
|
||||
### Changed
|
||||
- 允许 **CLI 与扩展包分开发版**:扩展包 SemVer 可与 `Cargo.toml` 不同(`MARQDO_EXT_VERSION` / `ext/VERSION` / CDN latest)。
|
||||
- 发版 skill:支持 `full` / `cli` / `ext`;扩展必传 R2+GitHub;CLI 走 PPA;需用户操作时打印确切命令。
|
||||
|
||||
## v1.0.2 — 2026-09-16
|
||||
|
||||
### Highlights
|
||||
|
||||
Generated
+2
-2
@@ -572,7 +572,7 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
|
||||
|
||||
[[package]]
|
||||
name = "marqdo"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_cmd",
|
||||
@@ -603,7 +603,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
dependencies = [
|
||||
"marqdo",
|
||||
"serde_json",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo — Markdown markers as an interpreted programming language"
|
||||
|
||||
@@ -98,7 +98,7 @@ marqdo catalog public -o .marqdo
|
||||
|
||||
---
|
||||
|
||||
## 现状(v1.0.2)
|
||||
## 现状(v1.0.3)
|
||||
|
||||
- 映射与解释器:**v0.3 语法宪法**(`**` 代码 / `*` 返回 / 叙述声明);Phase I 树遍历 + 字节码后端;金样例在 `tests/` 与 `tests/markup-v03/`
|
||||
- **对象**:`#` = 类型/构造,`##`+ = 函数/方法;见 [objects.md](doc/design/objects.md)
|
||||
@@ -120,12 +120,22 @@ marqdo catalog public -o .marqdo
|
||||
- **浏览器 Marqdo(WASM)**:`marqdo wasm build` + 官方 bridge 自启(作者零业务 JS;桥内可含列表/路由/storage/ws/文件/Canvas/音频/Observer/拖放);`lib/browser` + GFM;`web.client_embed`;示例 [browser-hello](examples/browser-hello/) · [browser-app](examples/browser-app/) · [browser-media](examples/browser-media/) · [web-client-site](examples/web-client-site/)([ADR 0002](doc/adr/0002-browser-marqdo-wasm.md) · [D](doc/roadmap/browser-wasm-d.md) · [E](doc/roadmap/browser-wasm-e.md) · [F](doc/roadmap/browser-wasm-f.md))
|
||||
- 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) · [ADR 0002 — 浏览器 WASM](doc/adr/0002-browser-marqdo-wasm.md)(C0–C5 完结,见 [roadmap/browser-wasm.md](doc/roadmap/browser-wasm.md))· [ADR 0003 — 异步效应](doc/adr/0003-browser-async-effects.md)
|
||||
|
||||
### 如何使用最新 Marqdo(v1.0.2)
|
||||
### 如何使用最新 Marqdo(v1.0.3)
|
||||
|
||||
**安装解释器(任选其一)**
|
||||
|
||||
| 方式 | 适用 | 命令 / 链接 |
|
||||
|------|------|-------------|
|
||||
| **Ubuntu PPA** | Ubuntu 24.04+(`noble` 等) | `sudo add-apt-repository ppa:cflmy/marqdo && sudo apt update && sudo apt install marqdo` |
|
||||
| **GitHub Releases** | Windows / Linux 预编译 zip | https://github.com/cflmy/marqdo/releases |
|
||||
| **源码** | 需本机 Rust | 见下方 `git clone` + `cargo build --release` |
|
||||
|
||||
**安装官方扩展**(与 CLI 可分开发版):`marqdo ext add …` 默认下载顺序为 **CDN** [`https://ext.marqdo.com`](https://ext.marqdo.com) → GitHub Releases → [`proxy.cflmy.top`](https://proxy.cflmy.top) 镜像。
|
||||
|
||||
```bash
|
||||
# 1) 源码安装(跟 tag 或 main)
|
||||
git clone https://github.com/cflmy/marqdo.git && cd marqdo
|
||||
git checkout v1.0.2 # 或留在 main
|
||||
git checkout v1.0.3 # 或留在 main
|
||||
cargo build --release
|
||||
export PATH="$PWD/target/release:$PATH"
|
||||
marqdo version
|
||||
@@ -138,7 +148,7 @@ marqdo view public --no-open
|
||||
marqdo debug public --no-open
|
||||
marqdo catalog public -o .marqdo
|
||||
|
||||
# 3) 安装官方扩展(Release 用户:无需本机 Rust/Go)
|
||||
# 3) 安装官方扩展(Release / CDN 用户:无需本机 Rust/Go)
|
||||
marqdo ext add web # 或:网页 — 自动下载 L1 + 预编译 native(Go libweb)
|
||||
marqdo ext add agent # 或:智能体
|
||||
marqdo ext add quantum # 或:量子
|
||||
@@ -159,7 +169,7 @@ marqdo wasm build
|
||||
# → dist/wasm/ … 见 examples/browser-hello/
|
||||
|
||||
# 6) Releases:Windows exe/zip/vsix;Linux CLI zip + native `.so` zip
|
||||
# https://github.com/cflmy/marqdo/releases/tag/v1.0.2
|
||||
# https://github.com/cflmy/marqdo/releases/tag/v1.0.3
|
||||
```
|
||||
|
||||
开发期也可用 `cargo run -- …` 代替已安装的 `marqdo`:
|
||||
@@ -171,7 +181,7 @@ cargo run -- view output public -o public
|
||||
powershell -File ./scripts/build-public.ps1
|
||||
```
|
||||
|
||||
**发布包(GitHub Releases)**:单独二进制 **已内置**官方 `lib/`(`import …:lib/…` 可直接导入)。仍提供带 `lib/` 的 zip 便于覆盖或离线分发。**扩展库**用 `marqdo ext add …` 安装(见 [CHANGELOG](CHANGELOG.md) 与 [ext-cli.md](doc/design/ext-cli.md));原生 `.so`/`.dll` 需本地编译或从带 `native/` 的安装目录解析。
|
||||
**发布包**:GitHub Releases 的单独二进制 **已内置**官方 `lib/`。Ubuntu 用户优先 **PPA** 装 CLI。**扩展库**用 `marqdo ext add …`(CDN → GitHub);设计见 [ext-cdn.md](doc/design/ext-cdn.md) · [ubuntu-ppa.md](doc/design/ubuntu-ppa.md) · [ext-cli.md](doc/design/ext-cli.md)。
|
||||
|
||||
文档:用户站 [public/](public/) · 设计 [doc/](doc/) · OKF / catalog [catalog-cli.md](doc/design/catalog-cli.md) · 调试 [view-debug.md](doc/design/view-debug.md) · 变更 [CHANGELOG.md](CHANGELOG.md)
|
||||
---
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo interpreter as WebAssembly (browser host bridge)"
|
||||
|
||||
@@ -921,8 +921,23 @@ function runIntervalEffect(exports, spec, onError) {
|
||||
intervalHandles.set(id, handle);
|
||||
}
|
||||
|
||||
const memoryKV = new Map();
|
||||
|
||||
function storageStore(scope) {
|
||||
if (scope === "cookie") return null;
|
||||
if (scope === "memory" || scope === "mem" || scope === "内存") {
|
||||
return {
|
||||
getItem(key) {
|
||||
return memoryKV.has(key) ? memoryKV.get(key) : null;
|
||||
},
|
||||
setItem(key, value) {
|
||||
memoryKV.set(key, String(value));
|
||||
},
|
||||
removeItem(key) {
|
||||
memoryKV.delete(key);
|
||||
},
|
||||
};
|
||||
}
|
||||
if (typeof localStorage === "undefined") return null;
|
||||
return scope === "session" ? sessionStorage : localStorage;
|
||||
}
|
||||
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
Marqdo for Ubuntu (PPA)
|
||||
=======================
|
||||
|
||||
This package installs the marqdo CLI (/usr/bin/marqdo).
|
||||
|
||||
Official language extensions are not bundled. After install:
|
||||
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
|
||||
Extension packs download from https://ext.marqdo.com (then GitHub / proxy).
|
||||
|
||||
Install this package from:
|
||||
|
||||
sudo add-apt-repository ppa:cflmy/marqdo
|
||||
sudo apt update
|
||||
sudo apt install marqdo
|
||||
|
||||
Also available: GitHub Releases (Windows/Linux zips) and building from source.
|
||||
|
||||
Documentation: https://github.com/cflmy/marqdo
|
||||
PPA notes: doc/design/ubuntu-ppa.md in the upstream repository.
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
marqdo (1.0.3-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.0.3: PPA + ext CDN; unbound GFM tables; bundled Rust for noble.
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Thu, 17 Sep 2026 10:35:00 +0800
|
||||
|
||||
marqdo (1.0.2-1ppa2) noble; urgency=medium
|
||||
|
||||
* Bundle Rust toolchain in orig (third_party/rust) so noble can build
|
||||
Cargo.lock v4 offline; drop distro cargo/rustc Build-Depends.
|
||||
* Architecture: amd64 (bundled toolchain is x86_64).
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Thu, 17 Sep 2026 10:20:00 +0800
|
||||
|
||||
marqdo (1.0.2-1ppa1) noble; urgency=medium
|
||||
|
||||
* Initial Ubuntu PPA packaging for Marqdo 1.0.2.
|
||||
* CLI only; extensions via "marqdo ext add" / ext.marqdo.com.
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Thu, 17 Sep 2026 09:55:00 +0800
|
||||
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
Source: marqdo
|
||||
Section: devel
|
||||
Priority: optional
|
||||
Maintainer: cflmy <pingan@cflmy.cn>
|
||||
Build-Depends:
|
||||
debhelper-compat (= 13),
|
||||
build-essential,
|
||||
pkg-config,
|
||||
libssl-dev,
|
||||
ca-certificates
|
||||
Standards-Version: 4.7.0
|
||||
Homepage: https://github.com/cflmy/marqdo
|
||||
Vcs-Git: https://github.com/cflmy/marqdo.git
|
||||
Vcs-Browser: https://github.com/cflmy/marqdo
|
||||
Rules-Requires-Root: no
|
||||
|
||||
Package: marqdo
|
||||
Architecture: amd64
|
||||
Depends: ${shlibs:Depends}, ${misc:Depends}, ca-certificates
|
||||
Description: Markdown markers as an interpreted programming language
|
||||
Marqdo treats Markdown markers as executable syntax (code / return /
|
||||
control flow). This package installs the marqdo CLI.
|
||||
.
|
||||
Official extensions (web, quantum, …) are installed separately via
|
||||
"marqdo ext add" and download from https://ext.marqdo.com .
|
||||
.
|
||||
Launchpad builds use a bundled Rust toolchain under third_party/rust
|
||||
(Ubuntu noble cargo is too old for Cargo.lock v4). Rebuilds on newer
|
||||
series may use distro rustc if cargo ≥ 1.78.
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
|
||||
Upstream-Name: marqdo
|
||||
Upstream-Contact: cflmy <pingan@cflmy.cn>
|
||||
Source: https://github.com/cflmy/marqdo
|
||||
|
||||
Files: *
|
||||
Copyright: 2024-2026 cflmy
|
||||
License: Apache-2.0
|
||||
|
||||
License: Apache-2.0
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
.
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
.
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
.
|
||||
On Debian systems, the complete text of the Apache License version 2
|
||||
can be found in "/usr/share/common-licenses/Apache-2.0".
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/make -f
|
||||
|
||||
export DH_VERBOSE = 1
|
||||
# Offline builds on Launchpad: vendored crates + optional bundled Rust toolchain.
|
||||
export CARGO_HOME = $(CURDIR)/debian/cargo_home
|
||||
export CARGO_NET_OFFLINE = true
|
||||
|
||||
# Noble (and older) distro cargo cannot read Cargo.lock v4 / modern crates.
|
||||
# Prefer third_party/rust from the orig tarball when present (see ppa-build-source.sh).
|
||||
BUNDLED_RUST = $(CURDIR)/third_party/rust
|
||||
ifneq ($(wildcard $(BUNDLED_RUST)/bin/cargo),)
|
||||
export PATH := $(BUNDLED_RUST)/bin:$(PATH)
|
||||
endif
|
||||
|
||||
%:
|
||||
dh $@
|
||||
|
||||
override_dh_auto_configure:
|
||||
mkdir -p debian/cargo_home
|
||||
@if [ -x "$(BUNDLED_RUST)/bin/cargo" ]; then \
|
||||
echo "Using bundled Rust: $$($(BUNDLED_RUST)/bin/rustc --version)"; \
|
||||
else \
|
||||
echo "Using system Rust: $$(rustc --version 2>/dev/null || echo missing)"; \
|
||||
fi
|
||||
@if [ -f "$(CURDIR)/.cargo/config.toml" ] || [ -d "$(CURDIR)/vendor" ]; then \
|
||||
echo "Using vendored Cargo dependencies"; \
|
||||
else \
|
||||
echo "WARNING: no vendor/ — online cargo fetch required (not for Launchpad)"; \
|
||||
fi
|
||||
|
||||
override_dh_auto_build:
|
||||
mkdir -p debian/cargo_home
|
||||
@command -v cargo >/dev/null || { echo "error: cargo not found (bundle third_party/rust or install rustc/cargo)" >&2; exit 1; }
|
||||
if [ -d "$(CURDIR)/vendor" ]; then \
|
||||
CARGO_NET_OFFLINE=true cargo build --release --locked --offline \
|
||||
-p marqdo; \
|
||||
else \
|
||||
cargo build --release --locked -p marqdo; \
|
||||
fi
|
||||
|
||||
override_dh_auto_test:
|
||||
@echo "Skipping full test suite on package build (run cargo test in CI)."
|
||||
|
||||
override_dh_auto_install:
|
||||
install -d $(CURDIR)/debian/marqdo/usr/bin
|
||||
install -m 0755 $(CURDIR)/target/release/marqdo \
|
||||
$(CURDIR)/debian/marqdo/usr/bin/marqdo
|
||||
|
||||
override_dh_auto_clean:
|
||||
dh_auto_clean
|
||||
rm -rf debian/cargo_home
|
||||
# keep vendor/ and third_party/rust (part of source package)
|
||||
-cargo clean
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
3.0 (quilt)
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
# Ignore temporary files while editing patches
|
||||
extend-diff-ignore = "^vendor/"
|
||||
@@ -53,6 +53,8 @@
|
||||
| [design/ext-agent.md](design/ext-agent.md) | **官方扩展 `ext/agent`(文档驱动:单步 / 多步工作簿 + 自写回;废弃 thin TOOL 循环)** |
|
||||
| [design/ext-agent-plan.md](design/ext-agent-plan.md) | **`## 多步` / `## plan` 锁定设计**(工作簿文件、subtask 执行、修订循环、`step` 默认写回) |
|
||||
| [design/ext-cli.md](design/ext-cli.md) | **`marqdo ext list/add/remove` 官方扩展安装器** |
|
||||
| [design/ext-cdn.md](design/ext-cdn.md) | **扩展 CDN(R2 / ext.marqdo.com)与分开发版** |
|
||||
| [design/ubuntu-ppa.md](design/ubuntu-ppa.md) | **Ubuntu PPA 打包与上传** |
|
||||
| [design/ext-web.md](design/ext-web.md) | **官方扩展 `ext/web`(页面/库表/绑定/后台;ABI + CLI;设计锁定)** |
|
||||
| [design/ext-web-go-rewrite.md](design/ext-web-go-rewrite.md) | **`plugins/web` Go 全量重写**(多语言 ABI · 网页制备完备 · anlian 验收) |
|
||||
| [adr/0004-web-plugin-go.md](adr/0004-web-plugin-go.md) | **Accepted:web 原生插件实现语言为 Go** |
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
# Extension CDN (Cloudflare R2 → https://ext.marqdo.com)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Status | **Accepted** — download prefer CDN; upload via `scripts/upload-ext-r2.py` |
|
||||
| Date | 2026-09-17 |
|
||||
| Related | [ext-cli.md](ext-cli.md) · [marqdo-release](../../.cursor/skills/marqdo-release/SKILL.md) |
|
||||
|
||||
## Why
|
||||
|
||||
CLI (语法解释器) and official **extension packs** (`ext/` L1 + native plugins) update on different cadences. Users behind unstable GitHub links should get packs from a stable object store first.
|
||||
|
||||
## Public URL layout
|
||||
|
||||
Bucket: `marqdo` · Custom domain: **https://ext.marqdo.com**
|
||||
|
||||
| Key | Meaning |
|
||||
|-----|---------|
|
||||
| `v{VER}/marqdo-{VER}-ext.zip` | L1 sources |
|
||||
| `v{VER}/marqdo-{VER}-native-{triple}.zip` | Prebuilt plugins |
|
||||
| `v{VER}/VERSION` | Pack SemVer echo |
|
||||
| `latest/VERSION` | Recommended pack SemVer (plain text) |
|
||||
| `latest/marqdo-…zip` | Same assets mirrored for convenience |
|
||||
|
||||
`{VER}` is the **extension pack** version (`ext/VERSION`), which **may differ** from CLI `Cargo.toml` version.
|
||||
|
||||
## Client download order (`marqdo ext add`)
|
||||
|
||||
1. `MARQDO_EXT_DOWNLOAD_BASE` (optional override)
|
||||
2. **CDN** `https://ext.marqdo.com/v{VER}/…` (override host with `MARQDO_EXT_CDN`)
|
||||
3. GitHub Releases
|
||||
4. `https://proxy.cflmy.top/github.com/…`
|
||||
|
||||
Pack SemVer resolution (`release_version()`):
|
||||
|
||||
1. `MARQDO_EXT_VERSION`
|
||||
2. CDN `GET …/latest/VERSION` (short timeout)
|
||||
3. Embedded `ext/VERSION` at build time
|
||||
4. CLI `CARGO_PKG_VERSION`
|
||||
|
||||
## Separate ext release (no CLI bump)
|
||||
|
||||
1. Bump `ext/VERSION` (e.g. `1.0.3`) — leave root `Cargo.toml` alone if CLI unchanged.
|
||||
2. Build native zips (CI Linux/Windows jobs or local `scripts/build-web-plugin.sh` + cargo plugins).
|
||||
3. Package `marqdo-{VER}-ext.zip` and `marqdo-{VER}-native-*.zip`.
|
||||
4. Upload **both**:
|
||||
- R2: `python3 scripts/upload-ext-r2.py --version VER --from-dir dist/`
|
||||
- GitHub: attach to a GitHub Release (tag `ext-vVER` **or** reuse latest CLI release assets)
|
||||
5. Confirm `https://ext.marqdo.com/latest/VERSION` and a sample zip URL return 200.
|
||||
|
||||
## Full product release (CLI + ext)
|
||||
|
||||
Keep `ext/VERSION` equal to (or compatible with) CLI SemVer unless documenting a deliberate skew. CI still publishes GitHub assets; **after** CI green, run `upload-ext-r2.py` so CDN is populated (or add a CI step with R2 secrets).
|
||||
|
||||
## Credentials
|
||||
|
||||
| Where | What |
|
||||
|-------|------|
|
||||
| Local | `~/.marqdo/r2.env` (`chmod 600`) — see `.env.r2.example` |
|
||||
| CI (optional) | GitHub Actions secrets: `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_ENDPOINT`, `R2_BUCKET` |
|
||||
|
||||
**Never** commit Account API tokens or S3 secret keys. If a token was pasted into chat, **rotate** it in the Cloudflare dashboard.
|
||||
|
||||
## Object ACL
|
||||
|
||||
Custom domain public reads require the bucket/objects to be publicly readable (R2 public bucket / r2.dev / custom domain). Uploads use S3 API credentials; downloads by `ext add` are anonymous HTTPS GETs.
|
||||
@@ -51,10 +51,16 @@ L1 sources:
|
||||
|
||||
| Variable | Meaning |
|
||||
|----------|---------|
|
||||
| `MARQDO_EXT_VERSION` | Override SemVer used for Release asset names (default: CLI `CARGO_PKG_VERSION`) |
|
||||
| `MARQDO_EXT_VERSION` | Override **extension pack** SemVer (default: CDN `latest/VERSION` → embedded `ext/VERSION` → CLI version) |
|
||||
| `MARQDO_EXT_CDN` | Public CDN base (default `https://ext.marqdo.com`) |
|
||||
| `MARQDO_EXT_NO_DOWNLOAD=1` | Never hit the network (CI / air-gap); local/cargo only |
|
||||
| `MARQDO_EXT_DOWNLOAD_BASE` | Optional extra base tried **before** CDN |
|
||||
| `MARQDO_WEB_PLUGIN` / `MARQDO_AGENT_PLUGIN` / `MARQDO_QUANTUM_PLUGIN` | Explicit native path |
|
||||
|
||||
Download order for each Release asset: `MARQDO_EXT_DOWNLOAD_BASE` → **CDN `ext.marqdo.com`** → `github.com` → **`proxy.cflmy.top`**. Connect timeout 20s; read timeout 10min.
|
||||
|
||||
See [ext-cdn.md](ext-cdn.md) for R2 layout, separate ext releases, and `scripts/upload-ext-r2.py`.
|
||||
|
||||
Windows Release **zip** also ships `ext/` + `ext/native/*.dll` next to `marqdo.exe` so a portable unzip works offline after one download.
|
||||
|
||||
## Source for `add` (developers)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# Collaborative document MVP
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · MVP (LWW)** |
|
||||
| 日期 | 2026-09-17 |
|
||||
|
||||
## Approach
|
||||
|
||||
Not a full CRDT. Peers sync plain text over `route_ws mode=room` with:
|
||||
|
||||
- Client → server: `DOC\\t<body>`
|
||||
- Server fan-out (existing G-WS1/2)
|
||||
- Apply remote if body ≠ local; `applying` flag suppresses echo
|
||||
- Presence JSON on `room.presence` (`presence=True`)
|
||||
|
||||
## Demo
|
||||
|
||||
`examples/web-collab-doc/` — open two browsers on the same room.
|
||||
@@ -0,0 +1,80 @@
|
||||
# `ext/web` 可配置 RBAC(角色 ⊥ 权限)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 验收 | qdqc:注册登录、评论、desk 内建角色/赋权;`tests/ext/web-rbac-*-smoke.mq.md` |
|
||||
| 状态 | **Accepted · 已落地核心**(HTTP register + `/_rbac/*` + gate permissions;样站 qdqc 已接线) |
|
||||
|
||||
---
|
||||
|
||||
## 0. 一句话
|
||||
|
||||
权限是原子能力(`resource:action`);角色是权限包;用户通过 `user_roles` 获得角色。门禁与业务都问 `can(user, permission)`。管理员可在后台新建角色、勾选权限、赋给用户——**禁止**只靠写死的 `role=admin|user` 字符串作为唯一模型(兼容期仍可读旧 `roles=` gate)。
|
||||
|
||||
---
|
||||
|
||||
## 1. 表结构(SQLite / Postgres 同构)
|
||||
|
||||
| 表 | 列 |
|
||||
|----|-----|
|
||||
| `web_permissions` | `code` PK, `resource`, `action`, `description` |
|
||||
| `web_roles` | `id` PK, `name` UNIQUE, `is_system` INT, `tenant_id` TEXT NULL |
|
||||
| `web_role_permissions` | `role_id`, `permission_code` PK(role_id, permission_code) |
|
||||
| `web_user_roles` | `user_id`, `role_id`, `tenant_id` TEXT NULL, PK(user_id, role_id, tenant_id) |
|
||||
| `web_users` | `id` PK, `username` UNIQUE, `password_hash`, `created_at` |
|
||||
|
||||
命名带 `web_` 前缀,避免与业务表冲突。站点也可映射自定义表名(后期);首版固定。
|
||||
|
||||
---
|
||||
|
||||
## 2. 权限码约定
|
||||
|
||||
形如 `comments:create`、`desk:access`、`roles:manage`。内置种子见 `DefaultCatalog`。`*` 或角色名 `superadmin` / 兼容 `admin`:登录时展开为目录全集。
|
||||
|
||||
---
|
||||
|
||||
## 3. 作者 API
|
||||
|
||||
```markdown
|
||||
**`应用` = > `应用`.权限 确保结构=真 目录=`权限目录`**
|
||||
**`应用` = > `应用`.门禁 路径="/desk" 权限="desk:access" 匹配="prefix" 拒绝="redirect"**
|
||||
**`应用` = > `应用`.鉴权 用户表=`管理员` 注册=真 注册路径="/register" …**
|
||||
```
|
||||
|
||||
| 方法 | ABI | 说明 |
|
||||
|------|-----|------|
|
||||
| `app.rbac` / `权限` | `web_app_rbac` | ensure schema + optional seed catalog table |
|
||||
| `app.gate` | `web_app_gate` | 新增 `permissions`/`权限`(CSV);与 `roles` 可并存:有 permissions 时按权限判定 |
|
||||
| `rbac.can` | `web_rbac_can` | `{ok, allowed}` |
|
||||
| `rbac.assign_role` | `web_rbac_assign_role` | 用户←角色 |
|
||||
| `rbac.set_role_permissions` | `web_rbac_set_role_permissions` | 角色←权限列表 |
|
||||
|
||||
防提权:`set_role_permissions` / `assign_role` 在 HTTP 管理面检查操作者 `roles:manage` 且不能授予自身没有的 permission。
|
||||
|
||||
---
|
||||
|
||||
## 4. 运行时
|
||||
|
||||
1. `listen`:若 `auth.rbac=true` 或调用过 `app.rbac`,对 `db` URL `EnsureSchema` + 种子。
|
||||
2. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。
|
||||
3. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。
|
||||
4. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。
|
||||
|
||||
---
|
||||
|
||||
## 5. qdqc 映射
|
||||
|
||||
| 产品 | 权限码 |
|
||||
|------|--------|
|
||||
| 发评论 | `comments:create` |
|
||||
| 删任意评论 | `comments:delete` |
|
||||
| 进 desk | `desk:access` |
|
||||
| 管角色 | `roles:manage` |
|
||||
|
||||
首个 GFM `admin` 用户登录后视为 `superadmin`(全权限);DB 迁移后绑定 `superadmin` 角色。
|
||||
|
||||
---
|
||||
|
||||
## 6. 非目标(本切片)
|
||||
|
||||
对象级 ACL 全图、ABAC;租户列先可空,多租户波次再启用。
|
||||
@@ -0,0 +1,24 @@
|
||||
# Native SPA(Marqdo WASM 全页客户端)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · MVP** |
|
||||
| 日期 | 2026-09-17 |
|
||||
| 相关 | [browser-marqdo-wasm.md](browser-marqdo-wasm.md) · route D/E |
|
||||
|
||||
## 约定
|
||||
|
||||
| 表 | 列 | 用途 |
|
||||
|----|-----|------|
|
||||
| 客户端路由 | `路径` / `面板` / `标题` | 文档化视图;运行时用 `path` + `set_attr(hidden)` 切换 |
|
||||
| 客户端状态 | `browser.store_set` / `store_get` `scope=memory` | 页内状态(列表 JSON、当前 id) |
|
||||
|
||||
作者零业务 JS:`web.client_embed` + `static/client.mq.md` + `lib/browser`。
|
||||
|
||||
## Bridge
|
||||
|
||||
`storage` effect 支持 `scope=memory`(进程内 `Map`),与 `local` / `session` / `cookie` 并列。
|
||||
|
||||
## Demo
|
||||
|
||||
`examples/web-spa-app/` — 列表 → 详情 → 编辑,全客户端路由。
|
||||
@@ -0,0 +1,23 @@
|
||||
# ext/web multi-tenant (path / subdomain / header)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · MVP** |
|
||||
| 日期 | 2026-09-17 |
|
||||
| 相关 | [ext-web-rbac.md](ext-web-rbac.md) · full-ambition plan |
|
||||
|
||||
## Author API
|
||||
|
||||
```markdown
|
||||
**应用 = > `应用`.tenant mode="path" param="t" column="tenant_id" default_scope=False**
|
||||
```
|
||||
|
||||
JSON route table may set `tenant_scope` / `租户作用域` = true so `GET /api/items?t=acme` only returns rows with `tenant_id=acme`.
|
||||
|
||||
## Runtime
|
||||
|
||||
1. `withTenant` resolves id into request context.
|
||||
2. Scoped JSON selects `AND tenant_id=?`; missing tenant → 400.
|
||||
3. `default_scope=True` also stamps form POST bodies.
|
||||
|
||||
Demo: `examples/web-tenant-saas/`.
|
||||
@@ -580,7 +580,9 @@ submit → action=insert → db.insert;action=update → db.update
|
||||
## 9. 非本波次(可记入路线图)
|
||||
|
||||
- Postgres / Redis / S3 驱动细节(SQLite 已落地);
|
||||
- 复杂权限与多租户;
|
||||
- ~~复杂权限与多租户~~ → **可配置 RBAC** 见 [ext-web-rbac.md](ext-web-rbac.md);**多租户** 见 [ext-web-tenant.md](ext-web-tenant.md)(path/subdomain/header + `tenant_scope`);
|
||||
- ~~重客户端 SPA~~ → **Native SPA** 见 [ext-web-spa.md](ext-web-spa.md)(WASM + 路由表 + `store_set` memory);
|
||||
- ~~实时协同编辑~~ → **Collab MVP** 见 [ext-web-collab.md](ext-web-collab.md)(WS room LWW 文本 + presence);
|
||||
- 子页路由糖的更多约定(`# 应用.route` 已够用)。
|
||||
|
||||
---
|
||||
|
||||
@@ -211,7 +211,7 @@ v0.3:
|
||||
| 体内 `1.` `2.` … | 分支 |
|
||||
| 体内 `-` | 循环 |
|
||||
| `N. *` | else 臂(此处 `*` 仍是「空条件」标记,**不是**返回值;见 §7.3) |
|
||||
| GFM 表格 | 集合(不变) |
|
||||
| GFM 表格 | 集合;`` `名` = `` 后绑定,或**未绑定**时求值后丢弃(文档中可写表) |
|
||||
| 表达式 `[键](集合)` | **取元**(与 foreach 同形;见 [collection-access-link.md](collection-access-link.md)) |
|
||||
| `修饰… [函数名] 实参…` | **方括号标记调用**(前置修饰 → `名=True`;见 [bracket-call-modifiers.md](bracket-call-modifiers.md)) |
|
||||
| `` 集合[^键] `` | **过渡**:旧脚注取元(仍可用) |
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
# Ubuntu PPA packaging (Marqdo)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Status | **Active** — `ppa:cflmy/marqdo`; noble builds need bundled Rust (Cargo.lock v4) |
|
||||
| Maintainer | cflmy \<pingan@cflmy.cn\> |
|
||||
| GPG | `505943294D04C803` (fingerprint `52A1…C803`) |
|
||||
| PPA | `ppa:cflmy/marqdo` |
|
||||
|
||||
## Goal
|
||||
|
||||
Users install with:
|
||||
|
||||
```bash
|
||||
sudo add-apt-repository ppa:cflmy/marqdo
|
||||
sudo apt update
|
||||
sudo apt install marqdo
|
||||
```
|
||||
|
||||
CLI and extension packs stay independent: the `.deb` ships the **interpreter**; `marqdo ext add …` still pulls from **https://ext.marqdo.com** (then GitHub / proxy).
|
||||
|
||||
## Why bundled Rust?
|
||||
|
||||
Ubuntu **noble** ships cargo/rustc **1.75**, which cannot parse **Cargo.lock version 4** (`lock file version 4 requires -Znext-lockfile-bump`). Launchpad builders also have **no network**, so `ppa-build-source.sh` vendors crates **and** installs a modern toolchain into `third_party/rust` inside the `.orig.tar.xz`. `debian/rules` prefers that toolchain over distro cargo.
|
||||
|
||||
## One-time Launchpad setup
|
||||
|
||||
1. Create PPA: https://launchpad.net/~cflmy/+activate-ppa → name e.g. `marqdo`
|
||||
2. OpenPGP key already on Launchpad (done)
|
||||
3. Sign Ubuntu Code of Conduct if Launchpad asks
|
||||
4. Enable the Ubuntu **series** you upload for (e.g. noble) under PPA → Change details
|
||||
5. On the build machine:
|
||||
|
||||
```bash
|
||||
sudo apt install build-essential debhelper devscripts dput lintian \
|
||||
dpkg-dev quilt cargo rustc pkg-config curl xz-utils
|
||||
```
|
||||
|
||||
## Package layout
|
||||
|
||||
| File | Role |
|
||||
|------|------|
|
||||
| `debian/control` | Metadata (amd64; no distro cargo BD) |
|
||||
| `debian/changelog` | Upload revisions (`1ppaN`) |
|
||||
| `debian/rules` | `dh` + bundled/system cargo build |
|
||||
| `scripts/ppa-build-source.sh` | Vendor + bundle Rust + `debuild -S` |
|
||||
| `scripts/ppa-ship.sh` | apt deps + build + `dput` |
|
||||
|
||||
## Versioning
|
||||
|
||||
- Upstream SemVer: `Cargo.toml` / git tag `vX.Y.Z`
|
||||
- Debian revision: `X.Y.Z-1ppaN~SERIES` (SERIES = `noble`, …) — bump `1ppaN` on packaging-only reuploads
|
||||
- **Ext pack** SemVer (`ext/VERSION`) is **not** the deb version
|
||||
|
||||
## One-shot (recommended)
|
||||
|
||||
```bash
|
||||
cd ~/work/marqdo
|
||||
./scripts/ppa-ship.sh noble # tools → debuild -S → dput
|
||||
./scripts/ppa-ship.sh noble -n # build only
|
||||
```
|
||||
|
||||
If automation cannot enter sudo/GPG, tell the user these commands (do not invent alternatives):
|
||||
|
||||
```bash
|
||||
./scripts/ppa-build-source.sh noble
|
||||
dput ppa:cflmy/marqdo ../build-area/marqdo_*~noble_source.changes
|
||||
```
|
||||
|
||||
Watch: https://launchpad.net/~cflmy/+archive/ubuntu/marqdo/+builds
|
||||
|
||||
## Failure notes
|
||||
|
||||
| Symptom | Fix |
|
||||
|---------|-----|
|
||||
| `lock file version 4 requires -Znext-lockfile-bump` | Rebuild with current `ppa-build-source.sh` (bundles Rust); bump `1ppaN` |
|
||||
| Same version rejected by Launchpad | Bump `debian/changelog` (`1ppa2`, …) |
|
||||
| Wrong series | Enable series on PPA; rebuild with that series name |
|
||||
|
||||
## Out of scope (v1 deb)
|
||||
|
||||
- Shipping `ext/native/*.so` inside the deb (use CDN `ext add`)
|
||||
- WASM / VSIX / non-amd64
|
||||
@@ -0,0 +1,12 @@
|
||||
# web-collab-doc
|
||||
|
||||
Two-browser collaborative notepad over WebSocket room (last-write-wins text).
|
||||
|
||||
```bash
|
||||
marqdo wasm build -o examples/web-collab-doc/static
|
||||
cd examples/web-collab-doc
|
||||
marqdo run index.mq.md
|
||||
# open http://127.0.0.1:18094 twice
|
||||
```
|
||||
|
||||
Design: [ext-web-collab.md](../../doc/design/ext-web-collab.md).
|
||||
@@ -0,0 +1,24 @@
|
||||
---
|
||||
title: web-collab-doc
|
||||
description: Two-browser collaborative plain-text editor over WS room (LWW snapshot sync)
|
||||
import web:ext/web/web.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Collaborative notepad: open the page in two browsers; edits fan out via
|
||||
`/live/{room}` WebSocket (mode=room + presence). Build WASM first:
|
||||
|
||||
```
|
||||
marqdo wasm build -o examples/web-collab-doc/static
|
||||
```
|
||||
|
||||
**`embed` = > web.client_embed bridge="/static/marqdo-bridge.js" wasm="/static/marqdo_wasm.wasm" source="/static/client.mq.md" boot=True**
|
||||
|
||||
**`intro` = "<p class='lede'>Room <code>demo</code> · open twice to co-edit.</p><p id='presence'>presence: —</p><textarea id='doc' rows='16' cols='72' placeholder='Type together…'></textarea><pre id='log'></pre>" + embed**
|
||||
|
||||
**page = > web.page title="Collab doc" intro=intro layout="bare"**
|
||||
**app = > web.app page=page host="127.0.0.1" port=18094**
|
||||
**app = > app.static dir="static" mount="/static"**
|
||||
**app = > `app`.route_ws path="/live/{id}" mode="room" room_key="doc.{id}" presence=True**
|
||||
> `app`.listen
|
||||
@@ -0,0 +1,85 @@
|
||||
---
|
||||
title: collab client
|
||||
description: LWW text sync over WS room + presence (DOC\\t protocol)
|
||||
import browser:lib/browser.mq.md
|
||||
import table:lib/table.mq.md
|
||||
import text:lib/text.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
**`ws_id` = "doc"**
|
||||
**`applying` = False**
|
||||
**`doc` = ""**
|
||||
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 | 值选择器 |
|
||||
|---|--------|------|------|----------|
|
||||
| 1 | "#doc" | input | on_edit | "#doc" |
|
||||
|
||||
`open` =
|
||||
|
||||
| op | id | url | then_open | then_message | then_error | then_close |
|
||||
|----|----|-----|-----------|--------------|------------|------------|
|
||||
| open | `ws_id` | "ws://127.0.0.1:18094/live/demo" | on_open | on_msg | on_err | on_close |
|
||||
|
||||
**`w` = > table.put in=None at="wire" value=wire**
|
||||
**`ws` = > browser.ws spec=open**
|
||||
**`boot` = > browser.merge a=w b=ws**
|
||||
**`ready` = > browser.set_text sel="#log" text="connecting…"**
|
||||
*> browser.merge a=boot b=ready*
|
||||
|
||||
## on_open
|
||||
+ `ok`=True
|
||||
|
||||
*> browser.set_text sel="#log" text="ws open · room demo"*
|
||||
|
||||
## on_err
|
||||
+ `error`=""
|
||||
|
||||
**`msg` = "ws error: " + error**
|
||||
*> browser.set_text sel="#log" text=msg*
|
||||
|
||||
## on_close
|
||||
*> browser.set_text sel="#log" text="ws closed"*
|
||||
|
||||
## on_edit
|
||||
+ `value`=""
|
||||
|
||||
1. `applying`
|
||||
**`applying` = False**
|
||||
****
|
||||
2. *
|
||||
**`doc` = value**
|
||||
**`payload` = "DOC\t" + value**
|
||||
`send` =
|
||||
|
||||
| op | id | data |
|
||||
|----|----|------|
|
||||
| send | `ws_id` | `payload` |
|
||||
|
||||
*> browser.ws spec=send*
|
||||
|
||||
## on_msg
|
||||
+ `data`=""
|
||||
|
||||
1. `data` != ""
|
||||
**`is_pres` = > text.starts_with text=data prefix="{"**
|
||||
1. `is_pres`
|
||||
**`msg` = "presence: " + data**
|
||||
*> browser.set_text sel="#presence" text=msg*
|
||||
2. *
|
||||
**`is_doc` = > text.starts_with text=data prefix="DOC\t"**
|
||||
1. `is_doc`
|
||||
**`remote` = > text.replace text=data old="DOC\t" new="" count=1**
|
||||
1. `remote` != `doc`
|
||||
**`doc` = remote**
|
||||
**`applying` = True**
|
||||
*> browser.set_value sel="#doc" value=remote*
|
||||
2. *
|
||||
****
|
||||
2. *
|
||||
****
|
||||
2. *
|
||||
****
|
||||
@@ -0,0 +1,14 @@
|
||||
# web-spa-app
|
||||
|
||||
Marqdo **native SPA**: list → detail → edit with client routes + `scope=memory` store. Zero author JS.
|
||||
|
||||
## Run
|
||||
|
||||
```bash
|
||||
marqdo wasm build -o examples/web-spa-app/static
|
||||
cd examples/web-spa-app
|
||||
marqdo run index.mq.md
|
||||
# http://127.0.0.1:18093
|
||||
```
|
||||
|
||||
Design: [ext-web-spa.md](../../doc/design/ext-web-spa.md).
|
||||
@@ -0,0 +1,23 @@
|
||||
---
|
||||
title: web-spa-app
|
||||
description: Native Marqdo SPA — list/detail/edit via WASM client routes + memory store
|
||||
import web:ext/web/web.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Full-page client app (zero author JS). Build assets first:
|
||||
|
||||
```
|
||||
marqdo wasm build -o examples/web-spa-app/static
|
||||
```
|
||||
|
||||
**`embed` = > web.client_embed bridge="/static/marqdo-bridge.js" wasm="/static/marqdo_wasm.wasm" source="/static/client.mq.md" boot=True**
|
||||
|
||||
**`intro` = "<nav class='spa-nav'><button type='button' id='nav-list'>Items</button> <button type='button' id='nav-new'>New</button></nav><p id='title' class='spa-title'>Items</p><section id='view-list'><ul id='items'></ul><p class='hint'>Click an item for detail.</p></section><section id='view-detail' hidden><p id='detail-body'></p><p><button type='button' id='btn-edit'>Edit</button> <button type='button' id='btn-back'>Back</button></p></section><section id='view-edit' hidden><label>Title <input id='edit-title' /></label><p><button type='button' id='btn-save'>Save</button> <button type='button' id='btn-cancel'>Cancel</button></p></section><pre id='log'></pre>" + embed**
|
||||
|
||||
**page = > web.page title="Marqdo SPA" intro=intro layout="bare"**
|
||||
**app = > web.app page=page host="127.0.0.1" port=18093**
|
||||
**app = > app.static dir="static" mount="/static"**
|
||||
**app = > app.redirect from="/item" to="/" permanent=False**
|
||||
> `app`.listen
|
||||
@@ -0,0 +1,174 @@
|
||||
---
|
||||
title: spa client
|
||||
description: routes table + memory store; list / detail / edit
|
||||
import browser:lib/browser.mq.md
|
||||
import table:lib/table.mq.md
|
||||
import text:lib/text.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Routes (documentation + titles). Runtime switches panels by `path`.
|
||||
|
||||
`routes` =
|
||||
|
||||
| @ | 路径 | 面板 | 标题 |
|
||||
|---|------|------|------|
|
||||
| 1 | / | #view-list | Items |
|
||||
| 2 | /item | #view-detail | Detail |
|
||||
| 3 | /edit | #view-edit | Edit |
|
||||
|
||||
**`path` = "/"**
|
||||
**`cur_id` = ""**
|
||||
**`items` = "Alpha\nBeta"**
|
||||
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 | 委托 | 值选择器 |
|
||||
|---|--------|------|------|------|----------|
|
||||
| 1 | "#nav-list" | click | go_list | | |
|
||||
| 2 | "#nav-new" | click | go_new | | |
|
||||
| 3 | "#items" | click | open_item | "li[data-id]" | |
|
||||
| 4 | "#btn-edit" | click | go_edit | | |
|
||||
| 5 | "#btn-back" | click | go_list | | |
|
||||
| 6 | "#btn-save" | click | save_item | | "#edit-title" |
|
||||
| 7 | "#btn-cancel" | click | go_list | | |
|
||||
| 8 | "window" | popstate | on_pop | | |
|
||||
|
||||
`seed` =
|
||||
|
||||
| op | key | value | scope |
|
||||
|----|-----|-------|-------|
|
||||
| set | spa-items | `items` | memory |
|
||||
|
||||
**`w` = > table.put in=None at="wire" value=wire**
|
||||
**`s` = > browser.storage spec=seed**
|
||||
**`boot` = > browser.merge a=w b=s**
|
||||
**`ready` = > browser.set_text sel="#log" text="spa ready — click Items"**
|
||||
*> browser.merge a=boot b=ready*
|
||||
|
||||
## on_pop
|
||||
+ `path`=""
|
||||
|
||||
1. `path` != ""
|
||||
**`path` = path**
|
||||
2. *
|
||||
****
|
||||
*> spa_render*
|
||||
|
||||
## go_list
|
||||
**`path` = "/"**
|
||||
**`cur_id` = ""**
|
||||
*> spa_render*
|
||||
|
||||
## go_new
|
||||
**`path` = "/edit"**
|
||||
**`cur_id` = ""**
|
||||
*> spa_render*
|
||||
|
||||
## go_edit
|
||||
**`path` = "/edit"**
|
||||
*> spa_render*
|
||||
|
||||
## open_item
|
||||
+ `data_id`=""
|
||||
|
||||
1. `data_id` != ""
|
||||
**`cur_id` = data_id**
|
||||
**`path` = "/item"**
|
||||
*> spa_render*
|
||||
2. *
|
||||
****
|
||||
|
||||
## save_item
|
||||
+ `value`=""
|
||||
|
||||
1. `value` != ""
|
||||
1. `cur_id` != ""
|
||||
**`parts` = > text.str_split s=items sep="\n"**
|
||||
**`next` = ""**
|
||||
- [item](parts)
|
||||
1. `item` != ""
|
||||
1. `item` == `cur_id`
|
||||
1. `next` == ""
|
||||
**`next` = value**
|
||||
2. *
|
||||
**`next` = next + "\n" + value**
|
||||
2. *
|
||||
1. `next` == ""
|
||||
**`next` = item**
|
||||
2. *
|
||||
**`next` = next + "\n" + item**
|
||||
**`items` = next**
|
||||
**`cur_id` = value**
|
||||
2. *
|
||||
1. `items` == ""
|
||||
**`items` = value**
|
||||
2. *
|
||||
**`items` = items + "\n" + value**
|
||||
**`cur_id` = value**
|
||||
**`path` = "/item"**
|
||||
*> spa_render*
|
||||
2. *
|
||||
*> browser.set_text sel="#log" text="title required"*
|
||||
|
||||
## spa_render
|
||||
*> spa_paint*
|
||||
|
||||
## spa_paint
|
||||
**`hide` = > table.put in=None at="hidden" value=""**
|
||||
**`show` = > table.put in=None at="hidden" value=False**
|
||||
1. `path` == "/item"
|
||||
`attrs` =
|
||||
|
||||
| #view-detail | #view-list | #view-edit |
|
||||
|--------------|------------|------------|
|
||||
| `show` | `hide` | `hide` |
|
||||
**`title` = "Detail"**
|
||||
**`body` = cur_id**
|
||||
2. *
|
||||
1. `path` == "/edit"
|
||||
`attrs` =
|
||||
|
||||
| #view-edit | #view-list | #view-detail |
|
||||
|------------|------------|--------------|
|
||||
| `show` | `hide` | `hide` |
|
||||
**`title` = "Edit"**
|
||||
**`body` = cur_id**
|
||||
2. *
|
||||
`attrs` =
|
||||
|
||||
| #view-list | #view-detail | #view-edit |
|
||||
|------------|--------------|------------|
|
||||
| `show` | `hide` | `hide` |
|
||||
**`title` = "Items"**
|
||||
**`body` = ""**
|
||||
**`parts` = > text.str_split s=items sep="\n"**
|
||||
**`html` = ""**
|
||||
- [item](parts)
|
||||
1. `item` != ""
|
||||
**`html` = html + "<li data-id=\"" + item + "\">" + item + "</li>"**
|
||||
`persist` =
|
||||
|
||||
| op | key | value | scope |
|
||||
|----|-----|-------|-------|
|
||||
| set | spa-items | `items` | memory |
|
||||
|
||||
**`t` = > browser.set_text sel="#title" text=title**
|
||||
**`a` = > browser.wrap key="set_attr" value=attrs**
|
||||
**`ret` = > browser.merge a=t b=a**
|
||||
**`list` = > browser.set_html sel="#items" html=html**
|
||||
**`ret` = > browser.merge a=ret b=list**
|
||||
**`d` = > browser.set_text sel="#detail-body" text=body**
|
||||
**`ret` = > browser.merge a=ret b=d**
|
||||
1. `path` == "/edit"
|
||||
**`v` = > browser.set_value sel="#edit-title" value=cur_id**
|
||||
**`ret` = > browser.merge a=ret b=v**
|
||||
2. *
|
||||
****
|
||||
**`nav` = > browser.spa_goto url=path**
|
||||
**`ret` = > browser.merge a=ret b=nav**
|
||||
**`log` = > browser.set_text sel="#log" text=path**
|
||||
**`ret` = > browser.merge a=ret b=log**
|
||||
**`sav` = > browser.storage spec=persist**
|
||||
*> browser.merge a=ret b=sav*
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: web-tenant-saas demo
|
||||
description: Dual-tenant isolation via ?t= query + tenant_scope JSON API.
|
||||
import web:ext/web/web.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`items_schema` =
|
||||
|
||||
| name | type | nullable |
|
||||
|------|------|----------|
|
||||
| id | integer | false |
|
||||
| title | text | false |
|
||||
| tenant_id | text | false |
|
||||
|
||||
`api` =
|
||||
|
||||
| path | method | table | order | limit | tenant_scope |
|
||||
|------|--------|-------|-------|-------|--------------|
|
||||
| api/items | GET | items | id | 50 | true |
|
||||
|
||||
**db = > web.db url="sqlite:data/tenant-saas.db"**
|
||||
> `db`.init name=items fields=`items_schema`
|
||||
> `db`.exec sql="DELETE FROM items"
|
||||
> `db`.exec sql="INSERT INTO items(title, tenant_id) VALUES('Acme note','acme')"
|
||||
> `db`.exec sql="INSERT INTO items(title, tenant_id) VALUES('Beta note','beta')"
|
||||
|
||||
**page = > web.page title="Tenant SaaS" intro="<p>Try <a href='/api/items?t=acme'>/api/items?t=acme</a> vs <a href='/api/items?t=beta'>/api/items?t=beta</a>.</p>"**
|
||||
**app = > web.app page=`page` db=`db` host="127.0.0.1" port=18092 admin=False**
|
||||
**app = > `app`.tenant mode="path" param="t" default_scope=False**
|
||||
**app = > `app`.configure json=`api`**
|
||||
> `app`.listen
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
title: web-ws-room demo
|
||||
description: Dynamic room WS with presence (G-WS1/2 bag). Open / and connect via bridge or wscat.
|
||||
import web:ext/web/web.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
**page = > web.page title="WS room" intro="<p class='lede'>Chat room <code>lobby</code> via <code>/chat/lobby</code>. Presence joins <code>chat.room.lobby.presence</code>.</p><pre id='log'></pre>"**
|
||||
**app = > web.app page=`page` host="127.0.0.1" port=18091 admin=False**
|
||||
**app = > `app`.route_ws path="/chat/{id}" mode="room" room_key="chat.room.{id}" presence=True**
|
||||
> `app`.listen
|
||||
@@ -0,0 +1 @@
|
||||
1.0.4
|
||||
+73
-6
@@ -733,10 +733,13 @@ Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes),
|
||||
+ `path`
|
||||
+ `echo`=True
|
||||
+ `mode`=None
|
||||
+ `room_key`=None
|
||||
+ `on_message`=None
|
||||
+ `presence`=False
|
||||
|
||||
Register a WebSocket endpoint at `path` (e.g. `/live`). `mode` is `echo` (default), `broadcast` (fan-out text to all sockets on this path), or `drain`. Legacy `echo=False` maps to `drain`. Connect from a client with `web.ws.connect`.
|
||||
Register a WebSocket endpoint at `path` (e.g. `/live` or `/chat/{id}`). `mode` is `echo` (default), `broadcast` (fan-out text to all sockets on this path), `room` (named room via `room_key`, supports `{id}` from the path), or `drain`. Legacy `echo=False` maps to `drain`. `on_message` is an optional `lib.member` hook (G-WS2): called before fan-out with `message`/`room`/`user`; return `{skip:true}` to drop or `{message:…}` to rewrite. `presence=True` also joins `room.presence` and emits join/leave JSON. Connect from a client with `web.ws.connect`.
|
||||
|
||||
*> web_app_route_ws app=`self` path=`path` echo=`echo` mode=`mode`*
|
||||
*> web_app_route_ws app=`self` path=`path` echo=`echo` mode=`mode` room_key=`room_key` on_message=`on_message` presence=`presence`*
|
||||
|
||||
## auth
|
||||
+ `users`
|
||||
@@ -745,21 +748,42 @@ Register a WebSocket endpoint at `path` (e.g. `/live`). `mode` is `echo` (defaul
|
||||
+ `login_redirect`=None
|
||||
+ `logout_redirect`=None
|
||||
+ `login_path`=None
|
||||
+ `register`=False
|
||||
+ `register_path`=/register
|
||||
+ `default_role`="member"
|
||||
|
||||
Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings.
|
||||
Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings. With `register=True` and `enable_rbac`, exposes `register_path` (writes `web_users` + default role).
|
||||
|
||||
*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path`*
|
||||
*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path` register=`register` register_path=`register_path` default_role=`default_role`*
|
||||
|
||||
## gate
|
||||
+ `path`
|
||||
+ `roles`=admin
|
||||
+ `permissions`=""
|
||||
+ `match`=prefix
|
||||
+ `on_deny`=forbid
|
||||
+ `exclude`=None
|
||||
|
||||
Require one of `roles` (CSV) for `path`. `match=prefix` uses segment boundaries; `exact` is equality. Trailing `*` on `path` means prefix. `on_deny=redirect` sends visitors to `login_path` (with `?next=`); `forbid` returns 403. `exclude` is CSV or list of open paths.
|
||||
Require access for `path`. Prefer `permissions` (CSV of `resource:action` codes such as `desk:access`); when non-empty, authorization uses the session permission set. Otherwise require one of `roles` (legacy). `match=prefix` uses segment boundaries; `exact` is equality. Trailing `*` on `path` means prefix. `on_deny=redirect` sends visitors to `login_path` (with `?next=`); `forbid` returns 403. `exclude` is CSV or list of open paths.
|
||||
|
||||
*> web_app_gate app=`self` path=`path` roles=`roles` match=`match` on_deny=`on_deny` exclude=`exclude`*
|
||||
*> web_app_gate app=`self` path=`path` roles=`roles` permissions=`permissions` match=`match` on_deny=`on_deny` exclude=`exclude`*
|
||||
|
||||
## enable_rbac
|
||||
+ `catalog`=None
|
||||
|
||||
Enable database-backed RBAC (role ⊥ permission). On listen, ensures `web_permissions` / `web_roles` / `web_role_permissions` / `web_users` / `web_user_roles` and seeds system roles `superadmin` and `member`. See `doc/design/ext-web-rbac.md`. Management UI: `GET /_rbac/desk` (needs `roles:manage`).
|
||||
|
||||
*> web_app_rbac app=`self` catalog=`catalog`*
|
||||
|
||||
## tenant
|
||||
+ `mode`
|
||||
+ `param`=None
|
||||
+ `column`=tenant_id
|
||||
+ `default_scope`=False
|
||||
|
||||
Enable multi-tenant resolution: `mode` is `path` (query/`/t/{id}/…`), `subdomain`, or `header`. `param` is the path/query key or header name. When `default_scope=True`, JSON routes and form inserts auto-filter/stamp `column` (default `tenant_id`). Per-route override: JSON table column `tenant_scope` / `租户作用域`.
|
||||
|
||||
*> web_app_tenant app=`self` mode=`mode` param=`param` column=`column` default_scope=`default_scope`*
|
||||
|
||||
## gallery
|
||||
+ `path`=/gallery
|
||||
@@ -843,6 +867,49 @@ Mount `GET path` to stream an object. Path must capture `key` (use `{*key}` for
|
||||
|
||||
*> web_app_download app=`self` path=`path` storage=`storage` disposition=`disposition`*
|
||||
|
||||
# rbac
|
||||
|
||||
Offline helpers for role ⊥ permission. Prefer `app.enable_rbac` + gates for HTTP; use these for scripts/tests.
|
||||
|
||||
## can
|
||||
+ `permissions`
|
||||
+ `needed`
|
||||
|
||||
Return `{ok, allowed}` — whether held permission CSV includes any of `needed` (CSV). Held `*` allows all.
|
||||
|
||||
> ensure_plugin
|
||||
*> web_rbac_can permissions=`permissions` needed=`needed`*
|
||||
|
||||
## assign_role
|
||||
+ `url`
|
||||
+ `username`
|
||||
+ `role`
|
||||
|
||||
Bind `username` (in `web_users`) to role name.
|
||||
|
||||
> ensure_plugin
|
||||
*> web_rbac_assign_role url=`url` username=`username` role=`role`*
|
||||
|
||||
## create_role
|
||||
+ `url`
|
||||
+ `name`
|
||||
|
||||
Create a non-system role; returns `{ok, id, name}`.
|
||||
|
||||
> ensure_plugin
|
||||
*> web_rbac_create_role url=`url` name=`name`*
|
||||
|
||||
## set_role_permissions
|
||||
+ `url`
|
||||
+ `role`
|
||||
+ `permissions`
|
||||
+ `grantable`=""
|
||||
|
||||
Replace permissions on a non-system role (CSV). `grantable` restricts grants (anti-escalation); empty = full catalog.
|
||||
|
||||
> ensure_plugin
|
||||
*> web_rbac_set_role_permissions url=`url` role=`role` permissions=`permissions` grantable=`grantable`*
|
||||
|
||||
# auth
|
||||
+ `users`
|
||||
+ `session_ttl`=3600
|
||||
|
||||
+73
-6
@@ -699,10 +699,13 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
+ `路径`
|
||||
+ `回显`=真
|
||||
+ `模式`=None
|
||||
+ `房间`=None
|
||||
+ `消息钩子`=None
|
||||
+ `在场`=假
|
||||
|
||||
在 `路径`(如 `/live`)注册 WebSocket 端点。`模式` 为 `echo`(默认)、`broadcast`(同路径全体广播)或 `drain`。旧参数 `回显=假` 等价于 `drain`。客户端用 `网页.实时` 的 `连接` 接入。
|
||||
在 `路径`(如 `/live` 或 `/chat/{id}`)注册 WebSocket。`模式` 为 `echo`(默认)、`broadcast`、`room`(需 `房间` 键,可含 `{id}`)或 `drain`。旧参数 `回显=假` 等价于 `drain`。`消息钩子` 为可选 `库.成员`(G-WS2):广播前调用,入参含 `message`/`room`/`user`;返回 `{skip:真}` 丢弃或 `{message:…}` 改写。`在场=真` 时另进 `房间.presence` 并推送 join/leave。客户端用 `网页.实时` 的 `连接` 接入。
|
||||
|
||||
*> web_app_route_ws app=`self` path=`路径` echo=`回显` mode=`模式`*
|
||||
*> web_app_route_ws app=`self` path=`路径` echo=`回显` mode=`模式` room_key=`房间` on_message=`消息钩子` presence=`在场`*
|
||||
|
||||
## 鉴权
|
||||
+ `用户表`
|
||||
@@ -711,21 +714,42 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
+ `登录回跳`=None
|
||||
+ `登出回跳`=None
|
||||
+ `登录路径`=None
|
||||
+ `注册`=假
|
||||
+ `注册路径`=/register
|
||||
+ `默认角色`="member"
|
||||
|
||||
在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。
|
||||
在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。`注册=真` 且已 `启用权限` 时开放 `注册路径`(写入 `web_users` 并赋默认角色)。
|
||||
|
||||
*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径`*
|
||||
*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径` register=`注册` register_path=`注册路径` default_role=`默认角色`*
|
||||
|
||||
## 门禁
|
||||
+ `路径`
|
||||
+ `角色`=admin
|
||||
+ `权限`=""
|
||||
+ `匹配`=prefix
|
||||
+ `拒绝`=forbid
|
||||
+ `排除`=None
|
||||
|
||||
要求会话角色落在 `角色`(CSV)之一。`匹配=prefix` 为段边界前缀;`exact` 为精确路径。路径末尾 `*` 亦表示前缀。`拒绝=redirect` 时访客跳转登录页(带 `?next=`);`forbid` 返回 403。`排除` 为 CSV/列表(如登录页)。
|
||||
为 `路径` 要求访问权。优先使用 `权限`(CSV,如 `desk:access`);非空时按会话权限集判定。否则要求会话角色落在 `角色`(CSV)之一。`匹配=prefix` 为段边界前缀;`exact` 为精确路径。路径末尾 `*` 亦表示前缀。`拒绝=redirect` 时访客跳转登录页(带 `?next=`);`forbid` 返回 403。`排除` 为 CSV/列表(如登录页)。
|
||||
|
||||
*> web_app_gate app=`self` path=`路径` roles=`角色` match=`匹配` on_deny=`拒绝` exclude=`排除`*
|
||||
*> web_app_gate app=`self` path=`路径` roles=`角色` permissions=`权限` match=`匹配` on_deny=`拒绝` exclude=`排除`*
|
||||
|
||||
## 启用权限
|
||||
+ `目录`=None
|
||||
|
||||
启用库表 RBAC(角色 ⊥ 权限)。listen 时确保五表并种子 `superadmin` / `member`。见 `doc/design/ext-web-rbac.md`。管理台:`GET /_rbac/desk`(需 `roles:manage`)。
|
||||
|
||||
*> web_app_rbac app=`self` catalog=`目录`*
|
||||
|
||||
## 租户
|
||||
+ `模式`
|
||||
+ `参数`=None
|
||||
+ `列`=tenant_id
|
||||
+ `默认作用域`=假
|
||||
|
||||
启用多租户解析:`模式` 为 `path`(查询参数或 `/t/{id}/…`)、`subdomain` 或 `header`。`参数` 为路径/查询键或请求头名。`默认作用域=真` 时 JSON 路由与表单插入自动按 `列`(默认 `tenant_id`)过滤/盖戳。单路由可用表列 `租户作用域` 覆盖。
|
||||
|
||||
*> web_app_tenant app=`self` mode=`模式` param=`参数` column=`列` default_scope=`默认作用域`*
|
||||
|
||||
## 相册
|
||||
+ `路径`=/gallery
|
||||
@@ -809,6 +833,49 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
|
||||
*> web_app_download app=`self` path=`路径` storage=`存储` disposition=`处置`*
|
||||
|
||||
# 权限
|
||||
|
||||
角色 ⊥ 权限的离线助手。HTTP 侧优先 `应用.启用权限` + 门禁;脚本/测试可用本类。
|
||||
|
||||
## 可否
|
||||
+ `权限`
|
||||
+ `需要`
|
||||
|
||||
返回 `{ok, allowed}`:持有权限 CSV 是否包含 `需要`(CSV)中任一码。持有 `*` 则全允许。
|
||||
|
||||
> 确保插件
|
||||
*> web_rbac_can permissions=`权限` needed=`需要`*
|
||||
|
||||
## 赋角色
|
||||
+ `数据库`
|
||||
+ `用户名`
|
||||
+ `角色`
|
||||
|
||||
将 `用户名`(须已在 `web_users`)绑定到角色名。
|
||||
|
||||
> 确保插件
|
||||
*> web_rbac_assign_role url=`数据库` username=`用户名` role=`角色`*
|
||||
|
||||
## 建角色
|
||||
+ `数据库`
|
||||
+ `名称`
|
||||
|
||||
新建非系统角色;返回 `{ok, id, name}`。
|
||||
|
||||
> 确保插件
|
||||
*> web_rbac_create_role url=`数据库` name=`名称`*
|
||||
|
||||
## 设角色权限
|
||||
+ `数据库`
|
||||
+ `角色`
|
||||
+ `权限`
|
||||
+ `可授`=""
|
||||
|
||||
覆盖非系统角色的权限(CSV)。`可授` 限制可授予码(防提权);空则允许整份目录。
|
||||
|
||||
> 确保插件
|
||||
*> web_rbac_set_role_permissions url=`数据库` role=`角色` permissions=`权限` grantable=`可授`*
|
||||
|
||||
# 鉴权
|
||||
+ `用户表`
|
||||
+ `会话时长`=3600
|
||||
|
||||
@@ -154,6 +154,50 @@ focus.
|
||||
|
||||
*> table.put in=None at="navigate" value=spec*
|
||||
|
||||
## store_set
|
||||
+ `key`
|
||||
+ `value`
|
||||
+ `scope`="memory"
|
||||
|
||||
In-memory (or local/session) key write for SPA client state. Prefer `scope=memory` for tab-local app state.
|
||||
|
||||
`spec` =
|
||||
|
||||
| op | key | value | scope |
|
||||
|----|-----|-------|-------|
|
||||
| set | `key` | `value` | `scope` |
|
||||
|
||||
*> table.put in=None at="storage" value=spec*
|
||||
|
||||
## store_get
|
||||
+ `key`
|
||||
+ `then`
|
||||
+ `scope`="memory"
|
||||
|
||||
Read a key then call `then` with `{ok, value, found, key, scope}`.
|
||||
|
||||
`spec` =
|
||||
|
||||
| op | key | then | scope |
|
||||
|----|-----|------|-------|
|
||||
| get | `key` | `then` | `scope` |
|
||||
|
||||
*> table.put in=None at="storage" value=spec*
|
||||
|
||||
## spa_goto
|
||||
+ `url`
|
||||
+ `replace`=False
|
||||
|
||||
SPA navigation: `history.pushState` / `replaceState` via bridge `navigate` effect.
|
||||
|
||||
`spec` =
|
||||
|
||||
| url | replace |
|
||||
|-----|---------|
|
||||
| `url` | `replace` |
|
||||
|
||||
*> table.put in=None at="navigate" value=spec*
|
||||
|
||||
## storage
|
||||
+ `spec`
|
||||
|
||||
|
||||
@@ -153,6 +153,50 @@ import table:lib/table.mq.md
|
||||
|
||||
*> table.put in=None at="navigate" value=spec*
|
||||
|
||||
## 存设
|
||||
+ `键`
|
||||
+ `值`
|
||||
+ `作用域`="memory"
|
||||
|
||||
SPA 客户端状态写入。优先 `作用域=memory`(页内内存)。
|
||||
|
||||
`spec` =
|
||||
|
||||
| op | key | value | scope |
|
||||
|----|-----|-------|-------|
|
||||
| set | `键` | `值` | `作用域` |
|
||||
|
||||
*> table.put in=None at="storage" value=spec*
|
||||
|
||||
## 存取
|
||||
+ `键`
|
||||
+ `然后`
|
||||
+ `作用域`="memory"
|
||||
|
||||
读取键并回调 `然后`,参数含 `{ok, value, found, key, scope}`。
|
||||
|
||||
`spec` =
|
||||
|
||||
| op | key | then | scope |
|
||||
|----|-----|------|-------|
|
||||
| get | `键` | `然后` | `作用域` |
|
||||
|
||||
*> table.put in=None at="storage" value=spec*
|
||||
|
||||
## 页跳
|
||||
+ `url`
|
||||
+ `替换`=False
|
||||
|
||||
SPA 导航(bridge `navigate`)。
|
||||
|
||||
`spec` =
|
||||
|
||||
| url | replace |
|
||||
|-----|---------|
|
||||
| `url` | `替换` |
|
||||
|
||||
*> table.put in=None at="navigate" value=spec*
|
||||
|
||||
## 存储
|
||||
+ `规格`
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/httpx"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/password"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
@@ -493,3 +494,154 @@ func web_app_gate(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
out, err := app.Gate(asPageMap(args["app"]), path, args)
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_app_rbac
|
||||
func web_app_rbac(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
out, err := app.Rbac(asPageMap(args["app"]), args)
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_app_tenant
|
||||
func web_app_tenant(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
out, err := app.Tenant(asPageMap(args["app"]), args)
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_rbac_can
|
||||
func web_rbac_can(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
heldRaw, _ := argStrReq(args, "permissions", "权限", "held")
|
||||
neededRaw, _ := argStrReq(args, "needed", "需要")
|
||||
if neededRaw == "" {
|
||||
neededRaw, _ = argStrReq(args, "permission", "权限码")
|
||||
}
|
||||
ok := rbac.HasAny(rbac.ParseCSV(heldRaw), rbac.ParseCSV(neededRaw))
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true, "allowed": ok}, nil)
|
||||
}
|
||||
|
||||
//export web_rbac_ensure
|
||||
func web_rbac_ensure(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
url, err := dbURLOf(args)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
if err := rbac.EnsureSchema(url); err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true}, nil)
|
||||
}
|
||||
|
||||
//export web_rbac_assign_role
|
||||
func web_rbac_assign_role(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
url, err := dbURLOf(args)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
user, err := argStrReq(args, "username", "用户名", "user")
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
role, err := argStrReq(args, "role", "角色")
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
err = rbac.AssignRole(url, user, role)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true}, nil)
|
||||
}
|
||||
|
||||
//export web_rbac_create_role
|
||||
func web_rbac_create_role(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
url, err := dbURLOf(args)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
name, err := argStrReq(args, "name", "名称")
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
id, err := rbac.CreateRole(url, name)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true, "id": id, "name": name}, nil)
|
||||
}
|
||||
|
||||
//export web_rbac_set_role_permissions
|
||||
func web_rbac_set_role_permissions(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
url, err := dbURLOf(args)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
roleName, _ := argStrReq(args, "role", "角色")
|
||||
permsRaw, _ := argStrReq(args, "permissions", "权限")
|
||||
grantRaw, _ := argStrReq(args, "grantable", "可授")
|
||||
err = rbac.SetRolePermissionsByName(url, roleName, rbac.ParseCSV(permsRaw), rbac.ParseCSV(grantRaw))
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true}, nil)
|
||||
}
|
||||
|
||||
//export web_rbac_list_roles
|
||||
func web_rbac_list_roles(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
url, err := dbURLOf(args)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
roles, err := rbac.ListRoles(url)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true, "roles": roles}, nil)
|
||||
}
|
||||
|
||||
//export web_rbac_list_permissions
|
||||
func web_rbac_list_permissions(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
url, err := dbURLOf(args)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
perms, err := rbac.ListPermissions(url)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, map[string]any{"ok": true, "permissions": perms}, nil)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/assets"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/tenant"
|
||||
)
|
||||
|
||||
// New builds an app bag matching Rust web_app_new defaults used by zh-smoke.
|
||||
@@ -194,10 +195,25 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any
|
||||
loginPath = prefix + "/login"
|
||||
}
|
||||
out["login_path"] = loginPath
|
||||
out["auth"] = map[string]any{
|
||||
"users": users,
|
||||
"session_ttl": float64(sessionTTL),
|
||||
authBag, _ := out["auth"].(map[string]any)
|
||||
if authBag == nil {
|
||||
authBag = map[string]any{}
|
||||
} else {
|
||||
authBag = clone(authBag)
|
||||
}
|
||||
authBag["users"] = users
|
||||
authBag["session_ttl"] = float64(sessionTTL)
|
||||
if v, ok := first(opts, "register", "注册"); ok {
|
||||
authBag["register"] = boolish(v)
|
||||
}
|
||||
if v := firstStr(opts, "register_path", "注册路径"); v != "" {
|
||||
authBag["register_path"] = v
|
||||
out["register_path"] = v
|
||||
}
|
||||
if v := firstStr(opts, "default_role", "默认角色"); v != "" {
|
||||
authBag["default_role"] = v
|
||||
}
|
||||
out["auth"] = authBag
|
||||
gates := gatesOf(out)
|
||||
if !hasAdminGate(gates, prefix) {
|
||||
gates = append(gates, map[string]any{
|
||||
@@ -214,8 +230,15 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Tenant configures multi-tenant resolution on the app bag (path|subdomain|header).
|
||||
func Tenant(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||
return tenant.Configure(appBag, opts)
|
||||
}
|
||||
|
||||
// Gate appends an RBAC gate (Rust web_app_gate).
|
||||
// opts: roles/角色, match/匹配, on_deny/拒绝, exclude/排除.
|
||||
// opts: roles/角色, permissions/权限, match/匹配, on_deny/拒绝, exclude/排除.
|
||||
// When permissions is non-empty, the HTTP middleware authorizes by permission codes;
|
||||
// otherwise it falls back to roles (legacy).
|
||||
func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]any, error) {
|
||||
if opts == nil {
|
||||
opts = map[string]any{}
|
||||
@@ -225,7 +248,8 @@ func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]a
|
||||
return nil, fmt.Errorf("missing `path`")
|
||||
}
|
||||
rolesRaw := firstStr(opts, "roles", "角色")
|
||||
if rolesRaw == "" {
|
||||
permsRaw := firstStr(opts, "permissions", "权限")
|
||||
if rolesRaw == "" && permsRaw == "" {
|
||||
rolesRaw = "admin"
|
||||
}
|
||||
roles := session.ParseRolesCSV(rolesRaw)
|
||||
@@ -233,6 +257,11 @@ func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]a
|
||||
for i, r := range roles {
|
||||
roleAny[i] = r
|
||||
}
|
||||
perms := session.ParseRolesCSV(permsRaw)
|
||||
permAny := make([]any, len(perms))
|
||||
for i, p := range perms {
|
||||
permAny[i] = p
|
||||
}
|
||||
matchMode := firstStr(opts, "match", "匹配")
|
||||
if matchMode == "" {
|
||||
matchMode = "prefix"
|
||||
@@ -247,6 +276,9 @@ func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]a
|
||||
"match": matchMode,
|
||||
"on_deny": onDeny,
|
||||
}
|
||||
if len(permAny) > 0 {
|
||||
entry["permissions"] = permAny
|
||||
}
|
||||
if v, ok := first(opts, "exclude", "排除"); ok && v != nil {
|
||||
entry["exclude"] = v
|
||||
}
|
||||
@@ -257,6 +289,26 @@ func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]a
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Rbac enables DB-backed RBAC on the app (ensure schema at listen).
|
||||
// opts may include catalog table (unused beyond flag for now).
|
||||
func Rbac(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
authBag, _ := out["auth"].(map[string]any)
|
||||
if authBag == nil {
|
||||
authBag = map[string]any{}
|
||||
} else {
|
||||
authBag = clone(authBag)
|
||||
}
|
||||
authBag["rbac"] = true
|
||||
if opts != nil {
|
||||
if v, ok := first(opts, "catalog", "目录"); ok && v != nil {
|
||||
authBag["permission_catalog"] = v
|
||||
}
|
||||
}
|
||||
out["auth"] = authBag
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// NormalizeAdminPrefix cleans and validates admin_prefix.
|
||||
func NormalizeAdminPrefix(raw string) (string, error) {
|
||||
path, err := cleanMountPath(raw)
|
||||
|
||||
@@ -86,7 +86,7 @@ func cellStr(v any) string {
|
||||
}
|
||||
|
||||
// Login validates credentials and creates a session.
|
||||
// Success: {"ok":true,"session_id":…,"username":…,"role":…}
|
||||
// Success: {"ok":true,"session_id":…,"username":…,"role":…,"permissions":…}
|
||||
// Failure: {"ok":false}
|
||||
func Login(username, pass string, users any, sessionTTL uint64) map[string]any {
|
||||
u, role, ok := CheckCredentials(users, username, pass)
|
||||
@@ -104,6 +104,14 @@ func Login(username, pass string, users any, sessionTTL uint64) map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
// AttachPermissions writes permission codes onto an existing login session.
|
||||
func AttachPermissions(sessionID string, codes []string) {
|
||||
if sessionID == "" {
|
||||
return
|
||||
}
|
||||
session.SetPermissions(sessionID, codes)
|
||||
}
|
||||
|
||||
// Check returns session auth state (web_auth_check).
|
||||
func Check(sessionID string) map[string]any {
|
||||
v, ok := session.GetValue(sessionID, "username")
|
||||
@@ -120,7 +128,12 @@ func Check(sessionID string) map[string]any {
|
||||
role = s
|
||||
}
|
||||
}
|
||||
return map[string]any{"ok": true, "username": u, "role": role}
|
||||
perms := session.PermissionsFromSession(sessionID)
|
||||
out := map[string]any{"ok": true, "username": u, "role": role}
|
||||
if len(perms) > 0 {
|
||||
out["permissions"] = perms
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Logout destroys the session (web_auth_logout).
|
||||
|
||||
@@ -78,6 +78,12 @@ func open(url string) (*sql.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// OpenSQL returns a pooled *sql.DB for sqlite: (and existing) URLs.
|
||||
// Used by packages that need raw SQL beyond CRUD helpers (e.g. rbac).
|
||||
func OpenSQL(url string) (*sql.DB, error) {
|
||||
return open(url)
|
||||
}
|
||||
|
||||
// ResetPool closes pooled connections and open transactions (tests).
|
||||
func ResetPool() {
|
||||
resetTxns()
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/auth"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/password"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ratelimit"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
@@ -28,23 +32,28 @@ const (
|
||||
)
|
||||
|
||||
type gate struct {
|
||||
path string
|
||||
roles []string
|
||||
matchMode gateMatch
|
||||
onDeny onDeny
|
||||
exclude []string
|
||||
path string
|
||||
roles []string
|
||||
permissions []string
|
||||
matchMode gateMatch
|
||||
onDeny onDeny
|
||||
exclude []string
|
||||
}
|
||||
|
||||
type authConfig struct {
|
||||
users any
|
||||
sessionTTL uint64
|
||||
cookieSecure bool
|
||||
admin bool
|
||||
adminPrefix string
|
||||
loginPath string
|
||||
loginRedirect string
|
||||
logoutRedirect string
|
||||
gates []gate
|
||||
users any
|
||||
sessionTTL uint64
|
||||
cookieSecure bool
|
||||
admin bool
|
||||
adminPrefix string
|
||||
loginPath string
|
||||
loginRedirect string
|
||||
logoutRedirect string
|
||||
gates []gate
|
||||
rbac bool
|
||||
register bool
|
||||
registerPath string
|
||||
defaultRole string
|
||||
}
|
||||
|
||||
func authConfigOf(appBag map[string]any) authConfig {
|
||||
@@ -68,6 +77,7 @@ func authConfigOf(appBag map[string]any) authConfig {
|
||||
if v, ok := appBag["cookie_secure"].(bool); ok {
|
||||
cfg.cookieSecure = v
|
||||
}
|
||||
cfg.registerPath = strOpt(appBag, "register_path", "")
|
||||
if authBag, ok := appBag["auth"].(map[string]any); ok {
|
||||
cfg.users = authBag["users"]
|
||||
if n, ok := asUint64(authBag["session_ttl"]); ok && n > 0 {
|
||||
@@ -75,6 +85,18 @@ func authConfigOf(appBag map[string]any) authConfig {
|
||||
} else if n, ok := asUint64(authBag["ttl"]); ok && n > 0 {
|
||||
cfg.sessionTTL = n
|
||||
}
|
||||
cfg.rbac = boolish(authBag["rbac"])
|
||||
cfg.register = boolish(authBag["register"])
|
||||
if p := strOpt(authBag, "register_path", ""); p != "" {
|
||||
cfg.registerPath = p
|
||||
}
|
||||
cfg.defaultRole = strOpt(authBag, "default_role", "")
|
||||
if cfg.defaultRole == "" {
|
||||
cfg.defaultRole = "member"
|
||||
}
|
||||
}
|
||||
if cfg.registerPath == "" {
|
||||
cfg.registerPath = "/register"
|
||||
}
|
||||
cfg.gates = parseGates(appBag["gates"])
|
||||
return cfg
|
||||
@@ -106,7 +128,11 @@ func parseGates(v any) []gate {
|
||||
if roles == nil {
|
||||
roles = parseGateRoles(m["角色"])
|
||||
}
|
||||
if len(roles) == 0 {
|
||||
perms := parseGateRoles(m["permissions"])
|
||||
if perms == nil {
|
||||
perms = parseGateRoles(m["权限"])
|
||||
}
|
||||
if len(roles) == 0 && len(perms) == 0 {
|
||||
roles = []string{"admin"}
|
||||
}
|
||||
matchS := strings.ToLower(strings.TrimSpace(strOpt(m, "match", "")))
|
||||
@@ -136,11 +162,12 @@ func parseGates(v any) []gate {
|
||||
on = onDenyForbid
|
||||
}
|
||||
out = append(out, gate{
|
||||
path: path,
|
||||
roles: roles,
|
||||
matchMode: matchMode,
|
||||
onDeny: on,
|
||||
exclude: parseExclude(m["exclude"], m["排除"]),
|
||||
path: path,
|
||||
roles: roles,
|
||||
permissions: perms,
|
||||
matchMode: matchMode,
|
||||
onDeny: on,
|
||||
exclude: parseExclude(m["exclude"], m["排除"]),
|
||||
})
|
||||
}
|
||||
return out
|
||||
@@ -245,7 +272,17 @@ func withRBAC(next http.Handler, gates []gate, loginPath string) http.Handler {
|
||||
break
|
||||
}
|
||||
role := session.RoleFromCookie(cookie)
|
||||
if !session.RoleAllowed(role, g.roles) {
|
||||
allowed := false
|
||||
if len(g.permissions) > 0 {
|
||||
held := session.PermissionsFromCookie(cookie)
|
||||
if len(held) == 0 && role != "visitor" {
|
||||
held = expandLegacyRole(role)
|
||||
}
|
||||
allowed = session.PermissionAllowed(held, g.permissions)
|
||||
} else {
|
||||
allowed = session.RoleAllowed(role, g.roles)
|
||||
}
|
||||
if !allowed {
|
||||
if role == "visitor" && g.onDeny == onDenyRedirect {
|
||||
nextQ := urlEncodePath(path)
|
||||
dest := loginPath
|
||||
@@ -268,6 +305,22 @@ func withRBAC(next http.Handler, gates []gate, loginPath string) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func expandLegacyRole(role string) []string {
|
||||
switch strings.ToLower(strings.TrimSpace(role)) {
|
||||
case "superadmin", "admin":
|
||||
return []string{
|
||||
"comments:create", "comments:delete", "comments:delete_own",
|
||||
"desk:access", "roles:manage", "posts:edit", "users:manage",
|
||||
}
|
||||
case "member", "user":
|
||||
return []string{"comments:create", "comments:delete_own"}
|
||||
case "editor", "author":
|
||||
return []string{"comments:create", "comments:delete_own", "posts:edit", "desk:access"}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func urlEncodePath(path string) string {
|
||||
var out strings.Builder
|
||||
for _, b := range []byte(path) {
|
||||
@@ -303,20 +356,34 @@ func withNavAuth(page map[string]any, cookieHeader string) {
|
||||
}
|
||||
|
||||
func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
||||
if st.auth.users == nil {
|
||||
if st.auth.users == nil && !(st.auth.rbac && st.auth.register) {
|
||||
return
|
||||
}
|
||||
lp := st.auth.loginPath
|
||||
mux.HandleFunc("GET "+lp, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+lp, st.handleLoginPost)
|
||||
altLogin := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if altLogin != lp {
|
||||
mux.HandleFunc("GET "+altLogin, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+altLogin, st.handleLoginPost)
|
||||
if st.auth.users != nil || st.auth.rbac {
|
||||
lp := st.auth.loginPath
|
||||
mux.HandleFunc("GET "+lp, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+lp, st.handleLoginPost)
|
||||
altLogin := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if altLogin != lp {
|
||||
mux.HandleFunc("GET "+altLogin, st.handleLoginGet)
|
||||
mux.HandleFunc("POST "+altLogin, st.handleLoginPost)
|
||||
}
|
||||
logoutPath := strings.TrimRight(st.auth.adminPrefix, "/") + "/logout"
|
||||
mux.HandleFunc("GET "+logoutPath, st.handleLogout)
|
||||
}
|
||||
if st.auth.register && st.auth.rbac {
|
||||
rp := st.auth.registerPath
|
||||
mux.HandleFunc("GET "+rp, st.handleRegisterGet)
|
||||
mux.HandleFunc("POST "+rp, st.handleRegisterPost)
|
||||
}
|
||||
if st.auth.rbac && st.dbURL != "" {
|
||||
mux.HandleFunc("GET /_rbac/roles", st.handleRbacListRoles)
|
||||
mux.HandleFunc("GET /_rbac/permissions", st.handleRbacListPermissions)
|
||||
mux.HandleFunc("POST /_rbac/roles", st.handleRbacCreateRole)
|
||||
mux.HandleFunc("POST /_rbac/roles/{id}/permissions", st.handleRbacSetRolePerms)
|
||||
mux.HandleFunc("POST /_rbac/assign", st.handleRbacAssign)
|
||||
mux.HandleFunc("GET /_rbac/desk", st.handleRbacDesk)
|
||||
}
|
||||
// Logout is part of session auth, not only built-in admin=True chrome.
|
||||
logoutPath := strings.TrimRight(st.auth.adminPrefix, "/") + "/logout"
|
||||
mux.HandleFunc("GET "+logoutPath, st.handleLogout)
|
||||
if st.auth.admin {
|
||||
adminHome := strings.TrimRight(st.auth.adminPrefix, "/")
|
||||
if adminHome == "" {
|
||||
@@ -327,7 +394,7 @@ func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
||||
}
|
||||
|
||||
func (st *state) handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
||||
if st.auth.users == nil {
|
||||
if st.auth.users == nil && !st.auth.rbac {
|
||||
http.Redirect(w, r, st.auth.loginRedirect, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -344,7 +411,7 @@ func (st *state) handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
if st.auth.users == nil {
|
||||
if st.auth.users == nil && !st.auth.rbac {
|
||||
http.Redirect(w, r, st.auth.loginRedirect, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
@@ -374,7 +441,7 @@ func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, loginPageHTML(st.auth.loginPath, &msg, csrf, r.FormValue("next")))
|
||||
return
|
||||
}
|
||||
res := auth.Login(username, password, st.auth.users, st.auth.sessionTTL)
|
||||
res := st.tryLogin(username, password)
|
||||
if ok, _ := res["ok"].(bool); !ok {
|
||||
ratelimit.RecordFailure(ip, username)
|
||||
errMsg := "Invalid username or password."
|
||||
@@ -385,11 +452,54 @@ func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
ratelimit.ClearSuccess(ip, username)
|
||||
sessID, _ := res["session_id"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.attachLoginPermissions(sessID, username, role)
|
||||
dest := loginDest(r, st.auth.loginRedirect)
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(sessID))
|
||||
http.Redirect(w, r, dest, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// tryLogin: GFM users first, then web_users when RBAC is on.
|
||||
func (st *state) tryLogin(username, password string) map[string]any {
|
||||
if st.auth.users != nil {
|
||||
res := auth.Login(username, password, st.auth.users, st.auth.sessionTTL)
|
||||
if ok, _ := res["ok"].(bool); ok {
|
||||
u, _ := res["username"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.seedGFMUserIntoRBAC(u, password, role)
|
||||
return res
|
||||
}
|
||||
}
|
||||
if st.auth.rbac && st.dbURL != "" {
|
||||
role, ok, err := rbac.Authenticate(st.dbURL, username, password, passwordVerify)
|
||||
if err == nil && ok {
|
||||
id := session.NewID(st.auth.sessionTTL)
|
||||
session.SetValue(id, "username", username)
|
||||
session.SetValue(id, "role", role)
|
||||
return map[string]any{
|
||||
"ok": true, "session_id": id, "username": username, "role": role,
|
||||
}
|
||||
}
|
||||
}
|
||||
return map[string]any{"ok": false}
|
||||
}
|
||||
|
||||
func (st *state) attachLoginPermissions(sessID, username, role string) {
|
||||
var perms []string
|
||||
if st.dbURL != "" && st.auth.rbac {
|
||||
if dbPerms, err := rbac.PermissionsForUser(st.dbURL, username); err == nil && len(dbPerms) > 0 {
|
||||
perms = dbPerms
|
||||
}
|
||||
}
|
||||
if len(perms) == 0 {
|
||||
perms = rbac.ExpandRole(role)
|
||||
}
|
||||
if len(perms) == 0 {
|
||||
perms = expandLegacyRole(role)
|
||||
}
|
||||
auth.AttachPermissions(sessID, perms)
|
||||
}
|
||||
|
||||
func (st *state) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
if sid, ok := session.IDFromCookie(r.Header.Get("Cookie")); ok {
|
||||
auth.Logout(sid)
|
||||
@@ -469,6 +579,307 @@ a.back:hover { color:#f5e6c8; }
|
||||
</body></html>`, errHTML, esc(loginAction), csrfField, nextField)
|
||||
}
|
||||
|
||||
func passwordVerify(pass, encoded string) bool {
|
||||
return password.Verify(pass, encoded)
|
||||
}
|
||||
|
||||
func (st *state) seedGFMUserIntoRBAC(username, plaintext, role string) {
|
||||
if !st.auth.rbac || st.dbURL == "" || username == "" {
|
||||
return
|
||||
}
|
||||
exists, err := rbac.UserExists(st.dbURL, username)
|
||||
if err != nil || exists {
|
||||
return
|
||||
}
|
||||
hash, err := password.Hash(plaintext)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
target := "member"
|
||||
switch strings.ToLower(role) {
|
||||
case "admin", "superadmin":
|
||||
target = "superadmin"
|
||||
case "editor", "author":
|
||||
// custom roles may not exist yet; fall back to member + ExpandRole at session
|
||||
target = "member"
|
||||
}
|
||||
_ = rbac.EnsureUserWithRole(st.dbURL, username, hash, target)
|
||||
}
|
||||
|
||||
func (st *state) handleRegisterGet(w http.ResponseWriter, r *http.Request) {
|
||||
_, csrf, setCookie := resolveSession(r)
|
||||
html := registerPageHTML(st.auth.registerPath, nil, csrf)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) handleRegisterPost(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
sid, csrf, setCookie := resolveSession(r)
|
||||
csrfToken := r.FormValue("_csrf")
|
||||
if csrfToken != "" {
|
||||
if !session.ValidateCSRF(sid, csrfToken) {
|
||||
msg := "Invalid CSRF token."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
return
|
||||
}
|
||||
}
|
||||
username := strings.TrimSpace(r.FormValue("username"))
|
||||
pass := r.FormValue("password")
|
||||
if len(username) < 2 || len(pass) < 4 {
|
||||
msg := "Username (≥2) and password (≥4) required."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
return
|
||||
}
|
||||
hash, err := password.Hash(pass)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := rbac.RegisterUser(st.dbURL, username, hash, st.auth.defaultRole); err != nil {
|
||||
msg := err.Error()
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
return
|
||||
}
|
||||
res := st.tryLogin(username, pass)
|
||||
if ok, _ := res["ok"].(bool); ok {
|
||||
sessID, _ := res["session_id"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.attachLoginPermissions(sessID, username, role)
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(sessID))
|
||||
dest := st.auth.loginRedirect
|
||||
held := session.PermissionsFromSession(sessID)
|
||||
if !session.PermissionAllowed(held, []string{"desk:access"}) {
|
||||
dest = "/"
|
||||
}
|
||||
http.Redirect(w, r, dest, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, st.auth.loginPath, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func registerPageHTML(action string, errMsg *string, csrf string) string {
|
||||
errHTML := ""
|
||||
if errMsg != nil && *errMsg != "" {
|
||||
errHTML = fmt.Sprintf("<p class=\"flash err\">%s</p>", esc(*errMsg))
|
||||
}
|
||||
csrfField := ""
|
||||
if csrf != "" {
|
||||
csrfField = fmt.Sprintf("<input type=\"hidden\" name=\"_csrf\" value=\"%s\"/>", esc(csrf))
|
||||
}
|
||||
return fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Register</title>
|
||||
<style>
|
||||
:root { --ink:#0a0809; --paper:#f5e6c8; --gold:#d4af37; --err:#b91c1c; }
|
||||
* { box-sizing:border-box; }
|
||||
body { margin:0; min-height:100vh; display:grid; place-items:center; color:var(--paper); font-family:"Noto Sans SC","IBM Plex Sans",system-ui,sans-serif;
|
||||
background: radial-gradient(ellipse 90%% 55%% at 50%% -15%%, rgba(107,30,58,.5), transparent 58%%), linear-gradient(180deg,#0a0809,#120c10 45%%,#0a0809); }
|
||||
.login { background:rgba(0,0,0,.45); border:1px solid rgba(212,175,55,.28); border-radius:12px; padding:2rem 2.25rem; width:min(92vw,22rem); }
|
||||
.login h1 { margin:0 0 .25rem; font-size:1.55rem; color:var(--gold); }
|
||||
.login .sub { color:rgba(245,230,200,.72); margin:0 0 1.25rem; font-size:.9rem; }
|
||||
.login form { display:grid; gap:.9rem; }
|
||||
.login label { display:grid; gap:.25rem; font-size:.85rem; }
|
||||
.login input { padding:.6rem .75rem; border:1px solid rgba(212,175,55,.35); border-radius:8px; font:inherit; color:var(--paper); background:rgba(10,8,9,.7); }
|
||||
.login button { background:linear-gradient(135deg,#f5e6c8,var(--gold)); color:var(--ink); border:0; padding:.65rem 1rem; border-radius:999px; cursor:pointer; font:inherit; font-weight:600; }
|
||||
.flash.err { background:rgba(185,28,28,.15); color:#fecaca; border:1px solid rgba(185,28,28,.45); padding:.6rem .8rem; border-radius:8px; margin:0 0 .9rem; font-size:.9rem; }
|
||||
a.back { color:var(--gold); text-decoration:none; font-size:.85rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="login">
|
||||
<h1>Create account</h1>
|
||||
<p class="sub">New members get the default role (comments).</p>
|
||||
%s
|
||||
<form method="post" action="%s">
|
||||
%s
|
||||
<label>Username<input name="username" autocomplete="username" required autofocus/></label>
|
||||
<label>Password<input name="password" type="password" autocomplete="new-password" required/></label>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
<p style="margin:1.1rem 0 0"><a class="back" href="/login">← Sign in</a></p>
|
||||
</div>
|
||||
</body></html>`, errHTML, esc(action), csrfField)
|
||||
}
|
||||
|
||||
func (st *state) requireRolesManage(w http.ResponseWriter, r *http.Request) bool {
|
||||
held := session.PermissionsFromCookie(r.Header.Get("Cookie"))
|
||||
if !session.PermissionAllowed(held, []string{"roles:manage"}) {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (st *state) writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func (st *state) handleRbacListRoles(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
roles, err := rbac.ListRoles(st.dbURL)
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusInternalServerError, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true, "roles": roles})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacListPermissions(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
perms, err := rbac.ListPermissions(st.dbURL)
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusInternalServerError, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true, "permissions": perms})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacCreateRole(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || strings.TrimSpace(body.Name) == "" {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "name required"})
|
||||
return
|
||||
}
|
||||
id, err := rbac.CreateRole(st.dbURL, strings.TrimSpace(body.Name))
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true, "id": id, "name": body.Name})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacSetRolePerms(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
idStr := r.PathValue("id")
|
||||
roleID, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "bad id"})
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Permissions []string `json:"permissions"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "bad json"})
|
||||
return
|
||||
}
|
||||
grantable := session.PermissionsFromCookie(r.Header.Get("Cookie"))
|
||||
if err := rbac.SetRolePermissions(st.dbURL, roleID, body.Permissions, grantable); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacAssign(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Username string `json:"username"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": "bad json"})
|
||||
return
|
||||
}
|
||||
if err := rbac.AssignRole(st.dbURL, body.Username, body.Role); err != nil {
|
||||
st.writeJSON(w, http.StatusBadRequest, map[string]any{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
st.writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
}
|
||||
|
||||
func (st *state) handleRbacDesk(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.requireRolesManage(w, r) {
|
||||
return
|
||||
}
|
||||
roles, _ := rbac.ListRoles(st.dbURL)
|
||||
perms, _ := rbac.ListPermissions(st.dbURL)
|
||||
var roleOpts, permChecks strings.Builder
|
||||
for _, role := range roles {
|
||||
name, _ := role["name"].(string)
|
||||
id := role["id"]
|
||||
sys, _ := role["is_system"].(bool)
|
||||
tag := ""
|
||||
if sys {
|
||||
tag = " (system)"
|
||||
}
|
||||
fmt.Fprintf(&roleOpts, `<option value="%v">%s%s</option>`, id, esc(name), tag)
|
||||
}
|
||||
for _, p := range perms {
|
||||
code, _ := p["code"].(string)
|
||||
desc, _ := p["description"].(string)
|
||||
fmt.Fprintf(&permChecks, `<label style="display:block;margin:.25rem 0"><input type="checkbox" name="perm" value="%s"/> %s — %s</label>`,
|
||||
esc(code), esc(code), esc(desc))
|
||||
}
|
||||
html := fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html lang="zh"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>Roles · RBAC</title>
|
||||
<style>
|
||||
body{font-family:"Noto Sans SC",system-ui,sans-serif;max-width:42rem;margin:2rem auto;padding:0 1rem;line-height:1.5}
|
||||
h1{font-size:1.4rem} section{margin:1.5rem 0;padding:1rem;border:1px solid #ddd;border-radius:8px}
|
||||
button{padding:.5rem 1rem;cursor:pointer} input,select{padding:.4rem;margin:.25rem 0;width:100%%;max-width:20rem}
|
||||
#msg{min-height:1.2rem;color:#b91c1c}
|
||||
</style></head><body>
|
||||
<h1>角色与权限</h1>
|
||||
<p>需 <code>roles:manage</code>。系统角色权限不可在此改写;可新建角色并勾选权限,或给用户赋角色。</p>
|
||||
<p id="msg"></p>
|
||||
<section>
|
||||
<h2>新建角色</h2>
|
||||
<input id="new-role" placeholder="角色名"/><button type="button" id="btn-create">创建</button>
|
||||
</section>
|
||||
<section>
|
||||
<h2>设置角色权限</h2>
|
||||
<select id="role-sel">%s</select>
|
||||
<div>%s</div>
|
||||
<button type="button" id="btn-set">保存权限</button>
|
||||
</section>
|
||||
<section>
|
||||
<h2>赋角色给用户</h2>
|
||||
<input id="assign-user" placeholder="用户名"/>
|
||||
<input id="assign-role" placeholder="角色名(如 member)"/>
|
||||
<button type="button" id="btn-assign">赋权</button>
|
||||
</section>
|
||||
<p><a href="/desk">← 后台</a></p>
|
||||
<script>
|
||||
const msg=document.getElementById('msg');
|
||||
async function j(url,opt){const r=await fetch(url,Object.assign({credentials:'same-origin',headers:{'Content-Type':'application/json'}},opt||{}));const d=await r.json();if(!d.ok)throw new Error(d.error||r.status);return d;}
|
||||
document.getElementById('btn-create').onclick=async()=>{try{const name=document.getElementById('new-role').value.trim();await j('/_rbac/roles',{method:'POST',body:JSON.stringify({name})});msg.textContent='已创建 '+name;location.reload();}catch(e){msg.textContent=e.message;}};
|
||||
document.getElementById('btn-set').onclick=async()=>{try{const id=document.getElementById('role-sel').value;const permissions=[...document.querySelectorAll('input[name=perm]:checked')].map(x=>x.value);await j('/_rbac/roles/'+id+'/permissions',{method:'POST',body:JSON.stringify({permissions})});msg.textContent='权限已保存';}catch(e){msg.textContent=e.message;}};
|
||||
document.getElementById('btn-assign').onclick=async()=>{try{const username=document.getElementById('assign-user').value.trim();const role=document.getElementById('assign-role').value.trim();await j('/_rbac/assign',{method:'POST',body:JSON.stringify({username,role})});msg.textContent='已赋 '+role+' → '+username;}catch(e){msg.textContent=e.message;}};
|
||||
</script>
|
||||
</body></html>`, roleOpts.String(), permChecks.String())
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func clientIP(r *http.Request) string {
|
||||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
||||
if i := strings.Index(xff, ","); i >= 0 {
|
||||
|
||||
@@ -22,9 +22,11 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/form"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ratelimit"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/render"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/sitemap"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/tenant"
|
||||
)
|
||||
|
||||
// NewHandler builds the HTTP handler for an app bag (no listen).
|
||||
@@ -35,6 +37,11 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
}
|
||||
authCfg := authConfigOf(appBag)
|
||||
dbURL := dbURLOf(appBag)
|
||||
if authCfg.rbac && dbURL != "" {
|
||||
if err := rbac.EnsureSchema(dbURL); err != nil {
|
||||
return nil, fmt.Errorf("rbac ensure: %w", err)
|
||||
}
|
||||
}
|
||||
session.Configure(session.Config{
|
||||
DBURL: dbURL,
|
||||
TTLSec: authCfg.sessionTTL,
|
||||
@@ -99,6 +106,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
proxyRoutes: routeMapOf(appBag, "proxy_routes"),
|
||||
invokeRoutes: routeMapOf(appBag, "invoke_routes"),
|
||||
auth: authCfg,
|
||||
tenant: tenant.FromBag(appBag),
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
@@ -132,7 +140,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
route := jr
|
||||
pattern := route.Method + " " + route.Path
|
||||
mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.handleJSON(w, route)
|
||||
st.handleJSON(w, r, route)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -217,6 +225,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
|
||||
handler := withMiddleware(mux, mw)
|
||||
handler = withRBAC(handler, authCfg.gates, authCfg.loginPath)
|
||||
handler = withTenant(handler, st.tenant)
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
@@ -280,6 +289,7 @@ type state struct {
|
||||
proxyRoutes map[string]any
|
||||
invokeRoutes map[string]any
|
||||
auth authConfig
|
||||
tenant tenant.Config
|
||||
}
|
||||
|
||||
func (st *state) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -432,6 +442,11 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
|
||||
data[k] = vs[0]
|
||||
}
|
||||
}
|
||||
if st.tenant.Enabled() && st.tenant.DefaultScope {
|
||||
if tid := tenant.FromContext(r.Context()); tid != "" {
|
||||
data = tenant.StampRow(data, st.tenant.Column, tid)
|
||||
}
|
||||
}
|
||||
res, err := form.Submit(frm, data, st.dbURL)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -447,12 +462,21 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) handleJSON(w http.ResponseWriter, route middleware.JSONRouteMount) {
|
||||
func (st *state) handleJSON(w http.ResponseWriter, r *http.Request, route middleware.JSONRouteMount) {
|
||||
if st.dbURL == "" {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": "no database"})
|
||||
return
|
||||
}
|
||||
opts := db.SelectOpts{Where: route.Where, Order: route.Order}
|
||||
where := route.Where
|
||||
if (route.TenantScope || st.tenant.DefaultScope) && st.tenant.Enabled() {
|
||||
tid := tenant.FromContext(r.Context())
|
||||
if tid == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "tenant required"})
|
||||
return
|
||||
}
|
||||
where = tenant.MergeWhere(where, st.tenant.Column, tid)
|
||||
}
|
||||
opts := db.SelectOpts{Where: where, Order: route.Order}
|
||||
out, err := db.Select(st.dbURL, route.Table, route.Limit, opts)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
||||
@@ -461,6 +485,17 @@ func (st *state) handleJSON(w http.ResponseWriter, route middleware.JSONRouteMou
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func withTenant(next http.Handler, cfg tenant.Config) http.Handler {
|
||||
if !cfg.Enabled() {
|
||||
return next
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := tenant.Resolve(r, cfg)
|
||||
ctx := tenant.WithContext(r.Context(), id)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func withMiddleware(next http.Handler, mw middleware.Config) http.Handler {
|
||||
h := next
|
||||
if mw.BodyLimit != nil && *mw.BodyLimit > 0 {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/plugin"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ws"
|
||||
)
|
||||
|
||||
@@ -13,14 +18,44 @@ var wsUpgrader = websocket.Upgrader{
|
||||
|
||||
func (st *state) mountWSRoutes(mux *http.ServeMux) {
|
||||
for _, path := range sortedKeys(st.wsRoutes) {
|
||||
spec := ws.ParseRouteSpec(st.wsRoutes[path]) // mode + optional room_key
|
||||
spec := ws.ParseRouteSpec(st.wsRoutes[path]) // mode + optional room_key / on_message
|
||||
routePath := path
|
||||
mux.HandleFunc("GET "+routePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
pattern := routePath
|
||||
if strings.Contains(routePath, "{") {
|
||||
pattern = goMuxPattern(routePath)
|
||||
}
|
||||
mux.HandleFunc("GET "+pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.handleWS(w, r, routePath, spec)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func pathValuesOf(r *http.Request, routePath string) map[string]string {
|
||||
out := map[string]string{}
|
||||
// Collect {name} tokens from the route template.
|
||||
rest := routePath
|
||||
for {
|
||||
i := strings.Index(rest, "{")
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
rest = rest[i+1:]
|
||||
j := strings.Index(rest, "}")
|
||||
if j < 0 {
|
||||
break
|
||||
}
|
||||
name := rest[:j]
|
||||
name = strings.TrimPrefix(name, "*")
|
||||
if name != "" {
|
||||
if v := r.PathValue(name); v != "" {
|
||||
out[name] = v
|
||||
}
|
||||
}
|
||||
rest = rest[j+1:]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, spec ws.RouteSpec) {
|
||||
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
@@ -28,6 +63,7 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
vals := pathValuesOf(r, path)
|
||||
switch spec.Mode {
|
||||
case ws.ModeEcho:
|
||||
for {
|
||||
@@ -53,11 +89,26 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
case ws.ModeBroadcast, ws.ModeRoom:
|
||||
room := path
|
||||
if spec.RoomKey != "" {
|
||||
room = spec.RoomKey
|
||||
room = ws.ExpandTemplate(spec.RoomKey, vals)
|
||||
} else if len(vals) > 0 {
|
||||
room = ws.ExpandTemplate(path, vals)
|
||||
}
|
||||
user := session.UsernameFromCookie(r.Header.Get("Cookie"))
|
||||
if user == "" {
|
||||
user = "anon"
|
||||
}
|
||||
ch := ws.JoinRoom(room)
|
||||
defer ws.LeaveRoom(room, ch)
|
||||
|
||||
presenceRoom := room + ".presence"
|
||||
var presenceCh chan string
|
||||
if spec.Presence {
|
||||
presenceCh = ws.JoinRoom(presenceRoom)
|
||||
defer ws.LeaveRoom(presenceRoom, presenceCh)
|
||||
ws.PublishRoom(presenceRoom, presenceJSON("join", user, room))
|
||||
defer ws.PublishRoom(presenceRoom, presenceJSON("leave", user, room))
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
@@ -67,7 +118,25 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
return
|
||||
}
|
||||
if mt == websocket.TextMessage {
|
||||
ws.PublishRoom(room, string(data))
|
||||
text := string(data)
|
||||
if spec.OnMessage != "" {
|
||||
out, err := plugin.CallLibPath(spec.OnMessage, map[string]any{
|
||||
"message": text,
|
||||
"room": room,
|
||||
"path": path,
|
||||
"user": user,
|
||||
})
|
||||
if err != nil {
|
||||
_ = conn.WriteMessage(websocket.TextMessage, []byte(`{"error":`+jsonQuote(err.Error())+`}`))
|
||||
continue
|
||||
}
|
||||
skip, rewritten := interpretOnMessage(out, text)
|
||||
if skip {
|
||||
continue
|
||||
}
|
||||
text = rewritten
|
||||
}
|
||||
ws.PublishRoom(room, text)
|
||||
} else if mt == websocket.CloseMessage {
|
||||
return
|
||||
}
|
||||
@@ -85,9 +154,66 @@ func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, s
|
||||
if err := conn.WriteMessage(websocket.TextMessage, []byte(text)); err != nil {
|
||||
return
|
||||
}
|
||||
case text, ok := <-presenceOrNil(presenceCh):
|
||||
if presenceCh == nil {
|
||||
continue
|
||||
}
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := conn.WriteMessage(websocket.TextMessage, []byte(text)); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func presenceOrNil(ch chan string) <-chan string {
|
||||
if ch == nil {
|
||||
return nil
|
||||
}
|
||||
return ch
|
||||
}
|
||||
|
||||
func presenceJSON(event, user, room string) string {
|
||||
b, _ := json.Marshal(map[string]any{
|
||||
"type": "presence",
|
||||
"event": event,
|
||||
"user": user,
|
||||
"room": room,
|
||||
})
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func jsonQuote(s string) string {
|
||||
b, _ := json.Marshal(s)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func interpretOnMessage(out any, original string) (skip bool, text string) {
|
||||
text = original
|
||||
m, ok := out.(map[string]any)
|
||||
if !ok {
|
||||
if s, ok := out.(string); ok && s != "" {
|
||||
return false, s
|
||||
}
|
||||
return false, original
|
||||
}
|
||||
if v, ok := m["skip"]; ok && boolish(v) {
|
||||
return true, ""
|
||||
}
|
||||
if v, ok := m["drop"]; ok && boolish(v) {
|
||||
return true, ""
|
||||
}
|
||||
if v, ok := m["message"]; ok {
|
||||
text = fmt.Sprint(v)
|
||||
} else if v, ok := m["消息"]; ok {
|
||||
text = fmt.Sprint(v)
|
||||
} else if v, ok := m["text"]; ok {
|
||||
text = fmt.Sprint(v)
|
||||
}
|
||||
return false, text
|
||||
}
|
||||
@@ -34,12 +34,13 @@ type CORSConfig struct {
|
||||
|
||||
// JSONRouteMount is one JSON API route with leading-slash path.
|
||||
type JSONRouteMount struct {
|
||||
Path string
|
||||
Method string
|
||||
Table string
|
||||
Where any
|
||||
Order string
|
||||
Limit int64
|
||||
Path string
|
||||
Method string
|
||||
Table string
|
||||
Where any
|
||||
Order string
|
||||
Limit int64
|
||||
TenantScope bool
|
||||
}
|
||||
|
||||
// Configure merges configure tables into app.middleware (Rust web_app_middleware).
|
||||
@@ -178,6 +179,11 @@ func parseJSONRoute(path string, spec any) (JSONRouteMount, bool) {
|
||||
if w, ok := m["where"]; ok && w != nil {
|
||||
r.Where = w
|
||||
}
|
||||
if v, ok := m["tenant_scope"]; ok {
|
||||
r.TenantScope = boolish(v)
|
||||
} else if v, ok := m["租户作用域"]; ok {
|
||||
r.TenantScope = boolish(v)
|
||||
}
|
||||
return r, true
|
||||
}
|
||||
|
||||
@@ -255,7 +261,7 @@ func SecurityFromTable(tableV any) map[string]any {
|
||||
return out
|
||||
}
|
||||
|
||||
// JSONRoutesFromTable normalizes `|路径|方法|表|条件|排序|上限|` into json_routes.
|
||||
// JSONRoutesFromTable normalizes `|路径|方法|表|条件|排序|上限|租户作用域|` into json_routes.
|
||||
func JSONRoutesFromTable(tableV any) map[string]any {
|
||||
rows, _ := table.AsRows(tableV).([]any)
|
||||
out := map[string]any{}
|
||||
@@ -291,6 +297,9 @@ func JSONRoutesFromTable(tableV any) map[string]any {
|
||||
if order != "" {
|
||||
spec["order"] = order
|
||||
}
|
||||
if scope := col(m, "租户作用域", "tenant_scope", "TenantScope"); scope != "" {
|
||||
spec["tenant_scope"] = boolish(scope)
|
||||
}
|
||||
out[path] = spec
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -0,0 +1,510 @@
|
||||
// Package rbac implements configurable role ⊥ permission for ext/web.
|
||||
package rbac
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
)
|
||||
|
||||
// Perm is one catalog entry.
|
||||
type Perm struct {
|
||||
Code string
|
||||
Resource string
|
||||
Action string
|
||||
Description string
|
||||
}
|
||||
|
||||
// DefaultCatalog is seeded when EnsureSchema runs.
|
||||
func DefaultCatalog() []Perm {
|
||||
return []Perm{
|
||||
{"comments:create", "comments", "create", "Create a comment"},
|
||||
{"comments:delete", "comments", "delete", "Delete any comment"},
|
||||
{"comments:delete_own", "comments", "delete_own", "Delete own comment"},
|
||||
{"desk:access", "desk", "access", "Access writing desk / admin chrome"},
|
||||
{"roles:manage", "roles", "manage", "Create roles and assign permissions"},
|
||||
{"posts:edit", "posts", "edit", "Create or edit posts"},
|
||||
{"users:manage", "users", "manage", "Manage user accounts"},
|
||||
}
|
||||
}
|
||||
|
||||
// AllCodes returns every default permission code.
|
||||
func AllCodes() []string {
|
||||
cat := DefaultCatalog()
|
||||
out := make([]string, len(cat))
|
||||
for i, p := range cat {
|
||||
out[i] = p.Code
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ExpandRole maps legacy/system role names to permission codes when DB grants are empty.
|
||||
func ExpandRole(role string) []string {
|
||||
switch strings.ToLower(strings.TrimSpace(role)) {
|
||||
case "superadmin", "admin":
|
||||
return AllCodes()
|
||||
case "member", "user":
|
||||
return []string{"comments:create", "comments:delete_own"}
|
||||
case "editor", "author":
|
||||
return []string{"comments:create", "comments:delete_own", "posts:edit", "desk:access"}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ParseCSV splits a CSV of permission or role tokens.
|
||||
func ParseCSV(s string) []string {
|
||||
var out []string
|
||||
for _, part := range strings.Split(s, ",") {
|
||||
part = strings.ToLower(strings.TrimSpace(part))
|
||||
if part != "" {
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// HasAny reports whether held contains any of needed (case-insensitive).
|
||||
// Empty needed → false. Held containing "*" → true.
|
||||
func HasAny(held, needed []string) bool {
|
||||
if len(needed) == 0 {
|
||||
return false
|
||||
}
|
||||
set := map[string]struct{}{}
|
||||
for _, h := range held {
|
||||
h = strings.ToLower(strings.TrimSpace(h))
|
||||
if h == "*" {
|
||||
return true
|
||||
}
|
||||
if h != "" {
|
||||
set[h] = struct{}{}
|
||||
}
|
||||
}
|
||||
for _, n := range needed {
|
||||
n = strings.ToLower(strings.TrimSpace(n))
|
||||
if _, ok := set[n]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MergeUnique concatenates permission lists.
|
||||
func MergeUnique(lists ...[]string) []string {
|
||||
seen := map[string]struct{}{}
|
||||
var out []string
|
||||
for _, list := range lists {
|
||||
for _, p := range list {
|
||||
p = strings.ToLower(strings.TrimSpace(p))
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[p]; ok {
|
||||
continue
|
||||
}
|
||||
seen[p] = struct{}{}
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
const schemaSQL = `
|
||||
CREATE TABLE IF NOT EXISTS web_permissions (
|
||||
code TEXT PRIMARY KEY,
|
||||
resource TEXT NOT NULL,
|
||||
action TEXT NOT NULL,
|
||||
description TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS web_roles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
is_system INTEGER NOT NULL DEFAULT 0,
|
||||
tenant_id TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS web_role_permissions (
|
||||
role_id INTEGER NOT NULL,
|
||||
permission_code TEXT NOT NULL,
|
||||
PRIMARY KEY (role_id, permission_code),
|
||||
FOREIGN KEY (role_id) REFERENCES web_roles(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (permission_code) REFERENCES web_permissions(code) ON DELETE CASCADE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS web_users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TEXT
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS web_user_roles (
|
||||
user_id INTEGER NOT NULL,
|
||||
role_id INTEGER NOT NULL,
|
||||
tenant_id TEXT NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (user_id, role_id, tenant_id),
|
||||
FOREIGN KEY (user_id) REFERENCES web_users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (role_id) REFERENCES web_roles(id) ON DELETE CASCADE
|
||||
);
|
||||
`
|
||||
|
||||
// EnsureSchema creates RBAC tables and seeds default catalog + system roles.
|
||||
func EnsureSchema(dbURL string) error {
|
||||
if strings.TrimSpace(dbURL) == "" {
|
||||
return fmt.Errorf("rbac: empty db url")
|
||||
}
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := sqlDB.Exec(schemaSQL); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range DefaultCatalog() {
|
||||
_, _ = sqlDB.Exec(
|
||||
`INSERT OR IGNORE INTO web_permissions(code, resource, action, description) VALUES(?,?,?,?)`,
|
||||
p.Code, p.Resource, p.Action, p.Description,
|
||||
)
|
||||
}
|
||||
if err := ensureSystemRole(sqlDB, "superadmin", true, AllCodes()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureSystemRole(sqlDB, "member", true, []string{"comments:create", "comments:delete_own"}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureSystemRole(sqlDB *sql.DB, name string, isSystem bool, perms []string) error {
|
||||
var id int64
|
||||
err := sqlDB.QueryRow(`SELECT id FROM web_roles WHERE name = ?`, name).Scan(&id)
|
||||
if err == sql.ErrNoRows {
|
||||
sys := 0
|
||||
if isSystem {
|
||||
sys = 1
|
||||
}
|
||||
res, err := sqlDB.Exec(`INSERT INTO web_roles(name, is_system) VALUES(?,?)`, name, sys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, err = res.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, code := range perms {
|
||||
_, _ = sqlDB.Exec(
|
||||
`INSERT OR IGNORE INTO web_role_permissions(role_id, permission_code) VALUES(?,?)`,
|
||||
id, code,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PermissionsForUser returns permission codes granted via user_roles for username.
|
||||
func PermissionsForUser(dbURL, username string) ([]string, error) {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := sqlDB.Query(`
|
||||
SELECT DISTINCT rp.permission_code
|
||||
FROM web_users u
|
||||
JOIN web_user_roles ur ON ur.user_id = u.id
|
||||
JOIN web_role_permissions rp ON rp.role_id = ur.role_id
|
||||
WHERE u.username = ?`, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var code string
|
||||
if err := rows.Scan(&code); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, strings.ToLower(code))
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// AssignRole binds username to role name (creates user row link only; user must exist in web_users).
|
||||
func AssignRole(dbURL, username, roleName string) error {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var uid, rid int64
|
||||
if err := sqlDB.QueryRow(`SELECT id FROM web_users WHERE username = ?`, username).Scan(&uid); err != nil {
|
||||
return fmt.Errorf("rbac assign: user %q: %w", username, err)
|
||||
}
|
||||
if err := sqlDB.QueryRow(`SELECT id FROM web_roles WHERE name = ?`, roleName).Scan(&rid); err != nil {
|
||||
return fmt.Errorf("rbac assign: role %q: %w", roleName, err)
|
||||
}
|
||||
_, err = sqlDB.Exec(
|
||||
`INSERT OR IGNORE INTO web_user_roles(user_id, role_id, tenant_id) VALUES(?,?,?)`,
|
||||
uid, rid, "",
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateRole inserts a non-system role; returns role id.
|
||||
func CreateRole(dbURL, name string) (int64, error) {
|
||||
if err := EnsureSchema(dbURL); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
res, err := sqlDB.Exec(`INSERT INTO web_roles(name, is_system) VALUES(?,0)`, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
||||
// SetRolePermissions replaces role's permissions. grantable restricts what can be set (anti-escalation);
|
||||
// empty grantable means allow all catalog codes.
|
||||
func SetRolePermissions(dbURL string, roleID int64, codes, grantable []string) error {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var isSystem int
|
||||
if err := sqlDB.QueryRow(`SELECT is_system FROM web_roles WHERE id = ?`, roleID).Scan(&isSystem); err != nil {
|
||||
return err
|
||||
}
|
||||
if isSystem == 1 {
|
||||
return fmt.Errorf("rbac: cannot rewrite system role permissions via set")
|
||||
}
|
||||
allow := map[string]struct{}{}
|
||||
if len(grantable) == 0 {
|
||||
for _, c := range AllCodes() {
|
||||
allow[c] = struct{}{}
|
||||
}
|
||||
} else {
|
||||
for _, c := range grantable {
|
||||
allow[strings.ToLower(c)] = struct{}{}
|
||||
}
|
||||
}
|
||||
tx, err := sqlDB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
if _, err := tx.Exec(`DELETE FROM web_role_permissions WHERE role_id = ?`, roleID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, code := range codes {
|
||||
code = strings.ToLower(strings.TrimSpace(code))
|
||||
if code == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := allow[code]; !ok {
|
||||
return fmt.Errorf("rbac: cannot grant permission %q (not held by actor)", code)
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO web_role_permissions(role_id, permission_code) VALUES(?,?)`,
|
||||
roleID, code,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// CreateUser inserts a web_users row with hashed password; returns user id.
|
||||
func CreateUser(dbURL, username, passwordHash string) (int64, error) {
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" || passwordHash == "" {
|
||||
return 0, fmt.Errorf("rbac: username and password_hash required")
|
||||
}
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
res, err := sqlDB.Exec(
|
||||
`INSERT INTO web_users(username, password_hash, created_at) VALUES(?,?,datetime('now'))`,
|
||||
username, passwordHash,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
||||
// UserExists reports whether username is in web_users.
|
||||
func UserExists(dbURL, username string) (bool, error) {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var id int64
|
||||
err = sqlDB.QueryRow(`SELECT id FROM web_users WHERE username = ?`, username).Scan(&id)
|
||||
if err == sql.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Authenticate checks username/password against web_users; returns primary role name.
|
||||
func Authenticate(dbURL, username, pass string, verify func(password, encoded string) bool) (role string, ok bool, err error) {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
var hash string
|
||||
var uid int64
|
||||
err = sqlDB.QueryRow(`SELECT id, password_hash FROM web_users WHERE username = ?`, username).Scan(&uid, &hash)
|
||||
if err == sql.ErrNoRows {
|
||||
return "", false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if verify == nil || !verify(pass, hash) {
|
||||
return "", false, nil
|
||||
}
|
||||
var roleName string
|
||||
err = sqlDB.QueryRow(`
|
||||
SELECT r.name FROM web_user_roles ur
|
||||
JOIN web_roles r ON r.id = ur.role_id
|
||||
WHERE ur.user_id = ?
|
||||
ORDER BY r.is_system DESC, r.name
|
||||
LIMIT 1`, uid).Scan(&roleName)
|
||||
if err == sql.ErrNoRows {
|
||||
return "member", true, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
return strings.ToLower(roleName), true, nil
|
||||
}
|
||||
|
||||
// RegisterUser creates web_users + assigns defaultRole (default member).
|
||||
func RegisterUser(dbURL, username, passwordHash, defaultRole string) error {
|
||||
if defaultRole == "" {
|
||||
defaultRole = "member"
|
||||
}
|
||||
exists, err := UserExists(dbURL, username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
return fmt.Errorf("rbac: username already taken")
|
||||
}
|
||||
if _, err := CreateUser(dbURL, username, passwordHash); err != nil {
|
||||
return err
|
||||
}
|
||||
return AssignRole(dbURL, username, defaultRole)
|
||||
}
|
||||
|
||||
// EnsureUserWithRole creates user if missing (hash may be empty → skip create) and assigns role.
|
||||
func EnsureUserWithRole(dbURL, username, passwordHash, roleName string) error {
|
||||
exists, err := UserExists(dbURL, username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if passwordHash == "" {
|
||||
return fmt.Errorf("rbac: cannot create user %q without hash", username)
|
||||
}
|
||||
if _, err := CreateUser(dbURL, username, passwordHash); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return AssignRole(dbURL, username, roleName)
|
||||
}
|
||||
|
||||
// ListRoles returns [{id,name,is_system}, …].
|
||||
func ListRoles(dbURL string) ([]map[string]any, error) {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := sqlDB.Query(`SELECT id, name, is_system FROM web_roles ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []map[string]any
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var name string
|
||||
var sys int
|
||||
if err := rows.Scan(&id, &name, &sys); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, map[string]any{"id": id, "name": name, "is_system": sys == 1})
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ListPermissions returns catalog rows.
|
||||
func ListPermissions(dbURL string) ([]map[string]any, error) {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := sqlDB.Query(`SELECT code, resource, action, description FROM web_permissions ORDER BY code`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []map[string]any
|
||||
for rows.Next() {
|
||||
var code, resource, action, desc string
|
||||
if err := rows.Scan(&code, &resource, &action, &desc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, map[string]any{
|
||||
"code": code, "resource": resource, "action": action, "description": desc,
|
||||
})
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// RolePermissionCodes returns permission codes for a role id.
|
||||
func RolePermissionCodes(dbURL string, roleID int64) ([]string, error) {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := sqlDB.Query(`SELECT permission_code FROM web_role_permissions WHERE role_id = ? ORDER BY permission_code`, roleID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var code string
|
||||
if err := rows.Scan(&code); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, code)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SetRolePermissionsByName looks up role by name then SetRolePermissions.
|
||||
func SetRolePermissionsByName(dbURL, roleName string, codes, grantable []string) error {
|
||||
sqlDB, err := openDB(dbURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var id int64
|
||||
if err := sqlDB.QueryRow(`SELECT id FROM web_roles WHERE name = ?`, roleName).Scan(&id); err != nil {
|
||||
return fmt.Errorf("rbac role %q: %w", roleName, err)
|
||||
}
|
||||
return SetRolePermissions(dbURL, id, codes, grantable)
|
||||
}
|
||||
|
||||
func openDB(dbURL string) (*sql.DB, error) {
|
||||
// Reuse db package pool via a no-op select path: open through Ensure-compatible helper.
|
||||
return db.OpenSQL(dbURL)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package rbac_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
)
|
||||
|
||||
func TestHasAnyAndExpandRole(t *testing.T) {
|
||||
if !rbac.HasAny([]string{"desk:access", "comments:create"}, []string{"desk:access"}) {
|
||||
t.Fatal("expected desk:access held")
|
||||
}
|
||||
if rbac.HasAny([]string{"comments:create"}, []string{"desk:access"}) {
|
||||
t.Fatal("member should not have desk")
|
||||
}
|
||||
if !rbac.HasAny([]string{"*"}, []string{"roles:manage"}) {
|
||||
t.Fatal("star should allow all")
|
||||
}
|
||||
admin := rbac.ExpandRole("admin")
|
||||
if !rbac.HasAny(admin, []string{"roles:manage", "desk:access"}) {
|
||||
t.Fatalf("admin expand incomplete: %v", admin)
|
||||
}
|
||||
member := rbac.ExpandRole("member")
|
||||
if rbac.HasAny(member, []string{"desk:access"}) {
|
||||
t.Fatal("member must not get desk:access")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureSchemaAndAssign(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
url := "sqlite:" + filepath.Join(dir, "rbac.db")
|
||||
if err := rbac.EnsureSchema(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sqlDB, err := db.OpenSQL(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = sqlDB.Exec(`INSERT INTO web_users(username, password_hash, created_at) VALUES('alice','x','now')`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := rbac.AssignRole(url, "alice", "member"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
perms, err := rbac.PermissionsForUser(url, "alice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !rbac.HasAny(perms, []string{"comments:create"}) {
|
||||
t.Fatalf("alice perms=%v", perms)
|
||||
}
|
||||
if rbac.HasAny(perms, []string{"roles:manage"}) {
|
||||
t.Fatal("member should not manage roles")
|
||||
}
|
||||
}
|
||||
@@ -612,6 +612,20 @@ func RoleFromCookie(cookieHeader string) string {
|
||||
return "visitor"
|
||||
}
|
||||
|
||||
// UsernameFromCookie returns the session username or empty string.
|
||||
func UsernameFromCookie(cookieHeader string) string {
|
||||
sid, ok := IDFromCookie(cookieHeader)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if v, ok := GetValue(sid, "username"); ok {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// RoleAllowed reports whether role is in allowed (empty allowed = all).
|
||||
func RoleAllowed(role string, allowed []string) bool {
|
||||
if len(allowed) == 0 {
|
||||
@@ -625,3 +639,51 @@ func RoleAllowed(role string, allowed []string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PermissionsFromCookie returns session permission codes (empty if anonymous).
|
||||
func PermissionsFromCookie(cookieHeader string) []string {
|
||||
sid, ok := IDFromCookie(cookieHeader)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return PermissionsFromSession(sid)
|
||||
}
|
||||
|
||||
// PermissionsFromSession reads the permissions CSV from a session id.
|
||||
func PermissionsFromSession(sid string) []string {
|
||||
if v, ok := GetValue(sid, "permissions"); ok {
|
||||
if s, ok := v.(string); ok && s != "" {
|
||||
return ParseRolesCSV(s)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPermissions stores permission codes on the session.
|
||||
func SetPermissions(sid string, codes []string) {
|
||||
SetValue(sid, "permissions", strings.Join(codes, ","))
|
||||
}
|
||||
|
||||
// PermissionAllowed reports whether the session holds any of needed.
|
||||
func PermissionAllowed(held, needed []string) bool {
|
||||
if len(needed) == 0 {
|
||||
return true
|
||||
}
|
||||
set := map[string]struct{}{}
|
||||
for _, h := range held {
|
||||
h = strings.ToLower(strings.TrimSpace(h))
|
||||
if h == "*" {
|
||||
return true
|
||||
}
|
||||
if h != "" {
|
||||
set[h] = struct{}{}
|
||||
}
|
||||
}
|
||||
for _, n := range needed {
|
||||
n = strings.ToLower(strings.TrimSpace(n))
|
||||
if _, ok := set[n]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
// Package tenant resolves multi-tenant request context for ext/web.
|
||||
package tenant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const requestKey ctxKey = 1
|
||||
|
||||
// Config is stored on the app bag under key "tenant".
|
||||
type Config struct {
|
||||
Mode string // path | subdomain | header
|
||||
Param string // path capture / query / header name
|
||||
Column string // DB column (default tenant_id)
|
||||
DefaultScope bool // auto-scope all selects/inserts when true
|
||||
}
|
||||
|
||||
// FromBag reads tenant config from an app bag.
|
||||
func FromBag(appBag map[string]any) Config {
|
||||
cfg := Config{Column: "tenant_id"}
|
||||
raw, ok := appBag["tenant"].(map[string]any)
|
||||
if !ok {
|
||||
return cfg
|
||||
}
|
||||
cfg.Mode = strings.ToLower(strings.TrimSpace(str(raw, "mode", "模式")))
|
||||
cfg.Param = strings.TrimSpace(str(raw, "param", "参数", "tenant_param"))
|
||||
if c := str(raw, "column", "列", "tenant_column"); c != "" {
|
||||
cfg.Column = c
|
||||
}
|
||||
if v, ok := raw["default_scope"]; ok {
|
||||
cfg.DefaultScope = boolish(v)
|
||||
} else if v, ok := raw["默认作用域"]; ok {
|
||||
cfg.DefaultScope = boolish(v)
|
||||
} else if v, ok := raw["scope"]; ok {
|
||||
cfg.DefaultScope = boolish(v)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// Enabled reports whether tenant resolution is configured.
|
||||
func (c Config) Enabled() bool {
|
||||
return c.Mode != ""
|
||||
}
|
||||
|
||||
// Configure merges tenant options onto the app bag.
|
||||
func Configure(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||
if opts == nil {
|
||||
opts = map[string]any{}
|
||||
}
|
||||
mode := strings.ToLower(strings.TrimSpace(str(opts, "mode", "模式")))
|
||||
if mode == "" {
|
||||
return nil, fmt.Errorf("tenant: mode required (path|subdomain|header)")
|
||||
}
|
||||
switch mode {
|
||||
case "path", "路径", "subdomain", "子域", "header", "头":
|
||||
default:
|
||||
return nil, fmt.Errorf("tenant: unknown mode %q", mode)
|
||||
}
|
||||
if mode == "路径" {
|
||||
mode = "path"
|
||||
}
|
||||
if mode == "子域" {
|
||||
mode = "subdomain"
|
||||
}
|
||||
if mode == "头" {
|
||||
mode = "header"
|
||||
}
|
||||
param := strings.TrimSpace(str(opts, "param", "参数", "tenant_param"))
|
||||
if param == "" {
|
||||
switch mode {
|
||||
case "path":
|
||||
param = "t"
|
||||
case "header":
|
||||
param = "X-Tenant-Id"
|
||||
case "subdomain":
|
||||
param = ""
|
||||
}
|
||||
}
|
||||
col := strings.TrimSpace(str(opts, "column", "列", "tenant_column"))
|
||||
if col == "" {
|
||||
col = "tenant_id"
|
||||
}
|
||||
entry := map[string]any{
|
||||
"mode": mode,
|
||||
"param": param,
|
||||
"column": col,
|
||||
}
|
||||
if v, ok := first(opts, "default_scope", "默认作用域", "scope"); ok {
|
||||
entry["default_scope"] = boolish(v)
|
||||
}
|
||||
out := clone(appBag)
|
||||
out["tenant"] = entry
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Resolve extracts tenant id from the HTTP request.
|
||||
func Resolve(r *http.Request, cfg Config) string {
|
||||
if !cfg.Enabled() {
|
||||
return ""
|
||||
}
|
||||
switch cfg.Mode {
|
||||
case "path":
|
||||
if cfg.Param != "" {
|
||||
if v := r.PathValue(cfg.Param); v != "" {
|
||||
return sanitize(v)
|
||||
}
|
||||
if v := r.URL.Query().Get(cfg.Param); v != "" {
|
||||
return sanitize(v)
|
||||
}
|
||||
// /t/{tenant}/… when param is t: take segment after /t/
|
||||
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
||||
if cfg.Param == "t" && len(parts) >= 2 && parts[0] == "t" {
|
||||
return sanitize(parts[1])
|
||||
}
|
||||
for i, p := range parts {
|
||||
if p == cfg.Param && i+1 < len(parts) {
|
||||
return sanitize(parts[i+1])
|
||||
}
|
||||
}
|
||||
}
|
||||
case "subdomain":
|
||||
host := r.Host
|
||||
if h, _, ok := strings.Cut(host, ":"); ok {
|
||||
host = h
|
||||
}
|
||||
labels := strings.Split(host, ".")
|
||||
if len(labels) >= 3 {
|
||||
return sanitize(labels[0])
|
||||
}
|
||||
case "header":
|
||||
name := cfg.Param
|
||||
if name == "" {
|
||||
name = "X-Tenant-Id"
|
||||
}
|
||||
return sanitize(r.Header.Get(name))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// WithContext stores tenant id on the request context.
|
||||
func WithContext(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, requestKey, id)
|
||||
}
|
||||
|
||||
// FromContext reads tenant id (empty if unset).
|
||||
func FromContext(ctx context.Context) string {
|
||||
if ctx == nil {
|
||||
return ""
|
||||
}
|
||||
if s, ok := ctx.Value(requestKey).(string); ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// MergeWhere adds tenant_id equality to a where value (map or list of maps).
|
||||
func MergeWhere(where any, column, tenantID string) any {
|
||||
if tenantID == "" || column == "" {
|
||||
return where
|
||||
}
|
||||
clause := map[string]any{"字段": column, "field": column, "操作": "=", "op": "=", "值": tenantID, "value": tenantID}
|
||||
switch t := where.(type) {
|
||||
case nil:
|
||||
return []any{clause}
|
||||
case []any:
|
||||
out := make([]any, 0, len(t)+1)
|
||||
out = append(out, t...)
|
||||
out = append(out, clause)
|
||||
return out
|
||||
case map[string]any:
|
||||
out := cloneMap(t)
|
||||
out[column] = tenantID
|
||||
return out
|
||||
default:
|
||||
return []any{where, clause}
|
||||
}
|
||||
}
|
||||
|
||||
// StampRow sets tenant column on an insert/update row map if missing.
|
||||
func StampRow(row map[string]any, column, tenantID string) map[string]any {
|
||||
if row == nil || tenantID == "" || column == "" {
|
||||
return row
|
||||
}
|
||||
if _, ok := row[column]; ok {
|
||||
return row
|
||||
}
|
||||
out := cloneMap(row)
|
||||
out[column] = tenantID
|
||||
return out
|
||||
}
|
||||
|
||||
func sanitize(s string) string {
|
||||
s = strings.TrimSpace(strings.ToLower(s))
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' || r == '_' {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func str(m map[string]any, keys ...string) string {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok && v != nil {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func first(m map[string]any, keys ...string) (any, bool) {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok && v != nil {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func boolish(v any) bool {
|
||||
switch t := v.(type) {
|
||||
case bool:
|
||||
return t
|
||||
case string:
|
||||
switch strings.TrimSpace(t) {
|
||||
case "true", "True", "1", "yes", "on", "真":
|
||||
return true
|
||||
}
|
||||
case float64:
|
||||
return t != 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func clone(m map[string]any) map[string]any {
|
||||
out := make(map[string]any, len(m))
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneMap(m map[string]any) map[string]any { return clone(m) }
|
||||
@@ -0,0 +1,58 @@
|
||||
package tenant_test
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/tenant"
|
||||
)
|
||||
|
||||
func TestResolvePathQuery(t *testing.T) {
|
||||
cfg := tenant.Config{Mode: "path", Param: "t", Column: "tenant_id"}
|
||||
r := httptest.NewRequest("GET", "/api/items?t=acme", nil)
|
||||
if got := tenant.Resolve(r, cfg); got != "acme" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolvePathPrefix(t *testing.T) {
|
||||
cfg := tenant.Config{Mode: "path", Param: "t"}
|
||||
r := httptest.NewRequest("GET", "/t/beta/items", nil)
|
||||
if got := tenant.Resolve(r, cfg); got != "beta" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHeader(t *testing.T) {
|
||||
cfg := tenant.Config{Mode: "header", Param: "X-Tenant-Id"}
|
||||
r := httptest.NewRequest("GET", "/", nil)
|
||||
r.Header.Set("X-Tenant-Id", "Gamma")
|
||||
if got := tenant.Resolve(r, cfg); got != "gamma" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeWhereAndStamp(t *testing.T) {
|
||||
w := tenant.MergeWhere(nil, "tenant_id", "acme")
|
||||
arr, ok := w.([]any)
|
||||
if !ok || len(arr) != 1 {
|
||||
t.Fatalf("where=%v", w)
|
||||
}
|
||||
row := tenant.StampRow(map[string]any{"title": "x"}, "tenant_id", "acme")
|
||||
if row["tenant_id"] != "acme" {
|
||||
t.Fatalf("row=%v", row)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigure(t *testing.T) {
|
||||
out, err := tenant.Configure(map[string]any{}, map[string]any{
|
||||
"mode": "path", "param": "org", "default_scope": true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := tenant.FromBag(out)
|
||||
if cfg.Mode != "path" || cfg.Param != "org" || !cfg.DefaultScope {
|
||||
t.Fatalf("%+v", cfg)
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,15 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/app"
|
||||
)
|
||||
|
||||
// RouteSpec is one ws_routes entry (mode + optional room_key template).
|
||||
// RouteSpec is one ws_routes entry (mode + optional room_key / on_message / presence).
|
||||
type RouteSpec struct {
|
||||
Mode Mode
|
||||
RoomKey string
|
||||
Mode Mode
|
||||
RoomKey string
|
||||
OnMessage string
|
||||
Presence bool
|
||||
}
|
||||
|
||||
// RouteWS registers path → {mode, room_key?} on app.ws_routes (Rust web_app_route_ws).
|
||||
// RouteWS registers path → {mode, room_key?, on_message?, presence?} on app.ws_routes.
|
||||
func RouteWS(appBag map[string]any, path string, args map[string]any) (map[string]any, error) {
|
||||
path, err := app.NormalizeRoutePath(path, appBag)
|
||||
if err != nil {
|
||||
@@ -24,6 +26,8 @@ func RouteWS(appBag map[string]any, path string, args map[string]any) (map[strin
|
||||
if mode == ModeRoom && roomKey == "" {
|
||||
return nil, fmt.Errorf("mode=room requires room_key")
|
||||
}
|
||||
onMsg := OnMessageFromArgs(args)
|
||||
presence := PresenceFromArgs(args)
|
||||
out := clone(appBag)
|
||||
wsRoutes := map[string]any{}
|
||||
if r, ok := out["ws_routes"].(map[string]any); ok {
|
||||
@@ -33,6 +37,12 @@ func RouteWS(appBag map[string]any, path string, args map[string]any) (map[strin
|
||||
if roomKey != "" {
|
||||
spec["room_key"] = roomKey
|
||||
}
|
||||
if onMsg != "" {
|
||||
spec["on_message"] = onMsg
|
||||
}
|
||||
if presence {
|
||||
spec["presence"] = true
|
||||
}
|
||||
wsRoutes[path] = spec
|
||||
out["ws_routes"] = wsRoutes
|
||||
return out, nil
|
||||
@@ -46,6 +56,34 @@ func RoomKeyFromArgs(args map[string]any) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// OnMessageFromArgs reads on_message / 消息钩子 (lib.member path).
|
||||
func OnMessageFromArgs(args map[string]any) string {
|
||||
if v, ok := first(args, "on_message", "消息钩子", "钩子"); ok {
|
||||
return strings.TrimSpace(fmt.Sprint(v))
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// PresenceFromArgs reads presence / 在场 flag.
|
||||
func PresenceFromArgs(args map[string]any) bool {
|
||||
if v, ok := first(args, "presence", "在场"); ok {
|
||||
return boolish(v)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ExpandTemplate replaces `{name}` tokens using values from PathValue-style map.
|
||||
func ExpandTemplate(tmpl string, values map[string]string) string {
|
||||
if tmpl == "" || len(values) == 0 {
|
||||
return tmpl
|
||||
}
|
||||
out := tmpl
|
||||
for k, v := range values {
|
||||
out = strings.ReplaceAll(out, "{"+k+"}", v)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RoutesOf reads ws_routes from an app bag.
|
||||
func RoutesOf(appBag map[string]any) map[string]Mode {
|
||||
out := map[string]Mode{}
|
||||
@@ -117,6 +155,16 @@ func parseRouteSpec(spec any) RouteSpec {
|
||||
if rk, ok := t["room_key"]; ok {
|
||||
out.RoomKey = strings.TrimSpace(fmt.Sprint(rk))
|
||||
}
|
||||
if om, ok := t["on_message"]; ok {
|
||||
out.OnMessage = strings.TrimSpace(fmt.Sprint(om))
|
||||
} else if om, ok := t["消息钩子"]; ok {
|
||||
out.OnMessage = strings.TrimSpace(fmt.Sprint(om))
|
||||
}
|
||||
if p, ok := t["presence"]; ok {
|
||||
out.Presence = boolish(p)
|
||||
} else if p, ok := t["在场"]; ok {
|
||||
out.Presence = boolish(p)
|
||||
}
|
||||
case string:
|
||||
out.Mode = ParseMode(t)
|
||||
default:
|
||||
|
||||
@@ -113,6 +113,33 @@ func TestRouteWSRoomRequiresKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandTemplate(t *testing.T) {
|
||||
got := ws.ExpandTemplate("chat.room.{id}", map[string]string{"id": "lobby"})
|
||||
if got != "chat.room.lobby" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteWSOnMessageAndPresence(t *testing.T) {
|
||||
a := app.New(nil)
|
||||
out, err := ws.RouteWS(a, "/chat/{id}", map[string]any{
|
||||
"mode": "room",
|
||||
"room_key": "chat.room.{id}",
|
||||
"on_message": "chat.persist",
|
||||
"presence": true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec, err := ws.RouteSpecOf(out, "/chat/{id}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec.OnMessage != "chat.persist" || !spec.Presence {
|
||||
t.Fatalf("spec=%+v", spec)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectRefusesBadURL(t *testing.T) {
|
||||
out := ws.Connect("http://127.0.0.1:1", "hi", nil, 1)
|
||||
if out["ok"] == true {
|
||||
|
||||
+27
-7
@@ -106,6 +106,15 @@ extern int web_auth_logout(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_auth_new(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_auth(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_gate(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_rbac(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_app_tenant(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_can(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_ensure(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_assign_role(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_create_role(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_set_role_permissions(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_list_roles(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_rbac_list_permissions(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_storage_new(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_storage_put(char *args_json, char **out_json, char **err_msg);
|
||||
extern int web_storage_get(char *args_json, char **out_json, char **err_msg);
|
||||
@@ -199,8 +208,17 @@ static int register_core(void) {
|
||||
if (host_register((char *)"web_auth_check", (char *)"session_id", web_auth_check) != 0) return 1;
|
||||
if (host_register((char *)"web_auth_logout", (char *)"session_id", web_auth_logout) != 0) return 1;
|
||||
if (host_register((char *)"web_auth_new", (char *)"users,session_ttl", web_auth_new) != 0) return 1;
|
||||
if (host_register((char *)"web_app_auth", (char *)"app,users,session_ttl,admin_prefix,login_redirect,logout_redirect,login_path", web_app_auth) != 0) return 1;
|
||||
if (host_register((char *)"web_app_gate", (char *)"app,path,roles,match,on_deny,exclude", web_app_gate) != 0) return 1;
|
||||
if (host_register((char *)"web_app_auth", (char *)"app,users,session_ttl,admin_prefix,login_redirect,logout_redirect,login_path,register,register_path,default_role", web_app_auth) != 0) return 1;
|
||||
if (host_register((char *)"web_app_gate", (char *)"app,path,roles,permissions,match,on_deny,exclude", web_app_gate) != 0) return 1;
|
||||
if (host_register((char *)"web_app_rbac", (char *)"app,catalog", web_app_rbac) != 0) return 1;
|
||||
if (host_register((char *)"web_app_tenant", (char *)"app,mode,param,column,default_scope", web_app_tenant) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_can", (char *)"permissions,needed", web_rbac_can) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_ensure", (char *)"url", web_rbac_ensure) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_assign_role", (char *)"url,username,role", web_rbac_assign_role) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_create_role", (char *)"url,name", web_rbac_create_role) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_set_role_permissions", (char *)"url,role,permissions,grantable", web_rbac_set_role_permissions) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_list_roles", (char *)"url", web_rbac_list_roles) != 0) return 1;
|
||||
if (host_register((char *)"web_rbac_list_permissions", (char *)"url", web_rbac_list_permissions) != 0) return 1;
|
||||
if (host_register((char *)"web_storage_new", (char *)"url", web_storage_new) != 0) return 1;
|
||||
if (host_register((char *)"web_storage_put", (char *)"url,key,body,path,content_type", web_storage_put) != 0) return 1;
|
||||
if (host_register((char *)"web_storage_get", (char *)"url,key", web_storage_get) != 0) return 1;
|
||||
@@ -226,7 +244,7 @@ static int register_core(void) {
|
||||
if (host_register((char *)"web_app_sitemap", (char *)"app,path,base,table,loc,limit,items", web_app_sitemap) != 0) return 1;
|
||||
if (host_register((char *)"web_app_robots", (char *)"app,body,sitemap", web_app_robots) != 0) return 1;
|
||||
if (host_register((char *)"web_sitemap_build", (char *)"base,items", web_sitemap_build) != 0) return 1;
|
||||
if (host_register((char *)"web_app_route_ws", (char *)"app,path,echo,mode", web_app_route_ws) != 0) return 1;
|
||||
if (host_register((char *)"web_app_route_ws", (char *)"app,path,echo,mode,room_key,on_message,presence", web_app_route_ws) != 0) return 1;
|
||||
if (host_register((char *)"web_ws_connect", (char *)"url,message,headers,timeout_sec", web_ws_connect) != 0) return 1;
|
||||
if (host_register((char *)"web_cache_new", (char *)"url", web_cache_new) != 0) return 1;
|
||||
if (host_register((char *)"web_cache_get", (char *)"url,key", web_cache_get) != 0) return 1;
|
||||
@@ -424,12 +442,14 @@ func resolveDbURL(url string) string {
|
||||
}
|
||||
|
||||
func dbURLOf(args map[string]any) (string, error) {
|
||||
if s, ok := argStr(args, "url", "db_url"); ok && s != "" {
|
||||
if s, ok := argStr(args, "url", "db_url", "数据库"); ok && s != "" {
|
||||
return resolveDbURL(s), nil
|
||||
}
|
||||
if dbObj, ok := args["db"].(map[string]any); ok {
|
||||
if s, ok := argStr(dbObj, "url"); ok && s != "" {
|
||||
return resolveDbURL(s), nil
|
||||
for _, key := range []string{"url", "db", "数据库"} {
|
||||
if dbObj, ok := args[key].(map[string]any); ok {
|
||||
if s, ok := argStr(dbObj, "url"); ok && s != "" {
|
||||
return resolveDbURL(s), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("missing db url")
|
||||
|
||||
@@ -13,6 +13,25 @@ AI already prefers Markdown-shaped output. The idea was: why keep docs and code
|
||||
|
||||
The language is still young: structure markers, bilingual core names (`print` / `打印`, …), importable stdlibs (see [`stdlib/`](stdlib/) on this site), local `view`, and static export. Contribute on [GitHub](https://github.com/cflmy/marqdo).
|
||||
|
||||
## Install
|
||||
|
||||
Pick one way to get the CLI:
|
||||
|
||||
| Method | Notes |
|
||||
|--------|-------|
|
||||
| Ubuntu PPA | `sudo add-apt-repository ppa:cflmy/marqdo` then `sudo apt install marqdo` |
|
||||
| GitHub Releases | Prebuilt Windows / Linux zips at [releases](https://github.com/cflmy/marqdo/releases) |
|
||||
| From source | `cargo build --release` (needs Rust) |
|
||||
|
||||
Official extensions version independently of the CLI:
|
||||
|
||||
```text
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
```
|
||||
|
||||
`ext add` tries CDN https://ext.marqdo.com first, then GitHub, then the proxy mirror. See [`features/05-extensions.mq.md`](features/05-extensions.mq.md).
|
||||
|
||||
中文文档请看同目录的 [`00-欢迎.mq.md`](00-欢迎.mq.md)(叙述与内置均用中文)。
|
||||
|
||||
A small function to say hello:
|
||||
|
||||
@@ -13,6 +13,25 @@ AI 偏爱 Markdown结构的输出。作者的念头是:何必再把文档和
|
||||
|
||||
语言仍在起步:结构标记、中英内核双名(`打印` / `print` …)、可导入的标准库(见本站 [`stdlib/`](stdlib/))、本地 `view` 与静态导出。欢迎到 [GitHub](https://github.com/cflmy/marqdo) 一起做。
|
||||
|
||||
## 安装
|
||||
|
||||
解释器(CLI)任选其一:
|
||||
|
||||
| 方式 | 说明 |
|
||||
|------|------|
|
||||
| Ubuntu PPA | `sudo add-apt-repository ppa:cflmy/marqdo` 后 `sudo apt install marqdo` |
|
||||
| GitHub Releases | [releases](https://github.com/cflmy/marqdo/releases) 下载 Windows / Linux 预编译包 |
|
||||
| 源码 | `cargo build --release`(需本机 Rust) |
|
||||
|
||||
官方扩展与 CLI 可分开更新。安装扩展:
|
||||
|
||||
```text
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
```
|
||||
|
||||
`ext add` 下载顺序:CDN https://ext.marqdo.com → GitHub → proxy 镜像。详见本站 [`features/05-扩展.mq.md`](features/05-扩展.mq.md)。
|
||||
|
||||
English docs: [`00-welcome.mq.md`](00-welcome.mq.md) (English prose and English builtins).
|
||||
|
||||
用一个小函数打个招呼:
|
||||
|
||||
@@ -5,7 +5,7 @@ description: Optional packages — not stdlib
|
||||
|
||||
# main
|
||||
|
||||
ext/ holds official optional extensions. They are not part of lib/. Resolve via MARQDO_EXT, ./ext, or ext/ next to the binary.
|
||||
ext/ holds official optional extensions. They are not part of lib/. Resolve via `~/.marqdo/ext`, MARQDO_EXT, ./ext, or ext/ next to the binary.
|
||||
|
||||
Install (v0.2+):
|
||||
|
||||
@@ -15,14 +15,18 @@ marqdo ext add llm
|
||||
marqdo ext add agent
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
marqdo ext add linalg
|
||||
```
|
||||
|
||||
For packages with a native plugin, build first then add:
|
||||
`ext add` downloads L1 sources and prebuilt natives when available. Order: CDN https://ext.marqdo.com → GitHub Releases → proxy mirror. Pack SemVer is `ext/VERSION` and may differ from the CLI.
|
||||
|
||||
Rebuild natives locally only when developing plugins:
|
||||
|
||||
```text
|
||||
cargo build --release -p marqdo_plugin_agent
|
||||
cargo build --release -p marqdo_plugin_web
|
||||
cargo build --release -p marqdo_plugin_quantum
|
||||
cargo build --release -p marqdo_plugin_linalg
|
||||
bash ./scripts/build-web-plugin.sh
|
||||
marqdo ext add agent
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
|
||||
@@ -5,7 +5,7 @@ description: 可选包 — 不是标准库
|
||||
|
||||
# main
|
||||
|
||||
ext/ 存放官方可选扩展,不属于 lib/。解析顺序:MARQDO_EXT、./ext、或可执行文件旁的 ext/。
|
||||
ext/ 存放官方可选扩展,不属于 lib/。解析顺序:`~/.marqdo/ext`、MARQDO_EXT、./ext、或可执行文件旁的 ext/。
|
||||
|
||||
安装(v0.2+):
|
||||
|
||||
@@ -15,14 +15,18 @@ marqdo ext add llm
|
||||
marqdo ext add agent
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
marqdo ext add linalg
|
||||
```
|
||||
|
||||
带原生插件的包需先编译再 add:
|
||||
`ext add` 会下载 L1 与预编译 native(若有)。下载顺序:CDN https://ext.marqdo.com → GitHub Releases → proxy 镜像。扩展包版本见 `ext/VERSION`,可与 CLI 分开发版。
|
||||
|
||||
带原生插件的包若需本地重编:
|
||||
|
||||
```text
|
||||
cargo build --release -p marqdo_plugin_agent
|
||||
cargo build --release -p marqdo_plugin_web
|
||||
cargo build --release -p marqdo_plugin_quantum
|
||||
cargo build --release -p marqdo_plugin_linalg
|
||||
bash ./scripts/build-web-plugin.sh
|
||||
marqdo ext add agent
|
||||
marqdo ext add web
|
||||
marqdo ext add quantum
|
||||
|
||||
Executable
+155
@@ -0,0 +1,155 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build a signed Ubuntu *source* package for Launchpad PPA.
|
||||
# Usage: ./scripts/ppa-build-source.sh [series]
|
||||
# Example: ./scripts/ppa-build-source.sh noble
|
||||
#
|
||||
# Launchpad builders have no network and noble's cargo (1.75) cannot read
|
||||
# Cargo.lock v4. This script therefore:
|
||||
# 1) cargo vendor → vendor/
|
||||
# 2) installs a modern Rust toolchain into third_party/rust (cached)
|
||||
# 3) packs both into the .orig.tar.xz for offline debuild on Launchpad
|
||||
set -euo pipefail
|
||||
|
||||
SERIES="${1:-noble}"
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
need() { command -v "$1" >/dev/null || { echo "missing tool: $1 (sudo apt install $2)"; exit 1; }; }
|
||||
need cargo cargo
|
||||
need debuild devscripts
|
||||
need dpkg-parsechangelog dpkg-dev
|
||||
need curl curl
|
||||
need tar tar
|
||||
need xz xz-utils
|
||||
|
||||
VER="$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)"
|
||||
# Debian revision comes from debian/changelog (e.g. 1.0.2-1ppa2); we append ~series.
|
||||
CHG_VER="$(dpkg-parsechangelog -l debian/changelog -S Version)"
|
||||
# Strip any previous ~series suffix from changelog version
|
||||
BASE_DEB_VER="${CHG_VER%%~*}"
|
||||
# If changelog still has old upstream, prefer Cargo VER for the upstream part
|
||||
if [[ "$BASE_DEB_VER" != "$VER"-* ]]; then
|
||||
# keep debian revision suffix after first '-' following VER if possible
|
||||
REV="${BASE_DEB_VER#*-}"
|
||||
if [[ "$BASE_DEB_VER" == *"-"* ]]; then
|
||||
BASE_DEB_VER="${VER}-${REV}"
|
||||
else
|
||||
BASE_DEB_VER="${VER}-1ppa1"
|
||||
fi
|
||||
fi
|
||||
DEB_VER="${BASE_DEB_VER}~${SERIES}"
|
||||
PKG_ROOT="${TMPDIR:-/tmp}/marqdo-ppa/${VER}"
|
||||
BUILD_AREA="${ROOT}/../build-area"
|
||||
GPG_KEY="${MARQDO_PPA_GPG_KEY:-505943294D04C803}"
|
||||
RUST_VER="${MARQDO_PPA_RUST_VERSION:-1.85.0}"
|
||||
RUST_TRIPLE="${MARQDO_PPA_RUST_TRIPLE:-x86_64-unknown-linux-gnu}"
|
||||
RUST_NAME="rust-${RUST_VER}-${RUST_TRIPLE}"
|
||||
RUST_CACHE="${MARQDO_PPA_RUST_CACHE:-${HOME}/.cache/marqdo-ppa}"
|
||||
RUST_TARBALL="${RUST_CACHE}/${RUST_NAME}.tar.gz"
|
||||
RUST_URL="${MARQDO_PPA_RUST_URL:-https://static.rust-lang.org/dist/${RUST_NAME}.tar.gz}"
|
||||
|
||||
echo "==> upstream ${VER} → debian ${DEB_VER} (${SERIES})"
|
||||
rm -rf "$PKG_ROOT"
|
||||
mkdir -p "$PKG_ROOT" "$BUILD_AREA" "$RUST_CACHE"
|
||||
|
||||
# Clean export of sources (no target/, .git)
|
||||
if command -v git >/dev/null && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
git archive --format=tar HEAD | tar -x -C "$PKG_ROOT"
|
||||
else
|
||||
rsync -a --exclude target --exclude .git --exclude build-area "$ROOT"/ "$PKG_ROOT"/
|
||||
fi
|
||||
|
||||
# Ensure debian/ from working tree (may be newer than last commit)
|
||||
rsync -a "$ROOT/debian/" "$PKG_ROOT/debian/"
|
||||
|
||||
cd "$PKG_ROOT"
|
||||
python3 - <<PY
|
||||
from pathlib import Path
|
||||
import re
|
||||
p = Path("debian/changelog")
|
||||
text = p.read_text(encoding="utf-8")
|
||||
text2 = re.sub(
|
||||
r"^marqdo \([^)]+\) [^;]+;",
|
||||
"marqdo (${DEB_VER}) ${SERIES};",
|
||||
text,
|
||||
count=1,
|
||||
flags=re.M,
|
||||
)
|
||||
p.write_text(text2, encoding="utf-8")
|
||||
print("changelog:", p.read_text(encoding="utf-8").splitlines()[0])
|
||||
PY
|
||||
|
||||
echo "==> cargo vendor (Launchpad offline)"
|
||||
mkdir -p .cargo
|
||||
cargo vendor --locked vendor >/tmp/marqdo-cargo-vendor.conf
|
||||
cat > .cargo/config.toml <<'EOF'
|
||||
[source.crates-io]
|
||||
replace-with = "vendored-sources"
|
||||
|
||||
[source.vendored-sources]
|
||||
directory = "vendor"
|
||||
EOF
|
||||
|
||||
echo "==> bundle Rust ${RUST_VER} (${RUST_TRIPLE}) for Launchpad"
|
||||
if [ ! -f "$RUST_TARBALL" ]; then
|
||||
echo " downloading ${RUST_URL}"
|
||||
curl -fL --retry 3 --retry-delay 2 -o "${RUST_TARBALL}.partial" "$RUST_URL"
|
||||
mv "${RUST_TARBALL}.partial" "$RUST_TARBALL"
|
||||
else
|
||||
echo " cache hit ${RUST_TARBALL}"
|
||||
fi
|
||||
rm -rf third_party/rust
|
||||
mkdir -p third_party
|
||||
RUST_EXTRACT="$(mktemp -d "${TMPDIR:-/tmp}/marqdo-rust.XXXXXX")"
|
||||
tar -xzf "$RUST_TARBALL" -C "$RUST_EXTRACT"
|
||||
# Official dist tarball contains install.sh
|
||||
INSTALL_SH="$(find "$RUST_EXTRACT" -maxdepth 2 -name install.sh | head -1)"
|
||||
if [ -z "$INSTALL_SH" ]; then
|
||||
echo "error: install.sh not found in ${RUST_TARBALL}" >&2
|
||||
exit 1
|
||||
fi
|
||||
# --disable-ldconfig avoids needing root; strip docs to save space
|
||||
bash "$INSTALL_SH" \
|
||||
--prefix="$PKG_ROOT/third_party/rust" \
|
||||
--without=rust-docs \
|
||||
--disable-ldconfig
|
||||
rm -rf "$RUST_EXTRACT"
|
||||
"$PKG_ROOT/third_party/rust/bin/rustc" --version
|
||||
"$PKG_ROOT/third_party/rust/bin/cargo" --version
|
||||
|
||||
# Always rebuild orig (vendor + rust toolchain change every run)
|
||||
cd "$(dirname "$PKG_ROOT")"
|
||||
ORIG="marqdo_${VER}.orig.tar.xz"
|
||||
echo "==> packing ${ORIG} (vendor + third_party/rust)"
|
||||
rm -f "$BUILD_AREA/$ORIG" "marqdo_${VER}.orig.tar.xz"
|
||||
tar -cJf "$BUILD_AREA/$ORIG" \
|
||||
--exclude=debian \
|
||||
-C "$(dirname "$PKG_ROOT")" "$(basename "$PKG_ROOT")"
|
||||
ln -sfn "$BUILD_AREA/$ORIG" "marqdo_${VER}.orig.tar.xz"
|
||||
ls -lh "$BUILD_AREA/$ORIG"
|
||||
|
||||
cd "$PKG_ROOT"
|
||||
echo "==> debuild -S (key ${GPG_KEY})"
|
||||
debuild -S -sa -k"${GPG_KEY}" -d
|
||||
|
||||
# debuild writes next to package parent (/tmp/marqdo-ppa/); collect into build-area
|
||||
mkdir -p "$BUILD_AREA"
|
||||
shopt -s nullglob
|
||||
for f in ../marqdo_"${DEB_VER}"* ../marqdo_"${VER}".orig.tar.*; do
|
||||
[ -e "$f" ] || continue
|
||||
if [ "$(realpath -m "$f")" = "$(realpath -m "$BUILD_AREA/$(basename "$f")")" ]; then
|
||||
continue
|
||||
fi
|
||||
cp -f "$f" "$BUILD_AREA/"
|
||||
done
|
||||
shopt -u nullglob
|
||||
|
||||
echo "==> artifacts in $BUILD_AREA:"
|
||||
ls -la "$BUILD_AREA"/marqdo_"${DEB_VER}"* "$BUILD_AREA"/marqdo_"${VER}".orig.tar.* 2>/dev/null || true
|
||||
CHANGES="$BUILD_AREA/marqdo_${DEB_VER}_source.changes"
|
||||
if [ -f "$CHANGES" ]; then
|
||||
echo
|
||||
echo "Upload with:"
|
||||
echo " dput ppa:cflmy/marqdo $CHANGES"
|
||||
echo " # or: ./scripts/ppa-ship.sh ${SERIES}"
|
||||
fi
|
||||
Executable
+107
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env bash
|
||||
# One-shot: install packaging tools → build signed source package → dput to Launchpad PPA.
|
||||
#
|
||||
# Run in YOUR terminal (needs sudo for apt + GPG passphrase for signing):
|
||||
# ./scripts/ppa-ship.sh # default series: noble
|
||||
# ./scripts/ppa-ship.sh resolute # match host /etc/os-release
|
||||
# ./scripts/ppa-ship.sh noble --no-upload
|
||||
# sudo ./scripts/ppa-ship.sh noble # also OK — build/sign/upload drop back to $SUDO_USER
|
||||
#
|
||||
# PPA: ppa:cflmy/marqdo | key: 505943294D04C803
|
||||
set -euo pipefail
|
||||
|
||||
SERIES="${1:-noble}"
|
||||
NO_UPLOAD=0
|
||||
for arg in "${@:2}"; do
|
||||
case "$arg" in
|
||||
--no-upload|-n) NO_UPLOAD=1 ;;
|
||||
*) echo "unknown arg: $arg" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
# Never cargo/debuild/dput as root — GPG lives in the human user's ~/.gnupg
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
if [ -z "${SUDO_USER:-}" ] || [ "$SUDO_USER" = root ]; then
|
||||
echo "error: run as a normal user, or: sudo -u \$USER ./scripts/ppa-ship.sh" >&2
|
||||
exit 1
|
||||
fi
|
||||
REAL_USER="$SUDO_USER"
|
||||
REAL_HOME="$(getent passwd "$REAL_USER" | cut -d: -f6)"
|
||||
echo "==> root detected; apt as root, build/upload as ${REAL_USER}"
|
||||
run_user() {
|
||||
local upath="${REAL_HOME}/.cargo/bin:/usr/local/bin:/usr/bin:/bin"
|
||||
sudo -u "$REAL_USER" -H --preserve-env=MARQDO_PPA_GPG_KEY \
|
||||
env HOME="$REAL_HOME" PATH="$upath" "$@"
|
||||
}
|
||||
APT=(apt-get)
|
||||
else
|
||||
REAL_USER="$(id -un)"
|
||||
REAL_HOME="$HOME"
|
||||
run_user() { "$@"; }
|
||||
APT=(sudo apt-get)
|
||||
fi
|
||||
|
||||
PPA_TARGET="${MARQDO_PPA:-ppa:cflmy/marqdo}"
|
||||
PKGS=(build-essential debhelper devscripts dput lintian
|
||||
dpkg-dev quilt cargo rustc pkg-config libssl-dev ca-certificates
|
||||
python3 xz-utils)
|
||||
|
||||
need_apt=0
|
||||
for bin in debuild dput dpkg-parsechangelog cargo gpg; do
|
||||
if ! command -v "$bin" >/dev/null 2>&1; then
|
||||
need_apt=1
|
||||
break
|
||||
fi
|
||||
done
|
||||
# dh_auto_* comes from debhelper even if debuild exists from a partial install
|
||||
if ! dpkg -s debhelper >/dev/null 2>&1; then
|
||||
need_apt=1
|
||||
fi
|
||||
|
||||
if [ "$need_apt" -eq 1 ]; then
|
||||
echo "==> installing packaging toolchain (sudo)…"
|
||||
"${APT[@]}" update -qq
|
||||
DEBIAN_FRONTEND=noninteractive "${APT[@]}" install -y "${PKGS[@]}"
|
||||
else
|
||||
echo "==> packaging tools already present"
|
||||
fi
|
||||
|
||||
echo "==> building source package for series=${SERIES}"
|
||||
run_user "$ROOT/scripts/ppa-build-source.sh" "$SERIES"
|
||||
|
||||
VER="$(sed -n 's/^version = "\(.*\)"/\1/p' "$ROOT/Cargo.toml" | head -1)"
|
||||
BUILD_AREA="$(cd "$ROOT/.." && pwd)/build-area"
|
||||
# Prefer changes matching this series; fall back to newest source.changes
|
||||
CHANGES="$(ls -1t "$BUILD_AREA"/marqdo_*"~${SERIES}"_source.changes 2>/dev/null | head -1 || true)"
|
||||
if [ -z "${CHANGES:-}" ]; then
|
||||
CHANGES="$(ls -1t "$BUILD_AREA"/marqdo_*_source.changes 2>/dev/null | head -1 || true)"
|
||||
fi
|
||||
if [ -z "${CHANGES:-}" ] || [ ! -f "$CHANGES" ]; then
|
||||
echo "error: no *_source.changes under $BUILD_AREA" >&2
|
||||
ls -la "$BUILD_AREA" 2>/dev/null || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "==> artifact: $CHANGES"
|
||||
if command -v lintian >/dev/null 2>&1; then
|
||||
echo "==> lintian (non-fatal)"
|
||||
run_user lintian -i "$CHANGES" || true
|
||||
fi
|
||||
|
||||
if [ "$NO_UPLOAD" -eq 1 ]; then
|
||||
echo
|
||||
echo "Skip upload (--no-upload). When ready:"
|
||||
echo " dput ${PPA_TARGET} ${CHANGES}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "==> dput ${PPA_TARGET}"
|
||||
run_user dput --unchecked "${PPA_TARGET}" "$CHANGES"
|
||||
|
||||
echo
|
||||
echo "Done. Watch builds:"
|
||||
echo " https://launchpad.net/~cflmy/+archive/ubuntu/marqdo/+builds"
|
||||
echo "Enable the Ubuntu series (${SERIES}) in PPA → Change details if Launchpad rejects the upload."
|
||||
Executable
+222
@@ -0,0 +1,222 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Upload Marqdo extension packs to Cloudflare R2 (CDN: https://ext.marqdo.com).
|
||||
|
||||
No third-party deps (stdlib only) — AWS Signature V4 PutObject.
|
||||
|
||||
Credentials: ~/.marqdo/r2.env (chmod 600; never commit). Required:
|
||||
R2_ENDPOINT, R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY
|
||||
Optional: R2_PUBLIC_BASE, R2_REGION (default auto)
|
||||
|
||||
Usage:
|
||||
python3 scripts/upload-ext-r2.py --version 1.0.2 dist/marqdo-1.0.2-ext.zip \\
|
||||
dist/marqdo-1.0.2-native-x86_64-unknown-linux-gnu.zip
|
||||
python3 scripts/upload-ext-r2.py --version 1.0.2 --from-dir dist/
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote, urlparse
|
||||
|
||||
|
||||
def load_env_file(path: Path) -> None:
|
||||
if not path.is_file():
|
||||
return
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
k, _, v = line.partition("=")
|
||||
os.environ.setdefault(k.strip(), v.strip().strip("'").strip('"'))
|
||||
|
||||
|
||||
def sha256_hex(data: bytes) -> str:
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def hmac_sha256(key: bytes, msg: str) -> bytes:
|
||||
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
|
||||
|
||||
|
||||
def signing_key(secret: str, datestamp: str, region: str, service: str) -> bytes:
|
||||
k_date = hmac_sha256(("AWS4" + secret).encode("utf-8"), datestamp)
|
||||
k_region = hmac.new(k_date, region.encode("utf-8"), hashlib.sha256).digest()
|
||||
k_service = hmac.new(k_region, service.encode("utf-8"), hashlib.sha256).digest()
|
||||
return hmac.new(k_service, b"aws4_request", hashlib.sha256).digest()
|
||||
|
||||
|
||||
def put_object(
|
||||
*,
|
||||
endpoint: str,
|
||||
bucket: str,
|
||||
key: str,
|
||||
body: bytes,
|
||||
content_type: str,
|
||||
access_key: str,
|
||||
secret_key: str,
|
||||
region: str,
|
||||
) -> None:
|
||||
endpoint = endpoint.rstrip("/")
|
||||
host = urlparse(endpoint).netloc
|
||||
# Path-style: https://endpoint/bucket/key
|
||||
canonical_uri = "/" + bucket + "/" + quote(key, safe="/~")
|
||||
amz_date = dt.datetime.now(dt.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
||||
datestamp = amz_date[:8]
|
||||
payload_hash = sha256_hex(body)
|
||||
canonical_headers = (
|
||||
f"content-type:{content_type}\n"
|
||||
f"host:{host}\n"
|
||||
f"x-amz-content-sha256:{payload_hash}\n"
|
||||
f"x-amz-date:{amz_date}\n"
|
||||
)
|
||||
signed_headers = "content-type;host;x-amz-content-sha256;x-amz-date"
|
||||
canonical_request = "\n".join(
|
||||
[
|
||||
"PUT",
|
||||
canonical_uri,
|
||||
"",
|
||||
canonical_headers,
|
||||
signed_headers,
|
||||
payload_hash,
|
||||
]
|
||||
)
|
||||
credential_scope = f"{datestamp}/{region}/s3/aws4_request"
|
||||
string_to_sign = "\n".join(
|
||||
[
|
||||
"AWS4-HMAC-SHA256",
|
||||
amz_date,
|
||||
credential_scope,
|
||||
sha256_hex(canonical_request.encode("utf-8")),
|
||||
]
|
||||
)
|
||||
sig = hmac.new(
|
||||
signing_key(secret_key, datestamp, region, "s3"),
|
||||
string_to_sign.encode("utf-8"),
|
||||
hashlib.sha256,
|
||||
).hexdigest()
|
||||
auth = (
|
||||
f"AWS4-HMAC-SHA256 Credential={access_key}/{credential_scope}, "
|
||||
f"SignedHeaders={signed_headers}, Signature={sig}"
|
||||
)
|
||||
url = endpoint + canonical_uri
|
||||
req = urllib.request.Request(url, data=body, method="PUT")
|
||||
req.add_header("Content-Type", content_type)
|
||||
req.add_header("x-amz-content-sha256", payload_hash)
|
||||
req.add_header("x-amz-date", amz_date)
|
||||
req.add_header("Authorization", auth)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||||
if resp.status not in (200, 201):
|
||||
raise RuntimeError(f"PUT {key} HTTP {resp.status}")
|
||||
except urllib.error.HTTPError as e:
|
||||
detail = e.read().decode("utf-8", errors="replace")
|
||||
raise RuntimeError(f"PUT {key} HTTP {e.code}: {detail}") from e
|
||||
|
||||
|
||||
def content_type_for(path: Path) -> str:
|
||||
if path.name == "VERSION" or path.suffix.lower() == ".txt":
|
||||
return "text/plain; charset=utf-8"
|
||||
if path.suffix.lower() == ".json":
|
||||
return "application/json"
|
||||
return "application/zip"
|
||||
|
||||
|
||||
def upload_file(
|
||||
path: Path,
|
||||
key: str,
|
||||
*,
|
||||
endpoint: str,
|
||||
bucket: str,
|
||||
access_key: str,
|
||||
secret_key: str,
|
||||
region: str,
|
||||
public_base: str,
|
||||
) -> None:
|
||||
body = path.read_bytes()
|
||||
print(f"upload {path.name} → s3://{bucket}/{key} ({len(body)} bytes)")
|
||||
put_object(
|
||||
endpoint=endpoint,
|
||||
bucket=bucket,
|
||||
key=key,
|
||||
body=body,
|
||||
content_type=content_type_for(path),
|
||||
access_key=access_key,
|
||||
secret_key=secret_key,
|
||||
region=region,
|
||||
)
|
||||
print(f" public: {public_base.rstrip('/')}/{key}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
load_env_file(Path.home() / ".marqdo" / "r2.env")
|
||||
load_env_file(Path(".env"))
|
||||
load_env_file(Path(".env.r2"))
|
||||
|
||||
ap = argparse.ArgumentParser(description=__doc__)
|
||||
ap.add_argument("--version", required=True, help="Extension pack SemVer (no v prefix)")
|
||||
ap.add_argument("--from-dir", type=Path, help="Upload all marqdo-{VER}-*.zip from dir")
|
||||
ap.add_argument("files", nargs="*", type=Path, help="Files to upload")
|
||||
ap.add_argument("--no-latest", action="store_true", help="Do not update latest/ pointers")
|
||||
args = ap.parse_args()
|
||||
|
||||
ver = args.version.lstrip("v")
|
||||
for key in ("R2_ENDPOINT", "R2_BUCKET", "R2_ACCESS_KEY_ID", "R2_SECRET_ACCESS_KEY"):
|
||||
if not os.environ.get(key):
|
||||
print(
|
||||
f"missing {key}; write ~/.marqdo/r2.env (see .env.r2.example)",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
endpoint = os.environ["R2_ENDPOINT"]
|
||||
bucket = os.environ["R2_BUCKET"]
|
||||
access_key = os.environ["R2_ACCESS_KEY_ID"]
|
||||
secret_key = os.environ["R2_SECRET_ACCESS_KEY"]
|
||||
region = os.environ.get("R2_REGION", "auto")
|
||||
public_base = os.environ.get("R2_PUBLIC_BASE", "https://ext.marqdo.com")
|
||||
|
||||
files: list[Path] = [p for p in args.files if p.is_file()]
|
||||
if args.from_dir:
|
||||
files.extend(sorted(args.from_dir.glob(f"marqdo-{ver}-*.zip")))
|
||||
if not files:
|
||||
print("no files to upload", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
kw = dict(
|
||||
endpoint=endpoint,
|
||||
bucket=bucket,
|
||||
access_key=access_key,
|
||||
secret_key=secret_key,
|
||||
region=region,
|
||||
public_base=public_base,
|
||||
)
|
||||
for path in files:
|
||||
upload_file(path, f"v{ver}/{path.name}", **kw)
|
||||
if not args.no_latest:
|
||||
upload_file(path, f"latest/{path.name}", **kw)
|
||||
|
||||
if not args.no_latest:
|
||||
with tempfile.NamedTemporaryFile("w", encoding="utf-8", delete=False) as tf:
|
||||
tf.write(ver + "\n")
|
||||
tmp = Path(tf.name)
|
||||
try:
|
||||
upload_file(tmp, "latest/VERSION", **kw)
|
||||
upload_file(tmp, f"v{ver}/VERSION", **kw)
|
||||
finally:
|
||||
tmp.unlink(missing_ok=True)
|
||||
|
||||
print("done.")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+173
-33
@@ -1,16 +1,32 @@
|
||||
//! Download official `ext/` L1 sources and native plugins from GitHub Releases.
|
||||
//! Download official `ext/` L1 sources and native plugins.
|
||||
//!
|
||||
//! Enables `marqdo ext add web` without a local Rust toolchain or repo checkout.
|
||||
//! Disable with `MARQDO_EXT_NO_DOWNLOAD=1`. Override tag with `MARQDO_EXT_VERSION=0.3.4`.
|
||||
//!
|
||||
//! Disable with `MARQDO_EXT_NO_DOWNLOAD=1`. Override pack SemVer with
|
||||
//! `MARQDO_EXT_VERSION` (independent of CLI `CARGO_PKG_VERSION`).
|
||||
//!
|
||||
//! Download order (first success wins):
|
||||
//! 1. `MARQDO_EXT_DOWNLOAD_BASE` (optional override)
|
||||
//! 2. **CDN** `https://ext.marqdo.com` (Cloudflare R2 custom domain)
|
||||
//! 3. GitHub Releases `github.com/cflmy/marqdo`
|
||||
//! 4. GitHub via reverse proxy `proxy.cflmy.top`
|
||||
|
||||
use std::env;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{self, Write};
|
||||
use std::io::{self, Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
|
||||
const REPO: &str = "cflmy/marqdo";
|
||||
/// Public CDN for extension packs (R2 bucket `marqdo`, custom domain).
|
||||
pub const EXT_CDN_BASE: &str = "https://ext.marqdo.com";
|
||||
/// HTTPS reverse proxy used when github.com is slow or blocked.
|
||||
const GITHUB_MIRROR_PREFIX: &str = "https://proxy.cflmy.top/github.com";
|
||||
/// Packaged with the repo; may diverge from CLI SemVer for ext-only releases.
|
||||
const EMBEDDED_EXT_VERSION: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/ext/VERSION"));
|
||||
|
||||
pub fn downloads_enabled() -> bool {
|
||||
match env::var("MARQDO_EXT_NO_DOWNLOAD") {
|
||||
@@ -22,17 +38,69 @@ pub fn downloads_enabled() -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// SemVer without leading `v` (matches release asset names).
|
||||
fn normalize_semver(raw: &str) -> Option<String> {
|
||||
let v = raw.trim().trim_start_matches('v').trim();
|
||||
if v.is_empty() {
|
||||
return None;
|
||||
}
|
||||
// one line only
|
||||
let v = v.lines().next()?.trim().trim_start_matches('v').trim();
|
||||
if v.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(v.to_string())
|
||||
}
|
||||
|
||||
/// SemVer of the **extension pack** (may differ from CLI version).
|
||||
///
|
||||
/// Resolution order:
|
||||
/// 1. `MARQDO_EXT_VERSION`
|
||||
/// 2. CDN `GET {EXT_CDN}/latest/VERSION` (short timeout; skipped if downloads disabled)
|
||||
/// 3. Embedded `ext/VERSION` at build time
|
||||
/// 4. CLI `CARGO_PKG_VERSION`
|
||||
pub fn release_version() -> String {
|
||||
if let Ok(v) = env::var("MARQDO_EXT_VERSION") {
|
||||
let v = v.trim().trim_start_matches('v');
|
||||
if !v.is_empty() {
|
||||
return v.to_string();
|
||||
if let Some(n) = normalize_semver(&v) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
if downloads_enabled() {
|
||||
if let Some(n) = fetch_cdn_latest_version() {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
if let Some(n) = normalize_semver(EMBEDDED_EXT_VERSION) {
|
||||
return n;
|
||||
}
|
||||
env!("CARGO_PKG_VERSION").to_string()
|
||||
}
|
||||
|
||||
fn fetch_cdn_latest_version() -> Option<String> {
|
||||
static CACHED: OnceLock<Option<String>> = OnceLock::new();
|
||||
CACHED
|
||||
.get_or_init(|| {
|
||||
let base = env::var("MARQDO_EXT_CDN")
|
||||
.ok()
|
||||
.map(|s| s.trim().trim_end_matches('/').to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| EXT_CDN_BASE.to_string());
|
||||
let url = format!("{base}/latest/VERSION");
|
||||
let agent = ureq::AgentBuilder::new()
|
||||
.timeout_connect(Duration::from_secs(5))
|
||||
.timeout_read(Duration::from_secs(10))
|
||||
.user_agent(&user_agent())
|
||||
.build();
|
||||
let resp = agent.get(&url).call().ok()?;
|
||||
if !(200..300).contains(&resp.status()) {
|
||||
return None;
|
||||
}
|
||||
let mut body = String::new();
|
||||
resp.into_reader().read_to_string(&mut body).ok()?;
|
||||
normalize_semver(&body)
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub fn host_target_triple() -> Option<&'static str> {
|
||||
if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
|
||||
Some("x86_64-pc-windows-msvc")
|
||||
@@ -61,13 +129,58 @@ fn user_agent() -> String {
|
||||
format!("marqdo/{}", env!("CARGO_PKG_VERSION"))
|
||||
}
|
||||
|
||||
fn http_agent() -> ureq::Agent {
|
||||
ureq::AgentBuilder::new()
|
||||
.timeout_connect(Duration::from_secs(20))
|
||||
.timeout_read(Duration::from_secs(600))
|
||||
.user_agent(&user_agent())
|
||||
.build()
|
||||
}
|
||||
|
||||
fn cdn_base() -> String {
|
||||
env::var("MARQDO_EXT_CDN")
|
||||
.ok()
|
||||
.map(|s| s.trim().trim_end_matches('/').to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| EXT_CDN_BASE.to_string())
|
||||
}
|
||||
|
||||
/// Candidate download URLs for a release asset.
|
||||
///
|
||||
/// Order: optional `MARQDO_EXT_DOWNLOAD_BASE` → CDN → GitHub → GitHub proxy.
|
||||
pub fn release_asset_urls(ver: &str, filename: &str) -> Vec<String> {
|
||||
let mut urls = Vec::new();
|
||||
if let Ok(base) = env::var("MARQDO_EXT_DOWNLOAD_BASE") {
|
||||
let base = base.trim().trim_end_matches('/');
|
||||
if !base.is_empty() {
|
||||
urls.push(format!("{base}/{REPO}/releases/download/v{ver}/{filename}"));
|
||||
urls.push(format!("{base}/releases/download/v{ver}/{filename}"));
|
||||
urls.push(format!("{base}/v{ver}/{filename}"));
|
||||
urls.push(format!("{base}/{filename}"));
|
||||
}
|
||||
}
|
||||
let cdn = cdn_base();
|
||||
// Preferred public layout on ext.marqdo.com / R2
|
||||
urls.push(format!("{cdn}/v{ver}/{filename}"));
|
||||
urls.push(format!("{cdn}/releases/v{ver}/{filename}"));
|
||||
urls.push(format!("{cdn}/{filename}"));
|
||||
urls.push(format!(
|
||||
"https://github.com/{REPO}/releases/download/v{ver}/{filename}"
|
||||
));
|
||||
urls.push(format!(
|
||||
"{GITHUB_MIRROR_PREFIX}/{REPO}/releases/download/v{ver}/{filename}"
|
||||
));
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
urls.into_iter().filter(|u| seen.insert(u.clone())).collect()
|
||||
}
|
||||
|
||||
fn download_to_file(url: &str, dest: &Path) -> Result<()> {
|
||||
if let Some(parent) = dest.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
let tmp = dest.with_extension("download-tmp");
|
||||
let resp = ureq::get(url)
|
||||
.set("User-Agent", &user_agent())
|
||||
let resp = http_agent()
|
||||
.get(url)
|
||||
.set("Accept", "application/octet-stream")
|
||||
.call()
|
||||
.with_context(|| format!("GET {url}"))?;
|
||||
@@ -86,6 +199,30 @@ fn download_to_file(url: &str, dest: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn download_first_ok(urls: &[String], dest: &Path) -> Result<()> {
|
||||
let mut errors: Vec<String> = Vec::new();
|
||||
for (i, url) in urls.iter().enumerate() {
|
||||
if i == 0 {
|
||||
println!("downloading ({url})…");
|
||||
} else {
|
||||
println!("retry ({url})…");
|
||||
}
|
||||
match download_to_file(url, dest) {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => {
|
||||
eprintln!("note: {e:#}");
|
||||
errors.push(format!("{url} → {e:#}"));
|
||||
let _ = fs::remove_file(dest.with_extension("download-tmp"));
|
||||
}
|
||||
}
|
||||
}
|
||||
bail!(
|
||||
"all download mirrors failed for {}:\n - {}",
|
||||
dest.file_name().and_then(|s| s.to_str()).unwrap_or("asset"),
|
||||
errors.join("\n - ")
|
||||
)
|
||||
}
|
||||
|
||||
fn extract_zip(zip_path: &Path, dest_dir: &Path) -> Result<()> {
|
||||
fs::create_dir_all(dest_dir)?;
|
||||
let file = File::open(zip_path).with_context(|| format!("open {}", zip_path.display()))?;
|
||||
@@ -121,10 +258,6 @@ fn extract_zip(zip_path: &Path, dest_dir: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn release_asset_url(ver: &str, filename: &str) -> String {
|
||||
format!("https://github.com/{REPO}/releases/download/v{ver}/{filename}")
|
||||
}
|
||||
|
||||
/// Ensure L1 `ext/` tree is available (repo checkout or downloaded Release zip).
|
||||
/// Returns a directory that contains `web/web.mq.md`, `ai/…`, etc.
|
||||
pub fn ensure_ext_source_tree() -> Result<PathBuf> {
|
||||
@@ -134,7 +267,6 @@ pub fn ensure_ext_source_tree() -> Result<PathBuf> {
|
||||
if marker.is_file() {
|
||||
return Ok(cache);
|
||||
}
|
||||
// Zip layout from CI: top-level `ext/…`
|
||||
let nested = cache.join("ext").join("web").join("web.mq.md");
|
||||
if nested.is_file() {
|
||||
return Ok(cache.join("ext"));
|
||||
@@ -143,16 +275,14 @@ pub fn ensure_ext_source_tree() -> Result<PathBuf> {
|
||||
bail!("extension sources not found and downloads disabled (MARQDO_EXT_NO_DOWNLOAD)");
|
||||
}
|
||||
let zip_name = format!("marqdo-{ver}-ext.zip");
|
||||
let url = release_asset_url(&ver, &zip_name);
|
||||
let urls = release_asset_urls(&ver, &zip_name);
|
||||
let zip_path = cache_root()?.join(&zip_name);
|
||||
println!("downloading official ext sources ({url})…");
|
||||
download_to_file(&url, &zip_path)?;
|
||||
download_first_ok(&urls, &zip_path)?;
|
||||
let stage = cache_root()?.join(format!("ext-src-v{ver}-extract"));
|
||||
if stage.exists() {
|
||||
let _ = fs::remove_dir_all(&stage);
|
||||
}
|
||||
extract_zip(&zip_path, &stage)?;
|
||||
// Prefer `ext/` child if present
|
||||
let src = if stage.join("ext").join("web").join("web.mq.md").is_file() {
|
||||
stage.join("ext")
|
||||
} else if stage.join("web").join("web.mq.md").is_file() {
|
||||
@@ -165,7 +295,6 @@ pub fn ensure_ext_source_tree() -> Result<PathBuf> {
|
||||
if cache.exists() {
|
||||
let _ = fs::remove_dir_all(&cache);
|
||||
}
|
||||
// Move/copy tree into stable cache path
|
||||
copy_dir_recursive(&src, &cache)?;
|
||||
let _ = fs::remove_dir_all(&stage);
|
||||
if !cache.join("web").join("web.mq.md").is_file() {
|
||||
@@ -206,24 +335,22 @@ pub fn download_native_plugin(short: &str, lib_name: &str) -> Result<PathBuf> {
|
||||
};
|
||||
let ver = release_version();
|
||||
let zip_name = format!("marqdo-{ver}-native-{triple}.zip");
|
||||
let url = release_asset_url(&ver, &zip_name);
|
||||
let urls = release_asset_urls(&ver, &zip_name);
|
||||
let cache = cache_root()?.join(format!("native-v{ver}-{triple}"));
|
||||
let lib_in_cache = cache.join("native").join(lib_name);
|
||||
if lib_in_cache.is_file() {
|
||||
return Ok(lib_in_cache);
|
||||
}
|
||||
// Also accept flat layout
|
||||
let flat = cache.join(lib_name);
|
||||
if flat.is_file() {
|
||||
return Ok(flat);
|
||||
}
|
||||
let zip_path = cache_root()?.join(&zip_name);
|
||||
println!("downloading prebuilt native plugins ({url})…");
|
||||
download_to_file(&url, &zip_path).with_context(|| {
|
||||
download_first_ok(&urls, &zip_path).with_context(|| {
|
||||
format!(
|
||||
"failed to download prebuilt plugins for {triple}. \
|
||||
Build locally with `cargo build --release -p marqdo_plugin_{short}`, \
|
||||
or check https://github.com/{REPO}/releases/tag/v{ver}"
|
||||
Set MARQDO_EXT_CDN / MARQDO_EXT_DOWNLOAD_BASE, build locally, \
|
||||
or check https://github.com/{REPO}/releases and https://ext.marqdo.com/v{ver}/"
|
||||
)
|
||||
})?;
|
||||
if cache.exists() {
|
||||
@@ -238,7 +365,6 @@ pub fn download_native_plugin(short: &str, lib_name: &str) -> Result<PathBuf> {
|
||||
if flat.is_file() {
|
||||
return Ok(flat);
|
||||
}
|
||||
// Search recursively for the lib name
|
||||
if let Some(found) = find_named_file(&cache, lib_name)? {
|
||||
return Ok(found);
|
||||
}
|
||||
@@ -283,17 +409,31 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn asset_url_shape() {
|
||||
let u = release_asset_url("0.3.4", "marqdo-0.3.4-ext.zip");
|
||||
assert_eq!(
|
||||
u,
|
||||
"https://github.com/cflmy/marqdo/releases/download/v0.3.4/marqdo-0.3.4-ext.zip"
|
||||
);
|
||||
fn asset_urls_prefer_cdn_then_github_then_proxy() {
|
||||
let u = release_asset_urls("1.0.2", "marqdo-1.0.2-ext.zip");
|
||||
assert!(u[0].starts_with("https://ext.marqdo.com/"));
|
||||
assert!(u.iter().any(|x| x.contains("github.com/cflmy/marqdo")));
|
||||
assert!(u
|
||||
.iter()
|
||||
.any(|x| x.contains("proxy.cflmy.top/github.com/cflmy/marqdo")));
|
||||
let gh = u
|
||||
.iter()
|
||||
.position(|x| x.starts_with("https://github.com/"))
|
||||
.unwrap();
|
||||
let proxy = u
|
||||
.iter()
|
||||
.position(|x| x.contains("proxy.cflmy.top"))
|
||||
.unwrap();
|
||||
assert!(gh < proxy);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedded_ext_version_parses() {
|
||||
assert!(normalize_semver(EMBEDDED_EXT_VERSION).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn triple_known_on_ci_hosts() {
|
||||
// At least one of these builds is what we ship.
|
||||
let t = host_target_triple();
|
||||
if cfg!(any(
|
||||
all(target_os = "linux", target_arch = "x86_64"),
|
||||
|
||||
+18
-5
@@ -140,10 +140,18 @@ pub fn resolve_import(from_dir: &Path, rel: &str) -> Result<PathBuf> {
|
||||
"cannot resolve import `{rel}` from {}",
|
||||
from_dir.display()
|
||||
);
|
||||
if env::var_os("MARQDO_EXT").is_none() {
|
||||
msg.push_str(
|
||||
"\nhint: set MARQDO_EXT to the directory that contains web/ (or run `marqdo ext add web`)",
|
||||
);
|
||||
let user_ext = crate::ext_cli::default_user_ext_dir();
|
||||
if env::var_os("MARQDO_EXT").is_none() && !user_ext.join("web").join("web.mq.md").is_file()
|
||||
{
|
||||
msg.push_str(&format!(
|
||||
"\nhint: run `marqdo ext add web` (installs under {}), or set MARQDO_EXT",
|
||||
user_ext.display()
|
||||
));
|
||||
} else if env::var_os("MARQDO_EXT").is_none() {
|
||||
msg.push_str(&format!(
|
||||
"\nhint: looked under {}; set MARQDO_EXT to override",
|
||||
user_ext.display()
|
||||
));
|
||||
}
|
||||
bail!("{msg}");
|
||||
}
|
||||
@@ -194,6 +202,8 @@ fn ext_search_roots() -> Vec<PathBuf> {
|
||||
if let Ok(h) = env::var("MARQDO_EXT") {
|
||||
roots.push(PathBuf::from(h));
|
||||
}
|
||||
// Default install root from `marqdo ext add` (~/.marqdo/ext)
|
||||
roots.push(crate::ext_cli::default_user_ext_dir());
|
||||
if let Ok(cwd) = env::current_dir() {
|
||||
roots.push(cwd.join("ext"));
|
||||
}
|
||||
@@ -318,7 +328,10 @@ mod tests {
|
||||
"{msg}"
|
||||
);
|
||||
assert!(
|
||||
msg.contains("hint: set MARQDO_EXT to the directory that contains web/"),
|
||||
msg.contains("hint:")
|
||||
&& (msg.contains("marqdo ext add")
|
||||
|| msg.contains("MARQDO_EXT")
|
||||
|| msg.contains(".marqdo/ext")),
|
||||
"{msg}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -323,6 +323,16 @@ impl<'a> Cursor<'a> {
|
||||
fun.body.push(self.parse_loop_or_err()?);
|
||||
continue;
|
||||
}
|
||||
// Unbound GFM table = collection value (not assigned); evaluate and discard.
|
||||
if trimmed.starts_with('|') {
|
||||
let span = Span {
|
||||
line: l.line_no,
|
||||
col: 1,
|
||||
};
|
||||
let value = self.consume_table()?;
|
||||
fun.body.push(Stmt::Expr { value, span });
|
||||
continue;
|
||||
}
|
||||
|
||||
let stmt = self.parse_simple_stmt()?;
|
||||
let end_body = matches!(stmt, Stmt::Return { .. }) && level >= 2;
|
||||
@@ -382,6 +392,13 @@ impl<'a> Cursor<'a> {
|
||||
return Ok(Stmt::Call { call, span });
|
||||
}
|
||||
|
||||
// Unbound GFM table (header already consumed by bump above).
|
||||
if trimmed.starts_with('|') {
|
||||
self.i -= 1;
|
||||
let value = self.consume_table()?;
|
||||
return Ok(Stmt::Expr { value, span });
|
||||
}
|
||||
|
||||
// Bare assign `` `x` = … ``
|
||||
if trimmed.starts_with('`') {
|
||||
if let Some(stmt) = self.parse_backtick_assign(trimmed, span.clone())? {
|
||||
@@ -695,6 +712,8 @@ impl<'a> Cursor<'a> {
|
||||
/// - first header `@` / `行` / `row` → List of row Maps (marker col excluded)
|
||||
/// - else ≥2-col → Map (column-oriented)
|
||||
///
|
||||
/// Also used for **unbound** tables in a function body (value computed, not stored).
|
||||
///
|
||||
/// Data cells are expressions ([`parse_call_arg_value`]); headers stay key text.
|
||||
fn consume_table(&mut self) -> Result<Expr> {
|
||||
self.skip_noise();
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
title: web collab bag smoke
|
||||
description: route_ws room+presence for collab doc
|
||||
import web:ext/web/web.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
**首页 = > web.page title="c"**
|
||||
**应用 = > web.app page=`首页` admin=False**
|
||||
**应用 = > `应用`.route_ws path="/live/{id}" mode="room" room_key="doc.{id}" presence=True**
|
||||
**routes = > json.get value=`应用` key="ws_routes"**
|
||||
**spec = > json.get value=`routes` key="/live/{id}"**
|
||||
**mode = > json.get value=`spec` key="mode"**
|
||||
**pr = > json.get value=`spec` key="presence"**
|
||||
1. `mode` == "room"
|
||||
1. `pr` == True
|
||||
> print text=collab-ws-bag-ok
|
||||
2. *
|
||||
> print text=presence-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=mode-fail
|
||||
> sys.exit code=1
|
||||
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: web RBAC smoke
|
||||
description: app.rbac flag + gate permissions CSV stored on app bag
|
||||
import web:ext/web/web.mq.md
|
||||
import json:lib/json.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
> print text=rbac-smoke-start
|
||||
|
||||
**首页 = > web.page title="home"**
|
||||
`用户表` =
|
||||
| 用户名 | 密码 | 角色 |
|
||||
|--------|------|------|
|
||||
| admin | secret | admin |
|
||||
|
||||
**应用 = > web.app page=`首页` admin=False**
|
||||
**应用 = > `应用`.enable_rbac**
|
||||
**authbag = > json.get value=`应用` key="auth"**
|
||||
**rbac_on = > json.get value=`authbag` key="rbac"**
|
||||
1. `rbac_on` == True
|
||||
> print text=rbac-flag-ok
|
||||
2. *
|
||||
> print text=rbac-flag-fail
|
||||
|
||||
**应用 = > `应用`.auth users=`用户表` login_path="/login"**
|
||||
**authbag2 = > json.get value=`应用` key="auth"**
|
||||
**rbac_kept = > json.get value=`authbag2` key="rbac"**
|
||||
1. `rbac_kept` == True
|
||||
> print text=rbac-survives-auth-ok
|
||||
2. *
|
||||
> print text=rbac-survives-auth-fail
|
||||
|
||||
**应用 = > `应用`.gate path="/desk" permissions="desk:access" match="prefix" on_deny="redirect" exclude="/login"**
|
||||
**gates = > json.get value=`应用` key="gates"**
|
||||
**g1 = > at value=`gates` index=1**
|
||||
**perms = > json.get value=`g1` key="permissions"**
|
||||
**p0 = > at value=`perms` index=0**
|
||||
1. `p0` == "desk:access"
|
||||
> print text=gate-perm-ok
|
||||
2. *
|
||||
> print text=gate-perm-fail
|
||||
|
||||
**应用2 = > web.app page=`首页` admin=False**
|
||||
**应用2 = > `应用2`.gate path="/desk/roles" permissions="roles:manage" roles="editor" match="exact"**
|
||||
**gates2 = > json.get value=`应用2` key="gates"**
|
||||
**g0 = > at value=`gates2` index=0**
|
||||
**perms2 = > json.get value=`g0` key="permissions"**
|
||||
**p1 = > at value=`perms2` index=0**
|
||||
1. `p1` == "roles:manage"
|
||||
> print text=gate-roles-manage-ok
|
||||
2. *
|
||||
> print text=gate-roles-manage-fail
|
||||
|
||||
> print text=rbac-smoke-done
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: web RBAC db register/assign smoke
|
||||
description: EnsureSchema path via create_role + assign + can
|
||||
import web:ext/web/web.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
> print text=rbac-db-start
|
||||
|
||||
**db = > web.db url="sqlite::memory:"**
|
||||
**url = > json.get value=`db` key="url"**
|
||||
**rbac = > web.rbac**
|
||||
|
||||
**seed = > `rbac`.create_role url=`db` name="reviewer"**
|
||||
**ok1 = > json.get value=`seed` key="ok"**
|
||||
1. `ok1` == True
|
||||
> print text=create-role-ok
|
||||
2. *
|
||||
> print text=create-role-fail
|
||||
> sys.exit code=1
|
||||
|
||||
**setp = > `rbac`.set_role_permissions url=`db` role="reviewer" permissions="comments:create,desk:access"**
|
||||
**ok2 = > json.get value=`setp` key="ok"**
|
||||
1. `ok2` == True
|
||||
> print text=set-perms-ok
|
||||
2. *
|
||||
> print text=set-perms-fail
|
||||
> sys.exit code=1
|
||||
|
||||
**can = > `rbac`.can permissions="comments:create,desk:access" needed="desk:access"**
|
||||
**allowed = > json.get value=`can` key="allowed"**
|
||||
1. `allowed` == True
|
||||
> print text=can-ok
|
||||
2. *
|
||||
> print text=can-fail
|
||||
> sys.exit code=1
|
||||
|
||||
**deny = > `rbac`.can permissions="comments:create" needed="roles:manage"**
|
||||
**denied = > json.get value=`deny` key="allowed"**
|
||||
1. `denied` == False
|
||||
> print text=can-deny-ok
|
||||
2. *
|
||||
> print text=can-deny-fail
|
||||
> sys.exit code=1
|
||||
|
||||
> print text=rbac-db-done
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: browser spa helpers smoke
|
||||
description: store_set / spa_goto effect bags
|
||||
import browser:lib/browser.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
**set = > browser.store_set key="k" value="v" scope="memory"**
|
||||
**op = > json.get value=`set` key="storage"**
|
||||
**op_name = > json.get value=`op` key="op"**
|
||||
**scope = > json.get value=`op` key="scope"**
|
||||
1. `op_name` == "set"
|
||||
1. `scope` == "memory"
|
||||
> print text=store-set-ok
|
||||
2. *
|
||||
> print text=scope-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=op-fail
|
||||
> sys.exit code=1
|
||||
|
||||
**nav = > browser.spa_goto url="/item" replace=False**
|
||||
**n = > json.get value=`nav` key="navigate"**
|
||||
**url = > json.get value=`n` key="url"**
|
||||
1. `url` == "/item"
|
||||
> print text=spa-goto-ok
|
||||
2. *
|
||||
> print text=spa-goto-fail
|
||||
> sys.exit code=1
|
||||
|
||||
> print text=spa-helpers-ok
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: web tenant bag smoke
|
||||
description: app.tenant mode=path stores config
|
||||
import web:ext/web/web.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
**首页 = > web.page title="t"**
|
||||
**应用 = > web.app page=`首页` admin=False**
|
||||
**应用 = > `应用`.tenant mode="path" param="t" column="tenant_id" default_scope=True**
|
||||
**bag = > json.get value=`应用` key="tenant"**
|
||||
**mode = > json.get value=`bag` key="mode"**
|
||||
**param = > json.get value=`bag` key="param"**
|
||||
**scope = > json.get value=`bag` key="default_scope"**
|
||||
1. `mode` == "path"
|
||||
1. `param` == "t"
|
||||
1. `scope` == True
|
||||
> print text=tenant-bag-ok
|
||||
2. *
|
||||
> print text=scope-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=param-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=mode-fail
|
||||
> sys.exit code=1
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: web WS room + on_message bag smoke
|
||||
description: route_ws mode=room stores room_key, on_message, presence
|
||||
import web:ext/web/web.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
**首页 = > web.page title="ws"**
|
||||
**应用 = > web.app page=`首页` admin=False**
|
||||
**应用 = > `应用`.route_ws path="/chat/{id}" mode="room" room_key="chat.room.{id}" on_message="chat.persist" presence=True**
|
||||
**routes = > json.get value=`应用` key="ws_routes"**
|
||||
**spec = > json.get value=`routes` key="/chat/{id}"**
|
||||
**mode = > json.get value=`spec` key="mode"**
|
||||
**rk = > json.get value=`spec` key="room_key"**
|
||||
**om = > json.get value=`spec` key="on_message"**
|
||||
**pr = > json.get value=`spec` key="presence"**
|
||||
1. `mode` == "room"
|
||||
1. `rk` == "chat.room.{id}"
|
||||
1. `om` == "chat.persist"
|
||||
1. `pr` == True
|
||||
> print text=ws-room-bag-ok
|
||||
2. *
|
||||
> print text=presence-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=on-message-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=room-key-fail
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=mode-fail
|
||||
> sys.exit code=1
|
||||
@@ -132,6 +132,11 @@ fn structure_hello() {
|
||||
assert_out("tests/structure/hello.mq.md", "Hello World!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structure_unbound_table() {
|
||||
assert_out("tests/structure/unbound-table.mq.md", "ok");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structure_object_handle() {
|
||||
assert_out("tests/structure/object-handle.mq.md", "counter\n3\n4");
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
title: Unbound GFM table is a discarded value
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Documentation may include a table that is not bound to a name. It is still a
|
||||
collection value; evaluating it must not error.
|
||||
|
||||
| Method | Notes |
|
||||
|--------|-------|
|
||||
| PPA | apt |
|
||||
| Source | cargo |
|
||||
|
||||
> print text=ok
|
||||
Reference in new issue
Block a user