58个版本 (13个破坏性更新)

0.13.0 2024年8月9日
0.12.3 2024年7月12日
0.11.1 2024年7月20日
0.5.2 2024年3月20日
0.4.3 2023年11月28日

#329 in WebSocket

Download history 129/week @ 2024-04-22 20/week @ 2024-04-29 794/week @ 2024-05-06 440/week @ 2024-05-13 167/week @ 2024-05-20 162/week @ 2024-05-27 574/week @ 2024-06-03 515/week @ 2024-06-10 168/week @ 2024-06-17 129/week @ 2024-06-24 323/week @ 2024-07-01 203/week @ 2024-07-08 74/week @ 2024-07-15 17/week @ 2024-07-22 157/week @ 2024-07-29 100/week @ 2024-08-05

每月362次下载
用于 hypers_middleware

Apache-2.0

250KB
6.5K SLoC

示例

hypers_rbatis_admin

⚡️ 快速开始

Cargo.toml

## use hypers's full feature
[dependencies]
hypers = { version = "0.13", features = ["full","debug"] }
tokio = { version = "=1.38.0", features = ["full"] }
serde = { version = "=1.0.205", features = ["derive"] }

Rust代码

use hypers::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct User {
    pub email: Option<String>,
    pub age: Option<u16>,
}

#[derive(Serialize, Deserialize)]
pub struct Header<'a> {
    pub host: Option<&'a str>,
    #[serde(rename(deserialize = "user-agent"))]
    pub user_agent: Option<&'a str>,
    pub accept: Option<&'a str>,
}

pub struct UserController;

#[controller(name = "macro")]
impl UserController {
    // http://127.0.0.1:7878/macro/header
    // Request Header Params
    #[get("/header")]
    async fn header<'a>(req: &'a mut Request) -> Result<Response> {
        let headers = req.parse_header::<Header<'a>>()?;
        let mut res = Response::default();
        res.status(200).json(&headers);
        Ok(res)
    }

    // http://127.0.0.1:7878/macro/[email protected]/18
    // Uri Path Params
    #[delete("/:email/:age")]
    pub async fn delete(req: &mut Request) -> impl Responder {
        let user = req.parse_param::<User>()?;
        Ok::<_, Error>((200, Json(user)))
    }

    // http://127.0.0.1:7878/macro/[email protected]&age=19
    // Query Params
    #[get("/query")]
    async fn query(req: &mut Request) -> impl Responder {
        let user = req.parse_query::<User>()?;
        Ok::<_, Error>((200, Json(user)))
    }

    // http://127.0.0.1:7878/macro/create
    // Context-Type : application/json
    #[post("/create")]
    pub async fn create(req: &mut Request) -> impl Responder {
        let _a = req.accept();
        let user = req.parse_json::<User>().await?;
        let _payload = req.payload().await?;
        Ok::<_, Error>((200, Json(user)))
    }

    // http://127.0.0.1:7878/macro/update
    // Context-Type : application/x-www-form-urlencoded
    #[put("/update")]
    async fn update(req: &mut Request) -> impl Responder {
        let user = req.parse_form::<User>().await?;
        Ok::<_, Error>((200, Form(user)))
    }

    // http://127.0.0.1:7878/macro/multipart_form
    // Context-Type : multipart/form-data Form Fields
    #[post("multipart_form")]
    pub async fn multipart_form(req: &mut Request) -> impl Responder {
        let user = req.parse_form::<User>().await?;
        Ok::<_, Error>((200, Form(user)))
    }

    // http://127.0.0.1:7878/macro/body_bytes
    #[post("/body_bytes")]
    pub async fn body_bytes(req: &mut Request) -> impl Responder {
        let bytes = req.payload().await?;
        let bytes = bytes.clone();
        Ok::<_, Error>((200, bytes))
    }
}
#[tokio::main]
async fn main() -> Result<()> {
    let mut root = Router::default();
    root.push(UserController);
    println!("root = {:#?}", root);
    let listener = hypers::TcpListener::bind("127.0.0.1:7878").await?;
    hypers::listen(root, listener).await
}

支持OpenAPI

hypers_openapi

依赖

~14–32MB
~584K SLoC