mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 03:36:33 +03:00
Move api middleware into its own file
This commit is contained in:
parent
d772ae0b31
commit
ad1994574d
@ -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)
|
||||
}
|
||||
|
75
pkg/api/middleware.go
Normal file
75
pkg/api/middleware.go
Normal file
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user