Skip to content

Instantly share code, notes, and snippets.

@alexdlaird
Last active September 21, 2021 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdlaird/522cba505b0a9f935f65036355c46f4a to your computer and use it in GitHub Desktop.
Save alexdlaird/522cba505b0a9f935f65036355c46f4a to your computer and use it in GitHub Desktop.
HTTP server integration example for java-ngrok, full documentation found at https://javadoc.io/doc/com.github.alexdlaird/java-ngrok/latest/index.html
package com.github.alexdlaird;
import com.github.alexdlaird.ngrok.NgrokClient;
import com.github.alexdlaird.ngrok.protocol.CreateTunnel;
import com.github.alexdlaird.ngrok.protocol.Tunnel;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.util.logging.Logger;
public class NgrokHttpServer {
private static final Logger LOGGER = Logger.getLogger(String.valueOf(NgrokHttpServer.class));
public static void main(String[] args) throws Exception {
final int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "80"));
final HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
final NgrokClient ngrokClient = new NgrokClient.Builder().build();
final CreateTunnel createTunnel = new CreateTunnel.Builder()
.withAddr(port)
.build();
final Tunnel tunnel = ngrokClient.connect(createTunnel);
LOGGER.info(String.format("ngrok tunnel \"%s\" -> \"http://127.0.0.1:%d\"", tunnel.getPublicUrl(), port));
server.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment