Skip to content

Commit

Permalink
wip (why is it hard to get the body in actix-web?)
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Dec 29, 2023
1 parent 6949a52 commit 82934f2
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions vstc/src/db_command.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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"))
Expand All @@ -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);
}
Expand Down

0 comments on commit 82934f2

Please sign in to comment.