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
ff5f12b0
authored
Aug 25, 2019
by
lancet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
even further WIP
parent
648258de
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
44 deletions
+85
-44
main.go
+8
-44
web/control.go
+57
-0
web/data.go
+20
-0
No files found.
main.go
View file @
ff5f12b0
...
@@ -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"
},
g
etValue
},
{
URL
:
"/storage/{id}"
,
Methods
:
[]
string
{
"GET"
},
Handler
:
router
.
G
etValue
},
{
"/storage/{id}"
,
[]
string
{
"PUT"
,
"POST"
},
s
etValue
},
{
URL
:
"/storage/{id}/{value}"
,
Methods
:
[]
string
{
"PUT"
,
"POST"
},
Handler
:
router
.
S
etValue
},
{
"/storage/{id}"
,
[]
string
{
"DELETE"
},
d
elValue
},
{
URL
:
"/storage/{id}"
,
Methods
:
[]
string
{
"DELETE"
},
Handler
:
router
.
D
elValue
},
}
}
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
...
...
web/control.go
0 → 100644
View file @
ff5f12b0
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
}
web/data.go
View file @
ff5f12b0
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
}
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