http.ServeMux to route POST /api/v1/events/x$
parent
bf7e067628
commit
84e4600737
15
main.go
15
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})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue