Skip to content

Commit

Permalink
Resolving conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
Archisman-Mridha committed Mar 15, 2024
1 parent f076957 commit e4b5965
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/bluebrown/kobold/store"
"github.com/bluebrown/kobold/store/schema"
"github.com/bluebrown/kobold/task"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
_ "modernc.org/sqlite"
Expand Down Expand Up @@ -85,7 +86,7 @@ func run(ctx context.Context, args []string, env []string) error {
})

g.Go(func() error {
whmux := http.NewServeMux()
whmux := mux.NewRouter( )

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

Expand All @@ -96,7 +97,7 @@ func run(ctx context.Context, args []string, env []string) error {
// 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)
whmux.Handle(prefix+"/events", eventHandler).Queries("chan", "{chan}")

return listenAndServeContext(ctx, "webhook", webhookAddr, whmux)
})
Expand Down
18 changes: 10 additions & 8 deletions http/webhook/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package webhook

import (
"bytes"
"log/slog"
"net/http"

"github.com/bluebrown/kobold/task"
Expand All @@ -25,19 +26,20 @@ func (api *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

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")
}
channelName := muxVars["chan"]

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

// Log and send back deprecation notice in response, if channel name is being sent using query
// parameter.
if r.URL.Query().Has("chan") {
slog.Warn("Sending channel name using query parameters is deprecated")
w.Write([]byte("Deprecated API: Send channel name using path parameter instead of query parameter"))

Check failure on line 41 in http/webhook/handler.go

View workflow job for this annotation

GitHub Actions / checks

Error return value of `w.Write` is not checked (errcheck)
}

w.WriteHeader(http.StatusAccepted)
}

0 comments on commit e4b5965

Please sign in to comment.