Skip to content

Commit 53ab83b

Browse files
ecklfdglsparsons
andauthored
update documentation and changelog (#94)
* update documentation and changelog * add example link * fix PR number * reword local dev instructions * add changes based on feedback * fix typo * move legacy runtime back to top * add missing punctation * Update README.md Co-authored-by: Douglas Parsons <dglsparsons@users.noreply.github.com> * add full `Cargo.toml` --------- Co-authored-by: Douglas Parsons <dglsparsons@users.noreply.github.com>
1 parent 90e8707 commit 53ab83b

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## Unreleased
44

5+
## 4.0.0.beta.4
6+
7+
- Update documentation #94
8+
- Add cargo build config and prebuilt support #91
9+
- Prevent leaking source code in examples #90
10+
- Add tests and docs for configurable toolchain overrides #89
11+
512
## 4.0.0.beta.3
613

714
- Execute cargo build inside `workPath` #85

README.md

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ Community-maintained package to support using [Rust](https://www.rust-lang.org/)
2020

2121
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](https://github.com/vercel-community/rust/tree/a9495a0f0d882a36ea165f1629fcc79c30bc3108).
2222

23-
## Usage
23+
## Getting Started
2424

25-
First, you'll need a `vercel.json` file in your project:
25+
> Please ensure [Vercel CLI](https://vercel.com/docs/cli#installing-vercel-cli) and the Rust toolchain is already installed on your system. We recommended setting up Rust with [rustup](https://rustup.rs/).
26+
27+
[Prefer looking at examples?](https://github.com/vercel-community/rust/tree/main/examples)
28+
29+
**Step 1** - Add a `vercel.json` file to your project.
2630

2731
```json
2832
{
@@ -34,9 +38,11 @@ First, you'll need a `vercel.json` file in your project:
3438
}
3539
```
3640

37-
A Vercel Function will be created for every file that matches `api/**/*.rs`.
41+
This turns every file matching `api/**/*.rs` into a Vercel Function.
3842

39-
Example:
43+
> **Note:** The npm dependency `vercel-rust` defined in [functions](https://vercel.com/docs/concepts/projects/project-configuration#functions) **does not** have to be installed manually.
44+
45+
**Step 2** - Create a function. As an example, here is `api/handler.rs`.
4046

4147
```rust
4248
use serde_json::json;
@@ -61,10 +67,13 @@ pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
6167
}
6268
```
6369

64-
Finally we need a `Cargo.toml` file at the root of your repository.
70+
**Step 3** - Create a `Cargo.toml` in the root directory of your project.
6571

6672
```toml
67-
# --snip--
73+
[package]
74+
name = "my-vercel-api"
75+
version = "0.1.0"
76+
edition = "2021"
6877

6978
[dependencies]
7079
tokio = { version = "1", features = ["macros"] }
@@ -81,27 +90,39 @@ vercel_runtime = { version = "0.2.1" }
8190
name = "handler"
8291
path = "api/handler.rs"
8392

84-
# Note that you need to provide unique names for each binary
85-
[[bin]]
86-
name = "user-id"
87-
path = "api/user/[id].rs"
93+
# Note that you need to provide unique names for each binary:
94+
# [[bin]]
95+
# name = "user-id"
96+
# path = "api/user/[id].rs"
97+
#
98+
# [[bin]]
99+
# name = "group-id"
100+
# path = "api/group/[id].rs"
101+
```
88102

89-
[[bin]]
90-
name = "group-id"
91-
path = "api/group/[id].rs"
103+
**Step 4** - Create a `.vercelignore` in the root directory of your project to ignore build artifacts.
92104

93-
# --snip--
105+
```shell
106+
target/
94107
```
95108

109+
**Step 5** - You're all set. Run `vercel dev` to develop your project locally. You can connect a Git repository to Vercel, or use `vercel` to start deploying your project on Vercel.
110+
111+
## Advanced Usage
112+
113+
### Toolchain Overrides
114+
115+
An example on how this can be achieved is using a `rust-toolchain` file adjacent to your `Cargo.toml`. Please refer to [Rust Documentation](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) for more details.
116+
96117
### Dependencies
97118

98-
This Builder supports installing dependencies defined in the `Cargo.toml` file.
119+
By default builder module supports installing dependencies defined in the `Cargo.toml` file.
99120

100-
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.
121+
More system dependencies can be installed at build time with the presence of a shell `build.sh` file in the root directory of your project.
101122

102-
## Prebuilt Deployments
123+
### Prebuilt Deployments
103124

104-
When creating a prebuilt deployment, the build output must be for x86_64 linux. To do this, create a Cargo build configuration at `.cargo/config.toml` with the following contents:
125+
When creating a prebuilt deployment, the build output must be for `x86_64 linux`. To do this, create a Cargo build configuration at `.cargo/config.toml` with the following contents:
105126

106127
```toml
107128
[build]
@@ -113,17 +134,17 @@ target = "x86_64-unknown-linux-musl"
113134
# linker = "x86_64-unknown-linux-gnu-gcc"
114135
```
115136

116-
You then can build the file and trigger the deployment with the Vercel CLI.
137+
You then can build the file and trigger the deployment via [Vercel CLI](https://vercel.com/docs/cli#installing-vercel-cli).
117138

118139
```shell
119140
vercel build && vercel deploy --prebuilt
120141
```
121142

122-
## Local Development
143+
### Musl/Static linking
123144

124-
With `vercel dev` and `vercel-rust`, you can develop your Rust-based lambdas on your own machine.
145+
Unfortunately, the AWS Lambda Runtime for Rust relies (tangentially) on `proc_macro`, which won't compile on musl targets. Without `musl`, all linking must be dynamic. If you have a crate that relies on system libraries like `postgres` or `mysql`, you can include those library files with the `includeFiles` config option and set the proper environment variables, config, etc. that you need to get the library to compile.
125146

126-
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](https://rustup.rs/).
147+
For more information, please see [this issue](https://github.com/mike-engel/vercel-rust/issues/2).
127148

128149
## Contributing
129150

@@ -137,29 +158,19 @@ pnpm install
137158
cargo fetch
138159
```
139160

140-
## Invocation Flowchart
161+
### Builder Module
162+
163+
The _npm_ module `vercel-rust` is implementing an interface which is primarily taking care of spawning a development server, caching between consecutive builds, and running the compilation. You can read more about the in-depths of implementing a builder [here](https://github.com/vercel/vercel/blob/main/DEVELOPING_A_RUNTIME.md).
164+
165+
Note that this dependency **does not** have to be installed manually as it is pulled automatically.
166+
167+
### Runtime Crate
168+
169+
The crate `vercel_runtime` is what you will consume in your Rust functions. As the name suggests, the runtime crate takes care of everything that happens during run-time. In specific it takes care of creating a [Tower](https://docs.rs/tower/latest/tower/trait.Service.html) service, which expects a specific handler signature. The flow of an invocation can be visualized as the following:
141170

142171
```mermaid
143172
graph TD
144-
A["Lambda Invocation"] --> |"process_request(event: LambdaEvent&lt;VercelEvent&gt;) → Request"| B[Request]
173+
A["Function Invocation"] --> |"process_request(event: InvocationEvent&lt;VercelEvent&gt;) → Request"| B[Request]
145174
B --> |"handler_fn(req: Request) → Future&lt;Output = Result&lt;Response&lt;Body&gt;, Error&gt;&gt;"| C["Runtime calls handler_fn"]
146175
C --> |"Ok(r) => process_response(r)"| D["Response"]
147176
```
148-
149-
## FAQ
150-
151-
<details>
152-
<summary>How to specify toolchain overrides</summary>
153-
154-
An example on how this can be achieved is using a `rust-toolchain` file adjacent to your `Cargo.toml`. Please refer to [Rust Documentation](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) for more details.
155-
156-
</details>
157-
158-
<details>
159-
<summary>Can I use musl/static linking?</summary>
160-
161-
Unfortunately, the AWS Lambda Runtime for Rust relies (tangentially) on `proc_macro`, which won't compile on musl targets. Without `musl`, all linking must be dynamic. If you have a crate that relies on system libraries like `postgres` or `mysql`, you can include those library files with the `includeFiles` config option and set the proper environment variables, config, etc. that you need to get the library to compile.
162-
163-
For more information, please see [this issue](https://github.com/mike-engel/vercel-rust/issues/2).
164-
165-
</details>

0 commit comments

Comments
 (0)