https://github.com/ohkami-rs/worker-bindings
Automatically detect Cloudflare Workers bindings in wrangler.toml and bind them to a Rust struct
https://github.com/ohkami-rs/worker-bindings
cloudflare cloudflare-workers rust
Last synced: 11 months ago
JSON representation
Automatically detect Cloudflare Workers bindings in wrangler.toml and bind them to a Rust struct
- Host: GitHub
- URL: https://github.com/ohkami-rs/worker-bindings
- Owner: ohkami-rs
- License: mit
- Created: 2024-06-25T08:40:21.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-08T20:06:04.000Z (over 1 year ago)
- Last Synced: 2025-03-24T08:54:41.327Z (11 months ago)
- Topics: cloudflare, cloudflare-workers, rust
- Language: Rust
- Homepage: https://crates.io/crates/worker-bindings
- Size: 22.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
worker-bindings
Automatically detect bindings in wrangler.toml and bind them to a Rust struct
## Example
*wrangler.toml*
```toml
[vars]
MY_VAR = "my-variable"
[[kv_namespaces]]
binding = "MY_KV"
id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
```
*lib.rs*
```rust
use worker::*;
use worker_bindings::bindings;
/* This knows all your bindings in wrangler.toml */
#[bindings]
struct Bindings;
#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result {
/* load bindings from env */
let b = Bindings::from(&env);
let var: &'static str = b.MY_VAR;
let data = b.MY_KV.get("data").text().await?;
//...
}
```
## Note
- `#[bindings]` works in a cargo workspace but has a limitation that it can't resolve `wrangler.toml` if **more than one** members have `wrangler.toml`s.
- This crate is originally developed in [Ohkami](https://crates.io/crates/ohkami) web framework and later extracted as an independent edition.
## License
`worker-bindings` is licensed under the MIT License ([LICENSE](https://github.com/ohkami-rs/worker-bindings/blob/main/LICENSE) or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)).