-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from TykTechnologies/feat/add-webhook-server
[TT-12162]add webhook server
- Loading branch information
Showing
6 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Use the official Golang image as the build stage | ||
FROM golang:alpine as builder | ||
|
||
# Set the Current Working Directory inside the container | ||
WORKDIR /app | ||
|
||
# Copy the source code into the container | ||
COPY . . | ||
|
||
# Build the Go app | ||
RUN go build -o webhook . | ||
|
||
# Expose port 9003 to the outside world | ||
EXPOSE 9003 | ||
|
||
# Command to run the executable | ||
CMD ["./webhook"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Webhook Server | ||
|
||
A simple Go application to receive and store webhook events, and retrieve them based on an ID. | ||
|
||
## Features | ||
|
||
- Receives webhook events at `/webhook/{id}` | ||
- Stores requests associated with each `{id}` | ||
- Retrieves stored requests at `/requests/{id}` | ||
|
||
|
||
## Usage | ||
|
||
### Start webhook server | ||
|
||
`go run main.go` runs the webhook server listening on port `8080`. | ||
|
||
### Sending a Webhook Event | ||
|
||
Send a POST request to `/webhook/{id}` with your webhook payload: | ||
|
||
``` | ||
curl -X POST http://localhost:8080/webhook/1 \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"event": "test"}' | ||
``` | ||
|
||
### Retrieving Stored Requests | ||
Send a GET request to `/requests/{id}` to retrieve the requests stored for a particular ID (ID is of string type) | ||
``` | ||
curl http://localhost:8080/requests/1 | ||
``` | ||
|
||
This will return a JSON array of all received webhook requests for the given ID. | ||
|
||
### Example | ||
1. Send a webhook event to ID `wh-1`: | ||
|
||
``` | ||
curl -X POST http://localhost:8080/webhook/wh-1 \ | ||
-H "Content-Type: application/json" \ | ||
-d '{"event": "test event for ID wh-1"}' | ||
``` | ||
|
||
2. Retrieve stored requests for ID `wh-1`: | ||
``` | ||
curl http://localhost:8080/requests/wh-1 | ||
``` | ||
|
||
Response | ||
``` | ||
[ | ||
{ | ||
"headers": { | ||
"Content-Type": ["application/json"], | ||
... | ||
}, | ||
"body": {"event": "test event for ID wh-1"} | ||
} | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module webhook | ||
|
||
go 1.22.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"regexp" | ||
"sync" | ||
) | ||
|
||
const root = "root" | ||
|
||
type WebhookRequest struct { | ||
Headers map[string][]string `json:"headers"` | ||
Body json.RawMessage `json:"body"` | ||
} | ||
|
||
var ( | ||
receivedRequests = make(map[string][]WebhookRequest) | ||
mutex sync.Mutex | ||
idPattern = regexp.MustCompile(`^/webhook/(\d+)$`) | ||
requestPattern = regexp.MustCompile(`^/requests/(\d+)$`) | ||
) | ||
|
||
func webhookHandler(w http.ResponseWriter, r *http.Request) { | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, "couldn't read body", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
matches := idPattern.FindStringSubmatch(r.URL.Path) | ||
id := root | ||
if len(matches) >= 2 { | ||
id = matches[1] | ||
} | ||
|
||
request := WebhookRequest{ | ||
Headers: r.Header, | ||
Body: body, | ||
} | ||
|
||
mutex.Lock() | ||
receivedRequests[id] = append(receivedRequests[id], request) | ||
mutex.Unlock() | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func requestsHandler(w http.ResponseWriter, r *http.Request) { | ||
matches := requestPattern.FindStringSubmatch(r.URL.Path) | ||
id := root | ||
if len(matches) >= 2 { | ||
id = matches[1] | ||
} | ||
|
||
log.Println("id =", id) | ||
|
||
mutex.Lock() | ||
defer mutex.Unlock() | ||
|
||
requests, exists := receivedRequests[id] | ||
if !exists { | ||
http.Error(w, "No requests found for the given ID", http.StatusNotFound) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(requests) | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/webhook/", webhookHandler) | ||
http.HandleFunc("/requests/", requestsHandler) | ||
|
||
fmt.Println("Starting server on :9003") | ||
if err := http.ListenAndServe(":9003", nil); err != nil { | ||
log.Fatalf("Could not start server: %s\n", err) | ||
} | ||
} |