feat(router): ngrok edge (#642)

This commit is contained in:
OlivierDehaene 2023-07-19 11:59:58 +02:00 committed by GitHub
parent fe80f5360c
commit b66b190403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 55 deletions

View File

@ -267,17 +267,9 @@ struct Args {
#[clap(long, env)] #[clap(long, env)]
ngrok_authtoken: Option<String>, ngrok_authtoken: Option<String>,
/// ngrok domain name where the axum webserver will be available at /// ngrok edge
#[clap(long, env)] #[clap(long, env)]
ngrok_domain: Option<String>, ngrok_edge: Option<String>,
/// ngrok basic auth username
#[clap(long, env)]
ngrok_username: Option<String>,
/// ngrok basic auth password
#[clap(long, env)]
ngrok_password: Option<String>,
/// Display a lot of information about your runtime environment /// Display a lot of information about your runtime environment
#[clap(long, short, action)] #[clap(long, short, action)]
@ -900,26 +892,11 @@ fn spawn_webserver(
// Ngrok // Ngrok
if args.ngrok { if args.ngrok {
let authtoken = args.ngrok_authtoken.ok_or_else(|| {
tracing::error!("`ngrok-authtoken` must be set when using ngrok tunneling");
LauncherError::WebserverCannotStart
})?;
router_args.push("--ngrok".to_string()); router_args.push("--ngrok".to_string());
router_args.push("--ngrok-authtoken".to_string()); router_args.push("--ngrok-authtoken".to_string());
router_args.push(authtoken); router_args.push(args.ngrok_authtoken.unwrap());
router_args.push("--ngrok-edge".to_string());
if let Some(domain) = args.ngrok_domain { router_args.push(args.ngrok_edge.unwrap());
router_args.push("--ngrok-domain".to_string());
router_args.push(domain);
}
if let (Some(username), Some(password)) = (args.ngrok_username, args.ngrok_password) {
router_args.push("--ngrok-username".to_string());
router_args.push(username);
router_args.push("--ngrok-password".to_string());
router_args.push(password);
}
} }
// Copy current process env // Copy current process env
@ -997,7 +974,7 @@ fn terminate(process_name: &str, mut process: Child, timeout: Duration) -> io::R
fn main() -> Result<(), LauncherError> { fn main() -> Result<(), LauncherError> {
// Pattern match configuration // Pattern match configuration
let args = Args::parse(); let args: Args = Args::parse();
// Filter events with LOG_LEVEL // Filter events with LOG_LEVEL
let env_filter = let env_filter =
@ -1067,6 +1044,20 @@ fn main() -> Result<(), LauncherError> {
} }
} }
if args.ngrok {
if args.ngrok_authtoken.is_none() {
return Err(LauncherError::ArgumentValidation(
"`ngrok-authtoken` must be set when using ngrok tunneling".to_string(),
));
}
if args.ngrok_edge.is_none() {
return Err(LauncherError::ArgumentValidation(
"`ngrok-edge` must be set when using ngrok tunneling".to_string(),
));
}
}
// Signal handler // Signal handler
let running = Arc::new(AtomicBool::new(true)); let running = Arc::new(AtomicBool::new(true));
let r = running.clone(); let r = running.clone();

View File

@ -64,11 +64,7 @@ struct Args {
#[clap(long, env)] #[clap(long, env)]
ngrok_authtoken: Option<String>, ngrok_authtoken: Option<String>,
#[clap(long, env)] #[clap(long, env)]
ngrok_domain: Option<String>, ngrok_edge: Option<String>,
#[clap(long, env)]
ngrok_username: Option<String>,
#[clap(long, env)]
ngrok_password: Option<String>,
} }
fn main() -> Result<(), RouterError> { fn main() -> Result<(), RouterError> {
@ -96,9 +92,7 @@ fn main() -> Result<(), RouterError> {
cors_allow_origin, cors_allow_origin,
ngrok, ngrok,
ngrok_authtoken, ngrok_authtoken,
ngrok_domain, ngrok_edge,
ngrok_username,
ngrok_password,
} = args; } = args;
// Validate args // Validate args
@ -274,9 +268,7 @@ fn main() -> Result<(), RouterError> {
cors_allow_origin, cors_allow_origin,
ngrok, ngrok,
ngrok_authtoken, ngrok_authtoken,
ngrok_domain, ngrok_edge,
ngrok_username,
ngrok_password,
) )
.await?; .await?;
Ok(()) Ok(())

View File

@ -524,9 +524,7 @@ pub async fn run(
allow_origin: Option<AllowOrigin>, allow_origin: Option<AllowOrigin>,
ngrok: bool, ngrok: bool,
ngrok_authtoken: Option<String>, ngrok_authtoken: Option<String>,
ngrok_domain: Option<String>, ngrok_edge: Option<String>,
ngrok_username: Option<String>,
ngrok_password: Option<String>,
) -> Result<(), axum::BoxError> { ) -> Result<(), axum::BoxError> {
// OpenAPI documentation // OpenAPI documentation
#[derive(OpenApi)] #[derive(OpenApi)]
@ -696,32 +694,25 @@ pub async fn run(
#[cfg(feature = "ngrok")] #[cfg(feature = "ngrok")]
{ {
use ngrok::config::TunnelBuilder; use ngrok::config::TunnelBuilder;
use ngrok::tunnel::UrlTunnel;
let _ = addr; let _ = addr;
let authtoken = let authtoken =
ngrok_authtoken.expect("`ngrok-authtoken` must be set when using ngrok tunneling"); ngrok_authtoken.expect("`ngrok-authtoken` must be set when using ngrok tunneling");
let mut tunnel = ngrok::Session::builder() let edge = ngrok_edge.expect("`ngrok-edge` must be set when using ngrok tunneling");
let tunnel = ngrok::Session::builder()
.authtoken(authtoken) .authtoken(authtoken)
.connect() .connect()
.await .await
.unwrap() .unwrap()
.http_endpoint(); .labeled_tunnel()
.label("edge", edge);
if let Some(domain) = ngrok_domain {
tunnel = tunnel.domain(domain);
}
if let (Some(username), Some(password)) = (ngrok_username, ngrok_password) {
tunnel = tunnel.basic_auth(username, password);
}
let listener = tunnel.listen().await.unwrap(); let listener = tunnel.listen().await.unwrap();
// Run server // Run server
tracing::info!("Ingress URL: {:?}", listener.url());
axum::Server::builder(listener) axum::Server::builder(listener)
.serve(app.into_make_service()) .serve(app.into_make_service())
//Wait until all requests are finished to shut down //Wait until all requests are finished to shut down