62 releases (14 breaking)

0.14.1 Sep 16, 2024
0.13.2 Aug 27, 2024
0.12.3 Jul 12, 2024
0.5.2 Mar 20, 2024
0.4.3 Nov 28, 2023

#204 in HTTP server

Download history 140/week @ 2024-06-21 219/week @ 2024-06-28 213/week @ 2024-07-05 112/week @ 2024-07-12 74/week @ 2024-07-19 146/week @ 2024-07-26 17/week @ 2024-08-02 109/week @ 2024-08-09 4/week @ 2024-08-16 289/week @ 2024-08-23 31/week @ 2024-08-30 381/week @ 2024-09-13 120/week @ 2024-09-20 79/week @ 2024-09-27 2/week @ 2024-10-04

582 downloads per month
Used in hypers_middleware

Apache-2.0

280KB
7K SLoC

examples

hypers_rbatis_admin

⚡️ Quick Start

Cargo.toml

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

Rust Code

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/admin@outlook.com/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/query?email=admin@outlook.com&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
}

Supports OpenAPI

hypers_openapi

Dependencies

~14–31MB
~590K SLoC