2014-10-08 21:26:57 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-10-16 16:38:11 -05:00
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
2014-11-10 23:10:05 -06:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-30 11:47:07 -05:00
|
|
|
"github.com/sosedoff/pgweb/pkg/api"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/client"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/command"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/connection"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/util"
|
2014-10-08 21:26:57 -05:00
|
|
|
)
|
|
|
|
|
2015-04-13 09:03:30 -05:00
|
|
|
const VERSION = "0.5.2"
|
2014-10-13 21:12:19 -05:00
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
var options command.Options
|
2014-10-10 00:03:03 -05:00
|
|
|
|
2014-10-11 22:33:59 -05:00
|
|
|
func exitWithMessage(message string) {
|
|
|
|
fmt.Println("Error:", message)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-10-08 21:26:57 -05:00
|
|
|
func initClient() {
|
2015-04-30 11:47:07 -05:00
|
|
|
if connection.IsBlank(command.Opts) {
|
2014-11-01 15:44:24 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
cl, err := client.New()
|
2014-10-08 21:26:57 -05:00
|
|
|
if err != nil {
|
2014-10-11 22:33:59 -05:00
|
|
|
exitWithMessage(err.Error())
|
2014-10-08 21:26:57 -05:00
|
|
|
}
|
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
if command.Opts.Debug {
|
|
|
|
fmt.Println("Server connection string:", cl.ConnectionString)
|
2014-12-06 12:44:56 -06:00
|
|
|
}
|
|
|
|
|
2014-10-11 22:38:32 -05:00
|
|
|
fmt.Println("Connecting to server...")
|
2015-04-30 11:47:07 -05:00
|
|
|
err = cl.Test()
|
2014-10-10 00:25:06 -05:00
|
|
|
if err != nil {
|
2014-10-11 22:33:59 -05:00
|
|
|
exitWithMessage(err.Error())
|
2014-10-10 00:25:06 -05:00
|
|
|
}
|
|
|
|
|
2014-10-11 22:38:32 -05:00
|
|
|
fmt.Println("Checking tables...")
|
2015-04-30 11:47:07 -05:00
|
|
|
_, err = cl.Tables()
|
2014-10-11 22:32:10 -05:00
|
|
|
if err != nil {
|
2014-10-11 22:33:59 -05:00
|
|
|
exitWithMessage(err.Error())
|
2014-10-11 22:32:10 -05:00
|
|
|
}
|
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
api.DbClient = cl
|
2014-10-08 21:26:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func initOptions() {
|
2015-04-30 11:47:07 -05:00
|
|
|
err := command.ParseOptions()
|
2014-10-08 21:26:57 -05:00
|
|
|
if err != nil {
|
2014-10-10 17:20:14 -05:00
|
|
|
os.Exit(1)
|
2014-10-08 21:26:57 -05:00
|
|
|
}
|
2014-10-22 07:54:47 -06:00
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
options = command.Opts
|
2014-10-28 08:52:04 -05:00
|
|
|
|
2014-10-22 07:54:47 -06:00
|
|
|
if options.Version {
|
|
|
|
fmt.Printf("pgweb v%s\n", VERSION)
|
2014-10-27 15:49:43 -05:00
|
|
|
os.Exit(0)
|
2014-10-22 07:54:47 -06:00
|
|
|
}
|
2015-04-30 11:47:07 -05:00
|
|
|
|
|
|
|
fmt.Println("Pgweb version", VERSION)
|
2014-10-08 21:26:57 -05:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:38:11 -05:00
|
|
|
func startServer() {
|
2014-10-08 21:26:57 -05:00
|
|
|
router := gin.Default()
|
2014-10-09 19:05:51 -05:00
|
|
|
|
2014-10-29 19:45:12 -05:00
|
|
|
// Enable HTTP basic authentication only if both user and password are set
|
|
|
|
if options.AuthUser != "" && options.AuthPass != "" {
|
|
|
|
auth := map[string]string{options.AuthUser: options.AuthPass}
|
|
|
|
router.Use(gin.BasicAuth(auth))
|
|
|
|
}
|
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
api.SetupRoutes(router)
|
2014-10-09 19:05:51 -05:00
|
|
|
|
2014-10-13 18:40:17 -05:00
|
|
|
fmt.Println("Starting server...")
|
2015-02-09 01:40:51 -06:00
|
|
|
go func() {
|
|
|
|
err := router.Run(fmt.Sprintf("%v:%v", options.HttpHost, options.HttpPort))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Cant start server:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
2014-10-16 16:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSignals() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, os.Kill)
|
|
|
|
<-c
|
|
|
|
}
|
|
|
|
|
|
|
|
func openPage() {
|
2014-10-30 18:51:49 -05:00
|
|
|
url := fmt.Sprintf("http://%v:%v", options.HttpHost, options.HttpPort)
|
2014-10-26 11:47:15 -05:00
|
|
|
fmt.Println("To view database open", url, "in browser")
|
2014-10-16 16:38:11 -05:00
|
|
|
|
2014-10-30 18:27:37 -05:00
|
|
|
if options.SkipOpen {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-16 16:38:11 -05:00
|
|
|
_, err := exec.Command("which", "open").Output()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-26 11:47:15 -05:00
|
|
|
exec.Command("open", url).Output()
|
2014-10-16 16:38:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
initOptions()
|
|
|
|
initClient()
|
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
if api.DbClient != nil {
|
|
|
|
defer api.DbClient.Close()
|
2014-11-01 15:44:24 -05:00
|
|
|
}
|
2014-10-16 16:38:11 -05:00
|
|
|
|
|
|
|
if !options.Debug {
|
|
|
|
gin.SetMode("release")
|
|
|
|
}
|
|
|
|
|
2015-04-30 11:47:07 -05:00
|
|
|
// Print memory usage every 30 seconds with debug flag
|
2014-11-20 20:00:45 -06:00
|
|
|
if options.Debug {
|
2015-04-30 11:47:07 -05:00
|
|
|
util.StartProfiler()
|
2014-11-20 20:00:45 -06:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:38:11 -05:00
|
|
|
startServer()
|
|
|
|
openPage()
|
|
|
|
handleSignals()
|
2014-10-08 21:26:57 -05:00
|
|
|
}
|