feat(server): add unix socket support

main
Jef Roosens 2025-04-16 10:24:56 +02:00
parent dca4d2d1ec
commit be04d0c7fe
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
3 changed files with 25 additions and 0 deletions

View File

@ -51,6 +51,7 @@ pub struct ClapConfig {
#[serde(rename_all = "lowercase")]
pub enum NetConfigType {
Tcp,
Unix,
}
#[derive(Serialize, Clone, Args, Debug)]
@ -66,6 +67,11 @@ pub struct NetConfig {
#[serde(skip_serializing_if = "Option::is_none")]
#[arg(long, value_name = "PORT", global = true)]
port: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "path")]
#[arg(long, value_name = "SOCKET", global = true)]
socket: Option<PathBuf>,
}
#[derive(Subcommand)]

View File

@ -65,6 +65,24 @@ pub fn serve(config: &crate::config::Config) -> u8 {
.unwrap()
});
}
NetConfig::Unix { path } => {
// Try to remove the socket file first if it exists
let _ = std::fs::remove_file(&path);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
tracing::info!("Listening on Unix socket {:?}", path);
rt.block_on(async {
let listener = tokio::net::UnixListener::bind(path).unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap()
});
}
}
0

View File

@ -28,6 +28,7 @@ impl From<LogLevel> for tracing::Level {
#[serde(tag = "type")]
pub enum NetConfig {
Tcp { domain: String, port: u16 },
Unix { path: PathBuf },
}
#[derive(Serialize, Deserialize)]