forked from smurfpandey/mysqlweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
226 lines (185 loc) · 6.03 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"os/user"
"strings"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jessevdk/go-flags"
)
//Current version of the app
const VERSION = "0.6.1"
var options struct {
Version bool `short:"v" long:"version" description:"Print version"`
Debug bool `short:"d" long:"debug" description:"Enable debugging mode" default:"false"`
Url string `long:"url" description:"Database connection string"`
Host string `long:"host" description:"Server hostname or IP"`
Port int `long:"port" description:"Server port" default:"5432"`
User string `long:"user" description:"Database user"`
Pass string `long:"pass" description:"Password for user"`
DbName string `long:"db" description:"Database name"`
Ssl string `long:"ssl" description:"SSL option"`
HttpHost string `long:"bind" description:"HTTP server host" default:"localhost"`
HttpPort uint `long:"listen" description:"HTTP server listen port" default:"8080"`
AuthUser string `long:"auth-user" description:"HTTP basic auth user"`
AuthPass string `long:"auth-pass" description:"HTTP basic auth password"`
SkipOpen bool `short:"s" long:"skip-open" description:"Skip browser open on start"`
}
var dbClient *Client
func exitWithMessage(message string) {
fmt.Println("Error:", message)
os.Exit(1)
}
func getConnectionString() string {
if options.Url != "" {
url := options.Url
if strings.Contains(url, "postgresql://") {
fmt.Println("Invalid URL format. It should match: postgres://user:password@host:port/db?sslmode=mode")
os.Exit(1)
}
// Append sslmode parameter only if its defined as a flag and not present
// in the connection string.
if options.Ssl != "" && !strings.Contains(url, "sslmode") {
url += fmt.Sprintf("?sslmode=%s", options.Ssl)
}
return url
}
// Try to detect user from current OS user
if options.User == "" {
user, err := user.Current()
if err == nil {
options.User = user.Username
}
}
str := fmt.Sprintf(
"host=%s port=%d user=%s dbname=%s",
options.Host, options.Port,
options.User, options.DbName,
)
if options.Ssl == "" {
// Disable ssl for localhost connections, most users have it disabled
if options.Host == "localhost" || options.Host == "127.0.0.1" {
options.Ssl = "disable"
}
}
if options.Ssl != "" {
str += fmt.Sprintf(" sslmode=%s", options.Ssl)
}
if options.Pass != "" {
str += fmt.Sprintf(" password=%s", options.Pass)
}
return str
}
func connectionSettingsBlank() bool {
return options.Host == "" &&
options.User == "" &&
options.DbName == "" &&
options.Url == ""
}
func initClient() {
if connectionSettingsBlank() {
return
}
client, err := NewClient()
if err != nil {
exitWithMessage(err.Error())
}
fmt.Println("Connecting to server...")
err = client.Test()
if err != nil {
exitWithMessage(err.Error())
}
dbClient = client
}
func initOptions() {
_, err := flags.ParseArgs(&options, os.Args)
if err != nil {
os.Exit(1)
}
if options.Url == "" {
options.Url = os.Getenv("DATABASE_URL")
}
if options.Version {
fmt.Printf("pgweb v%s\n", VERSION)
os.Exit(0)
}
}
func startServer() {
router := gin.Default()
// 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))
}
router.GET("/", APIHome)
router.POST("/connect", APIConnect)
router.DELETE("/disconnect", APIClose)
router.GET("/databases", APIGetDatabases)
router.GET("/databases/:database/tables", APIGetDatabaseTables)
router.GET("/databases/:database/tables/:table/column", APIGetColumnOfTable)
router.GET("/databases/:database/views", APIGetDatabaseViews)
router.GET("/databases/:database/procedures", APIGetDatabaseProcedures)
router.GET("/databases/:database/functions", APIGetDatabaseFunctions)
router.POST("/databases/:database/actions/default", APISetDefaultDatabase)
router.GET("/info", APIInfo)
router.GET("/tables/:table/info", APIGetTableInfo)
router.GET("/tables/:table/indexes", APITableIndexes)
router.GET("/query", APIRunQuery)
router.POST("/query", APIRunQuery)
router.GET("/explain", APIExplainQuery)
router.POST("/explain", APIExplainQuery)
router.GET("/history", APIHistory)
router.GET("/static/:type/:name", APIServeAsset)
router.GET("/procedures/:procedure/parameters", APIProcedureParameters)
router.GET("/collation", APIGetCollationCharSet)
router.POST("/databases/:database/actions/alter", APIAlterDatabase)
router.DELETE("/databases/:database/actions/drop", APIDropDatabase)
router.DELETE("/databases/:database/tables/:table/actions/drop", APIDropTable)
router.DELETE("/databases/:database/tables/:table/actions/truncate", APITruncateTable)
router.GET("/databases/:database/procedures/:procedure", APIProcedureDefinition)
router.GET("/databases/:database/functions/:function", APIFunctionDefinition)
router.POST("/databases/:database/procedures/:procedure", APICreateProcedure)
router.POST("/databases/:database/functions/:function", APICreateFunction)
router.DELETE("/databases/:database/procedures/:procedure/actions/drop", APIDropProcedure)
router.GET("/databases/:database/views/:view", APIViewDefinition)
router.GET("/search/:query", APISearch)
fmt.Println("Starting server...")
go router.Run(fmt.Sprintf("%v:%v", options.HttpHost, options.HttpPort))
}
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)
fmt.Println("To view database open", url, "in browser")
if options.SkipOpen {
return
}
_, err := exec.Command("which", "open").Output()
if err != nil {
return
}
exec.Command("open", url).Output()
}
func main() {
initOptions()
fmt.Println("mysqlweb version", VERSION)
initClient()
if dbClient != nil {
defer dbClient.db.Close()
}
if !options.Debug {
gin.SetMode("release")
}
if options.Debug {
go startRuntimeProfiler()
}
startServer()
openPage()
handleSignals()
}