Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Update doc.go
Browse files Browse the repository at this point in the history
* Updated README. Badge added for LICENSE.
* addr in Kvs struct type made exported variable.
* Doc comments added for struct types
  • Loading branch information
gozeloglu committed Oct 16, 2021
1 parent 515156e commit 82ac1b1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# kvs
# kvs ![LICENSE](https://img.shields.io/badge/license-MIT-green)


## Installation

Expand Down
10 changes: 6 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"time"
)

// Kvs keeps the essential variables.
type Kvs struct {

// name is database name.
name string

Expand All @@ -26,8 +28,8 @@ type Kvs struct {
// mu is mutex for avoiding conflicts.
mu sync.Mutex

// addr is the server address.
addr string
// Addr is the server address.
Addr string

// duration stands for the time interval to save data into file periodically.
duration time.Duration
Expand Down Expand Up @@ -65,7 +67,7 @@ func open(name string, addr string, duration time.Duration) (*Kvs, error) {
dbFile: dbFile,
kv: m,
mu: sync.Mutex{},
addr: addr,
Addr: addr,
duration: duration,
}

Expand Down Expand Up @@ -124,7 +126,7 @@ func openAndLoad(dbName string, addr string, duration time.Duration) (*Kvs, erro
dir: fullPath,
dbFile: dbFile,
mu: sync.Mutex{},
addr: addr,
Addr: addr,
duration: duration,
}
err = k.load()
Expand Down
9 changes: 9 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ You can call endpoints like this:
http://localhost:1234/save --> PUT
With /set endpoint, you can add key-value pair(s) in memory, map. Multiple
key-value pairs are allowed in "data" array.
With /get/<key> endpoint, you can get the value of <key>. It returns a JSON
object that stores key, value, and result.
With /save endpoint, you can save the data that stores in memory to the disk.
It returns result message.
Default port is assigned as :1234 for this server. Actually, it is not used for
package usage, only is used for server usage.
Expand Down
7 changes: 5 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import (
"time"
)

// KeyValue is used for unmarshalling JSON object in POST request.
type KeyValue struct {
Data []Data `json:"data"`
}

// Data is the element of array. It keeps Key and Value.
type Data struct {
Key string `json:"key"`
Value string `json:"value"`
}

// Response is struct type for JSON response body. It is used for get and save.
type Response struct {
Key string `json:"key"`
Value string `json:"value"`
Expand Down Expand Up @@ -50,11 +53,11 @@ func Create(addr string, dbName string, duration time.Duration) (*Kvs, error) {
// Open creates an HTTP connection. HTTP connection listens HTTP requests from
// client. Create function needs to be called before calling Open function.
func (k *Kvs) Open() {
log.Printf("Kvs server running on %s...", k.addr)
log.Printf("Kvs server running on %s...", k.Addr)
http.HandleFunc("/set", k.set)
http.HandleFunc("/get/", k.get)
http.HandleFunc("/save", k.save)
log.Fatal(http.ListenAndServe(k.addr, nil))
log.Fatal(http.ListenAndServe(k.Addr, nil))
}

// set is the /set API endpoint for setting a key-value pair.
Expand Down

0 comments on commit 82ac1b1

Please sign in to comment.