share/src/main.rs

50 lines
1.4 KiB
Rust
Raw Normal View History

2022-10-20 23:20:04 +02:00
use axum::{
extract::Multipart,
routing::{get, post},
Router,
};
use std::net::SocketAddr;
use tokio::fs::File;
use tokio::io::AsyncWriteExt; // for write_all()
#[tokio::main]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
// build our application with a route
let app = Router::new()
// `GET /` goes to `root`
.route("/", get(root))
.route("/u", post(upload));
// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.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();
2022-10-20 23:46:55 +02:00
let filename = field.file_name().unwrap().to_string();
2022-10-20 23:20:04 +02:00
let data = field.bytes().await.unwrap();
println!("Length of `{}` is {} bytes", name, data.len());
// tracing::debug!("Length of `{}` is {} bytes", name, data.len());
2022-10-20 23:46:55 +02:00
let mut file = File::create(filename).await.expect("error creando arquivo");
2022-10-20 23:20:04 +02:00
file.write_all(&data).await.expect("error gardando contido");
}
}