9 releases

0.1.4 Aug 29, 2024
0.1.3 Aug 25, 2024
0.1.2 Jun 23, 2024
0.1.1 Dec 24, 2023
0.0.4 Sep 16, 2023

#341 in HTTP server

Download history 58/week @ 2024-06-24 26/week @ 2024-07-01 17/week @ 2024-07-08 13/week @ 2024-07-22 48/week @ 2024-07-29 10/week @ 2024-08-05 13/week @ 2024-08-12 77/week @ 2024-08-19 208/week @ 2024-08-26 28/week @ 2024-09-02 28/week @ 2024-09-09 9/week @ 2024-09-16 25/week @ 2024-09-23 8/week @ 2024-09-30 7/week @ 2024-10-07

53 downloads per month

MIT license

25KB
576 lines

actix-msgpack

Msgpack payload extractor for Actix Web.

Installation

cargo add actix-msgpack

Documentation

Example

use actix_msgpack::MsgPack;
use actix_web::{post, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Data {
    payload: String,
}
#[post("/")]
async fn index(data: MsgPack<Data>) -> impl Responder {
    println!("payload: {}", data.payload);
    HttpResponse::Ok().finish()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new().service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

You can set settings:

use actix_msgpack::MsgPackConfig;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let mut config = MsgPackConfig::default();
        // set max limit in bytes (default is 256kb)
        config.limit(1024); // 1kb
        // set error handler
        config.error_handler(|err, _req| {
            InternalError::from_response(err, HttpResponse::BadRequest().finish()).into()
        });
        // set allowed content-type (default is application/msgpack)
        config.content_type(|mime_type| mime_type == mime::APPLICATION_JSON)

        App::new().app_data(Data::new(msgpack_config)).service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

You can use responder:

use actix_msgpack::MsgPackResponseBuilder;

#[derive(Serialize)]
struct Data {
    payload: bool,
}

#[post("/")]
async fn index() -> HttpResponse {
    let payload = Data { payload: true };
    HttpResponse::Ok().msgpack(payload)
}

License

This project is licensed under of MIT license (LICENSE or https://opensource.org/licenses/MIT)

Dependencies

~15–25MB
~444K SLoC