diff --git a/server.go b/server.go index dbd19ff..2c81661 100644 --- a/server.go +++ b/server.go @@ -125,6 +125,7 @@ func newEchoServer(log *otelzap.Logger, pool *pgxpool.Pool, conv *service.Sessio authMw := web.AuthenticatedMiddleware(conv) + app.Any("/api-v2/debug/dump", web.DumpEndpoint()) app.GET("/api-v2/application/summary", web.AppSummaryEndpoint()) app.GET("/api-v2/authinfo", web.AuthInfoEndpoint(), authMw) app.GET("/api-v2/gw2account", web.Gw2AccountsEndpoint(), authMw) diff --git a/web/dump.go b/web/dump.go new file mode 100644 index 0000000..29c3ff8 --- /dev/null +++ b/web/dump.go @@ -0,0 +1,61 @@ +package web + +import ( + "fmt" + "github.com/labstack/echo/v4" + "io" + "net/http" + "sort" +) + +func mapKeysSorted[K comparable, V any](m map[K]V, less func(k1, k2 K) bool) <-chan K { + sortedKeys := make([]K, 0, len(m)) + + for k := range m { + sortedKeys = append(sortedKeys, k) + } + + sort.Slice(sortedKeys, func(i, j int) bool { return less(sortedKeys[i], sortedKeys[j]) }) + + ch := make(chan K) + + go func() { + for _, k := range sortedKeys { + ch <- k + } + + close(ch) + }() + + return ch +} + +func DumpEndpoint() echo.HandlerFunc { + return wrapHandlerFunc(func(c echo.Context, rctx RequestContext) error { + req := c.Request() + res := fmt.Sprintf("%s %s %s\n", req.Method, req.RequestURI, req.Proto) + res += fmt.Sprintf("Host: %s", req.Host) + + h := c.Request().Header + for name := range mapKeysSorted(h, func(k1, k2 string) bool { return k1 < k2 }) { + res += fmt.Sprintf("\n%s: ", name) + + for i, v := range h[name] { + if i > 0 { + res += "," + } + + res += v + } + } + + if req.Body != nil { + defer req.Body.Close() + res += "\n\n" + b, _ := io.ReadAll(req.Body) + res += string(b) + } + + return c.String(http.StatusOK, res) + }) +}