ptth/crates/ptth_file_server_bin/src/main.rs

139 lines
3.0 KiB
Rust
Raw Normal View History

2020-11-29 20:22:40 +00:00
#![warn (clippy::pedantic)]
use std::{
net::SocketAddr,
path::PathBuf,
sync::Arc,
};
use hyper::{
Body,
Request,
Response,
Server,
service::{
make_service_fn,
service_fn,
},
StatusCode,
};
use serde::Deserialize;
2020-11-27 00:03:11 +00:00
use ptth_core::{
http_serde::RequestParts,
prelude::*,
};
use ptth_server::{
errors::ServerError,
file_server,
load_toml,
};
#[derive (Default)]
pub struct Config {
pub file_server_root: Option <PathBuf>,
}
struct ServerState <'a> {
config: Config,
handlebars: handlebars::Handlebars <'a>,
server_info: file_server::ServerInfo,
hidden_path: Option <PathBuf>,
}
async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
-> Result <Response <Body>, ServerError>
{
2020-11-29 19:05:28 +00:00
use std::str::FromStr;
debug! ("req.uri () = {:?}", req.uri ());
2020-11-29 20:22:40 +00:00
let path_and_query = req.uri ().path_and_query ().map_or_else (|| req.uri ().path (), http::uri::PathAndQuery::as_str);
let path_and_query = path_and_query.into ();
let (parts, _) = req.into_parts ();
2020-11-29 20:22:40 +00:00
let ptth_req = RequestParts::from_hyper (parts.method, path_and_query, parts.headers)?;
let default_root = PathBuf::from ("./");
let file_server_root: &std::path::Path = state.config.file_server_root
.as_ref ()
.unwrap_or (&default_root);
let ptth_resp = file_server::serve_all (
&state.handlebars,
&state.server_info,
file_server_root,
ptth_req.method,
&ptth_req.uri,
&ptth_req.headers,
2020-11-25 00:16:14 +00:00
state.hidden_path.as_deref ()
).await?;
let mut resp = Response::builder ()
.status (StatusCode::from (ptth_resp.parts.status_code));
2020-11-29 20:22:40 +00:00
for (k, v) in ptth_resp.parts.headers {
2020-11-29 19:05:28 +00:00
resp = resp.header (hyper::header::HeaderName::from_str (&k)?, v);
}
2020-11-29 20:22:40 +00:00
let body = ptth_resp.body.map_or_else (Body::empty, Body::wrap_stream);
Ok (resp.body (body)?)
}
#[derive (Deserialize)]
pub struct ConfigFile {
pub file_server_root: Option <PathBuf>,
pub name: Option <String>,
}
#[tokio::main]
async fn main () -> Result <(), anyhow::Error> {
tracing_subscriber::fmt::init ();
let path = PathBuf::from ("./config/ptth_server.toml");
let config_file: ConfigFile = load_toml::load (&path)?;
info! ("file_server_root: {:?}", config_file.file_server_root);
let addr = SocketAddr::from(([0, 0, 0, 0], 4000));
let handlebars = file_server::load_templates (&PathBuf::new ())?;
let state = Arc::new (ServerState {
config: Config {
file_server_root: config_file.file_server_root,
},
handlebars,
server_info: crate::file_server::ServerInfo {
2020-11-25 00:16:14 +00:00
server_name: config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()),
},
hidden_path: Some (path),
});
let make_svc = make_service_fn (|_conn| {
let state = state.clone ();
async {
Ok::<_, String> (service_fn (move |req| {
let state = state.clone ();
handle_all (req, state)
}))
}
});
2020-11-27 00:03:11 +00:00
let (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force ();
let server = Server::bind (&addr)
.serve (make_svc)
.with_graceful_shutdown (async move {
shutdown_rx.await.ok ();
});
forced_shutdown.wrap_server (server).await??;
Ok (())
}