Commit ff5f12b0 by lancet

even further WIP

parent 648258de
...@@ -4,57 +4,21 @@ import ( ...@@ -4,57 +4,21 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/gorilla/mux"
"gitlab.tq-nest.lan/lancet/kvcache/vault" "gitlab.tq-nest.lan/lancet/kvcache/vault"
"gitlab.tq-nest.lan/lancet/kvcache/web"
) )
type Route struct {
URL string
Methods []string
Handler func(http.ResponseWriter, *http.Request)
}
func getValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
resp := fmt.Sprintf("Get ID: %s\n", id)
fmt.Fprint(w, resp)
}
func setValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
resp := fmt.Sprintf("Set ID: %s\n", id)
fmt.Fprint(w, resp)
}
func delValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
resp := fmt.Sprintf("Delete ID: %s\n", id)
fmt.Fprint(w, resp)
}
func initRouter(router *mux.Router, routes []Route) {
for _, i := range routes {
for _, j := range i.Methods {
router.HandleFunc(i.URL, i.Handler).Methods(j)
}
}
return
}
func main() { func main() {
router := mux.NewRouter() router := new(web.Svc)
storage := new(vault.Store) storage := new(vault.Store)
storage.Init(30) storage.Init(30)
routing := []Route{ routing := []web.Route{
{"/storage/{id}", []string{"GET"}, getValue}, {URL: "/storage/{id}", Methods: []string{"GET"}, Handler: router.GetValue},
{"/storage/{id}", []string{"PUT", "POST"}, setValue}, {URL: "/storage/{id}/{value}", Methods: []string{"PUT", "POST"}, Handler: router.SetValue},
{"/storage/{id}", []string{"DELETE"}, delValue}, {URL: "/storage/{id}", Methods: []string{"DELETE"}, Handler: router.DelValue},
} }
initRouter(router, routing) router.InitRouter(routing, storage.Exchange)
http.Handle("/", router) http.Handle("/", router.GetRouter())
fmt.Println("Listening") fmt.Println("Listening")
http.ListenAndServe(":8881", nil) http.ListenAndServe(":8881", nil)
return return
......
package web
import (
"fmt"
"net/http"
"gitlab.tq-nest.lan/lancet/kvcache/vault"
"github.com/gorilla/mux"
)
// GetValue - получить значение ключа из хранилища
func (svc *Svc) GetValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
resp := fmt.Sprintf("Get ID: %s\n", id)
fmt.Fprint(w, resp)
}
// SetValue - установить/обновить значение ключа
func (svc *Svc) SetValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
resp := fmt.Sprintf("Set ID: %s\n", id)
fmt.Fprint(w, resp)
}
// DelValue - удалить ключ из хранилища
func (svc *Svc) DelValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
resp := fmt.Sprintf("Delete ID: %s\n", id)
fmt.Fprint(w, resp)
}
// InitRouter - инициализировать маршрутизацию запросов
func (svc *Svc) InitRouter(routes []Route, exch chan vault.Message) {
if svc.router != nil {
return
}
svc.router = mux.NewRouter()
for _, i := range routes {
for _, j := range i.Methods {
svc.router.HandleFunc(i.URL, i.Handler).Methods(j)
}
}
svc.Exchange = exch
return
}
// GetRouter - возвращаем ссылку на роутер для веб-сервера
func (svc *Svc) GetRouter() *mux.Router {
if svc.router != nil {
return svc.router
}
return nil
}
package web package web
import (
"net/http"
"github.com/gorilla/mux"
"gitlab.tq-nest.lan/lancet/kvcache/vault"
)
// Route - правило маршрутизации запросов
type Route struct {
URL string
Methods []string
Handler func(http.ResponseWriter, *http.Request)
}
// Svc - обмен данными с хранилищем
type Svc struct {
Exchange chan vault.Message
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