From 2a4edaf08f4613af4a6a0e074379e1a43b6752cc Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 4 Aug 2015 10:38:23 -0500 Subject: [PATCH] Add request middleware to log incoming form params --- pkg/api/helpers.go | 10 ++++++++++ pkg/api/routes.go | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/api/helpers.go b/pkg/api/helpers.go index ad13d66..931a5b4 100644 --- a/pkg/api/helpers.go +++ b/pkg/api/helpers.go @@ -1,6 +1,7 @@ package api import ( + "log" "mime" "path/filepath" @@ -76,6 +77,15 @@ func dbCheckMiddleware() gin.HandlerFunc { } } +// Middleware function to print out request parameters and body for debugging +func requestInspectMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + err := c.Request.ParseForm() + + log.Println("Request params:", err, c.Request.Form) + } +} + func serveStaticAsset(path string, c *gin.Context) { data, err := data.Asset("static" + path) if err != nil { diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 14b7c8a..baae9a9 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -2,15 +2,24 @@ package api import ( "github.com/gin-gonic/gin" + "github.com/sosedoff/pgweb/pkg/command" ) +func SetupMiddlewares(group *gin.RouterGroup) { + if command.Opts.Debug { + group.Use(requestInspectMiddleware()) + } + + group.Use(dbCheckMiddleware()) +} + func SetupRoutes(router *gin.Engine) { router.GET("/", GetHome) router.GET("/static/*path", GetAsset) api := router.Group("/api") { - api.Use(dbCheckMiddleware()) + SetupMiddlewares(api) api.GET("/info", GetInfo) api.POST("/connect", Connect)