Attempting to Learn Go - Issuer 04 - Addendum
Tags: atlg, go
Published: 2019-07-13 18:10:33

---
title: Attempting to Learn Go - Issuer 04 - Addendum
published: true
tags: atlg, go, 
canonical_url: https://shindakun.dev/posts/attempting-to-learn-go-issuer-04-addendum/
series: "Attempting to Learn Go"
cover_image: https://dev.shindakun.net/img/attemptinggo_w.png
---

## Whoops

Our last Issuer post left out one small bit of code. Let’s fix that and wrap this up. Also, let’s cover how to test the webhook before moving to Google Cloud Functions.

* * *

### Missed `if`

As I [published Issuer](https://dev.to/shindakun/attempting-to-learn-go-issuer-03-cloud-function-go-27co) to Google Cloud Functions I meant to update the code. The idea is to only create new issues when the originating payload action was `opened`. The final revision of the post left that out though so here we go. Let’s look at our updated `HandleWebhook()`.

```go
func HandleWebhook(res http.ResponseWriter, req *http.Request) {
  var Payload Payload
  defer req.Body.Close()

  p, err := github.ValidatePayload(req, []byte(Secret))
  if err != nil {
    http.Error(res, "bad request: "+err.Error(), 400)
    log.Printf("bad request: %v", err.Error())
    return
  }

  err = json.Unmarshal(p, &Payload)
  if err != nil {
    http.Error(res, "bad request: "+err.Error(), 400)
    log.Printf("bad request: %v", err.Error())
    return
  }
```

Here we are - we check `Payload.Action` to ensure it’s `opened` and if so then we create our new issue.

```go
  if Payload.Action == "opened" {
    err = createNewIssue(&Payload)
    if err != nil {
      log.Printf("bad request: %v", err.Error())
      return
    }
  }
}
```

* * *

## Testing Webhooks

Before pushing to GCF it was necessary to test the webhooks to ensure I was receiving what I needed. This is actually pretty easy thanks to [`ngrok`](https://ngrok.com/). `ngrok` is a command line tool which allows us to access our internal program from an external network. No need to worry about setting up your firewall or tunneling.

* * *

### `ngrok`

To do this we’ll need to make sure we have `ngrok` installed. On a Mac, which is where I tend to do most of my development lately, it’s a simple matter of installing via `brew`.

```bash
brew install ngrok
```

If you happen to be using Windows, you can download from the `ngrok` website, [https://ngrok.com/download](https://ngrok.com/download). Follow the instructions to install on the website. Once installed you should be able to open a command prompt or terminal window and run `ngrok`.

```bash
➜ ~ ngrok
NAME:
   ngrok - tunnel local ports to public URLs and inspect traffic

DESCRIPTION:
    ngrok exposes local networked services behinds NATs and firewalls to the
    public internet over a secure tunnel. Share local websites, build/test
    webhook consumers and self-host personal services.
    Detailed help for each command is available with 'ngrok help <command>'.
    Open http://localhost:4040 for ngrok's web interface to inspect traffic.
...
```

* * *

### Now What

Now what? That’s easy! Let’s run…

```bash
ngrok http 3000
```

Assuming you are running the code we wrote last time, that runs on port 3000. Now open a new terminal window and lets run our code.

```bash
go run main.go
```

Now we’re up and running, let's enable some webhooks on GitHub and do some actual testing.

* * *

### GitHub Webhooks

With both `ngrok` and our code running we go to GitHub! Pick a repo, or create a test one. Under the settings tab, chose “Webhooks” on the left. Click “Add webhook” on the upper right. Enter our `ngrok` URL, ex: `https://9ba7d0f6.ngrok.io` and add `/webhook`. In “Content type” make sure you select `application/json`. For this project I chose “Let me select individual events” and then selected only “Issues”. Then click “Add webhook” at the bottom.

![ngrok running on the command line](https://shindakun.dev/imgs/webhook-01.png) ![github webhook settings](https://shindakun.dev/imgs/webhook-02.png)

GitHub will send off a test post, we should see something right away.

![running issuer](https://shindakun.dev/imgs/webhook-03.png) ![test webhook](https://shindakun.dev/imgs/webhook-04.png)

And there we have it! It’s mostly blank because it’s just a test but it’s enough to show that we’re working as expected.

* * *

You can find the code for this and most of the other Attempting to Learn Go posts in the repo on GitHub.

{% github shindakun/atlg %}

* * *

| Enjoy this post? |
| --- |
| [How about buying me a coffee?](https://ko-fi.com/shindakun) |

* * *