Settings and navigation from data
Move the site name and tagline into a settings record and the header links into navigation items, so an editor changes them without editing files, and visitors see a change only once it is published.
Allow 40–60 minutes. This is an editorial estimate. Continue in the same my-site folder from lesson 22, or start from the L22 checkpoint.
Read
Start at L22. The site name, the tagline in the footer and the header links are written in components/layout.html. Changing them means editing a file and, on a server, installing it again. This lesson has two parts: settings, one record holding the site name and tagline, and navigation, a list of links to your pages.
Settings. The record comes from a new seed file, data/settings.json. Like the pages in lesson 14, the server copies it into the database once, on the first start after the [import] line appears, and never reads the file again. Nobody can add a second settings record or delete this one through the site (create = "none", delete = "none"): editors change it and publish it, exactly like a page.
Navigation. Each item has a label, the address (slug) of one of your pages and a position. The position is a whole number (fields.integer), so the server stores it as a number and 2 comes before 10. Give every item its own position: two items with the same position have no promised order. An item is shown only once it is published, and only while the page it points at is published; unpublish or delete that page and its link disappears from the header, and it comes back when the page is published again.
Published copies for everyone. The layout reads the published settings and navigation, also when you browse the public site signed in as the editor. Saving changes only the working copy; the public pages change when you publish. The forms send the revision they were opened with, so a save over a newer one is refused, as in lesson 16.
Where the layout gets its data. site/application.what applies to every page under site/, so the three fetches added there reach the layout on every public page. fetch.settings.single = true gives one record instead of a list.
Change
1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-A01.
2. Declare the settings and the navigation. Replace what.toml. The new parts are the settings line in [import] and the two collections.
# What Project Configuration
[server]
port = 8085
host = "127.0.0.1"
[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"
# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"
# The site's settings, one record. Read once, on the first start after this
# line appears, like the pages; never again.
settings = "data/settings.json"
# Editors may create, change and delete pages. A published page is
# unpublished before it can be deleted. No two pages may share a slug: the
# database itself refuses the second one.
[collections.pages]
create = "editor"
update = "editor"
delete = "editor"
unique = ["slug"]
# Visitors read published copies of these fields only. A page created now
# stays private until it is published.
public = ["title", "slug", "summary", "body"]
# Editors read the working copies, drafts included, from the admin area.
working_read = "editor"
# Editors publish. Publish copies a page's last save (its public fields) into
# the published copy visitors read; Unpublish removes that copy. Nobody can
# publish until this line exists, and only signed-in accounts can hold it.
publish = "editor"
# The server checks every new or changed page itself, whatever form sent it.
fields.writable = ["title", "slug", "summary", "body"]
fields.required = ["title", "slug"]
fields.max_length = { title = 120, summary = 280, body = 20000 }
fields.slug = ["slug"]
fields.immutable = ["slug"]
# The site's settings: one record, from data/settings.json. Nobody can add a
# second one or delete it through the site. Editors change it and publish it
# like a page; visitors read the published copy.
[collections.settings]
create = "none"
update = "editor"
delete = "none"
public = ["site_name", "tagline"]
working_read = "editor"
publish = "editor"
fields.writable = ["site_name", "tagline"]
fields.required = ["site_name"]
fields.max_length = { site_name = 60, tagline = 140 }
# Navigation items: a label, the address (slug) of a page, and a position.
# Positions are whole numbers, so 2 comes before 10. Editors publish an item
# before visitors see it, and the layout shows it only while that page is
# published.
[collections.nav]
create = "editor"
update = "editor"
delete = "editor"
public = ["label", "page_slug", "position"]
working_read = "editor"
publish = "editor"
fields.writable = ["label", "page_slug", "position"]
fields.required = ["label", "page_slug", "position"]
fields.max_length = { label = 40 }
fields.slug = ["page_slug"]
fields.integer = ["position"]
# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"
3. Seed the settings. Create data/settings.json. Change the name and tagline now if you like; after the first start, change them in the admin area instead.
[
{
"id": 1,
"site_name": "My website",
"tagline": "Made one page at a time."
}
]
4. Give the layout its data. Replace site/application.what.
layout = "components/layout.html"
title = "My website"
description = "Ideas, projects and useful things, one page at a time."
# Read on every public page, for the layout. Visitors and signed-in
# editors alike see the published copies here.
fetch.settings.url = "local:settings"
fetch.settings.single = true
fetch.nav.url = "local:nav"
fetch.nav.sort = "position"
fetch.navpages.url = "local:pages"
5. Read it in the layout. Replace components/layout.html. The site name and the tagline come from the settings; if there were no published settings, the header would say just "Website" and the footer would be empty. After the fixed links, two loops show each published item, in position order, only when a published page has its address.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="#description#">
<title>#title#</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body class="site-body">
<a class="skip-link" href="#main-content">Skip to content</a>
<header class="site-header">
<a class="site-name" href="/">#settings.site_name|default:"Website"#</a>
<nav class="site-nav" aria-label="Main navigation">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<!-- Published items, by position, each only while its page is published. -->
<loop data="#nav#" as="item">
<loop data="#navpages#" as="target">
<if target.slug == item.page_slug>
<a href="/pages/#target.slug|urlencode#">#item.label#</a>
</if>
</loop>
</loop>
</nav>
<div class="site-account">
<if user.authenticated>
<span>Signed in as #user.username#</span>
<form method="post" action="/w-auth/logout">
<button type="submit">Sign out</button>
</form>
<else/>
<a href="/login">Sign in</a>
</if>
</div>
</header>
<main id="main-content" class="site-main" tabindex="-1">
<slot/>
</main>
<footer class="site-footer">
<p>#settings.tagline#</p>
</footer>
</body>
</html>
6. Edit the settings. Create site/admin/settings.html: a form for the working copy and a Publish button.
<what>
title = "Settings | Admin | My website"
description = "The site name and tagline: edit the working copy, then publish it."
fetch.settings.url = "local:settings"
fetch.settings.copy = "working"
fetch.settings.single = true
</what>
<h1>Settings</h1>
<p>Visitors see the published settings. Saving changes the working copy only; publish it to change the public site.</p>
<if flash.success><p class="form-success" role="status">#flash.success#</p></if>
<if flash.error>
<div class="form-error" role="alert">
<p>#flash.error#</p>
<ul>
<if errors.site_name><li>Site name: #errors.site_name#</li></if>
<if errors.tagline><li>Tagline: #errors.tagline#</li></if>
</ul>
</div>
</if>
<if settings>
<p>Working copy: revision #settings._revision#. <if settings._published>Visitors see revision #settings._published_revision#.<else/>Not published: visitors see the default name.</if></p>
<!-- The form sends the revision it was opened with: a save over a newer one is refused. -->
<if old>
<form class="sign-in" method="post" action="/w-action/settings/#settings.id#?w-redirect=/admin/settings">
<input type="hidden" name="_revision" value="#old._revision#">
<div class="field">
<label for="site_name">Site name</label>
<input id="site_name" name="site_name" type="text" maxlength="60" required value="#old.site_name#" aria-describedby="site-name-error">
<span id="site-name-error" class="field-error">#errors.site_name#</span>
</div>
<div class="field">
<label for="tagline">Tagline</label>
<input id="tagline" name="tagline" type="text" maxlength="140" value="#old.tagline#" aria-describedby="tagline-error">
<span id="tagline-error" class="field-error">#errors.tagline#</span>
</div>
<button type="submit">Save settings</button>
</form>
<else/>
<form class="sign-in" method="post" action="/w-action/settings/#settings.id#?w-redirect=/admin/settings">
<input type="hidden" name="_revision" value="#settings._revision#">
<div class="field">
<label for="site_name">Site name</label>
<input id="site_name" name="site_name" type="text" maxlength="60" required value="#settings.site_name#" aria-describedby="site-name-error">
<span id="site-name-error" class="field-error"></span>
</div>
<div class="field">
<label for="tagline">Tagline</label>
<input id="tagline" name="tagline" type="text" maxlength="140" value="#settings.tagline#" aria-describedby="tagline-error">
<span id="tagline-error" class="field-error"></span>
</div>
<button type="submit">Save settings</button>
</form>
</if>
<unless flash.error>
<div class="publish-controls">
<form class="sign-in" method="post" action="/w-action/settings/#settings.id#?w-action=publish&w-redirect=/admin/settings">
<input type="hidden" name="_revision" value="#settings._revision#">
<input type="hidden" name="_live_version" value="#settings._live_version#">
<button type="submit">Publish revision #settings._revision#</button>
</form>
</div>
</unless>
<else/>
<p class="empty-state">No settings record. It is read from data/settings.json on the first start after the [import] line is added.</p>
</if>
7. Manage the navigation. Create the three navigation pages: the list with Publish, Unpublish and Delete buttons, the form for a new item, and the edit form. As with pages, a published item must be unpublished before it can be deleted.
<what>
title = "Navigation | Admin | My website"
description = "Links in the site header: each points at a page and shows only while that page is published."
fetch.items.url = "local:nav"
fetch.items.copy = "working"
fetch.items.sort = "position"
</what>
<h1>Navigation</h1>
<p>Each item links to a page by its address. Visitors see an item once it is published, in position order (2 before 10), and only while its page is published. Give each item its own position: two items with the same position have no promised order.</p>
<if flash.success><p class="form-success" role="status">#flash.success#</p></if>
<if flash.error><div class="form-error" role="alert"><p>#flash.error#</p><p>Reload this page before trying again.</p></div></if>
<p><a href="/admin/nav/new">Add an item</a></p>
<if items>
<div class="table-wrap">
<table class="inventory">
<caption>Navigation items (working copies)</caption>
<thead>
<tr><th scope="col">Position</th><th scope="col">Label</th><th scope="col">Page</th><th scope="col">Published</th><th scope="col">Actions</th></tr>
</thead>
<tbody>
<loop data="#items#" as="item">
<tr>
<td>#item.position#</td>
<td><a href="/admin/nav/edit/#item.id#">#item.label#</a></td>
<td><code>/pages/#item.page_slug#</code></td>
<td><if item._published>revision #item._published_revision#<else/>not published</if></td>
<td>
<form class="inline" method="post" action="/w-action/nav/#item.id#?w-action=publish&w-redirect=/admin/nav">
<input type="hidden" name="_revision" value="#item._revision#">
<input type="hidden" name="_live_version" value="#item._live_version#">
<button type="submit">Publish revision #item._revision#</button>
</form>
<if item._published>
<form class="inline" method="post" action="/w-action/nav/#item.id#?w-action=unpublish&w-redirect=/admin/nav">
<input type="hidden" name="_revision" value="#item._revision#">
<input type="hidden" name="_live_version" value="#item._live_version#">
<button type="submit">Unpublish</button>
</form>
<else/>
<form class="inline" method="post" action="/w-action/nav/#item.id#?w-action=delete&w-redirect=/admin/nav">
<input type="hidden" name="_revision" value="#item._revision#">
<input type="hidden" name="_live_version" value="#item._live_version#">
<button type="submit">Delete</button>
</form>
</if>
</td>
</tr>
</loop>
</tbody>
</table>
</div>
<else/>
<p class="empty-state">No navigation items yet.</p>
</if>
<what>
title = "New navigation item | Admin | My website"
description = "Add a link to a page; publish it to show it."
</what>
<h1>New navigation item</h1>
<p>The item starts as a draft. Visitors see it after you publish it, and only while its page is published.</p>
<if flash.error>
<div class="form-error" role="alert">
<p>#flash.error#</p>
<ul>
<if errors.label><li>Label: #errors.label#</li></if>
<if errors.page_slug><li>Page address: #errors.page_slug#</li></if>
<if errors.position><li>Position: #errors.position#</li></if>
</ul>
</div>
</if>
<form class="sign-in" method="post" action="/w-action/nav?w-redirect=/admin/nav">
<div class="field">
<label for="label">Label</label>
<input id="label" name="label" type="text" maxlength="40" required value="#old.label#" aria-describedby="label-error">
<span id="label-error" class="field-error">#errors.label#</span>
</div>
<div class="field">
<label for="page_slug">Page address (slug)</label>
<input id="page_slug" name="page_slug" type="text" required value="#old.page_slug#" aria-describedby="slug-help slug-error">
<span id="slug-help">The address of a page, for example why-i-started for /pages/why-i-started.</span>
<span id="slug-error" class="field-error">#errors.page_slug#</span>
</div>
<div class="field">
<label for="position">Position</label>
<input id="position" name="position" type="number" step="1" required value="#old.position#" aria-describedby="position-help position-error">
<span id="position-help">A whole number. Smaller numbers come first: 2 before 10.</span>
<span id="position-error" class="field-error">#errors.position#</span>
</div>
<button type="submit">Create item</button>
</form>
<p><a href="/admin/nav">Back to navigation</a></p>
<what>
title = "Edit navigation item | Admin | My website"
description = "Edit a navigation item's working copy."
fetch.item.url = "local:nav"
fetch.item.copy = "working"
fetch.item.filter = "id=#params.id#"
fetch.item.single = true
</what>
<h1>Edit navigation item</h1>
<p>Saving changes the working copy. Publish it from the <a href="/admin/nav">navigation list</a> to change the public site.</p>
<if flash.error>
<div class="form-error" role="alert">
<p>#flash.error#</p>
<ul>
<if errors.label><li>Label: #errors.label#</li></if>
<if errors.page_slug><li>Page address: #errors.page_slug#</li></if>
<if errors.position><li>Position: #errors.position#</li></if>
</ul>
</div>
</if>
<!-- After a refused save, the form shows what was typed and the revision it was opened with. -->
<if old>
<form class="sign-in" method="post" action="/w-action/nav/#item.id#?w-redirect=/admin/nav">
<input type="hidden" name="_revision" value="#old._revision#">
<div class="field">
<label for="label">Label</label>
<input id="label" name="label" type="text" maxlength="40" required value="#old.label#" aria-describedby="label-error">
<span id="label-error" class="field-error">#errors.label#</span>
</div>
<div class="field">
<label for="page_slug">Page address (slug)</label>
<input id="page_slug" name="page_slug" type="text" required value="#old.page_slug#" aria-describedby="slug-error">
<span id="slug-error" class="field-error">#errors.page_slug#</span>
</div>
<div class="field">
<label for="position">Position</label>
<input id="position" name="position" type="number" step="1" required value="#old.position#" aria-describedby="position-error">
<span id="position-error" class="field-error">#errors.position#</span>
</div>
<button type="submit">Save item</button>
</form>
<else/>
<form class="sign-in" method="post" action="/w-action/nav/#item.id#?w-redirect=/admin/nav">
<input type="hidden" name="_revision" value="#item._revision#">
<div class="field">
<label for="label">Label</label>
<input id="label" name="label" type="text" maxlength="40" required value="#item.label#" aria-describedby="label-error">
<span id="label-error" class="field-error"></span>
</div>
<div class="field">
<label for="page_slug">Page address (slug)</label>
<input id="page_slug" name="page_slug" type="text" required value="#item.page_slug#" aria-describedby="slug-error">
<span id="slug-error" class="field-error"></span>
</div>
<div class="field">
<label for="position">Position</label>
<input id="position" name="position" type="number" step="1" required value="#item.position#" aria-describedby="position-error">
<span id="position-error" class="field-error"></span>
</div>
<button type="submit">Save item</button>
</form>
</if>
<p><a href="/admin/nav">Back to navigation</a></p>
8. Link them from the admin home. Replace site/admin/index.html; only the Site-wide line is new. Replace static/styles.css; only the last rule is new: it keeps each item's buttons on one line.
<what>
title = "Admin | My website"
description = "The editing area: every page, published or draft."
fetch.pages = "local:pages"
fetch.drafts.url = "local:pages"
fetch.drafts.copy = "working"
</what>
<h1>Admin</h1>
<if flash.success><p class="form-success" role="status">#flash.success#</p></if>
<p>Only a signed-in editor can open this page. <a href="/admin/pages/new">Create a new page</a>: it starts as a draft that only editors see.</p>
<p>Site-wide: <a href="/admin/settings">Settings</a> (the site name and tagline) and <a href="/admin/nav">Navigation</a> (links to published pages).</p>
<if pages>
<div class="table-wrap">
<table class="inventory">
<caption>Published pages</caption>
<thead>
<tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th></tr>
</thead>
<tbody>
<loop data="#pages#" as="page">
<tr><td>#page.id#</td><td><a href="/pages/#page.slug|urlencode#">#page.title#</a></td><td><code>#page.slug#</code></td></tr>
</loop>
</tbody>
</table>
</div>
<else/>
<p class="empty-state">No pages yet.</p>
</if>
<h2>All pages</h2>
<p>The working copies, drafts included. Visitors never see this list. Follow a title to edit that page; its preview publishes it.</p>
<if drafts>
<div class="table-wrap">
<table class="inventory">
<caption>All pages (working copies)</caption>
<thead>
<tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th><th scope="col">Revision</th><th scope="col">Published</th></tr>
</thead>
<tbody>
<loop data="#drafts#" as="page">
<tr><td>#page.id#</td><td><a href="/admin/pages/edit/#page.id#">#page.title#</a></td><td><code>#page.slug#</code></td><td>#page._revision#</td><td><if page._published>revision #page._published_revision#<else/>not published</if></td></tr>
</loop>
</tbody>
</table>
</div>
</if>
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
margin: 0;
color: #26372c;
background: #faf8f2;
font-family: system-ui, sans-serif;
line-height: 1.65;
}
.site-header, .site-main, .site-footer {
width: min(100% - 2rem, 48rem);
margin-inline: auto;
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-block: 1.5rem;
border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
outline: 3px solid #825414;
outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
position: absolute;
top: -10rem;
left: 1rem;
padding: 0.6rem 1rem;
background: #fff;
z-index: 10;
}
.skip-link:focus { top: 1rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }
.listing-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1rem; margin-bottom: 1rem; }
.listing-controls .field { display: grid; gap: 0.25rem; }
.listing-controls input, .listing-controls select, .listing-controls button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.listing-controls input { min-width: min(100%, 16rem); }
.listing-controls button { background: #315f40; border-color: #315f40; color: #fff; cursor: pointer; }
.listing-controls :focus-visible { outline: 3px solid #825414; outline-offset: 2px; }
.result-count { color: #4b594e; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.pager [aria-current] { font-weight: 650; }
/* L13: the account corner and the sign-in form */
.site-account { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; }
.site-account form { margin: 0; }
.site-account button, .sign-in button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.9rem; border: 1px solid #315f40; border-radius: 0.5rem; background: #315f40; color: #fff; cursor: pointer; }
.sign-in { display: grid; gap: 1rem; max-width: 24rem; }
.sign-in .field { display: grid; gap: 0.25rem; }
.sign-in input { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.form-error { padding: 0.75rem 1rem; border-left: 4px solid #a33a2e; background: #fbeeec; }
/* L17: the note above a preview */
.preview-note { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
/* L18: Publish and Unpublish side by side; Unpublish is the quieter button */
.publish-controls { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-block: 1.5rem; }
.publish-controls .unpublish button { background: #fffdf8; color: #315f40; }
/* L19: the message after a change, and the delete button */
.form-success { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
.danger button { background: #a33a2e; border-color: #a33a2e; }
/* L20: the save messages inside the edit form; an empty one takes no space */
.save-alert, .save-status { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
.save-alert { border-left-color: #a33a2e; background: #fbeeec; }
.save-alert:empty, .save-status:empty { display: none; }
form.inline { display: inline-block; margin: 0 0.25rem 0.25rem 0; }
9. Update the server recipe. Replace deploy/what.toml (the same new parts, with the server settings kept) and deploy/README.txt, which now installs data/settings.json beside data/pages.json and explains how to upgrade a server set up earlier without touching its data.
# What Project Configuration: the server's copy. It is the project's
# what.toml with the [server] settings for running behind Caddy (see
# README.txt); keep the project's own copy for running on your computer.
[server]
port = 8085
host = "127.0.0.1"
# The address visitors use. Forms and sign-in accept state-changing
# requests only from this origin. Replace example.com with your domain,
# as in the Caddyfile.
allowed_origins = ["https://example.com"]
# Caddy, on this machine, is the one proxy in front of the site, so the
# visitor's address is the X-Forwarded-For entry it adds and rate limits
# apply per visitor, not to the proxy. Correct only while run-what listens
# on 127.0.0.1 and nothing but Caddy can reach it.
trusted_proxy_hops = 1
[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"
# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"
# The site's settings, one record. Read once, on the first start after this
# line appears, like the pages; never again.
settings = "data/settings.json"
# Editors may create, change and delete pages. A published page is
# unpublished before it can be deleted. No two pages may share a slug: the
# database itself refuses the second one.
[collections.pages]
create = "editor"
update = "editor"
delete = "editor"
unique = ["slug"]
# Visitors read published copies of these fields only. A page created now
# stays private until it is published.
public = ["title", "slug", "summary", "body"]
# Editors read the working copies, drafts included, from the admin area.
working_read = "editor"
# Editors publish. Publish copies a page's last save (its public fields) into
# the published copy visitors read; Unpublish removes that copy. Nobody can
# publish until this line exists, and only signed-in accounts can hold it.
publish = "editor"
# The server checks every new or changed page itself, whatever form sent it.
fields.writable = ["title", "slug", "summary", "body"]
fields.required = ["title", "slug"]
fields.max_length = { title = 120, summary = 280, body = 20000 }
fields.slug = ["slug"]
fields.immutable = ["slug"]
# The site's settings: one record, from data/settings.json. Nobody can add a
# second one or delete it through the site. Editors change it and publish it
# like a page; visitors read the published copy.
[collections.settings]
create = "none"
update = "editor"
delete = "none"
public = ["site_name", "tagline"]
working_read = "editor"
publish = "editor"
fields.writable = ["site_name", "tagline"]
fields.required = ["site_name"]
fields.max_length = { site_name = 60, tagline = 140 }
# Navigation items: a label, the address (slug) of a page, and a position.
# Positions are whole numbers, so 2 comes before 10. Editors publish an item
# before visitors see it, and the layout shows it only while that page is
# published.
[collections.nav]
create = "editor"
update = "editor"
delete = "editor"
public = ["label", "page_slug", "position"]
working_read = "editor"
publish = "editor"
fields.writable = ["label", "page_slug", "position"]
fields.required = ["label", "page_slug", "position"]
fields.max_length = { label = 40 }
fields.slug = ["page_slug"]
fields.integer = ["position"]
# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"
PRODUCTION RECIPE: one What site on a Linux server with systemd, Caddy in front.
Not run on a Linux server. These files are checked by parsing
(scripts/recipe-check.py) and the configuration by starting run-what on a
development machine. Starting the service under systemd on Linux, real
TLS and hosting are unverified.
FILES
what.service systemd unit, installed as /etc/systemd/system/what.service
Caddyfile Caddy site, installed as /etc/caddy/Caddyfile
env.example the form secret's file, copied to /etc/my-site/env
what.toml the server's what.toml: the project's own, with [server]
set for Caddy (the public HTTPS origin, one proxy hop)
PREREQUISITES (yours to provide; nothing here creates them)
1. A Linux server with systemd, a domain whose DNS points at it, ports 80
and 443 open, and Caddy installed (Caddy obtains the certificate).
2. A run-what binary built for that server. A binary built on macOS does
not run on Linux. Build it from the What source with
cargo build --release --bin run-what on the server, or on a Linux
machine with the same architecture and a C library no newer than the
server's. Install it as /usr/local/bin/run-what, owned by root, mode
0755.
3. A service account that cannot log in and owns only the data, with its
own group of the same name (Group=what in the unit):
sudo useradd --system --user-group --home-dir /srv/my-site --shell /usr/sbin/nologin what
4. The two persistent folders, owned by the account, private, created
before the service starts (systemd refuses a missing ReadWritePaths):
sudo install -d -o root -g root -m 0755 /srv/my-site
sudo install -d -o what -g what -m 0700 /srv/my-site/data /srv/my-site/uploads
data/ holds the store (data/app.db, with -wal and -shm while running)
and the sessions database (data/sessions.db); uploads/ holds uploaded
files. Both folders must persist across restarts and upgrades. A
backup (L22) holds the store and uploads/ only: sessions are never
backed up or restored, so after a restore, sign-ins through the site's
own accounts start again.
5. The site files in /srv/my-site, owned by root, directories 0755 and
files 0644, so the service reads them and cannot change them:
this folder's what.toml as /srv/my-site/what.toml (with your domain),
site/, static/ and components/. Put data/pages.json and
data/settings.json (each read once, on the first start that names it in
[import]) in on their own, so data/ and uploads/ keep the owner and mode
from step 4:
sudo install -o root -g root -m 0644 data/pages.json /srv/my-site/data/pages.json
sudo install -o root -g root -m 0644 data/settings.json /srv/my-site/data/settings.json
Never copy a whole data/ or uploads/ folder over them, and never
chown or chmod the project recursively. Copy no data/app.db, no
sessions database and no .env: the server creates its own data, and a
.env in the project would be loaded and travel with every copy of the
project.
6. The secret, outside the project:
sudo install -d -o root -g root -m 0755 /etc/my-site
sudo install -o root -g root -m 0600 env.example /etc/my-site/env
openssl rand -hex 32
Paste the generated value after WHAT_SERVER_FORM_SECRET= in
/etc/my-site/env (sudoedit /etc/my-site/env). Left empty, run-what
refuses to start.
7. Your domain in two places, the same origin in both: the site address
in the Caddyfile and allowed_origins in /srv/my-site/what.toml
("https://" plus the domain, nothing after it).
UPGRADING A RUNNING SERVER (lesson A01 on a server set up earlier)
This lesson needs a run-what built from the candidate source that has
integer fields (fields.integer) and typed values in application.what. A
binary built for an earlier lesson starts, but silently ignores both: it
stores positions as text (10 before 2) and fetch.settings.single (the
header shows "Website"). Build it as in prerequisite 2 and install it as
/usr/local/bin/run-what while the service is stopped. Not run on a Linux
server.
Stop the service (sudo systemctl stop what), install the new site files
and what.toml as in step 5, and install data/settings.json with the
install line above. Do not install data/pages.json again and do not touch
data/app.db: on the next start the server imports the settings once and
publishes them; pages, accounts and publication state stay as they are.
Then sudo systemctl start what.
START
The first editor account (there is no built-in one), as the service
account so its files belong to it, before the first start:
sudo -u what /usr/local/bin/run-what admin add editor --path /srv/my-site
Then:
sudo install -o root -g root -m 0644 what.service /etc/systemd/system/what.service
sudo systemctl daemon-reload
sudo systemctl enable --now what
sudo install -o root -g root -m 0644 Caddyfile /etc/caddy/Caddyfile
sudo systemctl reload caddy
Logs: journalctl -u what. For JSON log lines, add WHAT_LOG_FORMAT=json to
/etc/my-site/env and restart.
RECOVERY (back up and restore on the server; documented steps, not yet run
on a Linux server)
A backup holds the store (accounts, pages, publication state) and uploads/,
with backup.json listing the engine, the schema and every checksum. It
holds no sessions and no site files. Keep the site files and what.toml
that match each backup in a separate private folder beside it, never
inside the backup folder (an extra file there, even the .DS_Store that
Finder adds when you open it, makes verify and restore refuse the
backup; keep backups unopened and unmodified): a restore needs both. Stop the service first: a backup refuses a server that is writing,
but an idle server may not be noticed, so never rely on that.
Run each command on its own and stop at the first one that fails. The new
backup, restore, rollback and failed-attempt destinations must not exist
yet, not even as a link; the test commands check that.
Back up, as root, into a private folder outside the project:
sudo systemctl stop what
test ! -e /var/backups/my-site-2026-10-27 && test ! -L /var/backups/my-site-2026-10-27
sudo /usr/local/bin/run-what backup --path /srv/my-site --output /var/backups/my-site-2026-10-27
sudo /usr/local/bin/run-what backup --verify /var/backups/my-site-2026-10-27
sudo systemctl start what
The folder is owner-only (root). Keep a copy off the server as well.
Restore into a new folder, with the service stopped:
sudo systemctl stop what
test ! -e /srv/my-site-restored && test ! -L /srv/my-site-restored
sudo /usr/local/bin/run-what restore --from /var/backups/my-site-2026-10-27 --config /srv/my-site/what.toml --output /srv/my-site-restored
The restored folder and its files are private to root, and an empty
uploads folder in the backup leaves no uploads/ folder at all. Make the
project traversable and its configuration readable, copy in the site files
that match the backup as root (here the current ones), make sure data/ and
uploads/ exist, and give only those two to the service account:
sudo chmod 0755 /srv/my-site-restored
sudo chmod 0644 /srv/my-site-restored/what.toml
sudo cp -R --preserve=mode /srv/my-site/site /srv/my-site/static /srv/my-site/components /srv/my-site-restored/
sudo install -d -o what -g what -m 0700 /srv/my-site-restored/data /srv/my-site-restored/uploads
sudo chown -R what:what /srv/my-site-restored/data /srv/my-site-restored/uploads
Never copy the old data/, uploads/, sessions database or another
what.toml over the restored folder. The environment file in
/etc/my-site/env stays where it is.
Promote the restored folder, keeping the old installation for rollback.
mv -T (GNU) renames a folder and never moves it inside another one:
test ! -e /srv/my-site-before-restore-2026-10-27 && test ! -L /srv/my-site-before-restore-2026-10-27
sudo mv -T /srv/my-site /srv/my-site-before-restore-2026-10-27
sudo mv -T /srv/my-site-restored /srv/my-site
sudo systemctl start what
systemctl status what
The status must say active; then open your site and check the restored
pages and an editor sign-in. Users of the site's own accounts sign in
again: sessions are never restored. Credentials from an external sign-in
provider (JWTs) are not revoked by a restore.
Only if the promoted site fails verification, roll back, keeping the
failed restore for inspection:
sudo systemctl stop what
test ! -e /srv/my-site-failed-restore-2026-10-27 && test ! -L /srv/my-site-failed-restore-2026-10-27
sudo mv -T /srv/my-site /srv/my-site-failed-restore-2026-10-27
sudo mv -T /srv/my-site-before-restore-2026-10-27 /srv/my-site
sudo systemctl start what
systemctl status what
WHY EACH SETTING
--production no file watching, live reload, developer tools, debug
output or detailed error pages; session cookies keep
Secure, so the site needs HTTPS (Caddy).
127.0.0.1 only Caddy, on the same machine, reaches run-what; the
plain HTTP port is never public. trusted_proxy_hops = 1 is
correct only while this holds.
allowed_origins state-changing requests are accepted only from the
public HTTPS origin.
WHAT_SERVER_FORM_SECRET signs the rules of <form w-validate> forms; with
the same secret such a form opened before a restart still
submits after it, with a new one it must be reloaded. Forms
without w-validate (the course's edit forms) do not depend
on it; keep it stable for the w-validate forms you add.
KillSignal=SIGINT run-what shuts down cleanly on SIGINT; systemd's
default SIGTERM stops it without the clean shutdown.
ProtectSystem=strict, ReadWritePaths within the project the service can
write only data/ and uploads/; the site files and what.toml
stay as you installed them. (PrivateTmp gives it its own
writable temporary folder outside the project.)
10. Start the server again with run-what dev. The log says that it copied 1 record from data/settings.json and published it once. Your pages, accounts and publication state are unchanged.
Check
- Open the home page in a private window: this is your visitor. The header shows the site name from
data/settings.jsonand the footer its tagline. - Signed in as the editor, open Settings, change the site name and save. Reload the visitor's window: the old name is still there, and so it is on the public pages you open as the editor. Choose Publish revision N: every public page now shows the new name.
- Open Settings in two tabs. Save in the first, then save in the second: the second is refused and reports it. Reload before trying again.
- Open Navigation. Add three items pointing at three of your published pages, at positions 10, 2 and 1, and one more for an address no page has. Try position
1.5: the browser's number field refuses it before sending (and the server refuses it too if the form is bypassed). Nothing new appears in the visitor's header yet. - Publish the four items. The visitor's header lists the three links in the order 1, 2, 10 on every public page; the item for the missing address is never shown.
- Unpublish one of the three pages (its preview, lesson 18): its link disappears from every public page. Publish it again: the link is back.
- A published item shows Unpublish, not Delete. Unpublish it, then delete it.
- Sign out. As a visitor, /admin/settings and /admin/nav send you to sign in.
- Stop and start the server: the published name stays; the seed file does not overwrite it and no second settings record appears.
Exercise
Add a navigation item for a draft page that has never been published, publish the item, and explain what the visitor sees. Then publish the page and explain again.
If something goes wrong
- The header says "Website" and the footer is empty. The layout found no published settings. Check
site/application.what: the fetch is namedsettingsandfetch.settings.single = trueis present. - The server does not start: "[import] settings: cannot read data/settings.json". The
settingsline in[import]names a file that is not there. Create it (step 3) and start again. - A link does not appear. The item is not published, its page is not published, or its address does not match the page's slug exactly.
- "Use a whole number." A position must be a whole number such as 3 or 10.
- Start again from a known state. Download the A01 checkpoint into a new, empty folder; never unpack it over your project. It has no database, no account and no secret: run
run-what admin add editorin the new folder, thenrun-what dev.
Next: back to the course.