BlyxDocumentation
Browse chapters
Documentation / Framework projects

Swiftlet · Route a book catalog request

Dispatch a method and request path against a table of literal and parameterized routes. Return a matched template and the methods a client can use.

The developer problem

A method error and a missing route mean different things to an HTTP client. If their responses share one fallback, clients lose the list of methods a resource accepts. RFC 9110 requires an Allow field on a 405 response.

Run the project locally

Pass PATCH and /books/42?preview=yes to the dispatcher. It selects /books/:id and extracts 42. Try the same method on /books/new, an explicitly read-only resource. The CGI example also serves a health check through an existing web server. Install Blyx 0.2.1, Rust and a host linker, then run these commands.

git clone --depth 1 --branch v0.1.0 https://github.com/Rahulchaube1/blyx-swiftlet.git
cd blyx-swiftlet
blyxc native-run project.blyx

Use the API

Register a method and an absolute path template separated by one space. GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS use explicit registrations. Literal segments outrank parameters by their count. Equally specific templates use registration order. The matcher strips the query, preserves percent encoding and treats a trailing slash as significant. Hosting, URL decoding and authentication belong to the application.

FunctionBehavior
sw_dispatch(routes, method, target)Import router. Returns status text, matched template, and the Allow value in a three-element argument list.
sw_param(template, target, name)Extract an encoded path parameter from a matching path. An absent capture returns an empty string.
sw_enable(mask, method), sw_status(found, mask, method)Build a method set and apply the same status decisions as the router. Start at zero.
sw_allow(mask)Generate the comma-separated list of registered HTTP methods.

Verify a change

Run the package checks from its release directory. Compare the browser example with its expected output, then change the input in the exercise below.

blyxc native-run tests.blyx

Try the program

Output Ready
Expected output
200
405
404
Open in the full playground →

Practice

Change DELETE to POST in the second request. Which status changes?

Hint

Change only the worked example’s input. The API table describes the output.

Worked solution

The route accepts POST, so the second status becomes 200.

Find

sw_status(true, methods, "DELETE")

Replace with

sw_status(true, methods, "POST")

Output after the change

200
200
404