#worker #cloudflare-workers #cloudflare #serverless #events

worker-bindings

自动将`wrangler.toml`中的绑定绑定到Rust结构体中

2个稳定版本

2.0.0 2024年6月28日
1.0.0 2024年6月25日

509WebAssembly

每月27次下载

MIT许可

17KB
247

worker-bindings

自动将`wrangler.toml`中的绑定绑定到Rust结构体中

test status of worker-bindings crates.io

示例

wrangler.toml

[vars]
MY_VAR = "my-variable"

[[kv_namespaces]]
binding = "MY_KV"
id      = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

lib.rs

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<Response> {
    /* 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?;

    //...
}

注意

  • #[bindings]在cargo工作空间中工作,但有一个限制,如果成员超过一个,它将无法解析`wrangler.toml`。
  • 此crate最初是在Ohkami web框架中开发的,后来作为一个独立的版本提取出来。

许可

worker-bindings根据MIT许可证授权(LICENSEhttps://opensource.org/licenses/MIT)。

依赖

~4MB
~82K SLoC