pgweb/main.go

147 lines
2.5 KiB
Go
Raw Normal View History

2014-10-08 21:26:57 -05:00
package main
import (
"fmt"
"os"
"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
// The git commit that was compiled. This will be filled in by the compiler.
var GitCommit string
2015-04-30 11:47:07 -05:00
var options command.Options
2014-10-10 00:03:03 -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) {
return
}
2015-04-30 11:47:07 -05:00
cl, err := client.New()
2014-10-08 21:26:57 -05:00
if err != nil {
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-10-11 22:38:32 -05:00
fmt.Println("Connecting to server...")
2015-04-30 11:47:07 -05:00
err = cl.Test()
if err != nil {
exitWithMessage(err.Error())
}
2014-10-11 22:38:32 -05:00
fmt.Println("Checking tables...")
2015-04-30 11:47:07 -05:00
_, err = cl.Tables()
if err != nil {
exitWithMessage(err.Error())
}
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-22 07:54:47 -06:00
if options.Version {
printVersion()
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
printVersion()
}
func printVersion() {
str := fmt.Sprintf("Pgweb v%s", VERSION)
if GitCommit != "" {
str += fmt.Sprintf(" (git: %s)", GitCommit)
}
fmt.Println(str)
2014-10-08 21:26:57 -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...")
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", options.HttpHost, options.HttpPort)
2014-10-26 11:47:15 -05: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 11:47:15 -05:00
exec.Command("open", url).Output()
}
func main() {
initOptions()
initClient()
2015-04-30 11:47:07 -05:00
if api.DbClient != nil {
defer api.DbClient.Close()
}
if !options.Debug {
gin.SetMode("release")
}
2015-04-30 11:47:07 -05:00
// Print memory usage every 30 seconds with debug flag
if options.Debug {
2015-04-30 11:47:07 -05:00
util.StartProfiler()
}
startServer()
openPage()
handleSignals()
2014-10-08 21:26:57 -05:00
}