vistas separadas
This commit is contained in:
parent
283d1d6134
commit
5c2ba7536f
32
src/main.rs
32
src/main.rs
@ -1,11 +1,15 @@
|
|||||||
|
pub mod vistas;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Multipart,
|
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::fs::File;
|
|
||||||
use tokio::io::AsyncWriteExt; // for write_all()
|
use crate::vistas::{
|
||||||
|
root::basic,
|
||||||
|
upload::upload,
|
||||||
|
};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -15,7 +19,7 @@ async fn main() {
|
|||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// `GET /` goes to `root`
|
// `GET /` goes to `root`
|
||||||
.route("/", get(root))
|
.route("/", get(basic))
|
||||||
.route("/u", post(upload));
|
.route("/u", post(upload));
|
||||||
|
|
||||||
// run our app with hyper
|
// run our app with hyper
|
||||||
@ -27,23 +31,3 @@ async fn main() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.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
2
src/vistas/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod root;
|
||||||
|
pub mod upload;
|
4
src/vistas/root.rs
Normal file
4
src/vistas/root.rs
Normal 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
18
src/vistas/upload.rs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user