Skip to content

Instantly share code, notes, and snippets.

@stmcallister
Created June 9, 2023 23:43
Show Gist options
  • Save stmcallister/4c0ffce17c98d794ad72d709218d845b to your computer and use it in GitHub Desktop.
Save stmcallister/4c0ffce17c98d794ad72d709218d845b to your computer and use it in GitHub Desktop.
ngrok-pagerduty-webhook-verification
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"golang.ngrok.com/ngrok"
"golang.ngrok.com/ngrok/config"
)
func main() {
if err := run(context.Background()); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
tun, err := ngrok.Listen(ctx,
config.HTTPEndpoint(
config.WithDomain("<you_static_domain_here>"),
config.WithWebhookVerification("pagerduty", "y0urs3cretFr0mP4g3rdUty"),
),
ngrok.WithAuthtokenFromEnv(),
)
if err != nil {
return err
}
// Display URL of the created tunnel
log.Println("tunnel created:", tun.URL())
return http.Serve(tun, http.HandlerFunc(handler))
}
func handler(w http.ResponseWriter, r *http.Request) {
// Buffer the body
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("Body reading error: %v", err)
return
}
if len(bodyBytes) > 0 {
// format and print the response
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, bodyBytes, "", "\t"); err != nil {
fmt.Printf("JSON parse error: %v", err)
return
}
fmt.Println(string(prettyJSON.Bytes()))
} else {
fmt.Printf("Body: No Body Supplied\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment