diff --git a/main.go b/main.go index 3375e24..2da89cb 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,15 @@ package main import ( "context" + "encoding/json" "fmt" "io" "log" "net" "net/http" "os/signal" + "path" + "strings" "syscall" ) @@ -53,8 +56,20 @@ func listenAndServe(ctx context.Context, cfg Config) chan error { } func newHandler(cfg Config) http.HandlerFunc { + mux := http.NewServeMux() + + mux.Handle("POST /api/v1/events/{src}/{$}", http.HandlerFunc(newHandlerPostAPIV1Events(cfg))) + return func(w http.ResponseWriter, r *http.Request) { b, _ := io.ReadAll(r.Body) log.Printf("%s %s | %s", r.Method, r.URL, b) + mux.ServeHTTP(w, r) + } +} + +func newHandlerPostAPIV1Events(cfg Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + src := path.Base(strings.TrimSuffix(r.URL.Path, "/")) + json.NewEncoder(w).Encode(map[string]any{"src": src}) } }