build unix socket support on unix platforms only
yes windows technically supports unix sockets, but its not as good or the same as actual nix platform unix sockets Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
39aef8d1b9
commit
d84378db18
2 changed files with 41 additions and 31 deletions
11
Cargo.toml
11
Cargo.toml
|
@ -92,12 +92,6 @@ features = [
|
||||||
"util",
|
"util",
|
||||||
]
|
]
|
||||||
|
|
||||||
# unix socket support
|
|
||||||
[dependencies.hyperlocal]
|
|
||||||
git = "https://github.com/softprops/hyperlocal"
|
|
||||||
rev = "2ee4d149644600d326559af0d2b235c945b05c04"
|
|
||||||
features = ["server"]
|
|
||||||
|
|
||||||
[dependencies.hyper]
|
[dependencies.hyper]
|
||||||
version = "0.14"
|
version = "0.14"
|
||||||
features = [
|
features = [
|
||||||
|
@ -306,6 +300,11 @@ tokio = { version = "1.36.0", features = [
|
||||||
"sync",
|
"sync",
|
||||||
"signal",
|
"signal",
|
||||||
] }
|
] }
|
||||||
|
# unix socket support
|
||||||
|
[dependencies.hyperlocal]
|
||||||
|
git = "https://github.com/softprops/hyperlocal"
|
||||||
|
rev = "2ee4d149644600d326559af0d2b235c945b05c04"
|
||||||
|
features = ["server"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
61
src/main.rs
61
src/main.rs
|
@ -35,9 +35,9 @@ use ruma::api::{
|
||||||
};
|
};
|
||||||
#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))]
|
#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))]
|
||||||
use tikv_jemallocator::Jemalloc;
|
use tikv_jemallocator::Jemalloc;
|
||||||
|
#[cfg(unix)]
|
||||||
|
use tokio::signal;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
net::UnixListener,
|
|
||||||
signal,
|
|
||||||
sync::{oneshot, oneshot::Sender},
|
sync::{oneshot, oneshot::Sender},
|
||||||
task::JoinSet,
|
task::JoinSet,
|
||||||
};
|
};
|
||||||
|
@ -163,7 +163,15 @@ async fn main() {
|
||||||
|
|
||||||
/* ad-hoc config validation/checks */
|
/* ad-hoc config validation/checks */
|
||||||
|
|
||||||
if config.address.is_loopback() {
|
if config.unix_socket_path.is_some() && !cfg!(unix) {
|
||||||
|
error!(
|
||||||
|
"UNIX socket support is only available on *nix platforms. Please remove \"unix_socket_path\" from your \
|
||||||
|
config."
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.address.is_loopback() && cfg!(unix) {
|
||||||
debug!(
|
debug!(
|
||||||
"Found loopback listening address {}, running checks if we're in a container.",
|
"Found loopback listening address {}, running checks if we're in a container.",
|
||||||
config.address
|
config.address
|
||||||
|
@ -389,34 +397,37 @@ async fn run_server() -> io::Result<()> {
|
||||||
tokio::spawn(shutdown_signal(handle.clone(), tx));
|
tokio::spawn(shutdown_signal(handle.clone(), tx));
|
||||||
|
|
||||||
if let Some(path) = &config.unix_socket_path {
|
if let Some(path) = &config.unix_socket_path {
|
||||||
if path.exists() {
|
#[cfg(unix)]
|
||||||
warn!(
|
{
|
||||||
"UNIX socket path {:#?} already exists (unclean shutdown?), attempting to remove it.",
|
if path.exists() {
|
||||||
path.display()
|
warn!(
|
||||||
);
|
"UNIX socket path {:#?} already exists (unclean shutdown?), attempting to remove it.",
|
||||||
tokio::fs::remove_file(&path).await?;
|
path.display()
|
||||||
}
|
);
|
||||||
|
tokio::fs::remove_file(&path).await?;
|
||||||
|
}
|
||||||
|
|
||||||
tokio::fs::create_dir_all(path.parent().unwrap()).await?;
|
tokio::fs::create_dir_all(path.parent().unwrap()).await?;
|
||||||
|
|
||||||
let socket_perms = config.unix_socket_perms.to_string();
|
let socket_perms = config.unix_socket_perms.to_string();
|
||||||
let octal_perms = u32::from_str_radix(&socket_perms, 8).unwrap();
|
let octal_perms = u32::from_str_radix(&socket_perms, 8).unwrap();
|
||||||
|
|
||||||
let listener = UnixListener::bind(path.clone())?;
|
let listener = tokio::net::UnixListener::bind(path.clone())?;
|
||||||
tokio::fs::set_permissions(path, Permissions::from_mode(octal_perms)).await.unwrap();
|
tokio::fs::set_permissions(path, Permissions::from_mode(octal_perms)).await.unwrap();
|
||||||
let socket = SocketIncoming::from_listener(listener);
|
let socket = SocketIncoming::from_listener(listener);
|
||||||
|
|
||||||
#[cfg(feature = "systemd")]
|
#[cfg(feature = "systemd")]
|
||||||
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
|
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
|
||||||
|
|
||||||
info!("Listening at {:?}", path);
|
info!("Listening at {:?}", path);
|
||||||
let server = Server::builder(socket).serve(app);
|
let server = Server::builder(socket).serve(app);
|
||||||
let graceful = server.with_graceful_shutdown(async {
|
let graceful = server.with_graceful_shutdown(async {
|
||||||
rx.await.ok();
|
rx.await.ok();
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Err(e) = graceful.await {
|
if let Err(e) = graceful.await {
|
||||||
error!("Server error: {:?}", e);
|
error!("Server error: {:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match &config.tls {
|
match &config.tls {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue