Skip to content

Commit

Permalink
fix(storage): return empty slice on not found redis (#313)
Browse files Browse the repository at this point in the history
* fix(storage): return empty slice on not found redis

* feat: try to hot-reload json configuration and add caddy e2e tests on json configuration

* feat: add more use cases

* feat: bump version
  • Loading branch information
darkweak authored Mar 6, 2023
1 parent fd4237a commit e9a8e70
Show file tree
Hide file tree
Showing 50 changed files with 4,693 additions and 155 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/plugin_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ jobs:
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
-
name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.19
-
name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
-
name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
36 changes: 35 additions & 1 deletion .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@ jobs:
build-caddy-validator:
name: Check that Souin build as caddy module
runs-on: ubuntu-latest
services:
redis:
image: redis
ports:
- 6379:6379
etcd:
image: quay.io/coreos/etcd
env:
ETCD_NAME: etcd0
ETCD_ADVERTISE_CLIENT_URLS: http://etcd:2379,http://etcd:4001
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379,http://0.0.0.0:4001
ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd:2380
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
ETCD_INITIAL_CLUSTER_TOKEN: etcd-cluster-1
ETCD_INITIAL_CLUSTER: etcd0=http://etcd:2380
ETCD_INITIAL_CLUSTER_STATE: new
ports:
- 2379:2379
- 2380:2380
- 4001:4001
steps:
-
name: Add domain.com host to /etc/hosts
run: |
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 domain.com etcd redis" | sudo tee -a /etc/hosts
-
name: Install Go
uses: actions/setup-go@v2
Expand Down Expand Up @@ -40,6 +60,20 @@ jobs:
folder: Caddy
reporters: cli
delayRequest: 5000
-
name: Run detached caddy
run: cd plugins/caddy && ./caddy stop
-
name: Run detached caddy
run: cd plugins/caddy && ./caddy run --config ./configuration.json &
-
name: Run Caddy E2E tests
uses: anthonyvscode/newman-action@v1
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Caddy
reporters: cli
delayRequest: 5000
build-beego-validator:
name: Check that Souin build as middleware
uses: ./.github/workflows/plugin_template.yml
Expand Down
36 changes: 35 additions & 1 deletion .github/workflows/workflow_plugins_generator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,31 @@ jobs:
build-caddy-validator:
name: Check that Souin build as caddy module
runs-on: ubuntu-latest
services:
redis:
image: redis
ports:
- 6379:6379
etcd:
image: quay.io/coreos/etcd
env:
ETCD_NAME: etcd0
ETCD_ADVERTISE_CLIENT_URLS: http://etcd:2379,http://etcd:4001
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379,http://0.0.0.0:4001
ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd:2380
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
ETCD_INITIAL_CLUSTER_TOKEN: etcd-cluster-1
ETCD_INITIAL_CLUSTER: etcd0=http://etcd:2380
ETCD_INITIAL_CLUSTER_STATE: new
ports:
- 2379:2379
- 2380:2380
- 4001:4001
steps:
-
name: Add domain.com host to /etc/hosts
run: |
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 domain.com etcd redis" | sudo tee -a /etc/hosts
-
name: Install Go
uses: actions/setup-go@v2
Expand Down Expand Up @@ -45,6 +65,20 @@ jobs:
folder: Caddy
reporters: cli
delayRequest: 5000
-
name: Run detached caddy
run: cd plugins/caddy && ./caddy stop
-
name: Run detached caddy
run: cd plugins/caddy && ./caddy run --config ./configuration.json &
-
name: Run Caddy E2E tests
uses: anthonyvscode/newman-action@v1
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Caddy
reporters: cli
delayRequest: 5000
EOF
workflow+="$tpl"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ experimental:
plugins:
souin:
moduleName: github.com/darkweak/souin
version: v1.6.31
version: v1.6.32
```
After that you can declare either the whole configuration at once in the middleware block or by service. See the examples below.
```yaml
Expand Down
10 changes: 5 additions & 5 deletions context/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func (g *graphQLContext) SetContext(req *http.Request) *http.Request {
ctx = context.WithValue(ctx, IsMutationRequest, false)

if g.custom && req.Body != nil {
b, _ := io.ReadAll(req.Body)
req.Body = io.NopCloser(bytes.NewReader(b))
if len(b) > 0 {
if isMutation(b) {
b := bytes.NewBuffer([]byte{})
_, _ = io.Copy(b, req.Body)
if b.Len() > 0 {
if isMutation(b.Bytes()) {
ctx = context.WithValue(ctx, IsMutationRequest, true)
} else {
h := sha256.New()
h.Write(b)
h.Write(b.Bytes())
ctx = context.WithValue(ctx, HashBody, fmt.Sprintf("-%x", h.Sum(nil)))
}
}
Expand Down
23 changes: 23 additions & 0 deletions context/now.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package context

import (
"context"
"net/http"
"time"

"github.com/darkweak/souin/configurationtypes"
)

const Now ctxKey = "NOW"

type nowContext struct{}

func (cc *nowContext) SetupContext(_ configurationtypes.AbstractConfigurationInterface) {}

func (cc *nowContext) SetContext(req *http.Request) *http.Request {
now := time.Now().UTC()
req.Header.Set("Date", now.Format(time.RFC1123))
return req.WithContext(context.WithValue(req.Context(), Now, now))
}

var _ ctx = (*nowContext)(nil)
5 changes: 4 additions & 1 deletion context/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
GraphQL ctx
Key ctx
Method ctx
Now ctx
Timeout ctx
}
)
Expand All @@ -31,6 +32,7 @@ func GetContext() *Context {
GraphQL: &graphQLContext{},
Key: &keyContext{},
Method: &methodContext{},
Now: &nowContext{},
Timeout: &timeoutContext{},
}
}
Expand All @@ -40,11 +42,12 @@ func (c *Context) Init(co configurationtypes.AbstractConfigurationInterface) {
c.GraphQL.SetupContext(co)
c.Key.SetupContext(co)
c.Method.SetupContext(co)
c.Now.SetupContext(co)
c.Timeout.SetupContext(co)
}

func (c *Context) SetBaseContext(req *http.Request) *http.Request {
return c.Timeout.SetContext(c.Method.SetContext(c.CacheName.SetContext(req)))
return c.Timeout.SetContext(c.Method.SetContext(c.CacheName.SetContext(c.Now.SetContext(req))))
}

func (c *Context) SetContext(req *http.Request) *http.Request {
Expand Down
16 changes: 14 additions & 2 deletions docker-compose.yml.dev
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ services:

olric:
build:
context: ./olric
context: ./docker
dockerfile: Dockerfile-olric
target: olric
restart: on-failure
<<: *networks

etcd:
image: quay.io/coreos/etcd
command: sh -c 'apk update; apk add curl;etcd -listen-client-urls http://0.0.0.0:2379 -advertise-client-urls http://etcd:2379'
ports:
- 2379:2379
- 2380
- 4001
environment:
ETCD_NAME: etcd0
ETCD_ADVERTISE_CLIENT_URLS: http://etcd:2379,http://etcd:4001
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379,http://0.0.0.0:4001
ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd:2380
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
ETCD_INITIAL_CLUSTER_TOKEN: etcd-cluster-1
ETCD_INITIAL_CLUSTER: etcd0=http://etcd:2380
ETCD_INITIAL_CLUSTER_STATE: new
<<: *networks

redis:
Expand Down
20 changes: 13 additions & 7 deletions docker-compose.yml.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ services:

olric:
build:
context: ./olric
context: $PWD/docker
dockerfile: Dockerfile-olric
target: olric
ports:
- 3320:3320
- 3322:3322
restart: on-failure
<<: *networks

etcd:
image: quay.io/coreos/etcd
ports:
- "2379:2379"
- "2380:2380"
command: sh -c 'apk update; apk add curl;etcd -listen-client-urls http://0.0.0.0:2379 -advertise-client-urls http://etcd:2379'
- 2379:2379
- 2380
- 4001
environment:
ETCD_NAME: etcd0
ETCD_ADVERTISE_CLIENT_URLS: http://etcd:2379,http://etcd:4001
ETCD_LISTEN_CLIENT_URLS: http://0.0.0.0:2379,http://0.0.0.0:4001
ETCD_INITIAL_ADVERTISE_PEER_URLS: http://etcd:2380
ETCD_LISTEN_PEER_URLS: http://0.0.0.0:2380
ETCD_INITIAL_CLUSTER_TOKEN: etcd-cluster-1
ETCD_INITIAL_CLUSTER: etcd0=http://etcd:2380
ETCD_INITIAL_CLUSTER_STATE: new
<<: *networks

redis:
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e9a8e70

Please sign in to comment.