API routes
Overview
API routes offer a solution for building a public API using Tuono.
Any file inside the src/routes/api
folder is mapped to the /api/*
path
and will be treated as an API endpoint, rather than a React route.
Tuono server is internally managed by axum.
The API system allows you to create APIs using most of the axum syntax.
Tuono re-exports axum tuono_lib::axum::*
to be seamlessly used within any Tuono app.
The major differences are:
- The function to be matched with Tuono's file system routing must have the
tuono_lib::api(method)
macro attribute on top of it. - The first argument of the function is always a
tuono_lib::Request
.
Tuono also supports dynamic routes for APIs.
Example
If you create src/routes/api/books.rs
with the following content,
any HTTP GET request to /api/books
will return a list of the queried books as JSON.
Unlike React routes, API must not return
tuono_lib::Response
The same file can contain multiple HTTP methods.