75 lines
1.8 KiB
Rust
75 lines
1.8 KiB
Rust
use axum::{
|
|
extract::{ConnectInfo, Request},
|
|
middleware::{self, Next},
|
|
response::IntoResponse,
|
|
routing::get,
|
|
Router,
|
|
};
|
|
use axum_extra::TypedHeader;
|
|
use tower_http::{services::ServeDir, trace::TraceLayer};
|
|
use views::askama::WelcomeTemplate;
|
|
|
|
use std::{io, net::SocketAddr};
|
|
|
|
use nyazoom::*;
|
|
|
|
use util::{headers::ForwardedFor, logging, sweeper};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> io::Result<()> {
|
|
logging::init_tracing();
|
|
|
|
// uses create_dir_all to create both .cache and serve inside it in one go
|
|
util::make_dir(".cache/serve").await?;
|
|
|
|
let state = cache::fetch_cache().await;
|
|
|
|
sweeper::spawn(state.clone());
|
|
|
|
// Router Setup
|
|
let app = Router::new()
|
|
.route("/", get(welcome))
|
|
.nest("/download", get_download_router())
|
|
.nest("/upload", get_upload_router())
|
|
.nest("/records", get_records_router())
|
|
.nest("/link", get_link_router())
|
|
.with_state(state)
|
|
.fallback_service(ServeDir::new("dist"))
|
|
.layer(TraceLayer::new_for_http())
|
|
.layer(middleware::from_fn(log_source));
|
|
|
|
serve(app).await;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn welcome() -> impl IntoResponse {
|
|
let fact = views::get_cat_fact().await;
|
|
|
|
WelcomeTemplate { fact }
|
|
}
|
|
|
|
async fn log_source(
|
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
|
forwarded_for: Option<TypedHeader<ForwardedFor>>,
|
|
req: Request,
|
|
next: Next,
|
|
) -> impl IntoResponse {
|
|
tracing::info!("{} : {:?}", addr, forwarded_for);
|
|
|
|
next.run(req).await
|
|
}
|
|
|
|
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);
|
|
}
|