Download ngrok
ngrok is your app’s front door—and the fastest way to put anything on the internet.
ngrok is your app’s front door—and the fastest way to put anything on the internet.
Setup a project by making a directory:
mkdir hello-ngrok && cd hello-ngrokInstall rust on your system here or use Homebrew:
brew install rustInitialize your rust project:
cargo initInstall the ngrok-rust package and the required dependencies:
cargo add ngrok -F axum && cargo add axum && cargo add tokio -F rt-multi-thread -F macrosEdit your Cargo.toml file so that it has the correct dependency versions:
[package]
name = "hello-ngrok"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.6"
ngrok = { version = "0.13.1", features = ["axum"] }
tokio = { version = "1.41.0", features = ["full", "macros", "rt-multi-thread"] }
hyper = "0.14"
anyhow = "1.0"use axum::{routing::get, Router};
use ngrok::prelude::*;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// build our application with a route
let app = Router::new().route("/", get(|| async { "Hello from ngrok-rs!" }));
// listen on ngrok ingress (i.e. https://myapp.ngrok.io)
let listener = ngrok::Session::builder()
.authtoken_from_env()
.connect()
.await?
.http_endpoint()
.listen()
.await?;
println!("Ingress URL: {:?}", listener.url());
axum::Server::builder(listener)
.serve(app.into_make_service())
.await?;
Ok(())
}Run your Rust app with your ngrok authtoken as an environment variable. Sign up for a free account to get your authtoken.
NGROK_AUTHTOKEN=<token> cargo r