summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 35728a9e48f9007492874d78d28a49d372b25a63 (plain)
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
use std::path::{Path, PathBuf};
use serde::Deserialize;
use structopt::StructOpt;
use warp::Filter;

#[derive(Debug, Deserialize)]
struct Config {
    port: u16,
}

#[derive(Debug, StructOpt)]
#[structopt(name = "ewww", about = "Web server for static sites")]
struct Opt {
    #[structopt(parse(from_os_str))]
    config: PathBuf,
}

#[tokio::main]
async fn main() {
    let opt = Opt::from_args();
    let config = read_config(&opt.config).unwrap();

    let hello = warp::any()
        .map(|| "hello, world\n".to_string());

    eprintln!("starting server: {:?}", config);
    warp::serve(hello)
        .run(([127, 0, 0, 1], config.port))
        .await;
}

fn read_config(filename: &Path) -> anyhow::Result<Config> {
    let config = std::fs::read_to_string(filename)?;
    let config: Config = serde_yaml::from_str(&config)?;
    Ok(config)
}