yizzuide / predawn

Predawn is a Rust web framework like Spring Boot -- Predawn 是一个类似 Spring Boot 的 Rust web 框架

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Predawn

Crates.io version docs.rs docs

predawn is a Rust web framework like Spring Boot.

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

Features

  • Built-in OpenAPI support.
  • Automatic dependency injection.
  • Programmable configuration.

More examples can be found in the examples directories.

Credits

About

Predawn is a Rust web framework like Spring Boot -- Predawn 是一个类似 Spring Boot 的 Rust web 框架

License:Apache License 2.0


Languages

Language:Rust 100.0%