pgweb/api.go

159 lines
2.5 KiB
Go
Raw Normal View History

2014-10-10 17:14:17 -05:00
package main
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"strings"
)
type Error struct {
Message string `json:"error"`
}
2014-10-13 14:40:56 -05:00
func assetContentType(name string) string {
if strings.Contains(name, ".css") {
return "text/css"
}
if strings.Contains(name, ".js") {
return "application/javascript"
}
return "text/plain"
}
2014-10-13 13:55:19 -05:00
func API_Home(c *gin.Context) {
2014-10-13 14:40:56 -05:00
data, err := Asset("static/index.html")
if err != nil {
c.String(400, err.Error())
return
}
2014-10-13 19:49:43 -05:00
c.Data(200, "text/html; charset=utf-8", data)
2014-10-13 13:55:19 -05:00
}
2014-10-15 16:05:23 -05:00
func API_GetDatabases(c *gin.Context) {
names, err := dbClient.Databases()
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, names)
}
2014-10-10 17:14:17 -05:00
func API_RunQuery(c *gin.Context) {
query := strings.TrimSpace(c.Request.FormValue("query"))
if query == "" {
c.JSON(400, errors.New("Query parameter is missing"))
return
}
API_HandleQuery(query, c)
}
2014-10-11 13:24:12 -05:00
func API_ExplainQuery(c *gin.Context) {
query := strings.TrimSpace(c.Request.FormValue("query"))
if query == "" {
c.JSON(400, errors.New("Query parameter is missing"))
return
}
API_HandleQuery(fmt.Sprintf("EXPLAIN %s", query), c)
}
2014-10-10 17:14:17 -05:00
func API_GetTables(c *gin.Context) {
names, err := dbClient.Tables()
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, names)
}
func API_GetTable(c *gin.Context) {
2014-10-16 02:54:40 +00:00
res, err := dbClient.Table(c.Params.ByName("table"))
2014-10-10 17:14:17 -05:00
if err != nil {
c.JSON(400, NewError(err))
return
}
2014-10-13 13:55:19 -05:00
c.JSON(200, res)
2014-10-10 17:14:17 -05:00
}
func API_History(c *gin.Context) {
c.JSON(200, dbClient.history)
2014-10-10 17:14:17 -05:00
}
func API_Info(c *gin.Context) {
2014-10-14 21:53:57 -05:00
res, err := dbClient.Query(PG_INFO)
2014-10-10 17:14:17 -05:00
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, res.Format()[0])
}
2014-10-11 13:20:16 -05:00
func API_TableIndexes(c *gin.Context) {
res, err := dbClient.TableIndexes(c.Params.ByName("table"))
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, res)
}
2014-10-10 17:14:17 -05:00
func API_HandleQuery(query string, c *gin.Context) {
result, err := dbClient.Query(query)
if err != nil {
c.JSON(400, NewError(err))
return
}
q := c.Request.URL.Query()
2014-10-10 20:39:44 -05:00
if len(q["format"]) > 0 {
if q["format"][0] == "csv" {
c.String(200, result.CSV())
return
}
}
2014-10-10 17:14:17 -05:00
c.JSON(200, result)
}
2014-10-13 14:40:56 -05:00
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)
}