docs(examples): add the CMS proof-of-concept dogfooding artifact
A WordPress-inspired CMS whose entire backend is PiCloud Rhai scripts deployed with pic apply, plus a SvelteKit frontend. Built to dogfood the project tool, CLI, and SDK; exercises docs/kv/files/users, docs+queue+cron +pubsub triggers, the transactional-outbox notification chain, a docs before-interceptor, set_if CAS, SSE, per-app CORS, env overlays, and a durable Workflow (validate -> enrich || seo -> publish -> finalize) started with workflow::start and polled with workflow::run_status, visualized live with Svelte Flow. FINDINGS.md / SECURITY.md capture every gap, surprise, and security issue found (each tagged [PiCloud] vs [CMS]); the platform fixes for the actionable ones ship in the preceding commit. Also folds in the read-only `pic` allowlist entries added to .claude/settings.json during the session (fewer-permission-prompts). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
96
examples/cms-poc/scripts/pages/pages.rhai
Normal file
96
examples/cms-poc/scripts/pages/pages.rhai
Normal file
@@ -0,0 +1,96 @@
|
||||
// pages — one script handling all page operations, dispatched by method+path.
|
||||
// Demonstrates a single script bound to several routes (method/param varies).
|
||||
// GET /cms/pages -> public list (published)
|
||||
// GET /cms/pages/:slug -> public get by slug
|
||||
// GET /cms/admin/pages -> admin list (all)
|
||||
// POST /cms/admin/pages -> create (admin)
|
||||
// PUT /cms/admin/pages/:id -> update (admin)
|
||||
// DELETE /cms/admin/pages/:id -> delete (admin)
|
||||
import "auth" as auth;
|
||||
import "util" as util;
|
||||
|
||||
let m = ctx.request.method;
|
||||
let path = ctx.request.path;
|
||||
let is_admin_path = path.contains("/cms/admin/");
|
||||
let pages = docs::collection("pages");
|
||||
let idx = kv::collection("slugs");
|
||||
|
||||
// ---- public reads ----
|
||||
if !is_admin_path && m == "GET" && ctx.request.params.slug != () {
|
||||
let id = idx.get("page:" + ctx.request.params.slug);
|
||||
if id == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
let doc = pages.get(id);
|
||||
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
let p = util::shape_doc(doc);
|
||||
if p.status != "published" { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
return #{ statusCode: 200, body: p };
|
||||
}
|
||||
if !is_admin_path && m == "GET" {
|
||||
let rows = pages.find(#{ status: "published", "$sort": #{ title: 1 }, "$limit": 100 });
|
||||
let out = [];
|
||||
for r in rows { let p = util::shape_doc(r); p.remove("body_md"); out.push(p); }
|
||||
return #{ statusCode: 200, body: #{ pages: out } };
|
||||
}
|
||||
|
||||
// ---- everything else is admin ----
|
||||
let g = auth::guard(ctx, ["admin"]);
|
||||
if !g.ok { return g.response; }
|
||||
let user = g.user;
|
||||
|
||||
if m == "GET" {
|
||||
let rows = pages.find(#{ "$sort": #{ updated_at: -1 }, "$limit": 100 });
|
||||
let out = [];
|
||||
for r in rows { let p = util::shape_doc(r); p.remove("body_md"); out.push(p); }
|
||||
return #{ statusCode: 200, body: #{ pages: out } };
|
||||
}
|
||||
|
||||
if m == "POST" {
|
||||
let b = ctx.request.body;
|
||||
if b == () || b.title == () { return #{ statusCode: 400, body: #{ error: "title required" } }; }
|
||||
let slug = if b.slug == () { util::slugify(b.title) } else { util::slugify(b.slug) };
|
||||
if idx.has("page:" + slug) { slug = slug + "-" + random::string(4).to_lower(); }
|
||||
let data = #{
|
||||
title: b.title,
|
||||
slug: slug,
|
||||
body_md: if b.body_md == () { "" } else { b.body_md },
|
||||
status: if b.status == () { "draft" } else { b.status },
|
||||
author_id: user.id,
|
||||
author_name: user.display_name,
|
||||
};
|
||||
let id = pages.create(data);
|
||||
idx.set("page:" + slug, id);
|
||||
return #{ statusCode: 201, body: #{ id: id, slug: slug } };
|
||||
}
|
||||
|
||||
if m == "PUT" {
|
||||
let id = ctx.request.params.id;
|
||||
let doc = pages.get(id);
|
||||
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
let cur = doc.data;
|
||||
let b = ctx.request.body;
|
||||
if b.title != () { cur.title = b.title; }
|
||||
if b.body_md != () { cur.body_md = b.body_md; }
|
||||
if b.status != () { cur.status = b.status; }
|
||||
if b.slug != () {
|
||||
let ns = util::slugify(b.slug);
|
||||
if ns != cur.slug {
|
||||
idx.delete("page:" + cur.slug);
|
||||
if idx.has("page:" + ns) { ns = ns + "-" + random::string(4).to_lower(); }
|
||||
idx.set("page:" + ns, id);
|
||||
cur.slug = ns;
|
||||
}
|
||||
}
|
||||
pages.update(id, cur);
|
||||
return #{ statusCode: 200, body: #{ id: id, slug: cur.slug } };
|
||||
}
|
||||
|
||||
if m == "DELETE" {
|
||||
let id = ctx.request.params.id;
|
||||
let doc = pages.get(id);
|
||||
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
pages.delete(id);
|
||||
idx.delete("page:" + doc.data.slug);
|
||||
return #{ statusCode: 200, body: #{ ok: true } };
|
||||
}
|
||||
|
||||
#{ statusCode: 405, body: #{ error: "method not allowed" } }
|
||||
Reference in New Issue
Block a user