resolve host from args, better error handling
This commit is contained in:
parent
cc32fb9a2d
commit
bc1a386b80
29
src/main.rs
29
src/main.rs
|
@ -2,7 +2,11 @@ use anyhow::Context;
|
||||||
use axum::{Router, Server};
|
use axum::{Router, Server};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use mpvipc::Mpv;
|
use mpvipc::Mpv;
|
||||||
use std::{fs::create_dir_all, net::SocketAddr, path::Path};
|
use std::{
|
||||||
|
fs::create_dir_all,
|
||||||
|
net::{IpAddr, SocketAddr},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
use tokio::process::{Child, Command};
|
use tokio::process::{Child, Command};
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
@ -22,7 +26,7 @@ struct Args {
|
||||||
#[clap(long, value_name = "PATH")]
|
#[clap(long, value_name = "PATH")]
|
||||||
mpv_executable_path: Option<String>,
|
mpv_executable_path: Option<String>,
|
||||||
|
|
||||||
#[clap(short, long, default_value = "true")]
|
#[clap(long, default_value = "true")]
|
||||||
auto_start_mpv: bool,
|
auto_start_mpv: bool,
|
||||||
|
|
||||||
#[clap(long, default_value = "true")]
|
#[clap(long, default_value = "true")]
|
||||||
|
@ -113,6 +117,16 @@ async fn connect_to_mpv(args: &MpvConnectionArgs) -> anyhow::Result<(Mpv, Option
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn resolve(host: &str) -> anyhow::Result<IpAddr> {
|
||||||
|
let addr = format!("{}:0", host);
|
||||||
|
let addresses = tokio::net::lookup_host(addr).await?;
|
||||||
|
addresses
|
||||||
|
.filter(|addr| addr.is_ipv4())
|
||||||
|
.next()
|
||||||
|
.map(|addr| addr.ip())
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Failed to resolve address"))
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
@ -126,8 +140,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// TODO: fix address
|
let addr = SocketAddr::new(resolve(&args.host).await?, args.port);
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], args.port));
|
|
||||||
log::info!("Starting API on {}", addr);
|
log::info!("Starting API on {}", addr);
|
||||||
|
|
||||||
let app = Router::new().nest("/api", api::api_routes(mpv));
|
let app = Router::new().nest("/api", api::api_routes(mpv));
|
||||||
|
@ -141,9 +154,15 @@ async fn main() -> anyhow::Result<()> {
|
||||||
log::info!("Received Ctrl-C, exiting");
|
log::info!("Received Ctrl-C, exiting");
|
||||||
proc.kill().await?;
|
proc.kill().await?;
|
||||||
}
|
}
|
||||||
_ = Server::bind(&addr.clone()).serve(app.into_make_service()) => {
|
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");
|
log::info!("API server exited");
|
||||||
proc.kill().await?;
|
proc.kill().await?;
|
||||||
|
result?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue