74 lines
1.8 KiB
Rust
Executable File
74 lines
1.8 KiB
Rust
Executable File
use axum::{
|
|
Router,
|
|
routing::{get, post},
|
|
};
|
|
use instance::Instance;
|
|
use middleware::auth;
|
|
use routes::{
|
|
comment::video_comments,
|
|
video::{get_video, list_videos, upload_video},
|
|
};
|
|
use tokio::signal;
|
|
use tracing::info;
|
|
|
|
mod comment;
|
|
mod instance;
|
|
mod middleware;
|
|
mod routes;
|
|
mod video;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
tracing_subscriber::fmt::init();
|
|
dotenvy::dotenv()?;
|
|
|
|
let instance = Instance::new().await?;
|
|
|
|
info!(
|
|
"Instance configuration:\n+ Host: {}\n+ Port: {}\n+ Videos per page: {}",
|
|
instance.config.host, instance.config.port, instance.config.videos_per_page
|
|
);
|
|
|
|
let address = format!("{}:{}", instance.config.host, instance.config.port);
|
|
|
|
let almond = Router::new()
|
|
.route("/upload", post(upload_video))
|
|
.route_layer(axum::middleware::from_fn_with_state(instance.clone(), auth))
|
|
.route("/", get(list_videos))
|
|
.route("/video/{id}", get(get_video))
|
|
.route("/comments/{id}", get(video_comments))
|
|
.with_state(instance);
|
|
|
|
let listener = tokio::net::TcpListener::bind(address).await?;
|
|
|
|
axum::serve(listener, almond.into_make_service())
|
|
.with_graceful_shutdown(shutdown_signal())
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn shutdown_signal() {
|
|
let ctrl_c = async {
|
|
signal::ctrl_c()
|
|
.await
|
|
.expect("Failed to install Ctrl+C handler");
|
|
};
|
|
|
|
#[cfg(unix)]
|
|
let terminate = async {
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
.expect("failed to install signal handler")
|
|
.recv()
|
|
.await;
|
|
};
|
|
|
|
#[cfg(not(unix))]
|
|
let terminate = std::future::pending::<()>();
|
|
|
|
tokio::select! {
|
|
() = ctrl_c => {},
|
|
() = terminate => {},
|
|
}
|
|
}
|