Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lancet
/
kvcache
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3c6e13dd
authored
Sep 09, 2019
by
lancet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
heavy refactoring of vault and web parts
parent
4a2f5db4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
33 deletions
+25
-33
main.go
+2
-2
web/control.go
+21
-29
web/data.go
+2
-2
No files found.
main.go
View file @
3c6e13dd
...
@@ -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
.
Exchan
ge
)
router
.
InitRouter
(
routing
,
&
stora
ge
)
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
)
...
...
web/control.go
View file @
3c6e13dd
...
@@ -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
.
Messag
e
)
{
func
(
svc
*
Svc
)
InitRouter
(
routes
[]
Route
,
store
*
vault
.
Stor
e
)
{
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
}
}
...
...
web/data.go
View file @
3c6e13dd
...
@@ -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
.
Messag
e
store
*
vault
.
Stor
e
router
*
mux
.
Router
router
*
mux
.
Router
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment