xroute 0.1.0-alpha.2

A heavily opinionated HTTP server wrapper for Rust web applications
Documentation
use std::sync::atomic::AtomicUsize;
use xroute::prelude::*;

#[state]
struct AppState {
    counter: AtomicUsize,
}

router! {
    state = AppState;
    test,
}

#[main]
async fn main() {
    let state = AppState { counter: 0.into() };
    let router = router(state);
    run(router).await;
}

#[submodule(crate::AppState)]
mod test {
    #[response("state_example")]
    pub struct IncResponse {
        pub message: String,
        pub value: u16,
    }

    #[request("state_example")]
    pub struct Increment {
        pub value: u16,
    }

    #[post("/increment", state)]
    async fn increment_counter(inc: Increment) {
        let counter = state
            .counter
            .fetch_add(inc.value as usize, std::sync::atomic::Ordering::SeqCst)
            as u16;

        IncResponse {
            message: "ok".to_string(),
            value: counter + inc.value,
        }.res()
    }

    #[response("state_example")]
    pub struct RootResponse {
        pub value: u16,
    }

    #[get("/", state)]
    async fn get_counter() {
        let counter = state.counter.load(std::sync::atomic::Ordering::SeqCst) as u16;
        RootResponse { value: counter }.res()
    }
}