Cookbook
The Cookbook combines Khora language features and shipped standard-library APIs into complete application patterns. Each recipe includes the imports, types, functions, and boundary wiring needed to understand the example as a whole instead of showing an isolated call with important pieces omitted.
Start with the recipe closest to the problem you are solving:
- Build an HTTP service — route requests with
Router,Request,Response, andSharedFn. - Build a typed JSON API — parse JSON, decode typed request bodies, derive encoders/decoders, and return JSON responses.
- Run a database transaction — use the portable
Dbcapability andtransactionso failure and cancellation cannot leak an open transaction. - Take work off a queue safely — register a job with a region before the first
!after it, so a cancellation cannot drop work nobody knows the fiber was holding. - Bound concurrent work — use
Fiber,Nursery, andbounded_nurseryto turn a concurrency limit into backpressure. - Make resource cleanup cancellation-safe — pair
scopedandacquireso cleanup runs on return, failure, and cancellation. - Decode untrusted input — describe a value’s shape once with
std::schema, decode it from anywhere, and report every bad field at once. - Load application configuration — read settings with
std::config, report every bad key at once, and keep secrets out of the logs withRedacted. - Retry a flaky call — pick a
Schedule, drive it withretry_while, and test the whole thing in under a millisecond. - Test code that uses capabilities — supply deterministic handlers without global mutation or test-only branches in application code.
- Trace an operation — implement a
Tracer, wrap work witharound, and keep span lifetime structured. - Log, and correlate it with traces — emit one JSON object per line with
std::log, and carry the trace and span identifiers onto every line inside a span.
How to read the examples
Section titled “How to read the examples”The examples use current Khora syntax and the public APIs shipped in std. They favor small complete modules over framework-style pseudocode. When a recipe depends on a provider outside std—for example a concrete PostgreSQL connection—it says so at the construction boundary instead of inventing a driver API.
For exact declarations after you understand the pattern, use the Standard Library API reference. For the language rules underneath the pattern, use the Language Reference.
Khora 0.2.0built frombd964738afb6on 2026-10-17 20:41ZVersioningSecurityContributingReleasesSource