Rust

Servers

The base of Utopia-style servers: hooks with params, validators, injections, groups and labels, and the mode and hook registries a server keeps.
Rust 1,085 lines (503 code)split
PartCodeCommentsBlankTotalFiles
Rust source5034811011,0854
Rust tests2302251

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-servers = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with Servers, each a complete program you can copy and run.

Every Utopia server (HTTP, CLI, queue workers) runs callbacks declared the same way: a hook names the params it reads, with a default and a validator each, and the resources it injects. The server resolves the params from what a request or a job carries, validates them, and calls the callback.

Declare a hook

A hook's params and injections keep their declaration order, which is the order its callback receives them in.

Rust
use serde_json::json;
use utopia_servers::{Hook, Param};
use utopia_validators::Text;

let mut hook = Hook::new();
hook.desc("Send a welcome email")
    .groups(&["mails"])
    .label("scope", json!("users.write"))
    .param("email", Param::new(json!(""), Text::new(256), "Recipient.").example("me@example.com"))
    .inject("mailer");

assert_eq!(hook.get_groups(), ["mails"]);
assert_eq!(hook.get_label("scope"), Some(&json!("users.write")));
assert_eq!(hook.params()["email"].example, "me@example.com");
assert_eq!(hook.dependencies(), ["mailer"]);

Resolve and validate params

Base::prepare reads each param from the path values or the request (under its key or an alias), falls back to its default, validates it, and returns a child container holding the values.

Rust
use std::sync::Arc;
use serde_json::{json, Map, Value};
use utopia_di::Container;
use utopia_servers::{Base, Hook, Param};
use utopia_validators::Text;

let mut hook = Hook::new();
hook.param("projectId", Param::new(json!(""), Text::new(36), "").aliases(&["project"]));

let request: Map<String, Value> = [("project".to_owned(), json!("demo"))].into_iter().collect();
let scope = Base::new().prepare(&Arc::new(Container::new()), &mut hook, &Map::new(), &request)?;

assert_eq!(*scope.get::<Value>("projectId")?, json!("demo"));
assert_eq!(hook.param_value("projectId")?, &json!("demo"));

Report invalid params

A missing required param, or one its validator rejects, fails with a 400 error whose message names the param.

Rust
use std::sync::Arc;
use serde_json::{json, Map};
use utopia_di::Container;
use utopia_servers::{Base, Error, Hook, Param};
use utopia_validators::Text;

let mut hook = Hook::new();
hook.param("name", Param::new(json!(""), Text::new(10), ""));

let error = Base::new().prepare(&Arc::new(Container::new()), &mut hook, &Map::new(), &Map::new()).unwrap_err();
assert!(matches!(error, Error::Server { code: 400, .. }));
assert_eq!(error.to_string(), "Param \"name\" is not optional.");

Reference#

The base of Utopia-style servers: a Hook is a callback with the params it reads (each with a default, a validator and metadata), the resources it injects, its groups and its labels; a Base keeps the hooks a server runs around its work (init, shutdown, error, start, end), its mode, and resolves a hook's params (Base::prepare) from the values a request or a job carries, validating each one.

Servers built on it (HTTP, CLI, queue workers) declare what an action needs once, and resolve and validate it the same way.

Rust
use serde_json::json;
use utopia_servers::{Hook, Param};
use utopia_validators::Text;

let mut hook = Hook::new();
hook.desc("Create a user")
    .groups(&["api"])
    .param("name", Param::new(json!(""), Text::new(128), "User name."))
    .inject("db");

assert_eq!(hook.description(), "Create a user");
assert_eq!(hook.dependencies(), ["db"]);
assert_eq!(hook.params()["name"].description, "User name.");

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

API#

What utopia_servers exports at its root.