middleware to handle json payloads

This commit is contained in:
cupcakearmy 2021-12-20 17:42:16 +01:00
parent 8644a937d0
commit ac5d52a010
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
2 changed files with 21 additions and 2 deletions

View File

@ -5,19 +5,28 @@ extern crate lazy_static;
mod client; mod client;
mod note; mod note;
mod size;
mod store; mod store;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
HttpServer::new(|| { return HttpServer::new(|| {
App::new() App::new()
// .configure(|cfg: &mut web::ServiceConfig| {
// let limit_string = std::env::var("SIZE_LIMIT").unwrap_or("0.1 KiB".to_string());
// let limit = Byte::from_str(limit_string.clone()).unwrap().get_bytes() as usize;
// println!("SIZE_LIMIT: {}, {}", limit_string, limit);
// let config = web::JsonConfig::default().limit(limit);
// cfg.data(config);
// })
.wrap(middleware::Compress::default()) .wrap(middleware::Compress::default())
.wrap(middleware::DefaultHeaders::default()) .wrap(middleware::DefaultHeaders::default())
.configure(size::init)
.configure(note::init) .configure(note::init)
.configure(client::init) .configure(client::init)
.default_service(web::resource("").route(web::get().to(client::fallback_fn))) .default_service(web::resource("").route(web::get().to(client::fallback_fn)))
}) })
.bind("0.0.0.0:5000")? .bind("0.0.0.0:5000")?
.run() .run()
.await .await;
} }

10
src/size.rs Normal file
View File

@ -0,0 +1,10 @@
use actix_web::web;
use byte_unit::Byte;
pub fn init(cfg: &mut web::ServiceConfig) {
let limit_string = std::env::var("SIZE_LIMIT").unwrap_or("1 KiB".to_string());
let limit = Byte::from_str(limit_string.clone()).unwrap().get_bytes() as usize;
println!("SIZE_LIMIT: {}, {}", limit_string, limit);
let config = web::JsonConfig::default().limit(limit);
cfg.data(config);
}