Home > Backend Development > Golang > Building a Webhook payload delivery service in Go

Building a Webhook payload delivery service in Go

Linda Hamilton
Release: 2025-01-20 18:20:13
Original
877 people have browsed it

This article details creating a webhook proxy using Go, enabling local development of applications that listen to GitHub webhooks. The solution avoids using third-party services like smee.io or ngrok by building a custom server and CLI application.

Building a Webhook payload delivery service in Go

The challenge lies in forwarding webhook events from GitHub to a localhost development environment. This tutorial presents a two-part solution: a server to receive GitHub events and a CLI to forward them to a local application.

Architecture:

The system involves four key components:

  1. GitHub: The source of webhook events.
  2. Go Server: Receives events from GitHub and forwards them to the CLI.
  3. Go CLI: Runs locally, subscribes to server events, and forwards them to the local application.
  4. Local Application: A dummy server (for testing) receives the forwarded requests.

Building a Webhook payload delivery service in Go

Communication between the CLI and the server uses Server-Sent Events (SSE), a simple, one-way pushing technology.

Implementation:

The project structure includes a cmd directory for the CLI and server executables, and an internal directory for shared packages. The server utilizes the go-sse library for SSE functionality. The CLI uses the backoff library for robust connection handling.

*Server (cmd/web/main.go and internal/server/):**

The server has two endpoints:

  • /channel/{channel} (GET): Establishes an SSE connection with the CLI.
  • /channel/{channel} (POST): Receives GitHub webhook events, encodes them, and publishes them to the connected CLIs.

CLI (cmd/cli/main.go):

The CLI establishes an SSE connection to the server, receives encoded requests, decodes them, and forwards them to the specified local application. Error handling and exponential backoff are implemented using the backoff library.

Testing:

A simple test server (cmd/test/main.go) is provided to receive and process forwarded webhook events. After deploying the server and running the CLI, pushing changes to the GitHub repository triggers the webhook, which is then forwarded to the local test server.

Building a Webhook payload delivery service in Go

Building a Webhook payload delivery service in Go

Deployment (Appendices):

The article details deploying the server using Docker and Kamal, a deployment tool. It covers creating a Dockerfile, setting up Kamal, configuring environment variables (including using a password manager for security), and adding a health check endpoint. The process includes setting up a DigitalOcean droplet, configuring SSH access, and managing domain names and SSL certificates using Let's Encrypt through Kamal's proxy. Additional security measures and development environment improvements are also suggested.

This robust solution provides a flexible and reliable method for handling GitHub webhooks during local development, eliminating the need for external services. A future article will explore extending this to create a Telegram bot.

The above is the detailed content of Building a Webhook payload delivery service in Go. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template