Skip to content

Commit

Permalink
Events can now be received at both /events or `/events/<channel-nam…
Browse files Browse the repository at this point in the history
…e>` endpoints
  • Loading branch information
Archisman-Mridha committed Mar 14, 2024
1 parent 76e036a commit f076957
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 12 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,18 @@ func run(ctx context.Context, args []string, env []string) error {

g.Go(func() error {
whmux := http.NewServeMux()
whmux.Handle(prefix+"/events", http.StripPrefix(prefix, webhook.New(sched)))

eventHandler := http.StripPrefix(prefix, webhook.New(sched))

// CASE: When the event is being received at '/events/<channel-name>' and the channel name
// gets extracted from the path parameter.
whmux.Handle(prefix+"/events/{chan}", eventHandler)

// DEPRECATED.
// CASE: When the event is being received at '/events' and the channel name gets extracted
// from the 'chan' query parameter.
whmux.Handle(prefix+"/events", eventHandler)

return listenAndServeContext(ctx, "webhook", webhookAddr, whmux)
})

Expand Down
14 changes: 13 additions & 1 deletion http/webhook/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/bluebrown/kobold/task"
"github.com/gorilla/mux"
)

type Webhook struct {
Expand All @@ -23,7 +24,18 @@ func (api *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "unable to read body", http.StatusBadRequest)
return
}
if err := api.s.Schedule(r.Context(), r.URL.Query().Get("chan"), buf.Bytes()); err != nil {

var channelName string

// Try to get the channel name from the path parameter. If not found in the path parameter, then
// get it from the query parameter.
muxVars := mux.Vars(r)
channelName = muxVars["chan"]
if len(channelName) == 0 {
channelName = r.URL.Query().Get("chan")
}

if err := api.s.Schedule(r.Context(), channelName, buf.Bytes()); err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit f076957

Please sign in to comment.