Crates.io | actix-msgpack |
lib.rs | actix-msgpack |
version | 0.1.4 |
source | src |
created_at | 2023-09-10 09:13:18.1165 |
updated_at | 2024-08-29 20:52:35.710164 |
description | Msgpack payload extractor for Actix Web |
homepage | |
repository | https://github.com/ym-project/actix-msgpack/tree/v4 |
max_upload_size | |
id | 968732 |
size | 26,044 |
cargo add actix-msgpack
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
}
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
}
use actix_msgpack::MsgPackResponseBuilder;
#[derive(Serialize)]
struct Data {
payload: bool,
}
#[post("/")]
async fn index() -> HttpResponse {
let payload = Data { payload: true };
HttpResponse::Ok().msgpack(payload)
}
This project is licensed under of MIT license (LICENSE or https://opensource.org/licenses/MIT)