2023-04-07 06:59:26 -07:00
|
|
|
use axum::{
|
2024-08-29 23:32:49 -07:00
|
|
|
extract::{ConnectInfo, Request},
|
2023-07-25 08:14:42 -07:00
|
|
|
middleware::{self, Next},
|
2024-11-14 07:33:36 -08:00
|
|
|
response::IntoResponse,
|
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};
|
2024-11-14 08:00:56 -08:00
|
|
|
use views::askama::WelcomeTemplate;
|
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-11-14 01:03:58 -08:00
|
|
|
use nyazoom::*;
|
2023-04-12 04:59:43 -07:00
|
|
|
|
2024-11-14 07:33:36 -08:00
|
|
|
use util::{headers::ForwardedFor, logging, sweeper};
|
2024-08-29 22:48:47 -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))
|
2024-08-29 23:32:49 -07:00
|
|
|
.nest("/download", get_download_router())
|
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 {
|
2024-08-30 23:23:13 -07:00
|
|
|
let fact = views::get_cat_fact().await;
|
2024-11-14 06:24:25 -08:00
|
|
|
|
2024-11-14 08:00:56 -08:00
|
|
|
WelcomeTemplate { fact }
|
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
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|