The nilo guide¶
One page per thing you might want to do, in an order where each page builds on the ones before it.
Reference: every public name, one page a module · Design: why each part works the way it does, one page a topic
Read the pages in order the first time, since each one assumes the ones above it, and jump straight to what you need after that.
Which page covers which module¶
nilo is a toolkit of twelve modules, not one library. Which module a feature lives in is decided by one question: does it need the event loop? (ADR 038, ADR 061)
| Module | What it is | Pages |
|---|---|---|
nilo_http |
the server: routing, handlers, middleware, files, sockets | everything below except the ones named on the right |
nilo_sql |
Postgres and SQLite: your struct is the table | Talking to a database, nine pages |
nilo_s3 |
object storage: your bucket is a type | Object storage |
nilo_fetch |
calling somebody else's HTTP API from a handler | Calling somebody else's API |
nilo_job |
work that runs later, again, or on a schedule: a queue in your database | Work that runs later |
nilo_config |
settings read from the environment into a struct of yours | Settings |
nilo_pw |
password hashing: argon2id, stored as PHC | Sessions |
nilo_cache |
an expiring cache in this process, holding no pointers | A cache in this process, and the Space that Idempotency keys keeps its answers in |
nilo_jwt |
checking somebody else's signed token: RS256, ES256 and a JWKS | Checking somebody else's token |
nilo_proto |
protobuf messages as plain structs: OTLP, a gRPC method's message | Protobuf messages |
nilo_id |
UUIDs, v4 and v7 | Identifiers |
nilo_core |
Str, the Scope and the clock the rest share |
the reference |
There is no module called nilo. The word names the project, and the server module is nilo_http. Every example here imports it under the shorter name, which is one line:
const nilo = @import("nilo_http");
Start here¶
- Getting started: installing, the two lines of root wiring, and a server that answers.
- Handlers: the rule that decides what every argument means, and what a return value turns into.
- Routing: patterns, route priority, groups and plugins.
Handling a request¶
- Requests: path params, query structs, JSON bodies, and bodies too big to hold in memory.
- Forms: an HTML form as a struct of yours, file uploads, and binding that names the field that failed instead of rejecting the whole form.
- Responses: statuses, headers and redirects, and the
Ctxlayer underneath the typed one. - Cookies: reading them, setting them, and the signed-in user.
- Sessions: a struct of yours sealed into one cookie, with nothing kept on the server, and checking the password that opens one.
- Streaming: writing an answer whose length is not known yet, and server-sent events.
- WebSocket: a handler that keeps a connection open for a while.
- gRPC: a gRPC method as a route, on the port HTTP/1.1 is on, in a build that asks for it.
Building an application¶
- Middleware: code that runs around every handler, and resolved values for the signed-in user.
- Services: shared state across threads, locks, and the rule about blocking calls.
- Static files: a directory held in memory, with ETags and range requests, and files too big to hold opened per request.
- Errors: failing a request from anywhere, what the client is told, and request ids that tie a failure to its log line.
- Settings:
nilo_configreads the environment into a struct of yours before anything opens, reports every bad setting at once, and shows a completemainthat reads a.env. - Background work: work no request started, such as a summary written every minute or a cache warmed at startup. It runs in a fiber the server owns and stops at shutdown. For work that is a database row rather than a loop, see
nilo_jobbelow. - Idempotency keys: the
Idempotency-Keyheader as a typed argument. A retry gets the saved answer back, the order is placed once, and a key reused wrongly gets a 409 or 422.
The other modules¶
Each module has one page, except the database, which has a folder. Every page covers what the module is for, a complete example, every option with its default, what it returns instead of a value, what it costs, and what it will not do.
- Talking to a database:
nilo_sql. Your struct is the table, the query is a constant, and a misspelled column is a build error. Postgres and SQLite are written the same way. Nine pages, in order: tables, reading, parents, children and aggregates, writing, transactions, raw SQL, SQLite, migrations and running a database. - Calling somebody else's API:
nilo_fetch. One client for the whole program, a deadline on every call, a response body in the request's own memory, and anExchangefor a body too big to hold. - Object storage:
nilo_s3. A bucket is a type, a key is a string. Reading, writing, streaming an object through, and a presigned URL or POST form for a browser that talks to the bucket directly. - Work that runs later:
nilo_job. A job is a struct, the queue is a table in the database you already have,pushIn(&tx, …)commits together with your rows, and a schedule must declare what an overlap and a missed tick mean or it does not compile. - A cache in this process:
nilo_cache. A typed keyspace over one fixed memory budget, no pointers allowed in a value, lookups that take no lock, and astats()that explains why it is not hitting. - Checking somebody else's token:
nilo_jwt. A JWT signed by an identity provider, verified in a safe order and read into a struct of yours; the signed-in user as a resolved value; and what a key rotation looks like. - Protobuf messages:
nilo_proto. A message is the struct you wrote with its field numbers declared beside it, read and written with no generator and no.protofile; strings checked, unknown fields skipped, and the decoder within a few percent of one written by hand. - Identifiers:
nilo_id. A v7 for a key that sorts by creation time, what that order does and does not guarantee, and why the randomness is an argument.
Shipping it¶
- Testing: handlers as ordinary functions, and the test client for the ones that write their own answer.
- OpenAPI: an API document generated from the handler signatures.
- Metrics: how many requests, at what statuses, how long. A Prometheus page in one call, and counters of your own on it.
- Tracing: every request a span, sent to any OpenTelemetry receiver in one call; a trace that continues through
nilo_fetchinto the next service, and spans of your own. - Deploying: startup errors, panics, graceful shutdown, a health page the load balancer can trust, tuning, and what is not there yet.
Other documentation¶
- The reference: every public name, as a list.
- The design pages: one page a topic, with the rules in force and the decisions behind them.
../adr/: why each decision went the way it did.../todo.md: what's next.../decided.md: what's refused, and why.../../CONTEXT.md: the project's vocabulary.