Skip to content

Commit

Permalink
fix(deps): update code for new flexible-hyper-server-tls version
Browse files Browse the repository at this point in the history
Signed-off-by: MichaIng <[email protected]>
  • Loading branch information
MichaIng committed Jun 16, 2024
1 parent e23aca5 commit d7e05ae
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 152 deletions.
95 changes: 22 additions & 73 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ vite-embed = { git = "https://github.com/ravenclaw900/vite-embed", version = "0.
mime_guess = { version = "2.0.4", default-features = false }
flexible-hyper-server-tls = "0.3.0"
futures-util = { version = "0.3.30", default-features = false }
http-body-util = "0.1.2"
bytes = "1.6.0"

[features]
default = ["frontend"]
Expand Down
61 changes: 26 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![allow(clippy::too_many_lines)]
use crate::shared::CONFIG;
use anyhow::Context;
use hyper::service::{make_service_fn, service_fn};
use hyper::{body::Incoming, service::service_fn, Error, Request, Response};
use std::{net::IpAddr, str::FromStr};

mod config;
Expand All @@ -17,6 +17,24 @@ mod shared;
mod socket_handlers;
mod systemdata;

async fn svc(
req: Request<Incoming>,
) -> anyhow::Result<Response<http_body_util::combinators::BoxBody<bytes::Bytes, Error>>> {
// ToDo: How to get client addr, or can we skip it?
let remote_addr = "127.0.0.1";
let span = tracing::info_span!("request", %remote_addr);
span.in_scope(|| {
tracing::info!("Request to {}", req.uri().path());
tracing::debug!(
"using {:?}",
req.headers()
.get(hyper::header::USER_AGENT)
.map_or("unknown", |x| x.to_str().unwrap_or("unknown"))
);
});
routes::router(req, span).await
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
tracing::subscriber::set_global_default(
Expand All @@ -32,53 +50,26 @@ async fn main() -> anyhow::Result<()> {

let addr = std::net::SocketAddr::from((IpAddr::from([0; 8]), CONFIG.port));

let tcp = tokio::net::TcpListener::bind(&addr)
let listener = tokio::net::TcpListener::bind(&addr)
.await
.with_context(|| format!("Couldn't bind to {}", &addr))?;

let make_svc = make_service_fn(|conn: &flexible_hyper_server_tls::HttpOrHttpsConnection| {
let remote_addr = conn.remote_addr();
async move {
Ok::<_, std::convert::Infallible>(service_fn(move |req| async move {
let span = tracing::info_span!("request", %remote_addr);
span.in_scope(|| {
tracing::info!("Request to {}", req.uri().path());
tracing::debug!(
"using {:?}",
req.headers()
.get(hyper::header::USER_AGENT)
.map_or("unknown", |x| x.to_str().unwrap_or("unknown"))
);
});
routes::router(req, span).await
}))
}
});
let builder = flexible_hyper_server_tls::AcceptorBuilder::new(listener);

let acceptor = if CONFIG.tls {
let tls_acceptor = flexible_hyper_server_tls::tlsconfig::get_tlsacceptor_from_files(
let mut acceptor = if CONFIG.tls {
let tls_acceptor = flexible_hyper_server_tls::rustls_helpers::get_tlsacceptor_from_files(
&CONFIG.cert,
&CONFIG.key,
flexible_hyper_server_tls::tlsconfig::HttpProtocol::Http1,
)
.context("Couldn't get TLS config")?;

flexible_hyper_server_tls::HyperHttpOrHttpsAcceptor::new_https(
tcp,
tls_acceptor,
std::time::Duration::from_secs(10),
)
builder.https(tls_acceptor).build()
} else {
flexible_hyper_server_tls::HyperHttpOrHttpsAcceptor::new_http(tcp)
builder.build()
};

let mut server = hyper::server::Server::builder(acceptor).serve(make_svc);

// Ignore result, because it will never be an error
loop {
let _res = (&mut server).await.context("Server error").or_else(|e| {
tracing::warn!("{:?}", e);
anyhow::Ok(())
});
acceptor.accept(service_fn(svc)).await;
}
}
Loading

0 comments on commit d7e05ae

Please sign in to comment.