Refactor and simplify asset serving
This commit is contained in:
parent
dd2200bdb8
commit
0ac9d72deb
@ -12,20 +12,16 @@ import (
|
||||
"github.com/sosedoff/pgweb/pkg/client"
|
||||
"github.com/sosedoff/pgweb/pkg/command"
|
||||
"github.com/sosedoff/pgweb/pkg/connection"
|
||||
"github.com/sosedoff/pgweb/pkg/data"
|
||||
)
|
||||
|
||||
var DbClient *client.Client
|
||||
|
||||
func GetHome(c *gin.Context) {
|
||||
data, err := data.Asset("static/index.html")
|
||||
serveStaticAsset("/index.html", c)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.String(400, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(200, "text/html; charset=utf-8", data)
|
||||
func GetAsset(c *gin.Context) {
|
||||
serveStaticAsset(c.Params.ByName("path"), c)
|
||||
}
|
||||
|
||||
func GetConnect(c *gin.Context) {
|
||||
@ -248,20 +244,3 @@ func GetBookmarks(c *gin.Context) {
|
||||
|
||||
c.JSON(200, bookmarks)
|
||||
}
|
||||
|
||||
func GetAsset(c *gin.Context) {
|
||||
path := "static" + c.Params.ByName("path")
|
||||
data, err := data.Asset(path)
|
||||
|
||||
if err != nil {
|
||||
c.String(400, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
c.String(404, "Asset is empty")
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(200, assetContentType(path), data)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sosedoff/pgweb/pkg/data"
|
||||
)
|
||||
|
||||
var extraMimeTypes = map[string]string{
|
||||
@ -13,6 +14,7 @@ var extraMimeTypes = map[string]string{
|
||||
".woff": "application/x-font-woff",
|
||||
".eot": "application/vnd.ms-fontobject",
|
||||
".svg": "image/svg+xml",
|
||||
".html": "text/html; charset-utf-8",
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
@ -72,3 +74,13 @@ func dbCheckMiddleware() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func serveStaticAsset(path string, c *gin.Context) {
|
||||
data, err := data.Asset("static" + path)
|
||||
if err != nil {
|
||||
c.String(400, err.Error())
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
c.Data(200, assetContentType(path), data)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user