From 09765dea424a8bc25087fac54a379e49afeb0de8 Mon Sep 17 00:00:00 2001 From: Neeraj Date: Tue, 3 Mar 2015 00:18:42 +0530 Subject: [PATCH] Save host & user when connected. Fixes #20 --- api.go | 7 ++++++- client.go | 14 +++++++------- static/js/app.js | 11 +++++++---- utils.go | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/api.go b/api.go index 12314cd..e4b45b6 100644 --- a/api.go +++ b/api.go @@ -227,7 +227,12 @@ func APIInfo(c *gin.Context) { return } - c.JSON(200, res.Format()[0]) + formatedRes := res.Format()[0] + + formatedRes["host"] = dbClient.host + formatedRes["user"] = dbClient.user + + c.JSON(200, formatedRes) } //APITableIndexes returns the indexs of a table diff --git a/client.go b/client.go index 8fac8f3..783befd 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,8 @@ import ( type Client struct { db *sqlx.DB history []string + host string + user string } //Row will hold rows of our SQL table @@ -26,13 +28,9 @@ type Result struct { //NewClient will create a new client func NewClient() (*Client, error) { - db, err := sqlx.Open("mysql", getConnectionString()) + url := getConnectionString() - if err != nil { - return nil, err - } - - return &Client{db: db}, nil + return NewClientFromURL(url) } //NewClientFromURL will create a new mysql client using the URL provided in parameters @@ -44,7 +42,9 @@ func NewClientFromURL(url string) (*Client, error) { return nil, err } - return &Client{db: db}, nil + user, host := getHostUser(url) + + return &Client{db: db, host: host, user: user}, nil } //Close disconnects a existing connection diff --git a/static/js/app.js b/static/js/app.js index a03c881..104b31a 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -509,9 +509,9 @@ var fnSetDefaultDatabase = function(dbName) { }); }; -function fnSetConnectionInfo() { - var currentServer = $('#pg_host').val(); - var currentUser = $('#pg_user').val(); +function fnSetConnectionInfo(user, host) { + var currentServer = host || $('#pg_host').val(); + var currentUser = user || $('#pg_user').val(); $('#current-server').text(currentUser + '@' + currentServer); } @@ -1509,7 +1509,10 @@ $(document).ready(function() { loadDatabases(); $("#main").show(); - fnSetConnectionInfo(); + var hostName = resp.host; + var userName = resp.user; + + fnSetConnectionInfo(userName, hostName); } }); }); diff --git a/utils.go b/utils.go index b355a3d..22da316 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package main import ( "fmt" "runtime" + "strings" "time" ) @@ -26,3 +27,18 @@ func startRuntimeProfiler() { func splice(s string, idx int, rem int, sAdd string) string { return (s[0:idx] + sAdd + s[(idx+rem):len(s)]) } + +func getHostUser(url string) (string, string) { + colonIndx := strings.Index(url, ":") + userName := url[0:colonIndx] + + hostStart := strings.Index(url, "tcp(") + 4 + hostEnd := strings.Index(url, ")/") + + hostName := url[hostStart:hostEnd] + + colonIndx = strings.Index(hostName, ":") + hostName = hostName[0:colonIndx] + + return userName, hostName +}