diff options
| author | Christine Dodrill <me@christine.website> | 2021-04-01 22:30:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-01 22:30:45 -0400 |
| commit | 8383914aa5abc232fa56ce437e777d5024eb63e1 (patch) | |
| tree | bedceacc5a8e1260e73d251a84d44838a3e25acb /src/main.rs | |
| parent | 0360c87582e2e5025c06fec2431b867992dc71b0 (diff) | |
| download | xesite-8383914aa5abc232fa56ce437e777d5024eb63e1.tar.xz xesite-8383914aa5abc232fa56ce437e777d5024eb63e1.zip | |
Unix domain socket http server (#352)
* enable ipv6 support
Signed-off-by: Christine Dodrill <me@christine.website>
* enable unix socket powers
Signed-off-by: Christine Dodrill <me@christine.website>
* unix domain socket post
Signed-off-by: Christine Dodrill <me@christine.website>
* bump rust
Signed-off-by: Christine Dodrill <me@christine.website>
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 7ebfbdf..cac19cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,11 @@ extern crate tracing; use color_eyre::eyre::Result; use hyper::{header::CONTENT_TYPE, Body, Response}; use prometheus::{Encoder, TextEncoder}; +use std::net::IpAddr; +use std::str::FromStr; use std::sync::Arc; +use tokio::net::UnixListener; +use tokio_stream::wrappers::UnixListenerStream; use warp::{path, Filter}; pub mod app; @@ -232,17 +236,30 @@ async fn main() -> Result<()> { } } - warp::serve(site) - .run(( - [0, 0, 0, 0], - std::env::var("PORT") - .unwrap_or("3030".into()) - .parse::<u16>() - .unwrap(), - )) - .await; - - Ok(()) + let server = warp::serve(site); + + match std::env::var("SOCKPATH") { + Ok(sockpath) => { + let _ = std::fs::remove_file(&sockpath); + let listener = UnixListener::bind(sockpath)?; + let incoming = UnixListenerStream::new(listener); + server.run_incoming(incoming).await; + + Ok(()) + } + Err(_) => { + server + .run(( + IpAddr::from_str(&std::env::var("HOST").unwrap_or("::".into()))?, + std::env::var("PORT") + .unwrap_or("3030".into()) + .parse::<u16>()?, + )) + .await; + + Ok(()) + } + } } include!(concat!(env!("OUT_DIR"), "/templates.rs")); |
