feat: add network ngrok-demo

This commit is contained in:
2023-04-01 20:34:13 +08:00
parent 1defe5958f
commit 00bedd5ce1
4 changed files with 1246 additions and 1 deletions

1187
__network/ngrok-demo/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View 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"] }

View 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(())
}