Appearance
User-editable pages
Every signed-in user can replace the app's home page with one they write in Weft. Their page is checked and compiled when they save it, kept as a user tree, and served before the app's own routes; everyone else gets the app.
main.weft
weft
import { app } from "std/app"
import { session } from "std/http"
import type { Request } from "std/http"
import { trees } from "lib/trees"
try await app.serve(async (req: Request) => {
let user = try await session(req).user()
return [user != null ? trees.tree(`users/${user.id}`) : null, "./routes"]
}, {
// what a user's page gets: its `caps` prop (and `imports`, any of the app's components it may use)
sandbox: async (req: Request) => ({ caps: { user: (try await session(req).user())?.name ?? "" } }),
})lib/trees.weft
weft
// Where the users' pages are kept: a folder here (an S3 bucket's `trees` works the same).
import { dir } from "std/sandbox"
export let trees = dir("trees")lib/server/pages.weft
weft
import { session, request } from "std/http"
import type { AuthError } from "std/auth"
import type { Problem, TreeError } from "std/sandbox"
import { trees } from "lib/trees"
export type Saved = { version: string?, problems: Problem[] }
let STARTER = "prop caps: json\n---\n<Text>Hello from {caps.user}'s own page</Text>\n"
async fn who(): string? {
let req = request()
if req == null { return null }
return (try await session(req).user())?.id
}
/// Saves the user's home page: checked and compiled first; a page with problems isn't saved.
export action fn publish(source: string): Saved {
let id = (try await who()) ?? fail Unauthorized { message: "sign in first" } // a 401
let saved, err = try await trees.save(`users/${id}`, { files: [{ id: "home", source }], routes: [{ path: "/", file: "home" }] })
if err != null {
return match err {
Problems { problems } => Saved { version: null, problems },
Storage => fail err,
}
}
return Saved { version: saved.version, problems: [] }
}
/// The user's page as saved, or the starter.
export action fn mine(): string {
let id = try await who()
if id == null { return "" }
let m = try await trees.get(`users/${id}`)
if m == null { return STARTER }
return m.files.find(f => f.id == "home")?.source ?? STARTER
}
/// Takes the page down: the app's home serves them again.
export action fn unpublish(): boolean {
let id = try await who()
if id == null { return false }
try await trees.delete(`users/${id}`)
return true
}routes/edit/page.weft
weft
import type { AuthError } from "std/auth"
import { publish, mine, unpublish } from "lib/server/pages"
page({ label: "Your page" })
let mut source = ""
let mut status = ""
async fn save() {
let r, err = try await publish(source)
if err != null {
status = match err {
Unauthorized => "Sign in first.",
_ => "Couldn't publish; try again.",
}
return
}
status = r.version != null ? `Published (version ${r.version})` : r.problems.map(p => `line ${p.line}: ${p.message}`).join("; ")
}
async fn remove() {
let gone, err = try await unpublish()
if err != null {
status = "Couldn't take it down; try again."
return
}
if gone { status = "Taken down" }
}
source = try await mine() else ""
---
<Box class="flex flex-col gap-3 p-6">
<Textarea bind:value={source} className="min-h-64 font-mono" />
<Box class="flex gap-2">
<Button onpress={save}>Publish</Button>
<Button variant="outline" onpress={remove}>Take down</Button>
</Box>
<Text class="text-sm">{status}</Text>
<Link to={Route.url("/")}>See it</Link>
</Box>routes/page.weft
weft
---
<Box class="p-6">
<Text>Every signed-in user can have a home page of their own.</Text>
<Link to={Route.url("/edit")}>Make yours</Link>
</Box>What a saved page can do is what the app gave it: caps.user, here. A page that imports std/http or std/db, or anything of the app that no run's imports gives it, isn't saved: its problems come back with their lines, and the last good version keeps serving.
lib/treesis a plain module:main.weftand the server functions share it, and the pages never import it. An S3 bucket'strees(std/s3) works the same as the folder, and several servers can share one bucket.- A tree can have more files (its own components) and routes (
{ path: "/about", file: "about" }). examples/sitesin the repository is this app with S3 and sign-in, and its tests try to escape the sandbox.
How user code is run: User-editable apps.