some basics for a rust server

This commit is contained in:
Chris Cochrun 2023-06-15 07:20:50 -05:00
parent fce79269e1
commit 27d2e51160

View file

@ -1,6 +1,7 @@
use actix_multipart::{ use actix_multipart::{
form::{ form::{
tempfile::{TempFile, TempFileConfig}, tempfile::{TempFile, TempFileConfig},
text::Text,
MultipartForm, MultipartForm,
}, },
Multipart, Multipart,
@ -10,12 +11,14 @@ use actix_web::{middleware, post, web, App, Error, HttpResponse, HttpServer, Res
use futures_util::StreamExt as _; use futures_util::StreamExt as _;
use std::{env, io}; use std::{env, io};
// #[derive(Debug, MultipartForm)] #[derive(Debug, MultipartForm)]
// struct HealthForm { struct HealthForm {
// firstname: String, #[multipart(rename = "file")]
// lastname: String, files: Vec<TempFile>,
// birthdate: String, firstname: Text<String>,
// } lastname: Text<String>,
birthdate: Text<String>,
}
#[actix_web::main] #[actix_web::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
@ -35,37 +38,44 @@ async fn main() -> io::Result<()> {
.await .await
} }
#[post("/health-form")] // #[post("/health-form")]
async fn form(mut form: Multipart) -> Result<HttpResponse, Error> { // async fn form(mut form: Multipart) -> Result<HttpResponse, Error> {
// println!("{form:?}"); // // println!("{form:?}");
while let Some(item) = form.next().await { // while let Some(item) = form.next().await {
let mut field = item?; // let mut field = item?;
// let mut value = ""; // // let mut value = "";
let name = field.name(); // let name = field.name();
let content_type = field.content_type().unwrap().to_string(); // let content_type = field.content_type().unwrap().to_string();
if content_type == "application/octet-stream" { // if content_type == "application/octet-stream" {
// Handle file field // // Handle file field
// You can save the file or process its contents here // // You can save the file or process its contents here
while let Some(chunk) = field.try_next().await? { // while let Some(chunk) = field.next().await {
// Process file chunk // // Process file chunk
} // println!("chunk: {:?}", std::str::from_utf8(&chunk?));
} else { // }
// Handle other field types (e.g., text fields) // } else {
let field_value = field // // Handle other field types (e.g., text fields)
.try_next() // let field_value = field
.await? // .next()
.expect("Field value not found") // .await
.to_utf8() // .expect("Field value not found")
.expect("Failed to decode field value as UTF-8"); // .expect("Field value not found");
// Process the field value (e.g., store in a database, perform validation, etc.) // // Process the field value (e.g., store in a database, perform validation, etc.)
println!("Field {}: {}", name, field_value); // println!("Field {:?}: {:?}", name, field_value);
} // }
// while let Some(chunk) = field.try_next().await { // // while let Some(chunk) = field.try_next().await {
// let value = std::str::from_utf8(&chunk?).unwrap(); // // let value = std::str::from_utf8(&chunk?).unwrap();
// println!("{:?}: {:?}", name, value); // // println!("{:?}: {:?}", name, value);
// } // // }
} // }
// Ok(HttpResponse::Ok().into())
// }
#[post("/health-form")]
async fn form(form: MultipartForm<HealthForm>) -> Result<HttpResponse, Error> {
println!("{:?}", form.firstname);
println!("{:?}", form.lastname);
Ok(HttpResponse::Ok().into()) Ok(HttpResponse::Ok().into())
} }