Skip to content

Commit

Permalink
Merge pull request #24 from smurfpandey/save-connections
Browse files Browse the repository at this point in the history
[WIP] Save connection strings
  • Loading branch information
Neeraj committed May 12, 2015
2 parents fea4d9c + 26fd14a commit 35cb682
Show file tree
Hide file tree
Showing 13 changed files with 1,150 additions and 827 deletions.
59 changes: 59 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -433,6 +434,64 @@ func APIHandleQuery(query string, c *gin.Context) {
c.JSON(200, result)
}

func APIGetBookmarks(c *gin.Context) {
bookmarks, err := readBookmarks(getBookmarkPath())

if err != nil {
c.JSON(400, NewError(err))
return
}

c.JSON(200, bookmarks)

}

func APISaveBookmark(c *gin.Context) {
bookName := c.Params.ByName("name")

conHost := c.Request.FormValue("host")
strConPort := c.Request.FormValue("port")
intConPort, err := strconv.Atoi(strConPort)
if err != nil {
c.JSON(400, NewError(err))
return
}
conUser := c.Request.FormValue("user")
conDatabase := c.Request.FormValue("database")

objBookmark := Bookmark{
Name: bookName,
Connection: Connection{
Host: conHost,
Port: intConPort,
Username: conUser,
Database: conDatabase,
},
}

i, err := saveBookmark(objBookmark, getBookmarkPath())

if i == -1 {
c.JSON(400, NewError(errors.New("A connection with this name already exists")))
return
}

c.Writer.WriteHeader(204)
}

func APIDeleteBookmark(c *gin.Context) {
bookName := c.Params.ByName("name")

err := deleteBookmark(bookName, getBookmarkPath())

if err != nil {
c.JSON(400, NewError(err))
return
}

c.Writer.WriteHeader(204)
}

//APIServeAsset serves the static assets
func APIServeAsset(c *gin.Context) {
file := fmt.Sprintf(
Expand Down
129 changes: 129 additions & 0 deletions bookmark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/smurfpandey/go-homedir"
)

//Connection is a single saved connection-string object
type Connection struct {
Host string
Port int
Username string
Database string
}

type Bookmark struct {
Name string `json:"name"`
Connection Connection `json:"conn_info"`
}

type Bookmarks struct {
Bookmarks []Bookmark `json:"bookmarks"`
}

func fileBaseName(path string) string {
filename := filepath.Base(path)
return strings.Replace(filename, filepath.Ext(path), "", 1)
}

func getBookmarkPath() string {
path, _ := homedir.Dir()
bookmarkPath := fmt.Sprintf("%s/.mysqlweb/bookmark", path)

//Check if this path exists
isExists, _ := ExistsFileFolder(bookmarkPath)

//If no, then create directory
if !isExists {
os.MkdirAll(bookmarkPath, 0777)
}

return bookmarkPath
}

func readBookmarks(path string) (Bookmarks, error) {
results := Bookmarks{
Bookmarks: []Bookmark{},
}

files, err := ioutil.ReadDir(path)
if err != nil {
return results, err
}

for _, file := range files {
//We need .json files only
if filepath.Ext(file.Name()) != ".json" {
continue
}

fullpath := filepath.FromSlash(path + "/" + file.Name())
conName := fileBaseName(file.Name())

data, err := ioutil.ReadFile(fullpath)

if err != nil {
return results, err
}

thisConn := Connection{}

json.Unmarshal(data, &thisConn)

thisBookmark := Bookmark{
Name: conName,
Connection: thisConn,
}

results.Bookmarks = append(results.Bookmarks, thisBookmark)

}

return results, nil
}

func saveBookmark(objBookmark Bookmark, path string) (int, error) {
fileName := objBookmark.Name + ".json"

data, jerr := json.MarshalIndent(objBookmark.Connection, "", " ")
if jerr != nil {
return 0, jerr
}

fullFilePath := filepath.FromSlash(path + "/" + fileName)

// equivalent to Python's `if not os.path.exists(filename)`
if _, err := os.Stat(fullFilePath); err == nil {
//fmt.Printf("file exists; processing...")
return -1, nil
}

filErr := ioutil.WriteFile(fullFilePath, data, 0644)
if filErr != nil {
return 0, filErr
}

return 1, nil
}

func deleteBookmark(bookmarkName string, path string) error {

fileName := bookmarkName + ".json"

fullFilePath := filepath.FromSlash(path + "/" + fileName)

err := os.Remove(fullFilePath)

if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
go-bindata -ignore=\\.gitignore -ignore=\\.DS_Store -ignore=\\.gitkeep static/...
go-bindata -debug -ignore=\\.gitignore -ignore=\\.DS_Store -ignore=\\.gitkeep static/...
go build
mysqlweb.exe
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ func startServer() {
router.DELETE("/databases/:database/procedures/:procedure/actions/drop", APIDropProcedure)
router.GET("/databases/:database/views/:view", APIViewDefinition)
router.GET("/search/:query", APISearch)
router.GET("/bookmarks", APIGetBookmarks)
router.POST("/bookmarks/:name", APISaveBookmark)
router.DELETE("/bookmarks/:name", APIDeleteBookmark)

fmt.Println("Starting server...")
go router.Run(fmt.Sprintf("%v:%v", options.HttpHost, options.HttpPort))
Expand Down
3 changes: 2 additions & 1 deletion release.bat
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
gox -osarch="darwin/amd64 darwin/386 linux/amd64 linux/386 windows/amd64 windows/386" -output="./bin/mysqlweb_{{.OS}}_{{.Arch}}"
go-bindata -ignore=\\.gitignore -ignore=\\.DS_Store -ignore=\\.gitkeep static/...
gox -osarch="darwin/amd64 darwin/386 linux/amd64 linux/386 windows/amd64 windows/386" -output="./bin/mysqlweb_{{.OS}}_{{.Arch}}"
12 changes: 12 additions & 0 deletions static/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,15 @@ strong.form-control {
#sidebar .search{
margin: 5px;
}

.bookmark-list-item .delete-bookmark{
visibility: hidden;
}

.bookmark-list-item:hover .delete-bookmark{
visibility: visible;
}

.delete-bookmark{
font-size: 30px;
}
1 change: 0 additions & 1 deletion static/css/sweet-alert.css

This file was deleted.

Loading

0 comments on commit 35cb682

Please sign in to comment.