fedhub/dev-server.old.rs
2020-04-21 12:33:14 -05:00

55 lines
1.5 KiB
Rust

use std::convert::Infallible;
use std::sync::mpsc::{self};
use std::time::Duration;
use std::thread;
use std::path::PathBuf;
use anyhow::Result;
use hyper::{
service::{make_service_fn, service_fn},
Body, Response, Server,
};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
#[tokio::main]
async fn main() -> Result<()> {
let (watch_tx, watch_rx) = mpsc::channel();
let mut watcher = RecommendedWatcher::new(watch_tx, Duration::from_secs(3))?;
let path = PathBuf::from(".");
watcher.watch(&path, RecursiveMode::Recursive)?;
thread::spawn(move || {
loop {
match watch_rx.recv() {
Ok(evt) => {
println!("event: {:?}", evt);
}
Err(err) => eprintln!("watch error: {:?}", err),
}
}
});
let make_svc = make_service_fn(move |_conn| {
let fedhub = fedhub.clone();
let main = move |req| {
let fedhub = fedhub.clone();
future::ready(match fedhub.handle(req) {
Ok(res) => Ok::<_, Infallible>(res),
Err(err) => {
eprintln!("Error: {:?}", err);
Ok(Response::new(Body::from(format!("Error: {:?}", err))))
}
})
};
future::ok::<_, Infallible>(service_fn(main))
});
let server = Server::bind(&addr).serve(make_svc);
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
Ok(())
}