2024-04-14 00:55:00 +02:00
|
|
|
use anyhow::Context;
|
|
|
|
use axum::{Router, Server};
|
|
|
|
use clap::Parser;
|
2024-10-20 22:05:56 +02:00
|
|
|
use clap_verbosity_flag::Verbosity;
|
2024-10-20 00:05:30 +02:00
|
|
|
use mpv_setup::{connect_to_mpv, create_mpv_config_file, show_grzegorz_image};
|
2024-10-20 22:05:56 +02:00
|
|
|
use mpvipc_async::Mpv;
|
2024-10-20 00:04:59 +02:00
|
|
|
use std::net::{IpAddr, SocketAddr};
|
2024-10-20 22:05:56 +02:00
|
|
|
use systemd_journal_logger::JournalLog;
|
2024-08-04 18:58:17 +02:00
|
|
|
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,
|
|
|
|
|
2024-10-20 22:05:56 +02:00
|
|
|
#[command(flatten)]
|
|
|
|
verbose: Verbosity,
|
|
|
|
|
|
|
|
#[clap(long)]
|
|
|
|
systemd: bool,
|
|
|
|
|
2024-04-14 00:55:00 +02:00
|
|
|
#[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>,
|
|
|
|
|
2024-08-04 18:58:17 +02:00
|
|
|
#[clap(long, value_name = "PATH")]
|
|
|
|
mpv_config_file: Option<String>,
|
|
|
|
|
2024-04-14 02:08:06 +02:00
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
2024-08-04 18:58:17 +02:00
|
|
|
struct MpvConnectionArgs<'a> {
|
2024-04-14 00:55:00 +02:00
|
|
|
socket_path: String,
|
|
|
|
executable_path: Option<String>,
|
2024-08-04 18:58:17 +02:00
|
|
|
config_file: &'a NamedTempFile,
|
2024-04-14 00:55:00 +02:00
|
|
|
auto_start: bool,
|
|
|
|
force_auto_start: bool,
|
|
|
|
}
|
|
|
|
|
2024-10-20 22:05:56 +02:00
|
|
|
/// Helper function to resolve a hostname to an IP address.
|
|
|
|
/// Why is this not in the standard library? >:(
|
2024-04-14 02:08:06 +02:00
|
|
|
async fn resolve(host: &str) -> anyhow::Result<IpAddr> {
|
|
|
|
let addr = format!("{}:0", host);
|
|
|
|
let addresses = tokio::net::lookup_host(addr).await?;
|
|
|
|
addresses
|
2024-04-15 23:24:47 +02:00
|
|
|
.into_iter()
|
|
|
|
.find(|addr| addr.is_ipv4())
|
2024-04-14 02:08:06 +02:00
|
|
|
.map(|addr| addr.ip())
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("Failed to resolve address"))
|
|
|
|
}
|
|
|
|
|
2024-10-20 22:05:56 +02:00
|
|
|
/// Helper function that spawns a tokio thread that
|
|
|
|
/// continuously sends a ping to systemd watchdog, if enabled.
|
|
|
|
async fn setup_systemd_watchdog_thread() -> anyhow::Result<()> {
|
|
|
|
let mut watchdog_microsecs: u64 = 0;
|
|
|
|
if sd_notify::watchdog_enabled(true, &mut watchdog_microsecs) {
|
|
|
|
watchdog_microsecs = watchdog_microsecs.div_ceil(2);
|
|
|
|
tokio::spawn(async move {
|
|
|
|
log::debug!(
|
|
|
|
"Starting systemd watchdog thread with {} millisecond interval",
|
|
|
|
watchdog_microsecs.div_ceil(1000)
|
|
|
|
);
|
|
|
|
loop {
|
|
|
|
tokio::time::sleep(tokio::time::Duration::from_micros(watchdog_microsecs)).await;
|
|
|
|
if let Err(err) = sd_notify::notify(false, &[sd_notify::NotifyState::Watchdog]) {
|
|
|
|
log::warn!("Failed to notify systemd watchdog: {}", err);
|
|
|
|
} else {
|
|
|
|
log::trace!("Ping sent to systemd watchdog");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log::info!("Watchdog not enabled, skipping");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn shutdown(mpv: Mpv, proc: Option<tokio::process::Child>) {
|
|
|
|
log::info!("Shutting down");
|
|
|
|
sd_notify::notify(false, &[sd_notify::NotifyState::Stopping])
|
|
|
|
.unwrap_or_else(|e| log::warn!("Failed to notify systemd that the service is stopping: {}", e));
|
|
|
|
|
|
|
|
mpv.disconnect()
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|e| log::warn!("Failed to disconnect from mpv: {}", e));
|
|
|
|
if let Some(mut proc) = proc {
|
|
|
|
proc.kill()
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|e| log::warn!("Failed to kill mpv process: {}", e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-14 00:55:00 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
let args = Args::parse();
|
|
|
|
|
2024-10-20 22:05:56 +02:00
|
|
|
let systemd_mode = args.systemd && sd_notify::booted().unwrap_or(false);
|
|
|
|
if systemd_mode {
|
|
|
|
JournalLog::new()
|
|
|
|
.context("Failed to initialize journald logging")?
|
|
|
|
.install()
|
|
|
|
.context("Failed to install journald logger")?;
|
|
|
|
|
|
|
|
log::set_max_level(args.verbose.log_level_filter());
|
|
|
|
|
|
|
|
log::debug!("Running with systemd integration");
|
|
|
|
|
|
|
|
setup_systemd_watchdog_thread().await?;
|
|
|
|
} else {
|
|
|
|
env_logger::Builder::new()
|
|
|
|
.filter_level(args.verbose.log_level_filter())
|
|
|
|
.init();
|
|
|
|
|
|
|
|
log::info!("Running without systemd integration");
|
|
|
|
}
|
|
|
|
|
2024-08-04 18:58:17 +02:00
|
|
|
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,
|
2024-08-04 18:58:17 +02:00
|
|
|
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,
|
|
|
|
})
|
2024-10-20 22:05:56 +02:00
|
|
|
.await
|
|
|
|
.context("Failed to connect to mpv")?;
|
2024-04-14 00:55:00 +02:00
|
|
|
|
2024-10-20 19:45:31 +02:00
|
|
|
if let Err(e) = show_grzegorz_image(mpv.clone()).await {
|
2024-10-20 22:05:56 +02:00
|
|
|
log::warn!("Could not show Grzegorz image: {}", e);
|
2024-10-20 19:45:31 +02:00
|
|
|
}
|
2024-10-20 00:05:30 +02:00
|
|
|
|
2024-10-20 22:05:56 +02:00
|
|
|
let addr = match resolve(&args.host)
|
|
|
|
.await
|
|
|
|
.context(format!("Failed to resolve address: {}", &args.host))
|
|
|
|
{
|
|
|
|
Ok(addr) => addr,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("{}", e);
|
|
|
|
shutdown(mpv, proc).await;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let socket_addr = SocketAddr::new(addr, args.port);
|
|
|
|
log::info!("Starting API on {}", socket_addr);
|
2024-04-14 00:55:00 +02:00
|
|
|
|
2024-04-18 22:02:39 +02:00
|
|
|
let app = Router::new().nest("/api", api::rest_api_routes(mpv.clone()));
|
2024-10-20 22:05:56 +02:00
|
|
|
let server = match Server::try_bind(&socket_addr.clone())
|
|
|
|
.context(format!("Failed to bind API server to '{}'", &socket_addr))
|
|
|
|
{
|
|
|
|
Ok(server) => server,
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("{}", e);
|
|
|
|
shutdown(mpv, proc).await;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if systemd_mode {
|
|
|
|
match sd_notify::notify(false, &[sd_notify::NotifyState::Ready])
|
|
|
|
.context("Failed to notify systemd that the service is ready")
|
|
|
|
{
|
|
|
|
Ok(_) => log::trace!("Notified systemd that the service is ready"),
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("{}", e);
|
|
|
|
shutdown(mpv, proc).await;
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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?);
|
2024-10-20 22:05:56 +02:00
|
|
|
shutdown(mpv, Some(proc)).await;
|
2024-04-18 22:02:39 +02:00
|
|
|
}
|
|
|
|
_ = tokio::signal::ctrl_c() => {
|
|
|
|
log::info!("Received Ctrl-C, exiting");
|
2024-10-20 22:05:56 +02:00
|
|
|
shutdown(mpv, Some(proc)).await;
|
2024-04-18 22:02:39 +02:00
|
|
|
}
|
2024-10-20 22:05:56 +02:00
|
|
|
result = server.serve(app.into_make_service()) => {
|
2024-04-14 02:08:06 +02:00
|
|
|
log::info!("API server exited");
|
2024-10-20 22:05:56 +02:00
|
|
|
shutdown(mpv, Some(proc)).await;
|
2024-04-14 02:08:06 +02:00
|
|
|
result?;
|
2024-04-14 00:55:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tokio::select! {
|
|
|
|
_ = tokio::signal::ctrl_c() => {
|
|
|
|
log::info!("Received Ctrl-C, exiting");
|
2024-10-20 22:05:56 +02:00
|
|
|
shutdown(mpv.clone(), None).await;
|
2024-04-14 00:55:00 +02:00
|
|
|
}
|
2024-10-20 22:05:56 +02:00
|
|
|
result = server.serve(app.into_make_service()) => {
|
|
|
|
log::info!("API server exited");
|
|
|
|
shutdown(mpv.clone(), None).await;
|
|
|
|
result?;
|
2024-04-14 00:55:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 18:58:17 +02:00
|
|
|
std::mem::drop(mpv_config_file);
|
|
|
|
|
2024-04-14 00:55:00 +02:00
|
|
|
Ok(())
|
|
|
|
}
|