http.ServeMux to route POST /api/v1/events/x$

main
Bel LaPointe 2024-04-11 16:49:17 -06:00
parent bf7e067628
commit 84e4600737
1 changed files with 15 additions and 0 deletions

15
main.go
View File

@ -2,12 +2,15 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
"net" "net"
"net/http" "net/http"
"os/signal" "os/signal"
"path"
"strings"
"syscall" "syscall"
) )
@ -53,8 +56,20 @@ func listenAndServe(ctx context.Context, cfg Config) chan error {
} }
func newHandler(cfg Config) http.HandlerFunc { 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) { return func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body) b, _ := io.ReadAll(r.Body)
log.Printf("%s %s | %s", r.Method, r.URL, b) 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})
} }
} }