Skip to content

🧱 add support for unix sockets #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ unstable = []

[dependencies]
async-sse = "2.1.0"
http-types = "1.0.1"
http-types = "1.2.0"
http-service = "0.5.0"
http-service-h1 = { version = "0.1.0", optional = true }
route-recognizer = "0.1.13"
serde = "1.0.102"
serde_json = "1.0.41"
serde_qs = "0.5.0"
async-std = { version = "1.4.0", features = ["unstable"] }
async-std = { version = "1.5.0", features = ["unstable"] }
mime = "0.3.14"
cookie = { version="0.12.0", features = ["percent-encode"]}
futures-core = "0.3.1"
Expand All @@ -48,7 +48,7 @@ url = "2.1.1"
kv-log-macro = "1.0.4"

[dev-dependencies]
async-std = { version = "1.4.0", features = ["unstable", "attributes"] }
async-std = { version = "1.5.0", features = ["unstable", "attributes"] }
basic-cookies = "0.1.3"
bytes = "0.4.12"
futures-fs = "0.0.5"
Expand Down
15 changes: 15 additions & 0 deletions examples/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use async_std::task;

fn main() -> Result<(), std::io::Error> {
task::block_on(async {
let mut app = tide::new();
app.at("/").get(|req: tide::Request<()>| async move {
Ok(format!(
"Hello, world! This request was made to: {}",
req.uri()
))
});
app.listen_unix("./unix.socket").await?;
Ok(())
})
}
21 changes: 20 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,26 @@ impl<State: Send + Sync + 'static> Server<State> {

let addr = format!("http://{}", listener.local_addr()?);
log::info!("Server is listening on: {}", addr);
let mut server = http_service_h1::Server::new(addr, listener.incoming(), self);
let mut server = http_service_h1::Server::new(listener.incoming(), self);

server.run().await
}

#[cfg(all(feature = "h1-server", unix))]
pub async fn listen_unix(self, addr: impl AsRef<async_std::path::Path>) -> io::Result<()> {
let listener = async_std::os::unix::net::UnixListener::bind(addr).await?;

let addr = format!(
"unix://{}",
listener.local_addr()?.as_pathname().unwrap().display()
);

log::info!("Server is listening on: {}", addr);

let mut server = http_service_h1::Server::new(
http_service_h1::UnixStreamWrapper::stream_from_incoming(listener.incoming()),
self,
);

server.run().await
}
Expand Down