跳到正文

Workers-rs

热门趋势维护中
cloudflare/workers-rs

Cloudflare Workers 的官方 Rust SDK,让开发者使用 Rust 和 WebAssembly 构建高性能无服务器应用。

在 GitHub 查看
星标
3547
Fork
418
关注
3547
未关闭 Issue
174

该介绍目前仅提供英文版本。

Workers-rs

Workers-rs is the official Rust SDK for Cloudflare Workers, allowing developers to write and deploy Workers using Rust and WebAssembly. This SDK provides a seamless experience for building high-performance serverless applications with the safety and performance benefits of Rust.

Core Features

  • Rust-First Development: Write Workers entirely in Rust
  • WebAssembly Output: Compiles to WebAssembly for optimal performance
  • Type Safety: Leverage Rust’s strong type system
  • Workers Runtime API: Complete access to Workers runtime features
  • Middleware Support: Built-in middleware system
  • Ergonomic Macros: Simplified Worker development with proc macros
  • HTTP Integration: First-class support for HTTP requests and responses

Quick Start

  1. Prerequisites:

    # Install Rust
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
    # Install wrangler
    npm install -g wrangler
  2. Create a New Project:

    wrangler generate my-worker https://github.com/cloudflare/workers-rs/templates/worker
    cd my-worker
  3. Development:

    wrangler dev
  4. Deployment:

    wrangler deploy

Example Worker

use worker::*;

#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
    Response::ok("Hello from Rust!")
}

Key Components

  • Worker Runtime: Complete Rust bindings for the Workers runtime
  • HTTP Types: Ergonomic types for HTTP handling
  • Environment Bindings: Access to Workers KV, R2, and other services
  • Router: Built-in routing system
  • Middleware: Extensible middleware framework
  • WebAssembly: Optimized WASM compilation

Use Cases

  • High-performance API endpoints
  • Complex data processing
  • WebAssembly-powered applications
  • CPU-intensive tasks
  • Security-critical applications
  • Edge computing solutions

Advanced Features

  • KV Storage: Native bindings for Workers KV
  • R2 Storage: Interface with R2 object storage
  • Durable Objects: State management with DO support
  • Fetch API: Complete HTTP client functionality
  • Crypto Operations: Access to Web Crypto API
  • Custom Domains: Domain management support
  • Scheduled Tasks: Cron trigger support

Development Tools

  • wrangler: CLI tool for development and deployment
  • console_error_panic_hook: Better error messages
  • wasm-bindgen: WebAssembly bindings generation
  • workerd: Local development environment

Community and Support

License

This project is licensed under the Apache License 2.0.

按共同分类和标签排序的同类开源项目。

全部 Web 框架项目 →