feat: add network ngrok-demo
This commit is contained in:
1187
__network/ngrok-demo/Cargo.lock
generated
Normal file
1187
__network/ngrok-demo/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
__network/ngrok-demo/Cargo.toml
Normal file
12
__network/ngrok-demo/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "ngrok-demo"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.70"
|
||||
axum = "0.6.12"
|
||||
ngrok = { version = "0.11.2", features = ["axum"] }
|
||||
tokio = { version = "1.27.0", features = ["full"] }
|
||||
45
__network/ngrok-demo/src/main.rs
Normal file
45
__network/ngrok-demo/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{
|
||||
extract::ConnectInfo,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use ngrok::prelude::*;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// build our application with a single route
|
||||
let app = Router::new().route(
|
||||
"/",
|
||||
get(
|
||||
|ConnectInfo(remote_addr): ConnectInfo<SocketAddr>| async move {
|
||||
format!("Hello, {remote_addr:?}!\r\n")
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
let tun = ngrok::Session::builder()
|
||||
// Read the token from the NGROK_AUTHTOKEN environment variable
|
||||
.authtoken_from_env()
|
||||
// Connect the ngrok session
|
||||
.connect()
|
||||
.await?
|
||||
// Start a tunnel with an HTTP edge
|
||||
.http_endpoint()
|
||||
.listen()
|
||||
.await?;
|
||||
|
||||
println!("Tunnel started on URL: {:?}", tun.url());
|
||||
|
||||
// Instead of binding a local port like so:
|
||||
// axum::Server::bind(&"0.0.0.0:8000".parse().unwrap())
|
||||
// Run it with an ngrok tunnel instead!
|
||||
axum::Server::builder(tun)
|
||||
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user