Extend network stack: session/auth, WebSocket, cookie/multipart primitives
Add the network-extension wave to lib/net and the web ABI plugin:
- lib/net cookie_parse / multipart_parse host primitives (EN/ZH)
- plugins/web session store + /admin login gate + WebSocket client/server
- ext/web author surface (EN/ZH) for session, auth, ws
- form update flow and page-level validation echo tests
- fix string escapes: \xHH, \u{...}, unknown escapes kept verbatim
- map Chinese component slots (导航/侧栏/页脚) so sidebar renders
- resolve relative static/db paths against the entry script dir (entry_dir)
- new tutorial chapter 14, EN/ZH example sites, and gold tests
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7dfa161422
commit
7645dd4c1f
47 files changed
+3380
-75
No files matched your search
+83
-11
@@ -3,7 +3,6 @@ title: ext/web/web
|
||||
description: Official web site classes (English). Tables + methods; no bag glue.
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
import json:lib/json.mq.md
|
||||
---
|
||||
|
||||
## ensure_plugin
|
||||
@@ -53,7 +52,7 @@ Embed a form into the page main slot. `listen` auto-registers `GET|POST /_form/{
|
||||
Offline HTML for tests / previews.
|
||||
|
||||
1. `db`
|
||||
*url = > json.get value=`db` key="url"*
|
||||
*url = db[^url]*
|
||||
**> web_render page=`self` url=`url`**
|
||||
2. *
|
||||
**> web_render page=`self`**
|
||||
@@ -82,14 +81,14 @@ Style tables live as `##` exports in site style modules; compose resolves them b
|
||||
+ `name`
|
||||
+ `fields`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_init url=`url` name=`name` fields=`fields`**
|
||||
|
||||
## insert
|
||||
+ `table`
|
||||
+ `rows`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_insert url=`url` table=`table` rows=`rows`**
|
||||
|
||||
## select
|
||||
@@ -99,15 +98,15 @@ Style tables live as `##` exports in site style modules; compose resolves them b
|
||||
|
||||
Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作|值|` (`=` `!=` `>` `>=` `<` `<=` `like`).
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
*r = > web_db_select url=`url` table=`table` where=`where` limit=`limit`*
|
||||
**> json.get value=`r` key="rows"**
|
||||
**r[^rows]**
|
||||
|
||||
## get
|
||||
+ `table`
|
||||
+ `id`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_get url=`url` table=`table` id=`id`**
|
||||
|
||||
## update
|
||||
@@ -115,21 +114,21 @@ Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作
|
||||
+ `id`
|
||||
+ `row`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_update url=`url` table=`table` id=`id` row=`row`**
|
||||
|
||||
## delete
|
||||
+ `table`
|
||||
+ `id`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_delete url=`url` table=`table` id=`id`**
|
||||
|
||||
## exec
|
||||
+ `sql`
|
||||
+ `args`=None
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_exec url=`url` sql=`sql` args=`args`**
|
||||
|
||||
# form
|
||||
@@ -169,7 +168,7 @@ Field table + rules table; submit writes through `# db`.
|
||||
+ `data`
|
||||
+ `db`
|
||||
|
||||
*url = > json.get value=`db` key="url"*
|
||||
*url = db[^url]*
|
||||
**> web_form_submit form=`self` data=`data` url=`url`**
|
||||
|
||||
# app
|
||||
@@ -211,3 +210,76 @@ Serve files from `dir` under `mount` (default `/static`). Path is resolved from
|
||||
Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), and optional `/admin`.
|
||||
|
||||
**> web_listen app=`self`**
|
||||
|
||||
## route_ws
|
||||
+ `path`
|
||||
+ `echo`=True
|
||||
|
||||
Register a WebSocket endpoint at `path` (e.g. `/live`). With `echo=True` (default) the server replies to each received text frame; otherwise frames are drained. Connect from a client with `web.ws.connect`.
|
||||
|
||||
**> web_app_route_ws app=`self` path=`path` echo=`echo`**
|
||||
|
||||
## auth
|
||||
+ `users`
|
||||
+ `session_ttl`=3600
|
||||
|
||||
Keep the app's `admin=True`, and gate `/admin*` behind a login page. `users` is an admin-users table (`|username|password|` / `|用户名|密码|`); unauthenticated requests redirect to `/admin/login`.
|
||||
|
||||
**> web_app_auth app=`self` users=`users` session_ttl=`session_ttl`**
|
||||
|
||||
# auth
|
||||
+ `users`
|
||||
+ `session_ttl`=3600
|
||||
|
||||
Session/auth helper. Constructs a config object; `login` validates against the users table. To gate `/admin` on this app, use `app.auth users=…` instead.
|
||||
|
||||
> ensure_plugin
|
||||
**> web_auth_new users=`users` session_ttl=`session_ttl`**
|
||||
|
||||
## login
|
||||
+ `username`
|
||||
+ `password`
|
||||
|
||||
Validate credentials against the users table and create a session. Returns `{ok, session_id, username}`.
|
||||
|
||||
*users = self[^users]*
|
||||
*ttl = self[^session_ttl]*
|
||||
**> web_auth_login username=`username` password=`password` users=`users` session_ttl=`ttl`**
|
||||
|
||||
## check
|
||||
+ `session_id`
|
||||
|
||||
Returns `{ok, username}` when the session is valid.
|
||||
|
||||
**> web_auth_check session_id=`session_id`**
|
||||
|
||||
## logout
|
||||
+ `session_id`
|
||||
|
||||
Destroy the session.
|
||||
|
||||
**> web_auth_logout session_id=`session_id`**
|
||||
|
||||
# ws
|
||||
+ `timeout_sec`=30
|
||||
|
||||
WebSocket client helper.
|
||||
|
||||
> ensure_plugin
|
||||
`out` =
|
||||
|
||||
| timeout_sec | _type |
|
||||
|-------------|-------|
|
||||
| `timeout_sec` | ws |
|
||||
|
||||
**out**
|
||||
|
||||
## connect
|
||||
+ `url`
|
||||
+ `message`=""
|
||||
+ `headers`=None
|
||||
|
||||
Single request–response: connect to `url`, send `message`, collect all server text replies, close. Returns `{ok, messages}`.
|
||||
|
||||
*timeout = self[^timeout_sec]*
|
||||
**> web_ws_connect url=`url` message=`message` headers=`headers` timeout_sec=`timeout`**
|
||||
+83
-11
@@ -3,7 +3,6 @@ title: ext/web/网页
|
||||
description: 官方网站类(中文)。表格 + 方法;不写袋式胶水。
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
import json:lib/json.mq.md
|
||||
---
|
||||
|
||||
## 确保插件
|
||||
@@ -47,7 +46,7 @@ import json:lib/json.mq.md
|
||||
+ `数据库`=None
|
||||
|
||||
1. `数据库`
|
||||
*url = > json.get value=`数据库` key="url"*
|
||||
*url = 数据库[^url]*
|
||||
**> web_render page=`self` url=`url`**
|
||||
2. *
|
||||
**> web_render page=`self`**
|
||||
@@ -74,14 +73,14 @@ import json:lib/json.mq.md
|
||||
+ `名`
|
||||
+ `字段`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_init url=`url` name=`名` fields=`字段`**
|
||||
|
||||
## 插入
|
||||
+ `表`
|
||||
+ `行`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_insert url=`url` table=`表` rows=`行`**
|
||||
|
||||
## 查询
|
||||
@@ -91,15 +90,15 @@ import json:lib/json.mq.md
|
||||
|
||||
简单条件:单行列→值映射(AND `=`),或 `|字段|操作|值|` 表(`=` `!=` `>` `>=` `<` `<=` `like`)。
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
*r = > web_db_select url=`url` table=`表` where=`条件` limit=`上限`*
|
||||
**> json.get value=`r` key="rows"**
|
||||
**r[^rows]**
|
||||
|
||||
## 获取
|
||||
+ `表`
|
||||
+ `id`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_get url=`url` table=`表` id=`id`**
|
||||
|
||||
## 更新
|
||||
@@ -107,21 +106,21 @@ import json:lib/json.mq.md
|
||||
+ `id`
|
||||
+ `行`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_update url=`url` table=`表` id=`id` row=`行`**
|
||||
|
||||
## 删除
|
||||
+ `表`
|
||||
+ `id`
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_delete url=`url` table=`表` id=`id`**
|
||||
|
||||
## 执行
|
||||
+ `sql`
|
||||
+ `参数`=None
|
||||
|
||||
*url = > json.get value=`self` key="url"*
|
||||
*url = self[^url]*
|
||||
**> web_db_exec url=`url` sql=`sql` args=`参数`**
|
||||
|
||||
# 表单
|
||||
@@ -161,7 +160,7 @@ import json:lib/json.mq.md
|
||||
+ `数据`
|
||||
+ `数据库`
|
||||
|
||||
*url = > json.get value=`数据库` key="url"*
|
||||
*url = 数据库[^url]*
|
||||
**> web_form_submit form=`self` data=`数据` url=`url`**
|
||||
|
||||
# 应用
|
||||
@@ -201,3 +200,76 @@ import json:lib/json.mq.md
|
||||
提供 `/`、路由页、首页 `/_part/{id}`、路由 `{路径}/_part/{id}`、`/_form/{id}`、可选静态目录,以及可选 `/admin`。
|
||||
|
||||
**> web_listen app=`self`**
|
||||
|
||||
## 路由实时
|
||||
+ `路径`
|
||||
+ `回显`=真
|
||||
|
||||
在 `路径`(如 `/live`)注册 WebSocket 端点。`回显=真`(默认)时服务器对每条收到的文本帧原样回复;否则仅读取丢弃。客户端用 `网页.实时` 的 `连接` 接入。
|
||||
|
||||
**> web_app_route_ws app=`self` path=`路径` echo=`回显`**
|
||||
|
||||
## 鉴权
|
||||
+ `用户表`
|
||||
+ `会话时长`=3600
|
||||
|
||||
在保持 `后台=真` 的同时,为 `/admin*` 加登录门禁。`用户表` 是管理员表(`|用户名|密码|`);未登录请求重定向到 `/admin/login`。
|
||||
|
||||
**> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长`**
|
||||
|
||||
# 鉴权
|
||||
+ `用户表`
|
||||
+ `会话时长`=3600
|
||||
|
||||
会话/登录工具类。`登录` 用 `用户表` 校验。要给应用加 `/admin` 门禁,用 `应用` 的 `鉴权` 方法。
|
||||
|
||||
> 确保插件
|
||||
**> web_auth_new users=`用户表` session_ttl=`会话时长`**
|
||||
|
||||
## 登录
|
||||
+ `用户名`
|
||||
+ `密码`
|
||||
|
||||
用用户表校验并创建会话。返回 `{ok, session_id, username}`。
|
||||
|
||||
*users = self[^users]*
|
||||
*ttl = self[^session_ttl]*
|
||||
**> web_auth_login username=`用户名` password=`密码` users=`users` session_ttl=`ttl`**
|
||||
|
||||
## 校验
|
||||
+ `会话id`
|
||||
|
||||
会话有效时返回 `{ok, username}`。
|
||||
|
||||
**> web_auth_check session_id=`会话id`**
|
||||
|
||||
## 登出
|
||||
+ `会话id`
|
||||
|
||||
销毁会话。
|
||||
|
||||
**> web_auth_logout session_id=`会话id`**
|
||||
|
||||
# 实时
|
||||
+ `超时秒`=30
|
||||
|
||||
WebSocket 客户端工具。
|
||||
|
||||
> 确保插件
|
||||
`out` =
|
||||
|
||||
| timeout_sec | _type |
|
||||
|-------------|-------|
|
||||
| `超时秒` | ws |
|
||||
|
||||
**out**
|
||||
|
||||
## 连接
|
||||
+ `地址`
|
||||
+ `消息`=""
|
||||
+ `headers`=None
|
||||
|
||||
单次请求-响应:连接 `地址`,发送 `消息`,收集服务器全部文本回复后关闭。返回 `{ok, messages}`。
|
||||
|
||||
*timeout = self[^timeout_sec]*
|
||||
**> web_ws_connect url=`地址` message=`消息` headers=`headers` timeout_sec=`timeout`**
|
||||
Reference in new issue
Block a user