2014-10-09 06:26:57 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/jessevdk/go-flags"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2014-10-14 06:12:19 +04:00
|
|
|
const VERSION = "0.1.0"
|
|
|
|
|
2014-10-09 06:26:57 +04:00
|
|
|
var options struct {
|
2014-10-14 03:47:16 +04:00
|
|
|
Debug bool `short:"d" description:"Enable debugging mode" default:"false"`
|
2014-10-10 07:42:52 +04:00
|
|
|
Url string `long:"url" description:"Database connection string"`
|
2014-10-12 07:38:32 +04:00
|
|
|
Host string `long:"host" description:"Server hostname or IP" default:"localhost"`
|
|
|
|
Port int `long:"port" description:"Server port" default:"5432"`
|
|
|
|
User string `long:"user" description:"Database user" default:"postgres"`
|
|
|
|
DbName string `long:"db" description:"Database name" default:"postgres"`
|
2014-10-10 07:42:52 +04:00
|
|
|
Ssl string `long:"ssl" description:"SSL option" default:"disable"`
|
2014-10-09 06:26:57 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
var dbClient *Client
|
2014-10-10 09:03:03 +04:00
|
|
|
|
2014-10-12 07:33:59 +04:00
|
|
|
func exitWithMessage(message string) {
|
|
|
|
fmt.Println("Error:", message)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-10-09 06:26:57 +04:00
|
|
|
func getConnectionString() string {
|
2014-10-10 07:42:52 +04:00
|
|
|
if options.Url != "" {
|
|
|
|
return options.Url
|
|
|
|
}
|
|
|
|
|
2014-10-09 06:26:57 +04:00
|
|
|
return fmt.Sprintf(
|
|
|
|
"host=%s port=%d user=%s dbname=%s sslmode=disable",
|
|
|
|
options.Host, options.Port,
|
|
|
|
options.User, options.DbName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initClient() {
|
|
|
|
client, err := NewClient()
|
|
|
|
if err != nil {
|
2014-10-12 07:33:59 +04:00
|
|
|
exitWithMessage(err.Error())
|
2014-10-09 06:26:57 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 07:38:32 +04:00
|
|
|
fmt.Println("Connecting to server...")
|
|
|
|
err = client.Test()
|
2014-10-10 09:25:06 +04:00
|
|
|
if err != nil {
|
2014-10-12 07:33:59 +04:00
|
|
|
exitWithMessage(err.Error())
|
2014-10-10 09:25:06 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 07:38:32 +04:00
|
|
|
fmt.Println("Checking tables...")
|
2014-10-12 07:32:10 +04:00
|
|
|
tables, err := client.Tables()
|
|
|
|
if err != nil {
|
2014-10-12 07:33:59 +04:00
|
|
|
exitWithMessage(err.Error())
|
2014-10-12 07:32:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tables) == 0 {
|
2014-10-12 07:33:59 +04:00
|
|
|
exitWithMessage("Database does not have any tables")
|
2014-10-12 07:32:10 +04:00
|
|
|
}
|
|
|
|
|
2014-10-09 06:26:57 +04:00
|
|
|
dbClient = client
|
|
|
|
}
|
|
|
|
|
|
|
|
func initOptions() {
|
|
|
|
_, err := flags.ParseArgs(&options, os.Args)
|
|
|
|
|
|
|
|
if err != nil {
|
2014-10-11 02:20:14 +04:00
|
|
|
os.Exit(1)
|
2014-10-09 06:26:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
initOptions()
|
|
|
|
initClient()
|
2014-10-11 02:14:17 +04:00
|
|
|
|
2014-10-09 06:26:57 +04:00
|
|
|
defer dbClient.db.Close()
|
|
|
|
|
2014-10-14 03:47:16 +04:00
|
|
|
if !options.Debug {
|
|
|
|
gin.SetMode("release")
|
|
|
|
}
|
|
|
|
|
2014-10-09 06:26:57 +04:00
|
|
|
router := gin.Default()
|
2014-10-10 04:05:51 +04:00
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
router.GET("/", API_Home)
|
2014-10-16 01:05:23 +04:00
|
|
|
router.GET("/databases", API_GetDatabases)
|
2014-10-10 09:03:03 +04:00
|
|
|
router.GET("/info", API_Info)
|
2014-10-09 06:26:57 +04:00
|
|
|
router.GET("/tables", API_GetTables)
|
2014-10-11 22:20:16 +04:00
|
|
|
router.GET("/tables/:table", API_GetTable)
|
|
|
|
router.GET("/tables/:table/indexes", API_TableIndexes)
|
2014-10-11 02:14:17 +04:00
|
|
|
router.GET("/query", API_RunQuery)
|
|
|
|
router.POST("/query", API_RunQuery)
|
2014-10-11 22:24:12 +04:00
|
|
|
router.GET("/explain", API_ExplainQuery)
|
|
|
|
router.POST("/explain", API_ExplainQuery)
|
2014-10-10 04:59:18 +04:00
|
|
|
router.GET("/history", API_History)
|
2014-10-13 23:40:56 +04:00
|
|
|
router.GET("/static/:type/:name", API_ServeAsset)
|
2014-10-10 04:05:51 +04:00
|
|
|
|
2014-10-14 03:40:17 +04:00
|
|
|
fmt.Println("Starting server...")
|
|
|
|
fmt.Println("Once started you can view application at http://localhost:8080")
|
2014-10-09 06:26:57 +04:00
|
|
|
router.Run(":8080")
|
|
|
|
}
|