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

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