Commit 3c6e13dd by lancet

heavy refactoring of vault and web parts

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