forked from softprops/hyperlocal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.rs
35 lines (25 loc) · 827 Bytes
/
server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::{error::Error, fs, path::Path};
use hyper::Response;
use tokio::net::UnixListener;
use hyperlocal::UnixListenerExt;
const PHRASE: &str = "It's a Unix system. I know this.\n";
// Adapted from https://hyper.rs/guides/1/server/hello-world/
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let path = Path::new("/tmp/hyperlocal.sock");
if path.exists() {
fs::remove_file(path)?;
}
let listener = UnixListener::bind(path)?;
println!("Listening for connections at {}.", path.display());
listener
.serve(|| {
println!("Accepted connection.");
|_request| async {
let body = PHRASE.to_string();
Ok::<_, hyper::Error>(Response::new(body))
}
})
.await?;
Ok(())
}