greg-ng/src/main.rs

116 lines
3.3 KiB
Rust
Raw Normal View History

2024-04-14 00:55:00 +02:00
use anyhow::Context;
use axum::{Router, Server};
use clap::Parser;
2024-10-20 00:05:30 +02:00
use mpv_setup::{connect_to_mpv, create_mpv_config_file, show_grzegorz_image};
2024-10-20 00:04:59 +02:00
use std::net::{IpAddr, SocketAddr};
use tempfile::NamedTempFile;
2024-04-14 00:55:00 +02:00
mod api;
2024-10-20 00:04:59 +02:00
mod mpv_setup;
2024-04-14 00:55:00 +02:00
#[derive(Parser)]
struct Args {
#[clap(long, default_value = "localhost")]
host: String,
#[clap(short, long, default_value = "8008")]
port: u16,
#[clap(long, value_name = "PATH", default_value = "/run/mpv/mpv.sock")]
mpv_socket_path: String,
#[clap(long, value_name = "PATH")]
mpv_executable_path: Option<String>,
#[clap(long, value_name = "PATH")]
mpv_config_file: Option<String>,
#[clap(long, default_value = "true")]
2024-04-14 00:55:00 +02:00
auto_start_mpv: bool,
#[clap(long, default_value = "true")]
force_auto_start: bool,
}
struct MpvConnectionArgs<'a> {
2024-04-14 00:55:00 +02:00
socket_path: String,
executable_path: Option<String>,
config_file: &'a NamedTempFile,
2024-04-14 00:55:00 +02:00
auto_start: bool,
force_auto_start: bool,
}
async fn resolve(host: &str) -> anyhow::Result<IpAddr> {
let addr = format!("{}:0", host);
let addresses = tokio::net::lookup_host(addr).await?;
addresses
.into_iter()
.find(|addr| addr.is_ipv4())
.map(|addr| addr.ip())
.ok_or_else(|| anyhow::anyhow!("Failed to resolve address"))
}
2024-04-14 00:55:00 +02:00
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
let args = Args::parse();
let mpv_config_file = create_mpv_config_file(args.mpv_config_file)?;
2024-04-14 00:55:00 +02:00
let (mpv, proc) = connect_to_mpv(&MpvConnectionArgs {
socket_path: args.mpv_socket_path,
executable_path: args.mpv_executable_path,
config_file: &mpv_config_file,
2024-04-14 00:55:00 +02:00
auto_start: args.auto_start_mpv,
force_auto_start: args.force_auto_start,
})
.await?;
2024-10-20 00:05:30 +02:00
show_grzegorz_image(mpv.clone()).await?;
let addr = SocketAddr::new(resolve(&args.host).await?, args.port);
2024-04-14 00:55:00 +02:00
log::info!("Starting API on {}", addr);
2024-04-18 22:02:39 +02:00
let app = Router::new().nest("/api", api::rest_api_routes(mpv.clone()));
2024-04-14 00:55:00 +02:00
if let Some(mut proc) = proc {
tokio::select! {
2024-04-18 22:02:39 +02:00
exit_status = proc.wait() => {
log::warn!("mpv process exited with status: {}", exit_status?);
mpv.disconnect().await?;
}
_ = tokio::signal::ctrl_c() => {
log::info!("Received Ctrl-C, exiting");
mpv.disconnect().await?;
proc.kill().await?;
}
result = async {
match Server::try_bind(&addr.clone()).context("Failed to bind server") {
Ok(server) => server.serve(app.into_make_service()).await.context("Failed to serve app"),
Err(err) => Err(err),
}
} => {
log::info!("API server exited");
2024-04-18 22:02:39 +02:00
mpv.disconnect().await?;
proc.kill().await?;
result?;
2024-04-14 00:55:00 +02:00
}
}
} else {
tokio::select! {
_ = tokio::signal::ctrl_c() => {
log::info!("Received Ctrl-C, exiting");
2024-04-18 22:02:39 +02:00
mpv.disconnect().await?;
2024-04-14 00:55:00 +02:00
}
_ = Server::bind(&addr.clone()).serve(app.into_make_service()) => {
log::info!("API server exited");
2024-04-18 22:02:39 +02:00
mpv.disconnect().await?;
2024-04-14 00:55:00 +02:00
}
}
}
std::mem::drop(mpv_config_file);
2024-04-14 00:55:00 +02:00
Ok(())
}