Skip to content

Commit 801e534

Browse files
committed
upgrade
1 parent f22b8f7 commit 801e534

36 files changed

+948
-282
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pre-commite: setup lint build tests
3636
ci: install setup lint build tests
3737

3838
run_local_server:
39-
go run cmd/loop/main.go --config=config/config.dev.yaml
39+
go run cmd/loop/main.go -race --config=config/config.dev.yaml
4040

4141
run_local_cli:
4242
go run cmd/loopcli/main.go --server=127.0.0.1:9500 kv set "k1/k2/k3" "{\"data\":\"aaa\"}"
@@ -51,9 +51,9 @@ run_local_cli:
5151
go run cmd/loopcli/main.go --server=127.0.0.1:9500 kv list "k1/bbb/"
5252
go run cmd/loopcli/main.go --server=127.0.0.1:9500 kv list "users/"
5353

54-
run_local_cli_watch:
54+
run_local_template:
5555
go run cmd/loopcli/main.go --server=127.0.0.1:9500 kv set "users/demo" "Mike"
56-
go run cmd/loopcli/main.go --server=127.0.0.1:9500 template \
56+
go run cmd/loopcli/main.go -race --server=127.0.0.1:9500 template \
5757
test_data/template.tmpl:test_data/template.out
5858
run_local_cli_watch_data:
5959
go run cmd/loopcli/main.go --server=127.0.0.1:9500 kv set "users/demo" "Mike1"

README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
1-
# loopy
1+
# Loopy
2+
3+
Loopy is designed to store and manage keys and values that are used by various services and applications. It provides a reliable system for tracking changes and updating text templates in real time.
4+
5+
## Install with go
6+
7+
```shell
8+
go install github.com/arwos/loopy/...@latest
9+
```
10+
11+
## Server
12+
13+
Run server:
14+
15+
```shell
16+
loppy --config=./config.yaml
17+
```
18+
19+
## Console client
20+
21+
### Work with template
22+
23+
```shell
24+
loopcli --server=127.0.0.1:9500 template \
25+
test_data/template.tmpl:test_data/template.out \
26+
test_data/template2.tmpl:test_data/template2.out
27+
```
28+
29+
#### Template functions:
30+
31+
* __key__ - returns the key value or an empty string
32+
```json
33+
{{key "key/name"}}
34+
```
35+
* __key_or_default__ - returns the key value or default value
36+
```json
37+
{{key_or_default "key/name" "default value"}}
38+
```
39+
* __tree__ - returns a list of keys and value by prefix
40+
```json
41+
{{range $index, $data := tree "key/"}}
42+
index: {{$index}} key: {{$data.Key}} val: {{$data.Value}}
43+
{{end}}
44+
```
45+
46+
### Work with keys
47+
48+
* Set key
49+
```shell
50+
loopcli --server=127.0.0.1:9500 kv set "key/name" "key_value"
51+
```
52+
* Get key
53+
```shell
54+
loopcli --server=127.0.0.1:9500 kv get "key/name"
55+
```
56+
* Delete key
57+
```shell
58+
loopcli --server=127.0.0.1:9500 kv del "key/name"
59+
```
60+
* List key by prefix (with empty value)
61+
```shell
62+
loopcli --server=127.0.0.1:9500 kv list "key/"
63+
```
64+
* Search key by prefix (without empty value)
65+
```shell
66+
loopcli --server=127.0.0.1:9500 kv search "key/"
67+
```

api/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"net/http"
1313
"net/url"
1414

15-
"go.osspkg.com/goppy/sdk/webutil"
15+
"go.osspkg.com/goppy/web"
1616
)
1717

1818
type (
1919
_client struct {
2020
conf *Config
21-
cli *webutil.ClientHttp
21+
cli *web.ClientHttp
2222
}
2323
Client interface {
2424
Get(ctx context.Context, key string) (string, error)
@@ -30,12 +30,12 @@ type (
3030
)
3131

3232
func NewKV(c *Config) (Client, error) {
33-
opts := make([]webutil.ClientHttpOption, 0, 2)
33+
opts := make([]web.ClientHttpOption, 0, 2)
3434
if len(c.AuthToken) > 0 {
35-
opts = append(opts, webutil.ClientHttpOptionHeaders(AuthTokenHeaderName, c.AuthToken))
35+
opts = append(opts, web.ClientHttpOptionHeaders(AuthTokenHeaderName, c.AuthToken))
3636
}
3737
opts = append(opts, clientHttpOptionCodec())
38-
cli := webutil.NewClientHttp(opts...)
38+
cli := web.NewClientHttp(opts...)
3939

4040
if err := c.Validate(); err != nil {
4141
return nil, err
@@ -59,8 +59,8 @@ func (v *_client) buildUri(path string) string {
5959
return uri.String()
6060
}
6161

62-
func clientHttpOptionCodec() webutil.ClientHttpOption {
63-
return webutil.ClientHttpOptionCodec(
62+
func clientHttpOptionCodec() web.ClientHttpOption {
63+
return web.ClientHttpOptionCodec(
6464
func(in interface{}) (body []byte, contentType string, err error) {
6565
switch v := in.(type) {
6666
case []byte:

api/client_keys.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func (v *_client) Get(ctx context.Context, key string) (string, error) {
2121
if len(data) == 0 {
2222
return "", errRequestEmpty
2323
}
24-
return data[0].Value.String(), nil
24+
return data[0].ValueStrOrNull(), nil
2525
}
2626

2727
func (v *_client) Set(ctx context.Context, key, value string) error {
2828
data := make(EntitiesKV, 0, 1)
2929
data = append(data, EntityKV{
30-
Key: key,
31-
Value: []byte(value),
30+
Key: key,
31+
Val: &value,
3232
})
3333
return v.cli.Call(ctx, http.MethodPut, v.buildUri(PathApiV1KV), &data, nil)
3434
}

api/consts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
package api
77

88
import (
9-
"go.osspkg.com/goppy/sdk/errors"
10-
"go.osspkg.com/goppy/sdk/netutil/websocket"
9+
"go.osspkg.com/goppy/errors"
10+
"go.osspkg.com/goppy/ws/event"
1111
)
1212

1313
const (
@@ -25,7 +25,7 @@ const (
2525
)
2626

2727
const (
28-
EventKVGet websocket.EventID = iota + 1
28+
EventKVGet event.Id = iota + 1
2929
EventKVSet
3030
EventKVDel
3131
EventKVWatch

api/go.mod

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ go 1.20
44

55
require (
66
github.com/mailru/easyjson v0.7.7
7-
go.osspkg.com/goppy v0.14.1
7+
go.osspkg.com/goppy/errors v0.1.0
8+
go.osspkg.com/goppy/web v0.1.5
9+
go.osspkg.com/goppy/ws v0.1.6
10+
go.osspkg.com/goppy/xlog v0.1.4
811
)
912

1013
require (
11-
github.com/gorilla/websocket v1.5.0 // indirect
14+
github.com/gorilla/websocket v1.5.1 // indirect
1215
github.com/josharian/intern v1.0.0 // indirect
13-
github.com/kr/text v0.2.0 // indirect
14-
github.com/rogpeppe/go-internal v1.11.0 // indirect
15-
go.osspkg.com/algorithms v1.3.0 // indirect
16-
gopkg.in/yaml.v3 v3.0.1 // indirect
16+
go.osspkg.com/goppy/iosync v0.1.2 // indirect
17+
go.osspkg.com/goppy/ioutil v0.1.0 // indirect
18+
go.osspkg.com/goppy/plugins v0.1.1 // indirect
19+
go.osspkg.com/goppy/xc v0.1.0 // indirect
20+
go.osspkg.com/goppy/xnet v0.1.1 // indirect
21+
go.osspkg.com/static v1.4.0 // indirect
22+
golang.org/x/net v0.17.0 // indirect
1723
)

api/go.sum

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
2+
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
3+
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
44
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
55
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
6-
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
7-
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
8-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
96
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
107
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
118
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12-
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
13-
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
149
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
15-
go.osspkg.com/algorithms v1.3.0 h1:rrKO440aNTofYJwGnOVsW00w0VRtZtu+BQrgXMXw7j4=
16-
go.osspkg.com/algorithms v1.3.0/go.mod h1:J2SXxHdqBK9ALGYJomA9XGvTOhIwMK0fvVw+KusYyoo=
17-
go.osspkg.com/goppy v0.14.1 h1:bPYGep07AlvABuEcJIz6ySF7kMJYhticodycz6DNIOE=
18-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
10+
go.osspkg.com/goppy/errors v0.1.0 h1:2q8gdRZMEEDk7y/sneblQxpHhsi3pqF1BbFFHS7+FnE=
11+
go.osspkg.com/goppy/errors v0.1.0/go.mod h1:SvA8dgErO9e2i3TR8hgJV4uMAzs4TkL4FxXBSnw2UG4=
12+
go.osspkg.com/goppy/iosync v0.1.2 h1:w0BxqBa7PAdxFM2P8pZn3ToHK6ilX5IG+1nwIXJEoGg=
13+
go.osspkg.com/goppy/iosync v0.1.2/go.mod h1:huJpHhpIQ2DgzY3wVt72RUsJaje0uqYiUvMRovb1/Dg=
14+
go.osspkg.com/goppy/ioutil v0.1.0 h1:Z9CF1nzrjbcHJV1EIcLqOPAotePScuCjmTSyU7BoLzk=
15+
go.osspkg.com/goppy/ioutil v0.1.0/go.mod h1:WpZGj1HpuBlDDH5k8mn+2QwPssMP83jKj59U8qLsBoU=
16+
go.osspkg.com/goppy/plugins v0.1.1 h1:ly/g8LyGQNhT9BLKLbhHejUuPho5atd4uJmitorQyvM=
17+
go.osspkg.com/goppy/plugins v0.1.1/go.mod h1:oolaNTq9VCWBAApLUFCHvWZ/7tMUhzLaqQEIxmLviNQ=
18+
go.osspkg.com/goppy/web v0.1.5 h1:Q9ySetPvN5o8Pj5uiUl5pXMNnxSUxqq1B7kI1UU6SMw=
19+
go.osspkg.com/goppy/web v0.1.5/go.mod h1:e0XC5Kk+d+mFrOj2eViXsPooKc2SSc/9CWWXUJ9MWXY=
20+
go.osspkg.com/goppy/ws v0.1.6 h1:6xuu9NZsB5YlrWidDIOy/4UMlzddXbD2gI8TczxFt7U=
21+
go.osspkg.com/goppy/ws v0.1.6/go.mod h1:5jsI1DdoNUVchcylaHGCrw5+qFObf9lQ2qWAXYiB//c=
22+
go.osspkg.com/goppy/xc v0.1.0 h1:e2231FumnLEf1OjqEtbRaUxz3FT9M8pZVKg0C0aTf7g=
23+
go.osspkg.com/goppy/xc v0.1.0/go.mod h1:ocKrJbO+EADhuClTbOqzDfCqnUO9+ikEW0M7pqLl1Y4=
24+
go.osspkg.com/goppy/xlog v0.1.4 h1:3+o71O3Jb8UgfSA6nfpfQHfhHLIploHhlQ4p+Yfj5So=
25+
go.osspkg.com/goppy/xlog v0.1.4/go.mod h1:AtYBxgKaxQxFWmb/SbmLvYp7ysuE2i1YYSE3Pu29nNQ=
26+
go.osspkg.com/goppy/xnet v0.1.1 h1:nysNyS5O7nHXIN/IjP9HGfa6Qh5BTTSYLULijk+Sv9M=
27+
go.osspkg.com/goppy/xnet v0.1.1/go.mod h1:eB5pFfZTCrcaIOHzt4RlTgBVF5dRUV/u52qz/2hY3qk=
28+
go.osspkg.com/goppy/xtest v0.1.2 h1:rbUzEnWZW9vkGa24owydA9icQcfOaROJWSym1l0mCtU=
29+
go.osspkg.com/static v1.4.0 h1:2uy4/11c0QP+QLMucKQZbAU+e6lhVHKw5dWJPTk/DBg=
30+
go.osspkg.com/static v1.4.0/go.mod h1:94YydVU3qUtb1J534486lpm+qg6CviQjqtxKlkpSppM=
31+
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
32+
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
2033
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
21-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

api/models.go

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,27 @@
55

66
package api
77

8-
import (
9-
"bytes"
10-
"fmt"
11-
)
12-
138
//go:generate easyjson
149

1510
//easyjson:json
16-
type EntityKV struct {
17-
Key string `json:"k"`
18-
Value RawValue `json:"v,omitempty"`
19-
}
20-
21-
//easyjson:json
22-
type EntitiesKV []EntityKV
23-
24-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25-
26-
var (
27-
esc = []byte("\\\"")
28-
unesc = []byte("\"")
29-
)
30-
31-
type RawValue []byte
32-
33-
func (m RawValue) MarshalJSON() ([]byte, error) {
34-
if m == nil {
35-
return []byte("null"), nil
11+
type (
12+
EntitiesKV []EntityKV
13+
EntityKV struct {
14+
Key string `json:"k"`
15+
Val *string `json:"v"`
3616
}
37-
buf := bytes.NewBuffer(nil)
38-
buf.WriteString("\"")
39-
buf.Write(bytes.ReplaceAll(m, unesc, esc))
40-
buf.WriteString("\"")
41-
return buf.Bytes(), nil
42-
}
17+
)
4318

44-
func (m *RawValue) UnmarshalJSON(data []byte) error {
45-
if m == nil {
46-
return fmt.Errorf("json.RawMessage: UnmarshalJSON on nil pointer")
19+
func (v EntityKV) ValueStrOrNull() string {
20+
if v.Val == nil {
21+
return "null"
4722
}
48-
out := bytes.ReplaceAll(data[1:len(data)-1], esc, unesc)
49-
*m = append((*m)[0:0], out...)
50-
return nil
23+
return *v.Val
5124
}
5225

53-
func (m RawValue) String() string {
54-
if m == nil {
55-
return "null"
26+
func (v EntityKV) Value() string {
27+
if v.Val == nil {
28+
return ""
5629
}
57-
return string(m)
30+
return *v.Val
5831
}

api/models_easyjson.go

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)