2021-10-02 19:07:55 +00:00
|
|
|
#![warn (clippy::pedantic)]
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
ffi::OsString,
|
|
|
|
net::SocketAddr,
|
2021-10-02 19:49:19 +00:00
|
|
|
path::{PathBuf},
|
2021-10-02 19:07:55 +00:00
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use arc_swap::ArcSwap;
|
|
|
|
use hyper::{
|
|
|
|
Body,
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
Server,
|
|
|
|
service::{
|
|
|
|
make_service_fn,
|
|
|
|
service_fn,
|
|
|
|
},
|
|
|
|
StatusCode,
|
|
|
|
};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
|
|
|
use tracing::debug;
|
|
|
|
|
|
|
|
use ptth_core::{
|
|
|
|
http_serde::RequestParts,
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
use ptth_server::{
|
|
|
|
file_server::{
|
|
|
|
self,
|
|
|
|
metrics,
|
|
|
|
FileServer,
|
|
|
|
},
|
|
|
|
load_toml,
|
|
|
|
};
|
|
|
|
|
|
|
|
async fn handle_all (req: Request <Body>, state: Arc <FileServer>)
|
|
|
|
-> anyhow::Result <Response <Body>>
|
|
|
|
{
|
|
|
|
use std::str::FromStr;
|
|
|
|
use hyper::header::HeaderName;
|
|
|
|
|
|
|
|
debug! ("req.uri () = {:?}", req.uri ());
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
|
|
|
|
let ptth_req = RequestParts::from_hyper (parts.method, path_and_query, parts.headers)?;
|
|
|
|
|
|
|
|
let ptth_resp = state.serve_all (
|
|
|
|
ptth_req.method,
|
|
|
|
&ptth_req.uri,
|
|
|
|
&ptth_req.headers
|
|
|
|
).await?;
|
|
|
|
|
|
|
|
let mut resp = Response::builder ()
|
|
|
|
.status (StatusCode::from (ptth_resp.parts.status_code));
|
|
|
|
|
|
|
|
for (k, v) in ptth_resp.parts.headers {
|
|
|
|
resp = resp.header (HeaderName::from_str (&k)?, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
let body = ptth_resp.body.map_or_else (Body::empty, |body| {
|
|
|
|
Body::wrap_stream (ReceiverStream::new (body))
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok (resp.body (body)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Deserialize)]
|
|
|
|
struct ConfigFile {
|
|
|
|
file_server_root: Option <PathBuf>,
|
|
|
|
name: Option <String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn main (_args: &[OsString]) -> anyhow::Result <()> {
|
|
|
|
let path = PathBuf::from ("./config/ptth_server.toml");
|
2021-10-02 19:49:19 +00:00
|
|
|
|
|
|
|
let file_server_root;
|
|
|
|
let name;
|
|
|
|
|
|
|
|
match load_toml::load::<ConfigFile, _> (&path) {
|
|
|
|
Ok (config_file) => {
|
|
|
|
file_server_root = config_file.file_server_root;
|
|
|
|
name = config_file.name;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
info! ("No ptth_server.toml file, using default configs");
|
|
|
|
file_server_root = None;
|
|
|
|
name = None;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = name.unwrap_or_else (|| "PTTH File Server".to_string ());
|
|
|
|
|
|
|
|
info! ("file_server_root: {:?}", file_server_root);
|
2021-10-02 19:07:55 +00:00
|
|
|
|
|
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 4000));
|
2021-10-02 19:49:19 +00:00
|
|
|
info! ("Serving at {:?}", addr);
|
2021-10-02 19:07:55 +00:00
|
|
|
|
|
|
|
let metrics_interval = Arc::new (ArcSwap::default ());
|
|
|
|
|
|
|
|
let interval_writer = Arc::clone (&metrics_interval);
|
|
|
|
tokio::spawn (async move {
|
|
|
|
file_server::metrics::Interval::monitor (interval_writer).await;
|
|
|
|
});
|
|
|
|
|
|
|
|
let state = Arc::new (FileServer::new (
|
2021-10-14 23:51:12 +00:00
|
|
|
file_server_root.unwrap_or_else (|| PathBuf::from (".")),
|
2021-10-02 19:07:55 +00:00
|
|
|
&PathBuf::new (),
|
2021-10-02 19:49:19 +00:00
|
|
|
name,
|
2021-10-02 19:07:55 +00:00
|
|
|
metrics_interval,
|
|
|
|
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)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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 (())
|
|
|
|
}
|
|
|
|
|