forked from azwarnrst/hybrid-kvstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
101 lines (91 loc) · 2.67 KB
/
app.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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"hybrid-kvstore/badgerkv"
"log"
"net/http"
"time"
)
type XRouter struct {
Badger *badgerkv.BadgerStorage
}
func main() {
xRouter := XRouter{
Badger: badgerkv.Init(),
}
r := mux.NewRouter()
r.HandleFunc("/", xRouter.handler)
r.HandleFunc("/item", xRouter.newItem).Methods("POST")
r.HandleFunc("/item/ttl", xRouter.newItemTTL).Methods("POST")
r.HandleFunc("/item/update", xRouter.updateItem).Methods("POST")
r.HandleFunc("/item/volatile", xRouter.getVolatileList).Methods("GET")
r.HandleFunc("/item/persistence", xRouter.getPersistenceList).Methods("GET")
log.Fatal(http.ListenAndServe("localhost:8080", r))
}
func (x *XRouter) handler(w http.ResponseWriter, r *http.Request) {
return
}
func (x *XRouter) newItem(w http.ResponseWriter, r *http.Request) {
formData := badgerkv.MigratedCsvFile{}
err := json.NewDecoder(r.Body).Decode(&formData)
if err != nil {
log.Printf("error parse form data : %+v", err)
return
}
index := time.Now().Format("m_02_01_2006_15_04_05")
formData.Index = index
x.Badger.AddNewItem(index, formData)
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
return
}
func (x *XRouter) newItemTTL(w http.ResponseWriter, r *http.Request) {
formData := badgerkv.MigratedCsvFile{}
err := json.NewDecoder(r.Body).Decode(&formData)
if err != nil {
log.Printf("error parse form data : %+v", err)
return
}
index := time.Now().Format("m_02_01_2006_15_04_05")
formData.Index = index
ttl := time.Second * 15
x.Badger.AddNewItemTTL(index, formData, ttl)
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
return
}
func (x *XRouter) updateItem(w http.ResponseWriter, r *http.Request) {
formData := badgerkv.MigratedCsvFile{}
err := json.NewDecoder(r.Body).Decode(&formData)
if err != nil {
log.Printf("error parse form data : %+v", err)
return
}
x.Badger.UpdateItem(formData)
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
return
}
func (x *XRouter) getVolatileList(w http.ResponseWriter, r *http.Request) {
data := x.Badger.GetVolatileItemList()
w.Header().Set("Content-Type", "application/json")
byteResult, err := json.Marshal(data)
w.WriteHeader(http.StatusOK)
_, err = w.Write(byteResult)
if err != nil {
log.Printf("[writeErrorAuthResponse] error write response buffer : %#v", err)
}
return
}
func (x *XRouter) getPersistenceList(w http.ResponseWriter, r *http.Request) {
data := x.Badger.GetPersistenceItemList()
w.Header().Set("Content-Type", "application/json")
byteResult, err := json.Marshal(data)
w.WriteHeader(http.StatusOK)
_, err = w.Write(byteResult)
if err != nil {
log.Printf("[writeErrorAuthResponse] error write response buffer : %#v", err)
}
return
}