wip (why is it hard to get the body in actix-web?)

This commit is contained in:
Andrew Morris
2023-12-29 15:31:51 +11:00
parent 6949a52c9a
commit 82934f21f6

View File

@@ -1,8 +1,7 @@
use std::{io::Write, process::exit, rc::Rc};
use actix_web::{
web::{self},
App, HttpRequest, HttpResponse, HttpServer, Responder,
dev::Payload, web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder,
};
use storage::{storage_head_ptr, SledBackend, Storage, StorageReader};
@@ -155,18 +154,33 @@ fn db_call(path: &String, args: &[String]) {
.unwrap();
}
async fn get_body(req: &HttpRequest) -> Result<Val, actix_web::Error> {
let payload = web::Payload::from_request(req, &mut Payload::None).await?;
let body = payload
.to_bytes_limited(1_024 * 1_024)
.await
.map_err(|_| actix_web::error::PayloadError::Overflow)??;
if body.is_empty() {
Ok(Val::Undefined)
} else {
match serde_json::from_slice::<serde_json::Value>(&body) {
Ok(json_value) => Ok(Val::from_json(&json_value)),
Err(err) => Err(actix_web::error::ErrorBadRequest(err)),
}
}
}
async fn handle_request(req: HttpRequest, data: web::Data<String>) -> impl Responder {
let path = req.path();
let method = req.method();
let mut storage = Storage::new(SledBackend::open(data.as_ref()).unwrap());
// let body = Val::from_json(
// &web::Json::<serde_json::Value>::from_request(&req, &mut Payload::None)
// .await
// .unwrap()
// .into_inner(),
// );
let body = Val::Undefined;
let body = match get_body(&req).await {
Ok(body) => body,
Err(e) => return e.into(),
};
let mut instance: Val = storage
.get_head(storage_head_ptr(b"state"))
@@ -176,6 +190,7 @@ async fn handle_request(req: HttpRequest, data: web::Data<String>) -> impl Respo
let fn_ = inline_valuescript(
r#"
export default function(req) {
console.log('req', req.path, req.method, req.body);
if ("handleRequest" in this) {
return this.handleRequest(req);
}