Skip to content

Commit 7d23f7a

Browse files
dglsparsonsecklf
andauthored
Add bundled function support (#100)
* feat: add merged handler support * feat: add merged handler support docs * Update README.md * Update crates/vercel_runtime_macro/Cargo.toml Co-authored-by: Douglas Parsons <[email protected]> * rename paths * WIP (yikes) * Remove the need for env variables * Remove erroneous lockfile * Handle non-bundled routes with v2 output * Update README * Fixes with Florentin * add new dev builder * `4.0.0 stable` * bump crates and examples * bump crates * bump crates and builder modules * update lockfiles --------- Co-authored-by: Florentin / 珞辰 <[email protected]>
1 parent dd7a6f5 commit 7d23f7a

File tree

75 files changed

+1602
-1033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1602
-1033
lines changed

β€ŽCargo.lock

Lines changed: 147 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽCargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
[workspace]
22

33
members = [
4-
"vercel_runtime",
5-
"examples/simple",
4+
"crates/vercel_runtime",
5+
"crates/vercel_runtime_macro",
6+
"crates/vercel_runtime_router",
67
"examples/cron",
78
"examples/nextjs",
9+
"examples/simple",
10+
"examples/route-merge",
811
]
912

1013
exclude = [

β€ŽREADME.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The below documentation is for the `vercel_runtime` crate (in beta). If you are
3232
{
3333
"functions": {
3434
"api/**/*.rs": {
35-
"runtime": "[email protected]-beta.4"
35+
"runtime": "[email protected]"
3636
}
3737
}
3838
}
@@ -79,7 +79,7 @@ edition = "2021"
7979
tokio = { version = "1", features = ["macros"] }
8080
serde_json = { version = "1", features = ["raw_value"] }
8181
# Documentation: https://docs.rs/vercel_runtime/latest/vercel_runtime
82-
vercel_runtime = { version = "0.2.1" }
82+
vercel_runtime = { version = "1.0.0" }
8383

8484
# You can specify a library for shared logic here (optional)
8585
# [lib]
@@ -146,6 +146,66 @@ Unfortunately, the AWS Lambda Runtime for Rust relies (tangentially) on `proc_ma
146146

147147
For more information, please see [this issue](https://github.com/mike-engel/vercel-rust/issues/2).
148148

149+
### Experimental API Bundling
150+
151+
This feature allows you to bundle all of your routes into _a single_ deployed Vercel function.
152+
This serves to optimize cold starts, as lambda functions are reused as much as possible.
153+
In addition, this has the benefit of only needing to annotate a single `[[bin]]` in your `Cargo.toml`.
154+
155+
To enable this behaviour, take the following steps:
156+
157+
1. Create a `api/main.rs`.
158+
159+
```rust
160+
use vercel_runtime::{bundled_api, run, Body, Error, Request, Response};
161+
162+
#[tokio::main]
163+
async fn main() -> Result<(), Error> {
164+
run(handler).await
165+
}
166+
167+
// bundled_api is a proc macro which injects a router for all `*.rs` files in your `api` directory.
168+
// If you are using cargo workspaces (like `examples/route-merge` in this repository),
169+
// then an additional `path` argument must be passed to the macro. E.g.
170+
// #[bundled_api( path = "examples/route-merge" )]
171+
#[bundled_api]
172+
pub async fn handler(req: Request) -> Result<Response<Body>, Error> {}
173+
```
174+
175+
2. Change your `vercel.json` to only specify your `api/main.rs` file.
176+
177+
```json
178+
{
179+
"functions": {
180+
"api/main.rs": {
181+
"runtime": "[email protected]"
182+
}
183+
}
184+
}
185+
```
186+
187+
3. Change your `Cargo.toml` to specify the binary for `main.rs`.
188+
189+
```toml
190+
[[bin]]
191+
name = "main"
192+
path = "api/main.rs"
193+
```
194+
195+
4. Add a `handler` function to each route in `api/**`.
196+
197+
```rust
198+
// Example api/foo.rs
199+
use vercel_runtime::{Body, Error, Request, Response, StatusCode};
200+
201+
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
202+
Ok(Response::builder()
203+
.status(StatusCode::OK)
204+
.header("Content-Type", "application/json")
205+
.body(Body::Text("Route is /api/foo".into()))?)
206+
}
207+
```
208+
149209
## Contributing
150210

151211
Since this project contains both Rust and Node.js code, you need to install the relevant dependencies. If you're only working on the TypeScript side, you only need to install those dependencies (and vice-versa).
File renamed without changes.
File renamed without changes.

β€Žvercel_runtime/Cargo.toml renamed to β€Žcrates/vercel_runtime/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vercel_runtime"
3-
version = "0.2.1"
3+
version = "1.0.0"
44
edition = "2021"
55
authors = ["Vercel <[email protected]>"]
66
description = "Vercel Rust Function Runtime"
@@ -31,3 +31,7 @@ tower-service = "0.3.2"
3131
base64 = "0.21.0"
3232
bytes = "1.4.0"
3333
async-trait = "0.1.66"
34+
# vercel_runtime_router = { version = "1.0.0", path = "../vercel_runtime_router" }
35+
# vercel_runtime_macro = { version = "1.0.0", path = "../vercel_runtime_macro" }
36+
vercel_runtime_router = "1.0.0"
37+
vercel_runtime_macro = "1.0.0"
File renamed without changes.

β€Žvercel_runtime/src/lib.rs renamed to β€Žcrates/vercel_runtime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use response::EventResponse;
88
use std::future::Future;
99
use tracing::{debug, error};
1010

11+
pub use vercel_runtime_macro::bundled_api;
12+
pub use vercel_runtime_router::{Route, Router};
13+
1114
pub type Event<'a> = LambdaEvent<VercelEvent<'a>>;
1215

1316
pub use lambda_http::{
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
Β (0)