Serve assets with bindata
This commit is contained in:
parent
15c01e0a55
commit
f848116e1b
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
pgweb
|
pgweb
|
||||||
bin
|
bin
|
||||||
|
bindata.go
|
2
Makefile
2
Makefile
@ -1,4 +1,6 @@
|
|||||||
build:
|
build:
|
||||||
|
rm -f bindata.go
|
||||||
|
go-bindata static/...
|
||||||
gox -osarch="darwin/amd64" -output="./bin/pgweb_{{.OS}}_{{.Arch}}"
|
gox -osarch="darwin/amd64" -output="./bin/pgweb_{{.OS}}_{{.Arch}}"
|
||||||
|
|
||||||
deps:
|
deps:
|
||||||
|
43
api.go
43
api.go
@ -11,8 +11,27 @@ type Error struct {
|
|||||||
Message string `json:"error"`
|
Message string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assetContentType(name string) string {
|
||||||
|
if strings.Contains(name, ".css") {
|
||||||
|
return "text/css"
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(name, ".js") {
|
||||||
|
return "application/javascript"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "text/plain"
|
||||||
|
}
|
||||||
|
|
||||||
func API_Home(c *gin.Context) {
|
func API_Home(c *gin.Context) {
|
||||||
c.File("./static/index.html")
|
data, err := Asset("static/index.html")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.String(400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Data(200, "text/html", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func API_RunQuery(c *gin.Context) {
|
func API_RunQuery(c *gin.Context) {
|
||||||
@ -104,3 +123,25 @@ func API_HandleQuery(query string, c *gin.Context) {
|
|||||||
|
|
||||||
c.JSON(200, result)
|
c.JSON(200, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func API_ServeAsset(c *gin.Context) {
|
||||||
|
file := fmt.Sprintf(
|
||||||
|
"static/%s/%s",
|
||||||
|
c.Params.ByName("type"),
|
||||||
|
c.Params.ByName("name"),
|
||||||
|
)
|
||||||
|
|
||||||
|
data, err := Asset(file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.String(400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) == 0 {
|
||||||
|
c.String(404, "Asset is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Data(200, assetContentType(file), data)
|
||||||
|
}
|
||||||
|
2
main.go
2
main.go
@ -88,7 +88,7 @@ func main() {
|
|||||||
router.GET("/explain", API_ExplainQuery)
|
router.GET("/explain", API_ExplainQuery)
|
||||||
router.POST("/explain", API_ExplainQuery)
|
router.POST("/explain", API_ExplainQuery)
|
||||||
router.GET("/history", API_History)
|
router.GET("/history", API_History)
|
||||||
router.Static("/static", options.Static)
|
router.GET("/static/:type/:name", API_ServeAsset)
|
||||||
|
|
||||||
fmt.Println("Starting server at 0.0.0.0:8080")
|
fmt.Println("Starting server at 0.0.0.0:8080")
|
||||||
router.Run(":8080")
|
router.Run(":8080")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user