pgweb/main.go

143 lines
2.5 KiB
Go
Raw Normal View History

2014-10-09 06:26:57 +04:00
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
2014-11-11 08:10:05 +03:00
"github.com/gin-gonic/gin"
2015-04-30 19:47:07 +03: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-09 06:26:57 +04:00
)
2015-04-30 19:47:07 +03:00
var options command.Options
2014-10-10 09:03:03 +04:00
func exitWithMessage(message string) {
fmt.Println("Error:", message)
os.Exit(1)
}
2014-10-09 06:26:57 +04:00
func initClient() {
2015-04-30 19:47:07 +03:00
if connection.IsBlank(command.Opts) {
return
}
2015-04-30 19:47:07 +03:00
cl, err := client.New()
2014-10-09 06:26:57 +04:00
if err != nil {
exitWithMessage(err.Error())
2014-10-09 06:26:57 +04:00
}
2015-04-30 19:47:07 +03:00
if command.Opts.Debug {
fmt.Println("Server connection string:", cl.ConnectionString)
}
2014-10-12 07:38:32 +04:00
fmt.Println("Connecting to server...")
2015-04-30 19:47:07 +03:00
err = cl.Test()
if err != nil {
exitWithMessage(err.Error())
}
2016-01-13 06:33:44 +03:00
fmt.Println("Checking database objects...")
_, err = cl.Objects()
if err != nil {
exitWithMessage(err.Error())
}
2015-04-30 19:47:07 +03:00
api.DbClient = cl
2014-10-09 06:26:57 +04:00
}
func initOptions() {
2015-04-30 19:47:07 +03:00
err := command.ParseOptions()
2014-10-09 06:26:57 +04:00
if err != nil {
2014-10-11 02:20:14 +04:00
os.Exit(1)
2014-10-09 06:26:57 +04:00
}
2014-10-22 17:54:47 +04:00
2015-04-30 19:47:07 +03:00
options = command.Opts
2014-10-22 17:54:47 +04:00
if options.Version {
printVersion()
2014-10-27 23:49:43 +03:00
os.Exit(0)
2014-10-22 17:54:47 +04:00
}
2015-04-30 19:47:07 +03:00
printVersion()
}
func printVersion() {
str := fmt.Sprintf("Pgweb v%s", command.VERSION)
if command.GitCommit != "" {
str += fmt.Sprintf(" (git: %s)", command.GitCommit)
}
fmt.Println(str)
2014-10-09 06:26:57 +04:00
}
func startServer() {
2014-10-09 06:26:57 +04:00
router := gin.Default()
2014-10-10 04:05:51 +04:00
2014-10-30 03:45:12 +03: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 19:47:07 +03:00
api.SetupRoutes(router)
2014-10-10 04:05:51 +04:00
2014-10-14 03:40:17 +04:00
fmt.Println("Starting server...")
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)
}
}()
}
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
}
func openPage() {
url := fmt.Sprintf("http://%v:%v/%s", options.HttpHost, options.HttpPort, options.Prefix)
2014-10-26 19:47:15 +03:00
fmt.Println("To view database open", url, "in browser")
if options.SkipOpen {
return
}
_, err := exec.Command("which", "open").Output()
if err != nil {
return
}
2014-10-26 19:47:15 +03:00
exec.Command("open", url).Output()
}
func main() {
initOptions()
initClient()
2015-04-30 19:47:07 +03:00
if api.DbClient != nil {
defer api.DbClient.Close()
}
if !options.Debug {
gin.SetMode("release")
}
2015-04-30 19:47:07 +03:00
// Print memory usage every 30 seconds with debug flag
if options.Debug {
2015-04-30 19:47:07 +03:00
util.StartProfiler()
}
startServer()
openPage()
handleSignals()
2014-10-09 06:26:57 +04:00
}