Split up api into smaller files
This commit is contained in:
parent
595f51d76c
commit
891f45c1ca
133
pkg/api/api.go
133
pkg/api/api.go
@ -3,8 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -17,102 +15,9 @@ import (
|
|||||||
"github.com/sosedoff/pgweb/pkg/data"
|
"github.com/sosedoff/pgweb/pkg/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
var extraMimeTypes = map[string]string{
|
|
||||||
".icon": "image-x-icon",
|
|
||||||
".ttf": "application/x-font-ttf",
|
|
||||||
".woff": "application/x-font-woff",
|
|
||||||
".eot": "application/vnd.ms-fontobject",
|
|
||||||
".svg": "image/svg+xml",
|
|
||||||
}
|
|
||||||
|
|
||||||
var DbClient *client.Client
|
var DbClient *client.Client
|
||||||
|
|
||||||
type Error struct {
|
func GetHome(c *gin.Context) {
|
||||||
Message string `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewError(err error) Error {
|
|
||||||
return Error{err.Error()}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assetContentType(name string) string {
|
|
||||||
ext := filepath.Ext(name)
|
|
||||||
result := mime.TypeByExtension(ext)
|
|
||||||
|
|
||||||
if result == "" {
|
|
||||||
result = extraMimeTypes[ext]
|
|
||||||
}
|
|
||||||
|
|
||||||
if result == "" {
|
|
||||||
result = "text/plain; charset=utf-8"
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetupRoutes(router *gin.Engine) {
|
|
||||||
router.GET("/", API_Home)
|
|
||||||
router.GET("/static/*path", API_ServeAsset)
|
|
||||||
|
|
||||||
api := router.Group("/api")
|
|
||||||
{
|
|
||||||
api.Use(ApiMiddleware())
|
|
||||||
|
|
||||||
api.POST("/connect", API_Connect)
|
|
||||||
api.GET("/databases", API_GetDatabases)
|
|
||||||
api.GET("/connection", API_ConnectionInfo)
|
|
||||||
api.GET("/activity", API_Activity)
|
|
||||||
api.GET("/schemas", API_GetSchemas)
|
|
||||||
api.GET("/tables", API_GetTables)
|
|
||||||
api.GET("/tables/:table", API_GetTable)
|
|
||||||
api.GET("/tables/:table/rows", API_GetTableRows)
|
|
||||||
api.GET("/tables/:table/info", API_GetTableInfo)
|
|
||||||
api.GET("/tables/:table/indexes", API_TableIndexes)
|
|
||||||
api.GET("/query", API_RunQuery)
|
|
||||||
api.POST("/query", API_RunQuery)
|
|
||||||
api.GET("/explain", API_ExplainQuery)
|
|
||||||
api.POST("/explain", API_ExplainQuery)
|
|
||||||
api.GET("/history", API_History)
|
|
||||||
api.GET("/bookmarks", API_Bookmarks)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Middleware function to check database connection status before running queries
|
|
||||||
func ApiMiddleware() gin.HandlerFunc {
|
|
||||||
allowedPaths := []string{
|
|
||||||
"/api/connect",
|
|
||||||
"/api/bookmarks",
|
|
||||||
"/api/history",
|
|
||||||
}
|
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
|
||||||
if DbClient != nil {
|
|
||||||
c.Next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
currentPath := c.Request.URL.Path
|
|
||||||
allowed := false
|
|
||||||
|
|
||||||
for _, path := range allowedPaths {
|
|
||||||
if path == currentPath {
|
|
||||||
allowed = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if allowed {
|
|
||||||
c.Next()
|
|
||||||
} else {
|
|
||||||
c.JSON(400, Error{"Not connected"})
|
|
||||||
c.Abort()
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func API_Home(c *gin.Context) {
|
|
||||||
data, err := data.Asset("static/index.html")
|
data, err := data.Asset("static/index.html")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -123,7 +28,7 @@ func API_Home(c *gin.Context) {
|
|||||||
c.Data(200, "text/html; charset=utf-8", data)
|
c.Data(200, "text/html; charset=utf-8", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_Connect(c *gin.Context) {
|
func GetConnect(c *gin.Context) {
|
||||||
url := c.Request.FormValue("url")
|
url := c.Request.FormValue("url")
|
||||||
|
|
||||||
if url == "" {
|
if url == "" {
|
||||||
@ -164,7 +69,7 @@ func API_Connect(c *gin.Context) {
|
|||||||
c.JSON(200, info.Format()[0])
|
c.JSON(200, info.Format()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_GetDatabases(c *gin.Context) {
|
func GetGetDatabases(c *gin.Context) {
|
||||||
names, err := DbClient.Databases()
|
names, err := DbClient.Databases()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -175,7 +80,7 @@ func API_GetDatabases(c *gin.Context) {
|
|||||||
c.JSON(200, names)
|
c.JSON(200, names)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_RunQuery(c *gin.Context) {
|
func GetRunQuery(c *gin.Context) {
|
||||||
query := strings.TrimSpace(c.Request.FormValue("query"))
|
query := strings.TrimSpace(c.Request.FormValue("query"))
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
@ -183,10 +88,10 @@ func API_RunQuery(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
API_HandleQuery(query, c)
|
GetHandleQuery(query, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_ExplainQuery(c *gin.Context) {
|
func GetExplainQuery(c *gin.Context) {
|
||||||
query := strings.TrimSpace(c.Request.FormValue("query"))
|
query := strings.TrimSpace(c.Request.FormValue("query"))
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
@ -194,10 +99,10 @@ func API_ExplainQuery(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
API_HandleQuery(fmt.Sprintf("EXPLAIN ANALYZE %s", query), c)
|
GetHandleQuery(fmt.Sprintf("EXPLAIN ANALYZE %s", query), c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_GetSchemas(c *gin.Context) {
|
func GetGetSchemas(c *gin.Context) {
|
||||||
names, err := DbClient.Schemas()
|
names, err := DbClient.Schemas()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -208,7 +113,7 @@ func API_GetSchemas(c *gin.Context) {
|
|||||||
c.JSON(200, names)
|
c.JSON(200, names)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_GetTables(c *gin.Context) {
|
func GetGetTables(c *gin.Context) {
|
||||||
names, err := DbClient.Tables()
|
names, err := DbClient.Tables()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -219,7 +124,7 @@ func API_GetTables(c *gin.Context) {
|
|||||||
c.JSON(200, names)
|
c.JSON(200, names)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_GetTable(c *gin.Context) {
|
func GetGetTable(c *gin.Context) {
|
||||||
res, err := DbClient.Table(c.Params.ByName("table"))
|
res, err := DbClient.Table(c.Params.ByName("table"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -230,7 +135,7 @@ func API_GetTable(c *gin.Context) {
|
|||||||
c.JSON(200, res)
|
c.JSON(200, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_GetTableRows(c *gin.Context) {
|
func GetGetTableRows(c *gin.Context) {
|
||||||
limit := 1000 // Number of rows to fetch
|
limit := 1000 // Number of rows to fetch
|
||||||
limitVal := c.Request.FormValue("limit")
|
limitVal := c.Request.FormValue("limit")
|
||||||
|
|
||||||
@ -266,7 +171,7 @@ func API_GetTableRows(c *gin.Context) {
|
|||||||
c.JSON(200, res)
|
c.JSON(200, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_GetTableInfo(c *gin.Context) {
|
func GetGetTableInfo(c *gin.Context) {
|
||||||
res, err := DbClient.TableInfo(c.Params.ByName("table"))
|
res, err := DbClient.TableInfo(c.Params.ByName("table"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -277,11 +182,11 @@ func API_GetTableInfo(c *gin.Context) {
|
|||||||
c.JSON(200, res.Format()[0])
|
c.JSON(200, res.Format()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_History(c *gin.Context) {
|
func GetHistory(c *gin.Context) {
|
||||||
c.JSON(200, DbClient.History)
|
c.JSON(200, DbClient.History)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_ConnectionInfo(c *gin.Context) {
|
func GetConnectionInfo(c *gin.Context) {
|
||||||
res, err := DbClient.Info()
|
res, err := DbClient.Info()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -292,7 +197,7 @@ func API_ConnectionInfo(c *gin.Context) {
|
|||||||
c.JSON(200, res.Format()[0])
|
c.JSON(200, res.Format()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_Activity(c *gin.Context) {
|
func GetActivity(c *gin.Context) {
|
||||||
res, err := DbClient.Activity()
|
res, err := DbClient.Activity()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, NewError(err))
|
c.JSON(400, NewError(err))
|
||||||
@ -302,7 +207,7 @@ func API_Activity(c *gin.Context) {
|
|||||||
c.JSON(200, res)
|
c.JSON(200, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_TableIndexes(c *gin.Context) {
|
func GetTableIndexes(c *gin.Context) {
|
||||||
res, err := DbClient.TableIndexes(c.Params.ByName("table"))
|
res, err := DbClient.TableIndexes(c.Params.ByName("table"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -313,7 +218,7 @@ func API_TableIndexes(c *gin.Context) {
|
|||||||
c.JSON(200, res)
|
c.JSON(200, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_HandleQuery(query string, c *gin.Context) {
|
func GetHandleQuery(query string, c *gin.Context) {
|
||||||
result, err := DbClient.Query(query)
|
result, err := DbClient.Query(query)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -333,7 +238,7 @@ func API_HandleQuery(query string, c *gin.Context) {
|
|||||||
c.JSON(200, result)
|
c.JSON(200, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_Bookmarks(c *gin.Context) {
|
func GetBookmarks(c *gin.Context) {
|
||||||
bookmarks, err := bookmarks.ReadAll(bookmarks.Path())
|
bookmarks, err := bookmarks.ReadAll(bookmarks.Path())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -344,7 +249,7 @@ func API_Bookmarks(c *gin.Context) {
|
|||||||
c.JSON(200, bookmarks)
|
c.JSON(200, bookmarks)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_ServeAsset(c *gin.Context) {
|
func GetAsset(c *gin.Context) {
|
||||||
path := "static" + c.Params.ByName("path")
|
path := "static" + c.Params.ByName("path")
|
||||||
data, err := data.Asset(path)
|
data, err := data.Asset(path)
|
||||||
|
|
||||||
|
74
pkg/api/helpers.go
Normal file
74
pkg/api/helpers.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
var extraMimeTypes = map[string]string{
|
||||||
|
".icon": "image-x-icon",
|
||||||
|
".ttf": "application/x-font-ttf",
|
||||||
|
".woff": "application/x-font-woff",
|
||||||
|
".eot": "application/vnd.ms-fontobject",
|
||||||
|
".svg": "image/svg+xml",
|
||||||
|
}
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
Message string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func assetContentType(name string) string {
|
||||||
|
ext := filepath.Ext(name)
|
||||||
|
result := mime.TypeByExtension(ext)
|
||||||
|
|
||||||
|
if result == "" {
|
||||||
|
result = extraMimeTypes[ext]
|
||||||
|
}
|
||||||
|
|
||||||
|
if result == "" {
|
||||||
|
result = "text/plain; charset=utf-8"
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewError(err error) Error {
|
||||||
|
return Error{err.Error()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware function to check database connection status before running queries
|
||||||
|
func dbCheckMiddleware() gin.HandlerFunc {
|
||||||
|
allowedPaths := []string{
|
||||||
|
"/api/connect",
|
||||||
|
"/api/bookmarks",
|
||||||
|
"/api/history",
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if DbClient != nil {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
currentPath := c.Request.URL.Path
|
||||||
|
allowed := false
|
||||||
|
|
||||||
|
for _, path := range allowedPaths {
|
||||||
|
if path == currentPath {
|
||||||
|
allowed = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if allowed {
|
||||||
|
c.Next()
|
||||||
|
} else {
|
||||||
|
c.JSON(400, Error{"Not connected"})
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
32
pkg/api/routes.go
Normal file
32
pkg/api/routes.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetupRoutes(router *gin.Engine) {
|
||||||
|
router.GET("/", GetHome)
|
||||||
|
router.GET("/static/*path", GetAsset)
|
||||||
|
|
||||||
|
api := router.Group("/api")
|
||||||
|
{
|
||||||
|
api.Use(dbCheckMiddleware())
|
||||||
|
|
||||||
|
api.POST("/connect", GetConnect)
|
||||||
|
api.GET("/databases", GetGetDatabases)
|
||||||
|
api.GET("/connection", GetConnectionInfo)
|
||||||
|
api.GET("/activity", GetActivity)
|
||||||
|
api.GET("/schemas", GetGetSchemas)
|
||||||
|
api.GET("/tables", GetGetTables)
|
||||||
|
api.GET("/tables/:table", GetGetTable)
|
||||||
|
api.GET("/tables/:table/rows", GetGetTableRows)
|
||||||
|
api.GET("/tables/:table/info", GetGetTableInfo)
|
||||||
|
api.GET("/tables/:table/indexes", GetTableIndexes)
|
||||||
|
api.GET("/query", GetRunQuery)
|
||||||
|
api.POST("/query", GetRunQuery)
|
||||||
|
api.GET("/explain", GetExplainQuery)
|
||||||
|
api.POST("/explain", GetExplainQuery)
|
||||||
|
api.GET("/history", GetHistory)
|
||||||
|
api.GET("/bookmarks", GetBookmarks)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user