ngrok-go package, to create a tunnel!

Introduction

When developing a web application, you might find yourself needing to test it on a public URL or give access to it to someone outside of your local network. However, this can be a challenge as you might not have a public URL or a domain name. This is where ngrok comes in handy.

ngrok is a tool that allows you to create a secure public URL (a tunnel) to your local machine, thus allowing you to test and share your web application with others. The ngrok-go package is a Go package that allows you to programmatically create and manage ngrok tunnels from within your Go application.

In this article, we will explore the ngrok-go package and how it can be used to create ngrok tunnels programmatically.

Prerequisites

Before we dive into the ngrok-go package, there are a few prerequisites that you should have:

  • A Go development environment set up

  • A basic understanding of HTTP and networking

Installing the ngrok-go package

The ngrok-go package can be installed using the following command:

go get github.com/inconshreveable/ngrok/...

Creating a ngrok tunnel

Once the ngrok-go package is installed, you can start using it to create ngrok tunnels programmatically. Here's an example:

package main

import (
    "fmt"
    "net/http"

    "github.com/inconshreveable/ngrok"
)

func main() {
    // Create a new ngrok tunnel
    ngrokURL, err := ngrok.Connect(8080)
    if err != nil {
        panic(err)
    }

    fmt.Printf("ngrok tunnel URL: %s\n", ngrokURL)

    // Start a HTTP server to serve some content
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello, world!")
    })
    http.ListenAndServe(":8080", nil)
}

In this example, we first create a new ngrok tunnel by calling the ngrok.Connect function and passing in the port number that we want to expose (in this case, port 8080). The ngrok.Connect function returns the public URL of the ngrok tunnel that was created.

We then start a HTTP server on port 8080 and serve some content. This server will be accessible through the ngrok tunnel that we created earlier.

When we run this program, we should see output similar to the following:

ngrok tunnel URL: http://b873d185.ngrok.io

This URL can be shared with others to allow them to access our HTTP server.

Conclusion

A ngrok-go package is a powerful tool that allows you to create ngrok tunnels programmatically from within your Go applications. This can be incredibly useful when testing or sharing your web applications with others. With just a few lines of code, you can create a secure public URL to your local machine, and easily share your work with the world.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!