vistas separadas

This commit is contained in:
serxoz 2022-10-21 11:07:34 +02:00
parent 283d1d6134
commit 5c2ba7536f
4 changed files with 32 additions and 24 deletions

View File

@ -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");
}
}

2
src/vistas/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod root;
pub mod upload;

4
src/vistas/root.rs Normal file
View File

@ -0,0 +1,4 @@
// basic handler that responds with a static string
pub async fn basic() -> &'static str {
"Jao!"
}

18
src/vistas/upload.rs Normal file
View File

@ -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");
}
}