生态Web 与网络

言枢博客:完整示例

运行一个包含言枢配置、言标继承、命名路由、JSON、言据、静态文件和无端口测试的参考项目。

yanxu-webblog是言枢 0.2 与言标的参考项目。页面使用.yb.html模板,不再用节点工厂手写整棵 HTML 树。

运行

git clone https://github.com/yanxulang/yanxu-webblog.git
yanbao install --manifest-path yanxu-webblog
yanbao run --manifest-path yanxu-webblog

打开http://127.0.0.1:8787,或访问:

curl -i http://127.0.0.1:8787/
curl -i http://127.0.0.1:8787/posts/hello-yanxu
curl -i http://127.0.0.1:8787/api/posts
curl -i http://127.0.0.1:8787/api/posts.yj
curl -i http://127.0.0.1:8787/static/app.css
curl -i http://127.0.0.1:8787/health

阅读顺序

src/数据.yx → src/页面.yx → templates/ → src/应用.yx → src/主.yx → tests/集成.yx
  1. 数据层返回文章;
  2. 页面数据层使用反向路由建立模板上下文;
  3. 言标页面通过base.yb.html继承与 partial 包含复用布局;
  4. 应用层建立配置、处理器、命名路由组、自定义 404 和静态路由;
  5. 入口选择项目路径并启动开发服务器;
  6. 测试客户端不打开端口,直接覆盖完整应用链。

路由

方法与路径名称结果
GET /首页言标首页
GET /posts/:id文章详情详情或言标 404
GET /api/postsapi:文章列表JSON
GET /api/posts.yjapi:文章言据言据
GET /health健康纯文字
GET /static/*pathCSS 等静态资产

安全回归

文章正文中的<标签>经普通言标插值输出为&lt;标签&gt;。基础模板的静态 HTML 不需要可信包装,项目也没有动态可信 HTML 使用点。框架默认加入 CSP、nosniff与同源 referrer policy;静态和模板文件都拒绝目录穿越。

测试

yanxu yanxu-webblog/src/主.yx
yanxu yanxu-webblog/tests --并发 1 --json

可继续练习:新增模板页面、增加第三篇文章、用JSON正文建立 POST API、加入查询过滤,或把数据层替换为受校验的持久存储。