2015-05-01 03:59:48 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-01-08 06:18:22 +03:00
|
|
|
"fmt"
|
2015-08-04 18:38:23 +03:00
|
|
|
"log"
|
2015-05-01 03:59:48 +03:00
|
|
|
"mime"
|
|
|
|
"path/filepath"
|
2016-01-08 06:18:22 +03:00
|
|
|
"strconv"
|
2015-05-01 03:59:48 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2015-05-03 04:10:14 +03:00
|
|
|
"github.com/sosedoff/pgweb/pkg/data"
|
2015-05-01 03:59:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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",
|
2015-05-03 04:10:14 +03:00
|
|
|
".html": "text/html; charset-utf-8",
|
2015-05-01 03:59:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Error struct {
|
|
|
|
Message string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2016-01-05 03:03:53 +03:00
|
|
|
func getQueryParam(c *gin.Context, name string) string {
|
|
|
|
result := ""
|
|
|
|
q := c.Request.URL.Query()
|
|
|
|
|
|
|
|
if len(q[name]) > 0 {
|
|
|
|
result = q[name][0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-01-08 06:18:22 +03:00
|
|
|
func parseIntFormValue(c *gin.Context, name string, defValue int) (int, error) {
|
|
|
|
val := c.Request.FormValue(name)
|
|
|
|
|
|
|
|
if val == "" {
|
|
|
|
return defValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err := strconv.Atoi(val)
|
|
|
|
if err != nil {
|
|
|
|
return defValue, fmt.Errorf("%s must be a number", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if num < 1 {
|
|
|
|
return defValue, fmt.Errorf("%s must be greated than 0", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return num, nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 03:59:48 +03:00
|
|
|
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{
|
2015-05-05 08:34:23 +03:00
|
|
|
"/api/info",
|
2015-05-01 03:59:48 +03:00
|
|
|
"/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
|
|
|
|
}
|
|
|
|
}
|
2015-05-03 04:10:14 +03:00
|
|
|
|
2015-08-04 18:38:23 +03:00
|
|
|
// Middleware function to print out request parameters and body for debugging
|
|
|
|
func requestInspectMiddleware() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
err := c.Request.ParseForm()
|
|
|
|
|
|
|
|
log.Println("Request params:", err, c.Request.Form)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 04:10:14 +03:00
|
|
|
func serveStaticAsset(path string, c *gin.Context) {
|
|
|
|
data, err := data.Asset("static" + path)
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, err.Error())
|
2015-05-03 04:32:16 +03:00
|
|
|
return
|
2015-05-03 04:10:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(200, assetContentType(path), data)
|
|
|
|
}
|
2015-05-03 04:32:16 +03:00
|
|
|
|
|
|
|
func serveResult(result interface{}, err error, c *gin.Context) {
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(400, NewError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, result)
|
|
|
|
}
|