nyazoom/src/main.rs

131 lines
3.4 KiB
Rust
Raw Normal View History

2024-08-29 23:16:14 -07:00
use tokio_util::io::ReaderStream;
2024-08-29 22:50:21 -07:00
2023-04-07 06:59:26 -07:00
use axum::{
2024-08-28 17:53:13 -07:00
body::Body,
2024-08-29 23:16:14 -07:00
extract::{ConnectInfo, Request, State},
http::{HeaderMap, StatusCode},
2023-07-25 08:14:42 -07:00
middleware::{self, Next},
2023-07-31 06:32:17 -07:00
response::{Html, IntoResponse, Redirect},
2024-08-29 23:16:14 -07:00
routing::get,
2024-08-29 23:22:55 -07:00
Router,
2023-04-07 06:59:26 -07:00
};
2024-08-28 17:53:13 -07:00
use axum_extra::TypedHeader;
2024-08-29 23:16:14 -07:00
use tower_http::{services::ServeDir, trace::TraceLayer};
2023-04-11 03:29:21 -07:00
2024-08-29 23:16:14 -07:00
use std::{io, net::SocketAddr};
2023-04-11 03:29:21 -07:00
2024-08-29 23:16:14 -07:00
mod router {
2024-08-29 23:22:55 -07:00
pub mod link;
pub mod records;
2024-08-29 23:16:14 -07:00
pub mod upload;
}
2023-07-25 08:14:42 -07:00
mod cache;
2023-04-12 05:07:37 -07:00
mod state;
2023-07-25 08:14:42 -07:00
mod util;
2023-07-26 01:53:41 -07:00
mod views;
2023-04-12 04:59:43 -07:00
2024-08-29 22:48:47 -07:00
use util::{headers::ForwardedFor, logging, ssr, sweeper};
2024-08-29 22:50:21 -07:00
use state::*;
use views::*;
2023-07-26 01:53:41 -07:00
2024-08-29 23:22:55 -07:00
use router::link::get_link_router;
use router::records::get_records_router;
use router::upload::get_upload_router;
2024-08-29 23:16:14 -07:00
2023-04-07 06:59:26 -07:00
#[tokio::main]
2023-04-08 08:12:14 -07:00
async fn main() -> io::Result<()> {
2024-08-28 19:12:55 -07:00
logging::init_tracing();
2023-04-07 06:59:26 -07:00
2023-04-12 04:59:43 -07:00
// uses create_dir_all to create both .cache and serve inside it in one go
2023-07-25 08:14:42 -07:00
util::make_dir(".cache/serve").await?;
2023-04-08 08:12:14 -07:00
2023-07-25 08:14:42 -07:00
let state = cache::fetch_cache().await;
2023-04-12 04:59:43 -07:00
2024-08-28 19:19:15 -07:00
sweeper::spawn(state.clone());
2023-07-27 08:58:13 -07:00
2023-04-08 08:12:14 -07:00
// Router Setup
2023-04-12 06:40:55 -07:00
let app = Router::new()
2023-07-26 01:53:41 -07:00
.route("/", get(welcome))
2023-04-12 06:40:55 -07:00
.route("/download/:id", get(download))
2024-08-29 23:16:14 -07:00
.nest("/upload", get_upload_router())
2024-08-29 23:22:55 -07:00
.nest("/records", get_records_router())
.nest("/link", get_link_router())
2023-04-12 06:40:55 -07:00
.with_state(state)
2023-07-27 08:40:01 -07:00
.fallback_service(ServeDir::new("dist"))
2023-04-13 11:49:13 -07:00
.layer(TraceLayer::new_for_http())
.layer(middleware::from_fn(log_source));
2023-04-07 06:59:26 -07:00
2024-08-28 19:10:05 -07:00
serve(app).await;
2024-08-28 19:03:47 -07:00
2023-04-08 08:12:14 -07:00
Ok(())
2023-04-07 06:59:26 -07:00
}
2023-07-26 01:53:41 -07:00
async fn welcome() -> impl IntoResponse {
let cat_fact = views::get_cat_fact().await;
2024-08-28 18:45:12 -07:00
Html(ssr::render(move || {
2024-08-29 13:58:39 -07:00
leptos::view! { <WelcomePage fact=cat_fact /> }
2024-08-28 18:45:12 -07:00
}))
2023-07-26 01:53:41 -07:00
}
2024-08-28 17:53:13 -07:00
async fn log_source(
2023-04-15 06:36:04 -07:00
ConnectInfo(addr): ConnectInfo<SocketAddr>,
2023-07-25 08:14:42 -07:00
forwarded_for: Option<TypedHeader<ForwardedFor>>,
2024-08-28 17:53:13 -07:00
req: Request,
next: Next,
2023-07-31 06:32:17 -07:00
) -> impl IntoResponse {
2023-07-25 08:14:42 -07:00
tracing::info!("{} : {:?}", addr, forwarded_for);
2023-04-13 11:49:13 -07:00
next.run(req).await
}
2023-04-12 06:40:55 -07:00
async fn download(
axum::extract::Path(id): axum::extract::Path<String>,
2023-07-31 09:16:18 -07:00
headers: HeaderMap,
2023-08-02 01:39:50 -07:00
State(state): State<AppState>,
2023-04-12 06:40:55 -07:00
) -> Result<axum::response::Response, (StatusCode, String)> {
2023-07-31 09:16:18 -07:00
{
let mut records = state.records.lock().await;
if headers.get("hx-request").is_some() {
2023-04-12 06:40:55 -07:00
return Ok(axum::http::Response::builder()
2023-07-31 09:16:18 -07:00
.header("HX-Redirect", format!("/download/{id}"))
.status(204)
.body("".to_owned())
2023-04-12 06:40:55 -07:00
.unwrap()
.into_response());
2023-07-31 09:16:18 -07:00
}
2023-08-02 01:39:50 -07:00
if let Some(record) = records
.get_mut(&id)
.filter(|record| record.can_be_downloaded())
{
record.downloads += 1;
2023-07-31 09:16:18 -07:00
2023-08-02 01:39:50 -07:00
let file = tokio::fs::File::open(&record.file).await.unwrap();
2023-07-31 09:16:18 -07:00
2023-08-02 01:39:50 -07:00
return Ok(axum::response::Response::builder()
.header("Content-Type", "application/zip")
2024-08-28 17:53:13 -07:00
.body(Body::from_stream(ReaderStream::new(file)))
.unwrap());
2023-08-02 01:39:50 -07:00
} else {
records.remove_record(&id).await.unwrap()
2023-04-12 06:40:55 -07:00
}
}
Ok(Redirect::to("/404.html").into_response())
2023-04-08 08:12:14 -07:00
}
2024-08-28 19:10:05 -07:00
async fn serve(app: Router) {
// // Server creation
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap();
tracing::debug!("listening on http://{}/", addr);
}