Skip to content

crate

weft_api

Server-side API routes for Weft apps, written once with actix-web and run either way:

  • On Vercel, each api/<name>.rs is a Rust function: weft_api::vercel::run(my_app::server::configure).await (feature vercel, built on vercel_runtime's actix support).
  • In Docker (or locally), one process serves the built site and the same routes: weft_api::serve("dist", 8080, my_app::server::configure) (feature serve).

Ready-made routes: mongo (feature mongodb), halaxy (feature halaxy), and relay for sources browsers may not read. send makes weft_data requests from the server (feature client, on by default).

Modules

  • key_proxy

    A route in front of an API that needs a key, so a component's src can reach it without the key going anywhere near the browser.

  • relay

    The relay for sources the browser isn't allowed to read: a src or load on a site that sends no CORS header. The page asks /api/weft-relay?url=…, and this route fetches the URL and answers with what the site sent. See weft_data::relay.

Structs

  • Bridge

    A handle to an actix app running on its own thread.

  • Reply

    A finished response.

Functions

env

pub fn env(name: &str) -> Result<String, HttpResponse>

An environment variable, or a 500 naming the missing setting (never its value).

error

pub fn error(status: u16, message: &str) -> HttpResponse

{"error": message} with a status.

json

pub fn json(status: u16, body: &Value) -> HttpResponse

A JSON response.

load_dotenv

pub fn load_dotenv(path: impl AsRef<Path>) -> usize

Load KEY=value lines from a .env file into the environment, without overriding variables that are already set (so the host's settings win). Returns how many were loaded.

now

pub fn now() -> f64

Seconds since the epoch.

parse_dotenv

pub fn parse_dotenv(text: &str) -> Vec<(String, String)>

Parse .env text: KEY=value, optional export , quotes, # comments.

send

pub async fn send(req: Request) -> Result<Response, Error>

Send a request (with a 20 s timeout).

Re-exports