forked from sosedoff/pgweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (110 loc) · 2.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
_ "github.com/lib/pq"
"os"
"os/exec"
"os/signal"
)
const VERSION = "0.2.0"
var options struct {
Version bool `short:"v" description:"Print version"`
Debug bool `short:"d" description:"Enable debugging mode" default:"false"`
Url string `long:"url" description:"Database connection string"`
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"`
Ssl string `long:"ssl" description:"SSL option" default:"disable"`
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8080"`
}
var dbClient *Client
func exitWithMessage(message string) {
fmt.Println("Error:", message)
os.Exit(1)
}
func getConnectionString() string {
if options.Url != "" {
return options.Url
}
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 {
exitWithMessage(err.Error())
}
fmt.Println("Connecting to server...")
err = client.Test()
if err != nil {
exitWithMessage(err.Error())
}
fmt.Println("Checking tables...")
tables, err := client.Tables()
if err != nil {
exitWithMessage(err.Error())
}
if len(tables) == 0 {
exitWithMessage("Database does not have any tables")
}
dbClient = client
}
func initOptions() {
_, err := flags.ParseArgs(&options, os.Args)
if err != nil {
os.Exit(1)
}
if options.Version {
fmt.Printf("pgweb v%s\n", VERSION)
os.Exit(1)
}
}
func startServer() {
router := gin.Default()
router.GET("/", API_Home)
router.GET("/databases", API_GetDatabases)
router.GET("/info", API_Info)
router.GET("/tables", API_GetTables)
router.GET("/tables/:table", API_GetTable)
router.GET("/tables/:table/info", API_GetTableInfo)
router.GET("/tables/:table/indexes", API_TableIndexes)
router.GET("/query", API_RunQuery)
router.POST("/query", API_RunQuery)
router.GET("/explain", API_ExplainQuery)
router.POST("/explain", API_ExplainQuery)
router.GET("/history", API_History)
router.GET("/static/:type/:name", API_ServeAsset)
fmt.Println("Starting server...")
go router.Run(fmt.Sprintf(":%v", options.HttpPort))
}
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
}
func openPage() {
url := fmt.Sprintf("http://localhost:%v", options.HttpPort)
fmt.Println("To view database open", url, "in browser")
_, err := exec.Command("which", "open").Output()
if err != nil {
return
}
exec.Command("open", url).Output()
}
func main() {
initOptions()
initClient()
defer dbClient.db.Close()
if !options.Debug {
gin.SetMode("release")
}
startServer()
openPage()
handleSignals()
}