#hyper #web-framework #async #框架 #web-apps #web #http

athene

基于hyper的简单且轻量级的Rust Web框架

38个稳定版本

2.0.4 2023年9月9日
2.0.2 2023年9月6日
1.10.0 2023年9月1日
1.4.12 2023年8月31日
0.1.0 2023年4月9日

#520HTTP服务器

Download history 5/week @ 2024-03-09 1/week @ 2024-03-16 8/week @ 2024-03-30

每月下载量289次

Apache-2.0

150KB
4K SLoC

示例

⚡️ 快速开始

使用athene的全部功能

use athene::prelude::*;

#[derive(Debug, Serialize, Deserialize, Validate, Default)]
pub struct UserController {
    #[validate(email)]
    pub email: String, // [email protected]
    #[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/[email protected]/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 protected]&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 protected]&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
}

依赖项

~8–23MB
~338K SLoC