#web-framework #web #async #http #framework

predawn-core

predawn 的核心类型和特性

7 个版本 (破坏性更新)

0.7.0 2024年4月16日
0.6.0 2024年4月9日
0.5.0 2024年4月2日
0.4.0 2024年3月23日
0.1.0 2024年3月5日

464#web-framework 中排名

每月 26 次下载
2 个 crate 中使用 (通过 predawn)

MIT/Apache 协议

73KB
2K SLoC

Predawn

Crates.io version docs.rs docs

predawn 是一个类似于 Spring Boot 的 Rust Web 框架。

use predawn::{
    app::{run_app, Hooks},
    controller,
};
use rudi::Singleton;

struct App;

impl Hooks for App {}

#[tokio::main]
async fn main() {
    run_app::<App>().await;
}

#[derive(Clone)]
#[Singleton]
struct Controller;

#[controller]
impl Controller {
    #[handler(paths = ["/"], methods = [post])]
    async fn hello(&self, name: String) -> String {
        format!("Hello {name}")
    }
}

功能

  • 内置 OpenAPI 支持。
  • 自动依赖注入。
  • 可编程配置。

更多示例可以在 示例目录 中找到。

更复杂的示例

use std::sync::Arc;

use async_trait::async_trait;
use predawn::{
    app::{run_app, Hooks},
    controller,
};
use rudi::Singleton;

struct App;

impl Hooks for App {}

#[tokio::main]
async fn main() {
    run_app::<App>().await;
}

#[async_trait]
trait Service: Send + Sync {
    fn arc(self) -> Arc<dyn Service>
    where
        Self: Sized + 'static,
    {
        Arc::new(self)
    }

    async fn hello(&self) -> String;
}

#[derive(Clone)]
#[Singleton(binds = [Service::arc])]
struct ServiceImpl;

#[async_trait]
impl Service for ServiceImpl {
    async fn hello(&self) -> String {
        "Hello, World!".to_string()
    }
}

#[derive(Clone)]
#[Singleton]
struct Controller {
    svc: Arc<dyn Service>,
}

#[controller]
impl Controller {
    #[handler(paths = ["/"], methods = [GET])]
    async fn hello(&self) -> String {
        self.svc.hello().await
    }
}

鸣谢

依赖

~5.5–8MB
~136K SLoC