Quire
A Go site engine that turns a folder of markdown into a complete website served from a single binary, with plugins for anything live.
Quire turns a folder of markdown into a complete website: home page, listings, articles, navigation, light and dark themes, a sitemap and share cards. The default look is good enough to ship, and each layer of it can be replaced without touching the others. This site runs on it, as do a few others.
Motivation
I used to build sites like this with Astro, but I never enjoyed the JavaScript/npm ecosystem. I also wanted dynamic sites (I'm building a Shopify-style art gallery) without writing a client-side SPA in React, Svelte or similar.
Approach
- A single Go binary serves the whole site
- Pages are Templ components, which compile to Go
- Interactivity is Datastar, so there's no JavaScript build step, and state lives on the server rather than being split between back end and front end
The smallest site is a folder of markdown and this:
func main() {
site := model.Site{Name: "Example", BaseURL: "https://example.com"}
quire.Run(site, func(e *quire.Engine) error {
_, err := e.AddCollection(model.Basic{ID: "posts"})
return err
})
}
That gives you a home page, a post index, post pages, navigation, light and dark themes, a sitemap and robots.txt, with no templates or CSS of your own.
Colour
A palette in Quire is a set of roles. It picks a hue for each one: a neutral for surfaces and text, an accent for links and anything clickable, a second brand colour called spot, four swatches for telling things apart, and red, green and amber for meaning. Stylesheets only ever name the role.
Each role is a twelve-step scale from Radix, and each step has a job: 1-2 are backgrounds, 3-5 component backgrounds, 6-8 borders, 9-10 solid fills and 11-12 text.
So var(--qr-neutral-6) is a border in every palette, in light mode and dark, and no stylesheet has to mix its own tints.
Here is this site's palette, drawn from the page's own CSS. Switch the theme and it changes with everything else:
Everything on the page draws from the same roles: code highlighting, the diagrams, and the tags under the title, which use the swatches.
Changing palette is one line, WithPalette(model.PaletteMoss), and the whole site follows.
Palettes are defined in Go rather than CSS, so a test checks every role's text steps for contrast against the backgrounds they sit on, and a new palette can't ship unreadable text.
Mistakes stop the load
Wherever I could, I made a mistake in the content stop the site from loading instead of showing up on a page.
- An unknown frontmatter key is an error, so a typo in
featured:doesn't quietly leave a post unfeatured - Dates must be ISO and are parsed once, at load
- A site can give Quire its tag vocabulary, and then every tag has to match it, so a new tag can't ship unstyled
- An image or diagram missing from the bundle fails at boot, rather than as a 404 when someone asks for it
- A widget can fail the load too. The palette above is one, and it rejects a role it doesn't know
If the site starts, its content is sound.
Plugins
Anything the engine doesn't do itself, such as a basket, a search box or a comment thread, is a plugin.
- A plugin is two methods: a name, and an attach that mounts its routes, assets and state
- Its URLs sit under a prefix taken from its name, so two plugins can't collide, and any public path it claims is checked at boot
- Its CSS sits in its own cascade layer, so a site can restyle a plugin but a plugin can't restyle the site
- A site mounts a plugin in one line, and moving a plugin into Quire itself means moving its file
type Plugin interface {
Name() string
Attach(*Mount) error
}
e.Use(basket.New(catalogue))
Live pages
This is what replaces the SPA. State lives on the server, and changes are pushed to the browser over Datastar's server-sent events, grouped by topic. Add something to the basket in one tab and every open tab updates:
The basket's nav link and its add button are plain markup.
data-init opens the stream, data-text shows the count, and data-on:submit posts the form without leaving the page:
<a href="/basket"
data-signals="{basket:{count:0}}"
data-init="@get('/_basket/events')">
Basket <span data-text="$basket.count"></span>
</a>
<form method="post" action="/_basket/add"
data-on:submit="@post('/_basket/add', {contentType: 'form'})">
<input type="hidden" name="sku" value={ item.SKU }/>
<button type="submit">Add</button>
</form>
When the basket changes, the server publishes the new count to everyone watching that visitor's basket.
A plugin's signals land under its own name, so this becomes $basket.count on the page and can't collide with another plugin's:
p.streams.Publish(id, func(s *plugin.Stream) error {
return s.Signals(map[string]int{"count": b.Count()})
})
There's no JavaScript to write on the client.
- A page is rendered and cached once for everyone, and per-visitor parts like the basket count are patched in over the stream afterwards
- A session cookie tells visitors apart, so the server knows whose basket a patch belongs to. It's only set on a page navigation, never from a background request
- The server always pushes. It never prompts the page to come back and fetch its own update
Bake
Most of the engineering went into images and diagrams.
The site's own binary produces both by baking itself with go run . bake. The same bake converts the markdown to HTML and draws the site's share card.
The binary that ships is built without the bake, so production can't produce any of them.
- The cache is incremental: over an unchanged library, a bake costs a hash per file and opens no image
- Each run links a fresh bundle out of the cache, with a journal listing exactly what's in it
- Every file is written whole and renamed into place, so an interrupted bake picks up where it left off
- The shipped binary has no codecs, no markdown converter and no diagram renderer, so production never spends CPU converting anything
- Dev reads the same bundle off disk that production reads from the embed, so nothing behaves differently in dev
Images
Image sizes aren't tied to the layout. There's one global ladder of widths, so I can restyle a grid without re-encoding the library. Every variant is lossy WebP. AVIF isn't offered: on this site's art it came out bigger than WebP and took six times as long to bake.
Quire doesn't guess which pictures should be lossless. Counting colours in a sample looks like it should work: flat art has tens, photos have thousands. Screenshots break it. Anti-aliased text puts hundreds of colours into an image that is mostly flat, and scaling flat art down makes it less flat. On one screenshot, lossless matched lossy at full size and cost three times as much at the smaller widths. Only the site knows which pictures are art it wants exact, so that will be something a picture says where it is placed, along with anything else about its conversion.
In a template, an image is one line:
@ui.Img(im, model.SlotPage, "The ridge at first light")
The slot and the bake's journal decide what that line becomes: a plain <img> offering the widths that slot can use, up to the master:
<img src="/_quire/img/….webp" width="1800" height="1200"
sizes="(min-width: 46.5rem) 43.5rem, calc(100vw - 3rem)"
srcset="/_quire/img/b73b….webp 232w,
/_quire/img/41ba….webp 408w,
/_quire/img/4a24….webp 680w,
/_quire/img/3eac….webp 1096w,
/_quire/img/ddbc….webp 1800w"
loading="lazy" decoding="async" alt="The ridge at first light">
Diagrams
The diagrams on this page are Mermaid in the markdown, drawn at bake time and inlined as SVG. They're drawn by sketch, a pure Go Mermaid renderer, so the bake needs no browser or Node. Because they're inlined, they pick up the site's palette and switch with light and dark mode. Labels are measured in the fonts the site serves, and if those fonts change without a rebake the site refuses to load rather than ship labels that overflow their boxes.
The Bake diagram above is this in the markdown, with :::spot picking a palette colour for one node:
```mermaid
flowchart LR
images[images] --> bake
md[markdown + mermaid] --> bake
bake:::spot --> cache[(cache)]
cache --> bundle[bundle + journal]
bundle -->|off disk| dev[dev server]
bundle -->|embedded| prod[production binary]
```
The binary that builds isn't the binary that ships
Converting, encoding and drawing live in their own package, and the engine reaches it through one file behind a build tag.
A normal build includes the bake.
Built with -tags quire_nobake, the binary gets a stub that refuses to bake, and none of the bake is linked.
So the production binary can't produce a page, an image or a diagram because it doesn't contain the code to, and this site's is 9MB instead of 23MB before its bundle goes in.
Because the bake runs inside the site, everything it needs comes from the site's own Go configuration: the palette, the fonts and the text on its share card. Nothing has to be stated twice.
The bundle gets into the shipped binary without the site writing anything.
The bake leaves a small Go file beside the bundle that embeds it, and the shipping build adds that file to the site with Go's -overlay flag.
A normal build never sees it, so nothing has to exist before the first bake.
I considered three alternatives:
- Encode and draw on first request. Simplest, but dev and production drift apart, and a missing image costs CPU instead of failing the build
- One package with a runtime flag. The same rule, but enforced by discipline rather than by the linker
- A separate
quire bakecommand. Every setting the bake shares with the site would have to be passed to the tool as well, and the two could disagree
Caching
Compression happens once per page, never per request. Whether a response can be cached is decided when its route is mounted, not by looking at the request:
- Fixed pages are rendered and compressed at boot and kept for the life of the process
- Articles live in a size-capped cache keyed on path and encoding. Cookies never go in it, so one visitor's session can't leak to another
- Small image files are held in memory, and large ones stream from disk or the embed
Where it is
Quire is on GitHub, with its documentation at quire.hiddenbridge.io, a site it builds itself.
Next is RSS and Atom feeds.
After that, pre-rendered pages: the bake already runs inside the site, so it could render every page that's the same for everyone into the bundle, and the shipped binary would serve finished HTML. A site with nothing live in it wouldn't need a server at all, only a static host.
Then quire new, to generate a starter site, so the setup every site repeats lives in one place.
It's Go, with Templ, Datastar, goldmark for markdown, chroma for highlighting and the Radix colour scales.