2014-10-11 02:14:17 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-11-06 16:21:53 +03:00
|
|
|
"path/filepath"
|
2014-10-11 02:14:17 +04:00
|
|
|
"strings"
|
2014-11-11 08:10:05 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2014-10-11 02:14:17 +04:00
|
|
|
)
|
|
|
|
|
2014-11-11 08:19:21 +03:00
|
|
|
var MIME_TYPES = map[string]string{
|
|
|
|
".css": "text/css",
|
|
|
|
".js": "application/javascript",
|
|
|
|
".icon": "image-x-icon",
|
2014-12-02 05:23:10 +03:00
|
|
|
".eot": "application/vnd.ms-fontobject",
|
|
|
|
".svg": "image/svg+xml",
|
|
|
|
".ttf": "application/font-sfnt",
|
|
|
|
".woff": "application/font-woff",
|
2014-11-11 08:19:21 +03:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
type Error struct {
|
|
|
|
Message string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2014-11-21 06:43:51 +03:00
|
|
|
func NewError(err error) Error {
|
|
|
|
return Error{err.Error()}
|
|
|
|
}
|
|
|
|
|
2014-10-13 23:40:56 +04:00
|
|
|
func assetContentType(name string) string {
|
2014-11-11 08:19:21 +03:00
|
|
|
mime := MIME_TYPES[filepath.Ext(name)]
|
|
|
|
|
|
|
|
if mime != "" {
|
|
|
|
return mime
|
|
|
|
} else {
|
2014-11-06 16:21:53 +03:00
|
|
|
return "text/plain"
|
2014-11-04 04:06:05 +03:00
|
|
|
}
|
2014-10-13 23:40:56 +04:00
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
func API_Home(c *gin.Context) {
|
2014-10-13 23:40:56 +04:00
|
|
|
data, err := Asset("static/index.html")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:49:43 +04:00
|
|
|
c.Data(200, "text/html; charset=utf-8", data)
|
2014-10-13 22:55:19 +04:00
|
|
|
}
|
|
|
|
|
2014-11-01 06:37:58 +03:00
|
|
|
func API_Connect(c *gin.Context) {
|
|
|
|
url := c.Request.FormValue("url")
|
|
|
|
|
|
|
|
if url == "" {
|
|
|
|
c.JSON(400, Error{"Url parameter is required"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := NewClientFromUrl(url)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, Error{err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Test()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, Error{err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := client.Info()
|
|
|
|
|
|
|
|
if err == nil {
|
2014-11-01 23:44:24 +03:00
|
|
|
if dbClient != nil {
|
|
|
|
dbClient.db.Close()
|
|
|
|
}
|
|
|
|
|
2014-11-01 06:37:58 +03:00
|
|
|
dbClient = client
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, info.Format()[0])
|
|
|
|
}
|
|
|
|
|
2014-10-16 01:05:23 +04: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-11 02:14:17 +04: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 22:24:12 +04: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
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:58:12 +04:00
|
|
|
API_HandleQuery(fmt.Sprintf("EXPLAIN ANALYZE %s", query), c)
|
2014-10-11 22:24:12 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04: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 06:54:40 +04:00
|
|
|
res, err := dbClient.Table(c.Params.ByName("table"))
|
2014-10-11 02:14:17 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, NewError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
c.JSON(200, res)
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-18 07:30:08 +04:00
|
|
|
func API_GetTableInfo(c *gin.Context) {
|
|
|
|
res, err := dbClient.TableInfo(c.Params.ByName("table"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, NewError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-27 01:16:35 +03:00
|
|
|
c.JSON(200, res.Format()[0])
|
2014-10-18 07:30:08 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
func API_History(c *gin.Context) {
|
2014-10-11 06:25:02 +04:00
|
|
|
c.JSON(200, dbClient.history)
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2014-12-03 06:20:04 +03:00
|
|
|
func API_ConnectionInfo(c *gin.Context) {
|
2014-11-01 23:44:24 +03:00
|
|
|
if dbClient == nil {
|
|
|
|
c.JSON(400, Error{"Not connected"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:59:43 +04:00
|
|
|
res, err := dbClient.Info()
|
2014-10-11 02:14:17 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, NewError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, res.Format()[0])
|
|
|
|
}
|
|
|
|
|
2014-10-11 22:20:16 +04: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-11 02:14:17 +04:00
|
|
|
func API_HandleQuery(query string, c *gin.Context) {
|
|
|
|
result, err := dbClient.Query(query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, NewError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-11 03:56:02 +04:00
|
|
|
q := c.Request.URL.Query()
|
|
|
|
|
2014-10-11 05:39:44 +04:00
|
|
|
if len(q["format"]) > 0 {
|
|
|
|
if q["format"][0] == "csv" {
|
2014-10-18 19:15:13 +04:00
|
|
|
c.Data(200, "text/csv", result.CSV())
|
2014-10-11 05:39:44 +04:00
|
|
|
return
|
|
|
|
}
|
2014-10-11 03:56:02 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
c.JSON(200, result)
|
|
|
|
}
|
2014-10-13 23:40:56 +04:00
|
|
|
|
2014-12-03 07:19:38 +03:00
|
|
|
func API_Bookmarks(c *gin.Context) {
|
|
|
|
bookmarks, err := readAllBookmarks()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, NewError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, bookmarks)
|
|
|
|
}
|
|
|
|
|
2014-10-13 23:40:56 +04: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)
|
|
|
|
}
|