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日

#547 in #web-framework

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

MIT/Apache

71KB
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
    }
}

鸣谢

依赖项

~1–1.5MB
~30K SLoC