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

Commit

Permalink
Add get endpoint
Browse files Browse the repository at this point in the history
Content-Type and application/json made constant variables
  • Loading branch information
gozeloglu committed Oct 16, 2021
1 parent 89f3567 commit 4fd3bd7
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
)

type KeyValue struct {
Expand All @@ -23,6 +24,11 @@ type Response struct {
Result string `json:"result"`
}

const (
headerContent = "Content-Type"
contentValue = "application/json"
)

// Create creates database and Kvs object. It creates database and returns Kvs
// object. If HTTP address is empty, localhost and default port is used.
// In contrast, dbName name needs to be specified. If it is not specified, it
Expand All @@ -42,7 +48,7 @@ func Create(addr string, dbName string) (*Kvs, error) {
func (k *Kvs) Open() {
log.Printf("Kvs server running on %s...", k.addr)
http.HandleFunc("/set", k.set)
http.HandleFunc("/get", get)
http.HandleFunc("/get/", k.get)
log.Fatal(http.ListenAndServe(k.addr, nil))
}

Expand Down Expand Up @@ -86,12 +92,47 @@ func (k *Kvs) set(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set(headerContent, contentValue)
w.WriteHeader(http.StatusOK)
w.Write(j)
log.Printf("Key-value pair is set.")
}

func get(w http.ResponseWriter, r *http.Request) {
// TODO Handle get operation.
// get returns the value of the key.
func (k *Kvs) get(w http.ResponseWriter, r *http.Request) {
k.mu.Lock()
defer k.mu.Unlock()

if r.Method != http.MethodGet {
err := fmt.Sprintf("Wrong HTTP request. You need to send GET request.")
log.Printf(err)
http.Error(w, err, http.StatusBadRequest)
return
}

u := strings.Split(r.URL.String(), "/")
if u[len(u)-1] == "" && u[len(u)-2] == "get" {
err := fmt.Sprintf("Key is missing.")
log.Printf(err)
http.Error(w, err, http.StatusBadRequest)
return
}
key := u[len(u)-1]
value := k.Get(key)

resp := Response{
Key: key,
Value: value,
Result: "OK",
}
j, err := json.Marshal(resp)
if err != nil {
log.Printf(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(headerContent, contentValue)
w.WriteHeader(http.StatusOK)
w.Write(j)
log.Printf("%s=%s", key, value)
}

0 comments on commit 4fd3bd7

Please sign in to comment.