athene

Crates.ioathene
lib.rsathene
version2.0.4
sourcesrc
created_at2023-04-09 03:21:49.165248
updated_at2023-09-09 03:47:05.713418
descriptionA simple and lightweight rust web framework based on hyper
homepage
repositoryhttps://gitea.com/rustacean/athene
max_upload_size
id833919
size234,121
(lo-ferris)

documentation

https://docs.rs/athene

README

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
}
Commit count: 0

cargo fmt