10 unstable releases (3 breaking)

new 0.74.3 Dec 12, 2024
0.74.2 Nov 11, 2024
0.74.0 Oct 28, 2024
0.73.0 Oct 1, 2024
0.1.2 Sep 23, 2024

#532 in HTTP server

Download history 298/week @ 2024-09-18 308/week @ 2024-09-25 86/week @ 2024-10-02 72/week @ 2024-10-09 2037/week @ 2024-10-16 16270/week @ 2024-10-23 16665/week @ 2024-10-30 12034/week @ 2024-11-06 13602/week @ 2024-11-13 14240/week @ 2024-11-20 19614/week @ 2024-11-27 17530/week @ 2024-12-04

66,831 downloads per month
Used in 2 crates (via salvo)

MIT/Apache

9KB

Salvo is an extremely simple and powerful Rust web backend framework. Only basic Rust knowledge is required to develop backend services.

salvo-craft

Salvo Handler modular craft macros.

#[craft]

#[craft] is an attribute macro used to batch convert methods in an impl block into Salvo's Handler.

use salvo::oapi::extract::*;
use salvo::prelude::*;
use salvo_craft::craft;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    let service = Arc::new(Service::new(1));
    let router = Router::new()
        .push(Router::with_path("add1").get(service.add1()))
        .push(Router::with_path("add2").get(service.add2()))
        .push(Router::with_path("add3").get(Service::add3()));
    let acceptor = TcpListener::new("127.0.0.1:5800").bind().await;
    Server::new(acceptor).serve(router).await;
}

#[derive(Clone)]
pub struct Service {
    state: i64,
}

#[craft]
impl Service {
    fn new(state: i64) -> Self {
        Self { state }
    }
    /// doc line 1
    /// doc line 2
    #[craft(handler)]
    fn add1(&self, left: QueryParam<i64>, right: QueryParam<i64>) -> String {
        (self.state + *left + *right).to_string()
    }
    /// doc line 3
    /// doc line 4
    #[craft(handler)]
    pub(crate) fn add2(
        self: ::std::sync::Arc<Self>,
        left: QueryParam<i64>,
        right: QueryParam<i64>,
    ) -> String {
        (self.state + *left + *right).to_string()
    }
    /// doc line 5
    /// doc line 6
    #[craft(handler)]
    pub fn add3(left: QueryParam<i64>, right: QueryParam<i64>) -> String {
        (*left + *right).to_string()
    }
}

Sure, you can also replace #[craft(handler)] with #[craft(endpoint(...))].

NOTE: If the receiver of a method is &self, you need to implement the Clone trait for the type.

Documentation & Resources

☕ Donate

Salvo is an open source project. If you want to support Salvo, you can ☕ buy me a coffee here.

⚠️ License

Salvo is licensed under either of

Dependencies

~3.5–5MB
~94K SLoC