Skip to content

Commit

Permalink
Merge pull request #12 from ric-v/dev
Browse files Browse the repository at this point in the history
add new key-value done
  • Loading branch information
ric-v committed Feb 9, 2022
2 parents fbead61 + 05fb886 commit 9cf711c
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 179 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v.0.0.1
v.0.0.3
98 changes: 49 additions & 49 deletions database/db.go → database/database.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
package database

import "errors"

const (
// BOLT_DB - BoltDB
BOLT_DB = "boltdb"
// BUNT_DB - BuntDB
BUNT_DB = "buntdb"
)

type KeyValuePair struct {
Key string
Value string
}

// DB interface for underlying database packages
type DB interface {
Add(string, string, ...interface{}) error
CloseDB()
Get(key string) (string, error) // TODO: add list all keys
Delete(string) error
List(args ...interface{}) ([]KeyValuePair, error)
}

// Buckets interface for underlying bolt DB buckets
type Buckets interface {
Add()
Get()
List()
Delete()
}

// DBType for identifying underlying database packages
type DBType string

// NewDB godoc - creates a new DB instance abstracting the underlying database package
func NewDB(fileName string, dbtype string) (DB, error) {

// TODO: Add support for other database packages
switch dbtype {
case BOLT_DB:
return openBolt(fileName)
case BUNT_DB:
return openBunt(fileName)
default:
return nil, errors.New("invalid database type")
}
}
package database

import "errors"

const (
// BOLT_DB - BoltDB
BOLT_DB = "boltdb"
// BUNT_DB - BuntDB
BUNT_DB = "buntdb"
)

type KeyValuePair struct {
Key string
Value string
}

// DB interface for underlying database packages
type DB interface {
Add(string, string, ...interface{}) error
CloseDB()
Get(key string) (string, error) // TODO: add list all keys
Delete(string) error
List(args ...interface{}) ([]KeyValuePair, error)
}

// Buckets interface for underlying bolt DB buckets
type Buckets interface {
Add()
Get()
List()
Delete()
}

// DBType for identifying underlying database packages
type DBType string

// NewDB godoc - creates a new DB instance abstracting the underlying database package
func NewDB(fileName string, dbtype string) (DB, error) {

// TODO: Add support for other database packages
switch dbtype {
case BOLT_DB:
return openBolt(fileName)
case BUNT_DB:
return openBunt(fileName)
default:
return nil, errors.New("invalid database type")
}
}
File renamed without changes.
103 changes: 12 additions & 91 deletions server/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/google/uuid"
"github.com/ric-v/divulge-keyvalue-db-ui/database"
boltdb "github.com/ric-v/golang-key-value-db-browser/bolt-db"
"github.com/valyala/fasthttp"
)

Expand Down Expand Up @@ -157,6 +156,7 @@ func loadFile(ctx *fasthttp.RequestCtx) {
ctx.Error("Error reading db folder: "+err.Error(), fasthttp.StatusInternalServerError)
return
}
log.Println("dbTypes:", dbTypes)

// iterate over files
for _, dbType := range dbTypes {
Expand All @@ -168,9 +168,11 @@ func loadFile(ctx *fasthttp.RequestCtx) {
ctx.Error("Error reading db folder: "+err.Error(), fasthttp.StatusInternalServerError)
return
}
log.Println("dbKeys:", dbKeys)

// iterate over files
for _, dbkey := range dbKeys {
log.Println("dbkey:", dbkey.Name(), " | dbKey:", dbkey)

if dbkey.Name() == dbKey {

Expand All @@ -185,6 +187,8 @@ func loadFile(ctx *fasthttp.RequestCtx) {
// get the file name
file := files[0].Name()
userSession = Session{dbKey, file, dbType.Name(), nil}
log.Println("userSession: ", userSession)
session.Store(dbKey, userSession)
}
}
}
Expand Down Expand Up @@ -414,7 +418,7 @@ func insertKeyValue(ctx *fasthttp.RequestCtx) {
}

// get the dbKey from params
dbKey := string(ctx.UserValue("dbKey").(string))
dbKey := string(ctx.QueryArgs().Peek("dbkey"))
log.Println("dbKey:", dbKey)

// load the db from user session
Expand All @@ -434,32 +438,13 @@ func insertKeyValue(ctx *fasthttp.RequestCtx) {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
fmt.Println("data:", data)

switch sessionInfo.DBType {

case database.BOLT_DB:

// get the DB type from params
bucket := string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// open the boltdb file from temp dir
db, err := boltdb.New("temp" + string(os.PathSeparator) + dbKey + string(os.PathSeparator) + sessionInfo.FileName)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
defer db.Close()

// add the key-value pair to the boltdb file
err = db.Add([]byte(bucket), []byte(data.Key), []byte(data.Value))
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
// add new entry to DB
err = sessionInfo.DB.Add(data.Key, data.Value)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

// return success message to UI
Expand Down Expand Up @@ -497,22 +482,6 @@ func insertBucket(ctx *fasthttp.RequestCtx) {
bucket := string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// open the boltdb file from temp dir
db, err := boltdb.New("temp" + string(os.PathSeparator) + dbKey + string(os.PathSeparator) + sessionInfo.FileName)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
defer db.Close()

// remove the bucket from the boltdb file
err = db.AddBucket([]byte(bucket))
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
}

// return success message to UI
Expand Down Expand Up @@ -550,22 +519,6 @@ func deleteBucket(ctx *fasthttp.RequestCtx) {
bucket := string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// open the boltdb file from temp dir
db, err := boltdb.New("temp" + string(os.PathSeparator) + dbKey + string(os.PathSeparator) + sessionInfo.FileName)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
defer db.Close()

// remove the bucket from the boltdb file
err = db.RemoveBucket([]byte(bucket))
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
}

// return success message to UI
Expand Down Expand Up @@ -608,22 +561,6 @@ func deleteKeyValue(ctx *fasthttp.RequestCtx) {
bucket := string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// open the boltdb file from temp dir
db, err := boltdb.New("temp" + string(os.PathSeparator) + dbKey + string(os.PathSeparator) + sessionInfo.FileName)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
defer db.Close()

// delete the key from the boltdb file
err = db.Delete([]byte(bucket), []byte(key))
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
}

// return success message to UI
Expand Down Expand Up @@ -675,22 +612,6 @@ func updateKeyValue(ctx *fasthttp.RequestCtx) {
bucket := string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// open the boltdb file from temp dir
db, err := boltdb.New("temp" + string(os.PathSeparator) + dbKey + string(os.PathSeparator) + sessionInfo.FileName)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
defer db.Close()

// update the key in the boltdb file
err = db.Update([]byte(bucket), []byte(key), []byte(value))
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
}

// return success message to UI
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Serve(port string, debug bool) {

// create a new router
r := router.New()
r.HandleOPTIONS = true

// /api/v1/ routes
v1 := r.Group("/api/v1")
Expand All @@ -34,6 +35,7 @@ func Serve(port string, debug bool) {
// create new database file
v1.POST("/new", newFile)

// load existing file from browser cache
v1.POST("/load", loadFile)

// close the boltdb file and remove the entries
Expand Down Expand Up @@ -100,5 +102,6 @@ func Serve(port string, debug bool) {
LogAllErrors: true,
}
// serve the handlers on the router
log.Println("starting server on port:", port)
log.Fatal(server.ListenAndServe(":" + port))
}
3 changes: 2 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
"last 1 firefox version",
"last 1 safari version"
]
}
},
"proxy": "http://127.0.0.1:8080"
}
Loading

0 comments on commit 9cf711c

Please sign in to comment.