-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
162 lines (149 loc) · 3.61 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
_ "embed"
"encoding/hex"
"encoding/json"
"fmt"
"hash"
"log"
"net/http"
"strings"
badger "github.com/dgraph-io/badger/v3"
"github.com/gorilla/mux"
)
// Record represents key-value pair
type Record struct {
Key string `json:"key"`
Value string `json:"value"`
}
// HTTPRecord represents key-value pair
type HTTPRecord struct {
Sha string `json:"sha"`
Record
}
// helper function to handle http server errors
func handleError(w http.ResponseWriter, r *http.Request, msg string, err error) {
log.Println(msg, err)
rec := make(map[string]string)
rec["message"] = msg
rec["error"] = fmt.Sprintf("%v", err)
data, e := json.Marshal(rec)
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write(data)
}
// StoreHandler stores given key value pair in DB
func StoreHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var rec HTTPRecord
err := json.NewDecoder(r.Body).Decode(&rec)
if err != nil {
msg := "unable to marshal server settings"
handleError(w, r, msg, err)
return
}
// create hash value for given key
var h hash.Hash
sha := strings.ToLower(Config.SHA)
if sha == "sha256" || rec.Sha == "sha256" {
h = sha256.New()
rec.Sha = "sha256"
} else if sha == "sha512" || rec.Sha == "sha512" {
h = sha512.New()
rec.Sha = "sha512"
} else {
h = sha1.New()
rec.Sha = "sha1"
}
h.Write([]byte(rec.Key))
// if record value is not provided we'll create a hash for it
// this will allow to anonimise the data
if rec.Value == "" {
rec.Value = hex.EncodeToString(h.Sum(nil))
}
// commit new key-value records into our store
txn := DB.NewTransaction(true)
defer txn.Discard()
err = txn.Set([]byte(rec.Key), []byte(rec.Value))
if err != nil {
msg := "unable to set new key-value pair"
handleError(w, r, msg, err)
}
err = txn.Commit()
if err != nil {
msg := "unable to commit new key-value pair"
handleError(w, r, msg, err)
}
if Config.Verbose > 0 {
log.Printf("record key=%s value=%s", rec.Key, rec.Value)
}
txn = DB.NewTransaction(true)
defer txn.Discard()
err = txn.Set([]byte(rec.Value), []byte(rec.Key))
if err != nil {
msg := "unable to set new key-value pair"
handleError(w, r, msg, err)
}
err = txn.Commit()
if err != nil {
msg := "unable to commit new key-value pair"
handleError(w, r, msg, err)
}
if Config.Verbose > 0 {
log.Printf("record key=%s value=%s", rec.Value, rec.Key)
}
data, err := json.Marshal(rec)
if err != nil {
msg := "unable to marshal record"
handleError(w, r, msg, err)
}
w.Write(data)
}
// InfoHandler fetches key-value pair from DB
func InfoHandler(w http.ResponseWriter, r *http.Request) {
rec := make(map[string]string)
rec["server"] = Info()
data, err := json.Marshal(rec)
if err != nil {
log.Fatalf("Fail to marshal records, %v", err)
}
w.WriteHeader(http.StatusOK)
w.Write(data)
}
// FetchHandler fetches key-value pair from DB
func FetchHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := DB.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(vars["key"]))
if err != nil {
return err
}
val, _ := item.ValueCopy(nil)
rec := Record{Key: vars["key"], Value: string(val)}
data, err := json.Marshal(rec)
if err != nil {
return err
}
w.Write(data)
return nil
})
if err != nil {
msg := "unable to fetch key value"
handleError(w, r, msg, err)
return
}
}
var (
//go:embed static/index.html
index string
)
// IndexHandler fetches key-value pair from DB
func IndexHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(index))
}