PluginsBuild a plugin

Server routes

Write a Rustrak plugin's HTTP routes in Rust with the rustrak-plugin crate: route macros, public and instance routes, permissions, extractors, responses and errors.

A plugin’s server is a set of Rust functions, one per route, in src/server/routes/. The rustrak-plugin crate registers each one with an attribute. You write no main, no router and no mod lines: the build declares every file under src/server/ for you.

use rustrak_plugin::prelude::*;
use crate::Perm;

#[get("/visits", requires = Perm::Visits.read())]
fn count(ctx: &Ctx) -> Result<String> {
    let n: i64 = ctx.db().scalar(
        "SELECT count(*) FROM visits WHERE project_id = ?1",
        params![ctx.project_id()],
    )?;
    Ok(n.to_string())
}
Rust

Keep routes in routes/ and the queries they share in models/, as new lays them out. A route macro outside src/server/routes/ fails to compile with a route goes in src/server/routes/.

Where a route answers

#[get], #[post], #[put], #[patch] and #[delete] take the route’s path first. A path starts with / and has no placeholders: Rustrak matches the method and the whole path exactly. Pass an id in the query string or the body instead.

Where the route lives, and who may call it, depends on its flags:

AttributeURLWho can call it
#[get("/stats")]/api/projects/<project id>/plugins/<plugin>/statsA signed-in person or an API token that can see the project
#[get("/stats", requires = Perm::Stats.read())]SameThe same, holding <plugin>:stats read
#[post("/visit", public)]/api/projects/<project id>/plugins/<plugin>/visitAnyone with the project’s DSN key, as ?sentry_key=<key> or X-Rustrak-Key: <key>
#[get("/report", instance)]/api/plugins/<plugin>/reportA signed-in person or an API token. Not tied to a project

instance takes requires too. public takes neither: a public route has no user to check, and it always belongs to a project.

Rustrak checks all of this before your code runs. A project route also answers only in projects the plugin is turned on in, and only once its required settings are set. API tokens call routes with Authorization: Bearer <token>, like the rest of the API.

Arguments

A route takes &Ctx and any of these, in any order. Rustrak builds each from the request and answers 400 when it cannot.

TypeWhat it holds
BodyThe raw body. body.text() reads it as UTF-8
Json<T>The body parsed as JSON into T. Invalid JSON is 400 with invalid JSON body: ...
Query<T>The query string parsed into T. A bad one is 400 with invalid query: ...
use serde::Deserialize;

#[derive(Deserialize)]
struct Page {
    limit: Option<u32>,
}

#[get("/hits", requires = Perm::Hits.read())]
fn list(ctx: &Ctx, page: Query<Page>) -> Result<Json<Vec<Hit>>> {
    let limit = page.0.limit.unwrap_or(20).min(100);
    Ok(Json(hit::latest(ctx, limit)?))
}
Rust

sentry_key is removed from the query string before your code sees it.

The context

Ctx methodReturns
ctx.db()The plugin’s database. See Database
ctx.project_id()Option<u64>: the project, on project routes
ctx.project()The project id, or Error::NotFound outside one
ctx.header(name)A request header, matched case-insensitively
ctx.client_ip()The caller’s address as Rustrak sees it, after its trusted proxies
ctx.settings::<T>()The settings as T. Use the generated Settings::get(ctx) instead

The plugin never sees Cookie, Authorization or X-Rustrak-Key.

Responses

A route returns Result<T> where T is one of:

TypeStatusBody
String or &'static str200Text, text/plain; charset=utf-8
Json(value)200value as JSON
Created(value)201value as JSON
NoContent204None

The value inside Json and Created is anything that implements serde::Serialize.

Errors

Return an Error and Rustrak answers with its status and a JSON body, {"detail": "..."}:

VariantStatusdetail
Error::BadRequest(message)400message
Error::Forbidden403Forbidden
Error::NotFound404Not found
Error::Internal(message)500Internal error. message goes to Rustrak’s log, not to the caller
Error::Unavailable(message)503message

A database error turns into Error::Internal with ?, so a failed query is a 500 and its message lands in the log.

Some answers come from Rustrak, not from your code. They use Rustrak’s own error body, {"error": {"type": "...", "message": "..."}}, except the 504, which carries detail:

StatusWhen
401A public route without the key (Missing or invalid project key) or with another project’s (Invalid key for project)
403The caller lacks the permission the route requires (Missing permission <plugin>:<resource>:<action>)
404No such route, or the plugin is not on in that project (Plugin route not found in <plugin>)
503A required setting is not set, or 8 requests are already running and the next waited 5 seconds
504The route ran past its 2 seconds, its queries included (The plugin did not answer in time)
500The code panicked. A panic aborts the instance

Logs

What the plugin writes as an internal error goes to Rustrak’s log under the target rustrak::plugin, prefixed with the plugin’s id:

[my-plugin] the plugin's storage is full
Esc

Type to search guides, configuration and the API.