From ad1994574de3176d32578b06d6a519cdd4b2b9c0 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Sun, 10 Jan 2016 15:03:33 -0600 Subject: [PATCH] Move api middleware into its own file --- pkg/api/helpers.go | 69 --------------------------------------- pkg/api/middleware.go | 75 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 69 deletions(-) create mode 100644 pkg/api/middleware.go diff --git a/pkg/api/helpers.go b/pkg/api/helpers.go index 821abbb..3627d34 100644 --- a/pkg/api/helpers.go +++ b/pkg/api/helpers.go @@ -2,15 +2,11 @@ package api import ( "fmt" - "log" "mime" "path/filepath" "strconv" "github.com/gin-gonic/gin" - - "github.com/sosedoff/pgweb/pkg/command" - "github.com/sosedoff/pgweb/pkg/data" ) var extraMimeTypes = map[string]string{ @@ -91,68 +87,3 @@ func assetContentType(name string) string { func NewError(err error) Error { return Error{err.Error()} } - -// Middleware function to check database connection status before running queries -func dbCheckMiddleware() gin.HandlerFunc { - return func(c *gin.Context) { - if allowedPaths[c.Request.URL.Path] == true { - c.Next() - return - } - - // We dont care about sessions unless they're enabled - if !command.Opts.Sessions { - if DbClient == nil { - c.JSON(400, Error{"Not connected"}) - c.Abort() - return - } - - c.Next() - return - } - - sessionId := getSessionId(c) - if sessionId == "" { - c.JSON(400, Error{"Session ID is required"}) - c.Abort() - return - } - - conn := DbSessions[sessionId] - if conn == nil { - c.JSON(400, Error{"Not connected"}) - c.Abort() - return - } - - c.Next() - } -} - -// 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 { - c.String(400, err.Error()) - return - } - - c.Data(200, assetContentType(path), data) -} - -func serveResult(result interface{}, err error, c *gin.Context) { - if err != nil { - c.JSON(400, NewError(err)) - return - } - - c.JSON(200, result) -} diff --git a/pkg/api/middleware.go b/pkg/api/middleware.go new file mode 100644 index 0000000..6053cf1 --- /dev/null +++ b/pkg/api/middleware.go @@ -0,0 +1,75 @@ +package api + +import ( + "log" + + "github.com/gin-gonic/gin" + + "github.com/sosedoff/pgweb/pkg/command" + "github.com/sosedoff/pgweb/pkg/data" +) + +// Middleware function to check database connection status before running queries +func dbCheckMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + if allowedPaths[c.Request.URL.Path] == true { + c.Next() + return + } + + // We dont care about sessions unless they're enabled + if !command.Opts.Sessions { + if DbClient == nil { + c.JSON(400, Error{"Not connected"}) + c.Abort() + return + } + + c.Next() + return + } + + sessionId := getSessionId(c) + if sessionId == "" { + c.JSON(400, Error{"Session ID is required"}) + c.Abort() + return + } + + conn := DbSessions[sessionId] + if conn == nil { + c.JSON(400, Error{"Not connected"}) + c.Abort() + return + } + + c.Next() + } +} + +// 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 { + c.String(400, err.Error()) + return + } + + c.Data(200, assetContentType(path), data) +} + +func serveResult(result interface{}, err error, c *gin.Context) { + if err != nil { + c.JSON(400, NewError(err)) + return + } + + c.JSON(200, result) +}