38 stable releases

2.0.4 Sep 9, 2023
2.0.2 Sep 6, 2023
1.10.0 Sep 1, 2023
1.4.12 Aug 31, 2023
0.1.0 Apr 9, 2023

#377 in WebSocket

Apache-2.0

150KB
4K SLoC

examples

⚡️ Quick Start

use athene's full feature

use athene::prelude::*;

#[derive(Debug, Serialize, Deserialize, Validate, Default)]
pub struct UserController {
    #[validate(email)]
    pub email: String, // admin@outlook.com
    #[validate(range(min = 18, max = 20))]
    pub age: u16,
}

// http://127.0.0.1:7878/api/v1/user
#[controller(prefix = "api", version = 1, name = "user")]
impl UserController {

    // http://127.0.0.1:7878/api/v1/user/admin@outlook.com/18
    // Uri Path Params
    #[delete("/{email}/{age}")]
    pub async fn delete(&self, email: String, age: Option<u16>) -> impl Responder {
        (
            StatusCode::OK,
            format!("email is : {}, and age is : {:?}", email, age),
        )
    }

    // http://127.0.0.1:7878/api/v1/user/query_get/?email=admin@outlook.com&age=29
    // Query Params
    #[get("/query_get")]
    pub async fn query_get(&self, email: String, age: u16) -> impl Responder {
        let user = Self { email, age };
        user.validate()?; // user will be validate
        Ok::<_, Error>((200, Json(user)))
    }

    // http://127.0.0.1:7878/api/v1/user/query/?email=admin@outlook.com&age=19
    // Query Params
    #[get("/query")] // user will be validate
    pub async fn query(&self, user: Query<Self>) -> impl Responder {
        (200, Json(user.0))
    }

    // http://127.0.0.1:7878/api/v1/user/create
    // Context-Type : application/json
    #[post("/create")] // user will be validate
    async fn create(&self, user: Json<Self>) -> impl Responder {
        Ok::<_, Error>((200, user))
    }

    // http://127.0.0.1:7878/api/v1/user/update
    // Context-Type : application/x-www-form-urlencoded
    #[put("/update")]
    #[validator(exclude("user"))] // user will not be validate
    async fn update(&self, user: Form<Self>) -> impl Responder {
        Ok::<_, Error>((200, user))
    }
}

#[tokio::main]
pub async fn main() -> Result<()> {
    
    // Add Router
    let app = athene::new().router(upload_router).router(|r|r.controller(UserController::default()));

    // Start Server
    app.listen("127.0.0.1:7878").await
}

Dependencies

~8–22MB
~341K SLoC