aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/server.rs
blob: 9433e830a66d16ece16273e76d4cbea13cf3a3b4 (plain) (blame)
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
use snafu::ResultExt as _;
use tokio::prelude::*;

#[derive(Debug, snafu::Snafu)]
pub enum Error {
    #[snafu(display("failed to parse address: {}", source))]
    ParseAddress { source: std::net::AddrParseError },

    #[snafu(display("failed to bind: {}", source))]
    Bind { source: tokio::io::Error },
}

pub type Result<T> = std::result::Result<T, Error>;

pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
    app.about("Run a termcast server")
}

pub fn run<'a>(_matches: &clap::ArgMatches<'a>) -> super::Result<()> {
    run_impl().context(super::Server)
}

fn run_impl() -> Result<()> {
    let addr = "127.0.0.1:8000".parse().context(ParseAddress)?;
    let listener = tokio::net::TcpListener::bind(&addr).context(Bind)?;
    let server = listener
        .incoming()
        .for_each(|_sock| {
            println!("got a connection");
            Ok(())
        })
        .map_err(|e| {
            eprintln!("accept failed: {}", e);
        });
    tokio::run(server);
    Ok(())
}