2020-11-29 20:22:40 +00:00
|
|
|
#![warn (clippy::pedantic)]
|
|
|
|
|
2020-10-31 01:35:39 +00:00
|
|
|
use std::{
|
2020-11-07 01:34:58 +00:00
|
|
|
net::SocketAddr,
|
2020-10-31 01:35:39 +00:00
|
|
|
path::PathBuf,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
2020-12-20 19:35:32 +00:00
|
|
|
use arc_swap::ArcSwap;
|
2020-10-31 01:35:39 +00:00
|
|
|
use hyper::{
|
|
|
|
Body,
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
Server,
|
|
|
|
service::{
|
|
|
|
make_service_fn,
|
|
|
|
service_fn,
|
|
|
|
},
|
|
|
|
StatusCode,
|
|
|
|
};
|
2020-11-07 00:30:56 +00:00
|
|
|
use serde::Deserialize;
|
2021-03-06 21:15:41 +00:00
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
2020-12-18 20:43:34 +00:00
|
|
|
use tracing::debug;
|
2020-10-31 01:35:39 +00:00
|
|
|
|
2020-11-27 00:03:11 +00:00
|
|
|
use ptth_core::{
|
2020-10-31 17:56:36 +00:00
|
|
|
http_serde::RequestParts,
|
2020-11-08 16:00:31 +00:00
|
|
|
prelude::*,
|
2020-10-31 17:56:36 +00:00
|
|
|
};
|
2020-11-27 00:50:22 +00:00
|
|
|
use ptth_server::{
|
2020-12-18 20:43:34 +00:00
|
|
|
file_server::{
|
|
|
|
self,
|
2020-12-18 23:45:30 +00:00
|
|
|
metrics,
|
2021-04-18 00:10:52 +00:00
|
|
|
FileServer,
|
2020-12-18 20:43:34 +00:00
|
|
|
},
|
2020-11-27 00:50:22 +00:00
|
|
|
load_toml,
|
|
|
|
};
|
2020-10-31 17:56:36 +00:00
|
|
|
|
2021-04-18 00:10:52 +00:00
|
|
|
async fn handle_all (req: Request <Body>, state: Arc <FileServer>)
|
2021-03-05 03:17:56 +00:00
|
|
|
-> anyhow::Result <Response <Body>>
|
2020-10-31 01:35:39 +00:00
|
|
|
{
|
2020-11-29 19:05:28 +00:00
|
|
|
use std::str::FromStr;
|
2020-12-15 05:15:17 +00:00
|
|
|
use hyper::header::HeaderName;
|
2020-11-29 19:05:28 +00:00
|
|
|
|
2020-11-08 15:53:09 +00:00
|
|
|
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);
|
2020-10-31 01:35:39 +00:00
|
|
|
|
2020-11-10 02:39:20 +00:00
|
|
|
let path_and_query = path_and_query.into ();
|
2020-11-08 15:53:09 +00:00
|
|
|
|
|
|
|
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)?;
|
2020-11-08 15:53:09 +00:00
|
|
|
|
2021-04-18 00:10:52 +00:00
|
|
|
let ptth_resp = state.serve_all (
|
2020-11-08 15:53:09 +00:00
|
|
|
ptth_req.method,
|
|
|
|
&ptth_req.uri,
|
2020-12-20 19:55:20 +00:00
|
|
|
&ptth_req.headers
|
2020-11-29 19:19:59 +00:00
|
|
|
).await?;
|
2020-11-08 15:53:09 +00:00
|
|
|
|
|
|
|
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-12-15 05:15:17 +00:00
|
|
|
resp = resp.header (HeaderName::from_str (&k)?, v);
|
2020-10-31 01:35:39 +00:00
|
|
|
}
|
2020-11-08 15:53:09 +00:00
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
let body = ptth_resp.body.map_or_else (Body::empty, |body| {
|
|
|
|
Body::wrap_stream (ReceiverStream::new (body))
|
|
|
|
});
|
2020-11-08 15:53:09 +00:00
|
|
|
|
2020-11-29 19:19:59 +00:00
|
|
|
Ok (resp.body (body)?)
|
2020-10-31 01:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:30:56 +00:00
|
|
|
#[derive (Deserialize)]
|
|
|
|
pub struct ConfigFile {
|
|
|
|
pub file_server_root: Option <PathBuf>,
|
2020-11-10 00:44:21 +00:00
|
|
|
pub name: Option <String>,
|
2020-11-07 00:30:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 01:35:39 +00:00
|
|
|
#[tokio::main]
|
2021-03-05 03:17:56 +00:00
|
|
|
async fn main () -> anyhow::Result <()> {
|
2020-11-07 00:30:56 +00:00
|
|
|
tracing_subscriber::fmt::init ();
|
2020-11-08 17:58:14 +00:00
|
|
|
|
|
|
|
let path = PathBuf::from ("./config/ptth_server.toml");
|
2020-11-29 21:38:23 +00:00
|
|
|
let config_file: ConfigFile = load_toml::load (&path)?;
|
2020-11-07 00:30:56 +00:00
|
|
|
info! ("file_server_root: {:?}", config_file.file_server_root);
|
|
|
|
|
2020-10-31 01:35:39 +00:00
|
|
|
let addr = SocketAddr::from(([0, 0, 0, 0], 4000));
|
|
|
|
|
2020-12-20 23:17:31 +00:00
|
|
|
let metrics_interval = Arc::new (ArcSwap::default ());
|
2020-12-20 19:35:32 +00:00
|
|
|
|
2020-12-20 23:17:31 +00:00
|
|
|
let interval_writer = Arc::clone (&metrics_interval);
|
2020-12-20 19:35:32 +00:00
|
|
|
tokio::spawn (async move {
|
2020-12-20 23:32:13 +00:00
|
|
|
file_server::metrics::Interval::monitor (interval_writer).await;
|
2020-12-20 19:35:32 +00:00
|
|
|
});
|
2020-12-18 20:43:34 +00:00
|
|
|
|
2021-04-18 00:10:52 +00:00
|
|
|
let state = Arc::new (FileServer::new (
|
2021-04-17 23:59:59 +00:00
|
|
|
config_file.file_server_root,
|
|
|
|
&PathBuf::new (),
|
|
|
|
config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()),
|
2020-12-20 23:17:31 +00:00
|
|
|
metrics_interval,
|
2021-04-17 23:59:59 +00:00
|
|
|
Some (path),
|
|
|
|
)?);
|
2020-10-31 01:35:39 +00:00
|
|
|
|
|
|
|
let make_svc = make_service_fn (|_conn| {
|
|
|
|
let state = state.clone ();
|
|
|
|
|
|
|
|
async {
|
2020-11-07 01:34:58 +00:00
|
|
|
Ok::<_, String> (service_fn (move |req| {
|
2020-10-31 01:35:39 +00:00
|
|
|
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 ();
|
2020-11-07 01:34:58 +00:00
|
|
|
|
|
|
|
let server = Server::bind (&addr)
|
|
|
|
.serve (make_svc)
|
|
|
|
.with_graceful_shutdown (async move {
|
2020-11-07 02:26:34 +00:00
|
|
|
shutdown_rx.await.ok ();
|
2020-11-07 01:34:58 +00:00
|
|
|
});
|
|
|
|
|
2020-11-07 02:26:34 +00:00
|
|
|
forced_shutdown.wrap_server (server).await??;
|
2020-10-31 01:35:39 +00:00
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|