From 5c2ba7536fdf3dc79ee3c8cdd0330fdafe3ac433 Mon Sep 17 00:00:00 2001 From: serxoz Date: Fri, 21 Oct 2022 11:07:34 +0200 Subject: [PATCH] vistas separadas --- src/main.rs | 32 ++++++++------------------------ src/vistas/mod.rs | 2 ++ src/vistas/root.rs | 4 ++++ src/vistas/upload.rs | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 src/vistas/mod.rs create mode 100644 src/vistas/root.rs create mode 100644 src/vistas/upload.rs diff --git a/src/main.rs b/src/main.rs index a95f440..e3ae0ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,15 @@ +pub mod vistas; + use axum::{ - extract::Multipart, routing::{get, post}, Router, }; use std::net::SocketAddr; -use tokio::fs::File; -use tokio::io::AsyncWriteExt; // for write_all() + +use crate::vistas::{ + root::basic, + upload::upload, +}; #[tokio::main] async fn main() { @@ -15,7 +19,7 @@ async fn main() { // build our application with a route let app = Router::new() // `GET /` goes to `root` - .route("/", get(root)) + .route("/", get(basic)) .route("/u", post(upload)); // run our app with hyper @@ -27,23 +31,3 @@ async fn main() { .await .unwrap(); } - -// basic handler that responds with a static string -async fn root() -> &'static str { - "Jao!" -} - -// upload view -async fn upload(mut multipart: Multipart) { - while let Some(field) = multipart.next_field().await.unwrap() { - let name = field.name().unwrap().to_string(); - let filename = field.file_name().unwrap().to_string(); - let data = field.bytes().await.unwrap(); - - println!("Length of `{}` is {} bytes", name, data.len()); - // tracing::debug!("Length of `{}` is {} bytes", name, data.len()); - - let mut file = File::create(filename).await.expect("error creando arquivo"); - file.write_all(&data).await.expect("error gardando contido"); - } -} diff --git a/src/vistas/mod.rs b/src/vistas/mod.rs new file mode 100644 index 0000000..addc5a5 --- /dev/null +++ b/src/vistas/mod.rs @@ -0,0 +1,2 @@ +pub mod root; +pub mod upload; diff --git a/src/vistas/root.rs b/src/vistas/root.rs new file mode 100644 index 0000000..663a803 --- /dev/null +++ b/src/vistas/root.rs @@ -0,0 +1,4 @@ +// basic handler that responds with a static string +pub async fn basic() -> &'static str { + "Jao!" +} diff --git a/src/vistas/upload.rs b/src/vistas/upload.rs new file mode 100644 index 0000000..69bb3cb --- /dev/null +++ b/src/vistas/upload.rs @@ -0,0 +1,18 @@ +use axum::extract::Multipart; +use tokio::fs::File; +use tokio::io::AsyncWriteExt; // for write_all() + +// upload view +pub async fn upload(mut multipart: Multipart) { + while let Some(field) = multipart.next_field().await.unwrap() { + let name = field.name().unwrap().to_string(); + let filename = field.file_name().unwrap().to_string(); + let data = field.bytes().await.unwrap(); + + println!("Length of `{}` is {} bytes", name, data.len()); + // tracing::debug!("Length of `{}` is {} bytes", name, data.len()); + + let mut file = File::create(filename).await.expect("error creando arquivo"); + file.write_all(&data).await.expect("error gardando contido"); + } +}