-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update hyper and axum #137
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
/target | ||
/Cargo.lock | ||
.direnv | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
use std::net::SocketAddr; | ||
use std::{ | ||
convert::Infallible, | ||
net::SocketAddr, | ||
}; | ||
|
||
use axum::{ | ||
extract::ConnectInfo, | ||
routing::get, | ||
Router, | ||
}; | ||
use ngrok::{ | ||
prelude::*, | ||
tunnel::HttpTunnel, | ||
use axum_core::BoxError; | ||
use futures::stream::TryStreamExt; | ||
use hyper::{ | ||
body::Incoming, | ||
Request, | ||
}; | ||
use hyper_util::{ | ||
rt::TokioExecutor, | ||
server, | ||
}; | ||
use ngrok::prelude::*; | ||
use tower::{ | ||
Service, | ||
ServiceExt, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
async fn main() -> Result<(), BoxError> { | ||
// build our application with a single route | ||
let app = Router::new().route( | ||
"/", | ||
|
@@ -22,21 +36,7 @@ async fn main() -> anyhow::Result<()> { | |
), | ||
); | ||
|
||
// run it with hyper on localhost:8000 | ||
// axum::Server::bind(&"0.0.0.0:8000".parse().unwrap()) | ||
// Or with an ngrok tunnel | ||
axum::Server::builder(start_tunnel().await?) | ||
.serve(app.into_make_service_with_connect_info::<SocketAddr>()) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
} | ||
|
||
// const CA_CERT: &[u8] = include_bytes!("ca.crt"); | ||
|
||
async fn start_tunnel() -> anyhow::Result<HttpTunnel> { | ||
let tun = ngrok::Session::builder() | ||
let mut listener = ngrok::Session::builder() | ||
.authtoken_from_env() | ||
.connect() | ||
.await? | ||
|
@@ -74,9 +74,35 @@ async fn start_tunnel() -> anyhow::Result<HttpTunnel> { | |
.listen() | ||
.await?; | ||
|
||
println!("Tunnel started on URL: {:?}", tun.url()); | ||
println!("Listener started on URL: {:?}", listener.url()); | ||
|
||
let mut make_service = app.into_make_service_with_connect_info::<SocketAddr>(); | ||
|
||
Ok(tun) | ||
let server = async move { | ||
while let Some(conn) = listener.try_next().await? { | ||
let remote_addr = conn.remote_addr(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block shows up a bit, could it be made into a re-usable component for ourselves and users, or does that get too messy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, so this is what I originally tried to do and burned most of monday on. We could
The core issue with It's all a mess and I think I'd prefer to support the lowest-common-denominator for now, which seems to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eek, makes sense to just be raw hyper and everything else DIY, it's sad they dropped that. I do wonder if these accept loops could be moved to a helper in our sdk so the examples and users would have less to do directly. |
||
let tower_service = unwrap_infallible(make_service.call(remote_addr).await); | ||
|
||
tokio::spawn(async move { | ||
let hyper_service = | ||
hyper::service::service_fn(move |request: Request<Incoming>| { | ||
tower_service.clone().oneshot(request) | ||
}); | ||
|
||
if let Err(err) = server::conn::auto::Builder::new(TokioExecutor::new()) | ||
.serve_connection_with_upgrades(conn, hyper_service) | ||
.await | ||
{ | ||
eprintln!("failed to serve connection: {err:#}"); | ||
} | ||
}); | ||
} | ||
Ok::<(), BoxError>(()) | ||
}; | ||
|
||
server.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[allow(dead_code)] | ||
|
@@ -103,3 +129,12 @@ fn create_policy() -> Result<Policy, InvalidPolicy> { | |
) | ||
.to_owned()) | ||
} | ||
|
||
// const CA_CERT: &[u8] = include_bytes!("ca.crt"); | ||
|
||
fn unwrap_infallible<T>(result: Result<T, Infallible>) -> T { | ||
match result { | ||
Ok(value) => value, | ||
Err(err) => match err {}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe "That way we can handle multiple"