File: server.rs

package info (click to toggle)
rust-hyperlocal 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 974 bytes parent folder | download
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
36
37
38
39
40
41
42
#![allow(unused_imports)]
#![allow(dead_code)]
use std::{error::Error, fs, path::Path};

use hyper::Response;
use tokio::net::UnixListener;

#[cfg(feature = "server")]
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]
#[cfg(feature = "server")]
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(())
}

#[cfg(not(feature = "server"))]
fn main() {}