updates to attempt at common lisp server

This commit is contained in:
Chris Cochrun 2023-05-30 09:29:35 -05:00
parent d8740cdb73
commit 0cad38bb05
6 changed files with 543 additions and 42 deletions

View file

@ -1,6 +1,21 @@
use actix_multipart::{
form::{
tempfile::{TempFile, TempFileConfig},
MultipartForm,
},
Multipart,
};
use actix_web::{middleware, post, web, App, Error, HttpResponse, HttpServer, Result};
// use serde::Deserialize;
use futures_util::StreamExt as _;
use std::{env, io};
use actix_web::{middleware, web, App, HttpServer, get, post, Responder, http::header::ContentType, HttpResponse, HttpRequest, http::{StatusCode, Method}, HttpMessage};
use multer::Multipart;
// #[derive(Debug, MultipartForm)]
// struct HealthForm {
// firstname: String,
// lastname: String,
// birthdate: String,
// }
#[actix_web::main]
async fn main() -> io::Result<()> {
@ -9,22 +24,55 @@ async fn main() -> io::Result<()> {
HttpServer::new(|| {
App::new()
// enable logger - always register actix-web Logger middleware last
// enable logger - always register actix-web Logger middleware last
.wrap(middleware::Logger::default())
// register HTTP requests handlers
.app_data(web::FormConfig::default().limit(1048576))
// register HTTP requests handlers
.service(form)
})
.bind("0.0.0.0:4242")?
.run()
.await
.bind("0.0.0.0:4242")?
.run()
.await
}
#[post("/health-form")]
async fn form(req: HttpRequest) -> HttpResponse {
println!("{req:?}");
println!("{:?}", req.content_type());
println!("{:?}", req);
println!("{:?}", req.headers().get("content-type"));
// let multipart = Multipart::new(stream, boundary);
HttpResponse::Ok().respond_to(&req)
async fn form(mut form: Multipart) -> Result<HttpResponse, Error> {
// println!("{form:?}");
while let Some(item) = form.next().await {
let mut field = item?;
// let mut value = "";
let name = field.name();
let content_type = field.content_type().unwrap().to_string();
if content_type == "application/octet-stream" {
// Handle file field
// You can save the file or process its contents here
while let Some(chunk) = field.try_next().await? {
// Process file chunk
}
} else {
// Handle other field types (e.g., text fields)
let field_value = field
.try_next()
.await?
.expect("Field value not found")
.to_utf8()
.expect("Failed to decode field value as UTF-8");
// Process the field value (e.g., store in a database, perform validation, etc.)
println!("Field {}: {}", name, field_value);
}
// while let Some(chunk) = field.try_next().await {
// let value = std::str::from_utf8(&chunk?).unwrap();
// println!("{:?}: {:?}", name, value);
// }
}
Ok(HttpResponse::Ok().into())
}
fn get_boundary(s: &str) -> &str {
if let Some(index) = s.find("=") {
&s[(index + 1)..]
} else {
s
}
}