Skip to content

Commit

Permalink
add dump debug endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Nov 10, 2023
1 parent 5183d7b commit 4661745
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
61 changes: 61 additions & 0 deletions web/dump.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 4661745

Please sign in to comment.