Commit 3c6e13dd by lancet

heavy refactoring of vault and web parts

parent 4a2f5db4
......@@ -97,13 +97,13 @@ func main() {
flag.Parse()
listenStr := fmt.Sprintf("%s:%d", *hostF, *portF)
router := new(web.Svc)
storage := vault.Store{30}
storage := vault.Store{TTL: *ttlF}
routing := []web.Route{
{URL: "/storage/{id}", Methods: []string{"GET"}, Handler: router.GetValue},
{URL: "/storage/{id}/{value}", Methods: []string{"PUT", "POST"}, Handler: router.SetValue},
{URL: "/storage/{id}", Methods: []string{"DELETE"}, Handler: router.DelValue},
}
router.InitRouter(routing, storage.Exchange)
router.InitRouter(routing, &storage)
http.Handle("/", router.GetRouter())
fmt.Printf("Listening: %s\n", listenStr)
http.ListenAndServe(listenStr, nil)
......
......@@ -13,16 +13,13 @@ import (
func (svc *Svc) GetValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
reply := make(chan vault.Message)
msg := vault.Message{Action: "GET", Reply: reply, Key: id}
svc.exchange <- msg
resp := <-reply
if resp.Error != true {
fmt.Fprint(w, resp.Value)
return
value, err := svc.store.GetValue(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "FAILURE")
}
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "FAILURE")
fmt.Fprint(w, value)
return
}
// SetValue - установить/обновить значение ключа
......@@ -30,36 +27,31 @@ func (svc *Svc) SetValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
v := vars["value"]
reply := make(chan vault.Message)
msg := vault.Message{Action: "SET", Reply: reply, Key: id, Value: v}
svc.exchange <- msg
resp := <-reply
if resp.Error != true {
fmt.Fprint(w, "OK")
return
err := svc.store.SetValue(id, v)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "FAILURE")
}
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "FAILURE")
fmt.Fprint(w, "OK")
return
}
// DelValue - удалить ключ из хранилища
func (svc *Svc) DelValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
reply := make(chan vault.Message)
msg := vault.Message{Action: "DEL", Reply: reply, Key: id}
svc.exchange <- msg
resp := <-reply
if resp.Error != true {
fmt.Fprint(w, "OK")
return
err := svc.store.DelValue(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "FAILURE")
}
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "FAILURE")
fmt.Fprint(w, "OK")
return
}
// InitRouter - инициализировать маршрутизацию запросов
func (svc *Svc) InitRouter(routes []Route, exch chan vault.Message) {
func (svc *Svc) InitRouter(routes []Route, store *vault.Store) {
if svc.router != nil {
return
}
......@@ -69,7 +61,7 @@ func (svc *Svc) InitRouter(routes []Route, exch chan vault.Message) {
svc.router.HandleFunc(i.URL, i.Handler).Methods(j)
}
}
svc.exchange = exch
svc.store = store
return
}
......
......@@ -16,6 +16,6 @@ type Route struct {
// Svc - обмен данными с хранилищем
type Svc struct {
exchange chan vault.Message
router *mux.Router
store *vault.Store
router *mux.Router
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment