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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
29 deletions
+21
-29
main.go
+2
-2
web/control.go
+18
-26
web/data.go
+1
-1
No files found.
main.go
View file @
3c6e13dd
...
...
@@ -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
.
Exchan
ge
)
router
.
InitRouter
(
routing
,
&
stora
ge
)
http
.
Handle
(
"/"
,
router
.
GetRouter
())
fmt
.
Printf
(
"Listening: %s
\n
"
,
listenStr
)
http
.
ListenAndServe
(
listenStr
,
nil
)
...
...
web/control.go
View file @
3c6e13dd
...
...
@@ -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"
)
}
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"
)
}
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"
)
}
fmt
.
Fprint
(
w
,
"OK"
)
return
}
// 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
{
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
}
...
...
web/data.go
View file @
3c6e13dd
...
...
@@ -16,6 +16,6 @@ type Route struct {
// Svc - обмен данными с хранилищем
type
Svc
struct
{
exchange
chan
vault
.
Messag
e
store
*
vault
.
Stor
e
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