Rust runtime for Vercel Functions.
Community-maintained package to support using Rust inside Vercel Functions as a Runtime.
The below documentation is for the vercel_runtime
crate (in beta). If you are looking for the legacy runtime instructions using vercel_lambda
see tree/a9495a0.
First, you'll need a vercel.json
file in your project:
{
"functions": {
"api/**/*.rs": {
"runtime": "vercel-rust@4.0.0-beta.3"
}
}
}
A Vercel Function will be created for every file that matches api/**/*.rs
.
Example:
use serde_json::json;
use vercel_runtime::{run, Body, Error, Request, Response, StatusCode};
#[tokio::main]
async fn main() -> Result<(), Error> {
run(handler).await
}
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
Ok(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(
json!({
"message": "你好,世界"
})
.to_string()
.into(),
)?)
}
Finally we need a Cargo.toml
file at the root of your repository.
# --snip--
[dependencies]
# --snip--
# Documentation: https://docs.rs/vercel_runtime/latest/vercel_runtime
vercel_runtime = { version = "0.2.1" }
# You can specify a library for shared logic here (optional)
# [lib]
# path = "src-rs/lib.rs"
# Each handler has to be specified as [[bin]]
[[bin]]
name = "handler"
path = "api/handler.rs"
# Note that you need to provide unique names for dynamic paths
[[bin]]
name = "user-id"
path = "api/user/[id].rs"
[[bin]]
name = "group-id"
path = "api/group/[id].rs"
# --snip--
This Builder supports installing dependencies defined in the Cargo.toml
file.
Furthermore, more system dependencies can be installed at build time with the presence of a shell build.sh
file in the same directory as the entrypoint file.
With vercel dev
and vercel-rust
, you can develop your Rust-based lambdas on your own machine.
During local development with vercel dev
, ensure rust
and cargo
are already installed and available in your PATH
, since they will not be installed automatically. The recommended way to install is with rustup.
Since this project contains both Rust and Node.js code, you need to install the relevant dependencies. If you're only working on the JavaScript side, you only need to install those dependencies (and vice-versa).
# install node dependencies
pnpm install
# install cargo dependencies
cargo fetch
graph TD
A["Lambda Invocation"] --> |"process_request(event: LambdaEvent<VercelEvent>) → Request"| B[Request]
B --> |"handler_fn(req: Request) → Future<Output = Result<Response<Body>, Error>>"| C["Runtime calls handler_fn"]
C --> |"Ok(r) => process_response(r)"| D["Response"]
For more information, please see this issue.
Version | Tag | Published |
---|---|---|
3.1.2 | latest | 2yrs ago |
4.0.0-beta.3 | beta | 3d ago |
4.0.0-canary.4 | canary | 6d ago |