Skip to content

Commit

Permalink
fix(issue-204): Set request context in Souin plugin (#205)
Browse files Browse the repository at this point in the history
* fix(issue-204): Set request context in Souin plugin

* feat(plugins): Enhance caddy documentation (#206)

* feat(plugins): Introduce dotweb plugin (#207)

* feat(plugins): Introduce go-zero plugin support (#208)
  • Loading branch information
darkweak authored May 14, 2022
1 parent b62083b commit 3464dd0
Show file tree
Hide file tree
Showing 54 changed files with 3,828 additions and 341 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/plugins-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17
-
name: Checkout code
uses: actions/checkout@v2
Expand Down
97 changes: 92 additions & 5 deletions .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17
-
name: Checkout code
uses: actions/checkout@v2
Expand Down Expand Up @@ -107,7 +107,7 @@ jobs:
name: Wait for Souin is really loaded inside Chi as middleware
uses: jakejarvis/wait-action@master
with:
time: 30s
time: 20s
-
name: Set Chi logs configuration result as environment variable
run: cd plugins/chi/examples && echo "CHI_MIDDLEWARE_RESULT=$(docker-compose logs chi | grep Souin)" >> $GITHUB_ENV
Expand Down Expand Up @@ -149,7 +149,7 @@ jobs:
name: Wait for Souin is really loaded inside Fiber as middleware
uses: jakejarvis/wait-action@master
with:
time: 30s
time: 40s
-
name: Set Fiber logs configuration result as environment variable
run: cd plugins/fiber/examples && echo "FIBER_MIDDLEWARE_RESULT=$(docker-compose logs fiber | grep Souin)" >> $GITHUB_ENV
Expand Down Expand Up @@ -210,6 +210,51 @@ jobs:
folder: Gin
reporters: cli
delayRequest: 5000
build-go-zero-validator:
name: Check that Souin build as Go-zero middleware
runs-on: ubuntu-latest
steps:
-
name: Add domain.com host to /etc/hosts
run: |
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16
-
name: Checkout code
uses: actions/checkout@v2
-
name: Install goctl
run: go install github.com/zeromicro/go-zero/tools/goctl@latest
-
name: Build Souin as Go-zero plugin
run: make build-and-run-go-zero
-
name: Wait for Souin is really loaded inside Go-zero as middleware
uses: jakejarvis/wait-action@master
with:
time: 50s
-
name: Set Go-zero logs configuration result as environment variable
run: cd plugins/go-zero/examples && echo "GO_ZERO_MIDDLEWARE_RESULT=$(docker-compose logs go-zero | grep Souin)" >> $GITHUB_ENV
-
name: Check if the configuration is loaded to define if Souin is loaded too
uses: nick-invision/assert-action@v1
with:
expected: '"message":"Souin configuration is now loaded."'
actual: ${{ env.GO_ZERO_MIDDLEWARE_RESULT }}
comparison: contains
-
name: Run Go-zero E2E tests
uses: anthonyvscode/newman-action@v1
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Go-zero
reporters: cli
delayRequest: 5000
build-goyave-validator:
name: Check that Souin build as Goyave middleware
runs-on: ubuntu-latest
Expand All @@ -233,7 +278,7 @@ jobs:
name: Wait for Souin is really loaded inside Goyave as middleware
uses: jakejarvis/wait-action@master
with:
time: 30s
time: 20s
-
name: Set Goyave logs configuration result as environment variable
run: cd plugins/goyave/examples && echo "GOYAVE_MIDDLEWARE_RESULT=$(docker-compose logs goyave | grep Souin)" >> $GITHUB_ENV
Expand Down Expand Up @@ -294,6 +339,48 @@ jobs:
folder: Skipper
reporters: cli
delayRequest: 5000
build-souin-validator:
name: Check that Souin build as Souin middleware
runs-on: ubuntu-latest
steps:
-
name: Add domain.com host to /etc/hosts
run: |
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16
-
name: Checkout code
uses: actions/checkout@v2
-
name: Build Souin as Souin plugin
run: make build-and-run-souin
-
name: Wait for Souin is really loaded inside Souin as middleware
uses: jakejarvis/wait-action@master
with:
time: 40s
-
name: Set Souin logs configuration result as environment variable
run: cd plugins/souin/examples && echo "SOUIN_MIDDLEWARE_RESULT=$(docker-compose logs souin | grep Souin)" >> $GITHUB_ENV
-
name: Check if the configuration is loaded to define if Souin is loaded too
uses: nick-invision/assert-action@v1
with:
expected: '"message":"Souin configuration is now loaded."'
actual: ${{ env.SOUIN_MIDDLEWARE_RESULT }}
comparison: contains
-
name: Run Souin E2E tests
uses: anthonyvscode/newman-action@v1
with:
collection: "docs/e2e/Souin E2E.postman_collection.json"
folder: Souin
reporters: cli
delayRequest: 5000
build-tyk-validator:
name: Check that Souin build as Tyk middleware
runs-on: ubuntu-latest
Expand Down Expand Up @@ -407,7 +494,7 @@ jobs:
name: Wait for Souin is really loaded inside Webgo as middleware
uses: jakejarvis/wait-action@master
with:
time: 30s
time: 20s
-
name: Set Webgo logs configuration result as environment variable
run: cd plugins/webgo/examples && echo "WEBGO_MIDDLEWARE_RESULT=$(docker-compose logs webgo | grep Souin)" >> $GITHUB_ENV
Expand Down
63 changes: 32 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
.PHONY: build-and-run-caddy build-and-run-caddy-json build-and-run-chi build-and-run-echo build-and-run-fiber build-and-run-gin \
build-and-run-goyave build-and-run-skipper build-and-run-traefik build-and-run-tyk build-and-run-webgo build-app build-caddy \
build-dev coverage create-network down env-dev env-prod gatling generate-plantUML golangci-lint health-check-prod help \
lint log tests up validate vendor-plugins
.PHONY: build-and-run-caddy build-and-run-caddy-json build-and-run-chi build-and-run-dotweb build-and-run-echo build-and-run-fiber \
build-and-run-gin build-and-run-go-zero build-and-run-goyave build-and-run-skipper build-and-run-souin build-and-run-traefik \
build-and-run-tyk build-and-run-webgo build-app build-caddy build-dev bump-version coverage create-network down env-dev \
env-prod gatling generate-plantUML golangci-lint health-check-prod help lint log tests up validate vendor-plugins

DC=docker-compose
DC_BUILD=$(DC) build
DC_EXEC=$(DC) exec
PLUGINS_LIST=caddy chi dotweb echo fiber skipper gin go-zero goyave traefik tyk webgo

base-build-and-run-%:
cd plugins/$* && $(MAKE) prepare

build-and-run-caddy: ## Run caddy binary with the Caddyfile configuration
$(MAKE) build-caddy
Expand All @@ -15,32 +19,29 @@ build-and-run-caddy-json: ## Run caddy binary with the json configuration
$(MAKE) build-caddy
cd plugins/caddy && ./caddy run --config ./configuration.json

build-and-run-chi: ## Run Chi with Souin as plugin
cd plugins/chi && $(MAKE) prepare
build-and-run-chi: base-build-and-run-chi ## Run Chi with Souin as plugin

build-and-run-dotweb: base-build-and-run-dotweb ## Run Dotweb with Souin as plugin

build-and-run-echo: base-build-and-run-echo ## Run Echo with Souin as plugin

build-and-run-echo: ## Run Echo with Souin as plugin
cd plugins/echo && $(MAKE) prepare
build-and-run-fiber: base-build-and-run-fiber ## Run Fiber with Souin as plugin

build-and-run-fiber: ## Run Fiber with Souin as plugin
cd plugins/fiber && $(MAKE) prepare
build-and-run-skipper: base-build-and-run-skipper ## Run Skipper with Souin as plugin

build-and-run-skipper: ## Run Skipper with Souin as plugin
cd plugins/skipper && $(MAKE) prepare
build-and-run-souin: base-build-and-run-souin ## Run Souin as plugin

build-and-run-gin: ## Run Gin with Souin as plugin
cd plugins/gin && $(MAKE) prepare
build-and-run-gin: base-build-and-run-gin ## Run Gin with Souin as plugin

build-and-run-goyave: ## Run Goyave with Souin as plugin
cd plugins/goyave && $(MAKE) prepare
build-and-run-go-zero: base-build-and-run-go-zero ## Run Gin with Souin as plugin

build-and-run-traefik: ## Run træfik with Souin as plugin
cd plugins/traefik && $(MAKE) prepare
build-and-run-goyave: base-build-and-run-goyave ## Run Goyave with Souin as plugin

build-and-run-tyk: ## Run tyk with Souin as middleware
cd plugins/tyk && $(MAKE) prepare
build-and-run-traefik: base-build-and-run-traefik ## Run træfik with Souin as plugin

build-and-run-webgo: ## Run Echo with Souin as plugin
cd plugins/webgo && $(MAKE) prepare
build-and-run-tyk: base-build-and-run-tyk ## Run tyk with Souin as middleware

build-and-run-webgo: base-build-and-run-webgo ## Run Webgo with Souin as plugin

build-app: env-prod ## Build containers with prod env vars
$(DC_BUILD) souin
Expand All @@ -56,6 +57,12 @@ build-dev: env-dev ## Build containers with dev env vars
$(DC_BUILD) souin
$(MAKE) up

bump-version:
sed -i '' 's/version: $(from)/version: $(to)/' README.md
for plugin in $(PLUGINS_LIST) ; do \
sed -i '' 's/github.com\/darkweak\/souin $(from)/github.com\/darkweak\/souin $(to)/' plugins/$$plugin/go.mod ; \
done

coverage: ## Show code coverage
$(DC_EXEC) souin go test ./... -coverprofile cover.out
$(DC_EXEC) souin go tool cover -func cover.out
Expand Down Expand Up @@ -104,13 +111,7 @@ up: ## Up containers
validate: lint tests down health-check-prod ## Run lint, tests and ensure prod can build

vendor-plugins: ## Generate and prepare vendors for each plugin
cd plugins/chi && $(MAKE) vendor
cd plugins/echo && $(MAKE) vendor
cd plugins/fiber && $(MAKE) vendor
cd plugins/gin && $(MAKE) vendor
cd plugins/goyave && $(MAKE) vendor
cd plugins/skipper && $(MAKE) vendor
cd plugins/tyk && $(MAKE) vendor
cd plugins/webgo && $(MAKE) vendor
cd plugins/traefik && $(MAKE) vendor
for plugin in $(PLUGINS_LIST) ; do \
cd plugins/$$plugin && ($(MAKE) vendor || true) && cd -; \
done
cd plugins/caddy && go mod tidy && go mod download
66 changes: 55 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
8. [Plugins](#plugins)
8.1. [Caddy module](#caddy-module)
8.2. [Chi middleware](#chi-middleware)
8.3. [Echo middleware](#echo-middleware)
8.4. [Fiber middleware](#fiber-middleware)
8.5. [Gin middleware](#gin-middleware)
8.6. [Goyave middleware](#goyave-middleware)
8.7. [Skipper filter](#skipper-filter)
8.8. [Træfik plugin](#træfik-plugin)
8.9. [Tyk plugin](#tyk-plugin)
8.10. [Webgo middleware](#webgo-middleware)
8.11. [Prestashop plugin](#prestashop-plugin)
8.12. [Wordpress plugin](#wordpress-plugin)
8.3. [Dotweb middleware](#dotweb-middleware)
8.4. [Echo middleware](#echo-middleware)
8.5. [Fiber middleware](#fiber-middleware)
8.6. [Gin middleware](#gin-middleware)
8.7. [Go-zero middleware](#go-zero-middleware)
8.8. [Goyave middleware](#goyave-middleware)
8.9. [Skipper filter](#skipper-filter)
8.10. [Træfik plugin](#træfik-plugin)
8.11. [Tyk plugin](#tyk-plugin)
8.12. [Webgo middleware](#webgo-middleware)
8.13. [Prestashop plugin](#prestashop-plugin)
8.14. [Wordpress plugin](#wordpress-plugin)
9. [Credits](#credits)

[![Travis CI](https://travis-ci.com/Darkweak/Souin.svg?branch=master)](https://travis-ci.com/Darkweak/Souin)
Expand Down Expand Up @@ -477,6 +479,27 @@ func main(){
}
```

### Dotweb middleware
To use Souin as dotweb middleware, you can refer to the [Dotweb plugin integration folder](https://github.com/darkweak/souin/tree/master/plugins/dotweb) to discover how to configure it.
You just have to define a new dotweb router and tell to the instance to use the process method like below:
```go
import (
cache "github.com/darkweak/souin/plugins/dotweb"
"github.com/go-dotweb/dotweb/v5"
)
func main(){
// ...
httpcache := cache.NewHTTPCache(cache.DevDefaultConfiguration)
app.HttpServer.GET("/:p", func(ctx dotweb.Context) error {
return ctx.WriteString("Hello, World 👋!")
}).Use(httpcache)
// ...
}
```

### Echo middleware
To use Souin as echo middleware, you can refer to the [Echo plugin integration folder](https://github.com/darkweak/souin/tree/master/plugins/echo) to discover how to configure it.
You just have to define a new echo router and tell to the instance to use the process method like below:
Expand Down Expand Up @@ -540,6 +563,26 @@ func main(){
}
```

### Go-zero middleware
To use Souin as go-zero middleware, you can refer to the [Go-zero plugin integration folder](https://github.com/darkweak/souin/tree/master/plugins/go-zero) to discover how to configure it.
You just have to give a Condfiguration object to the `NewHTTPCache` method to get a new HTTP cache instance and use the Handle method as a GlobalMiddleware:
```go
import (
"net/http"
cache "github.com/darkweak/souin/plugins/go-zero"
)
func main(){
// ...
httpcache := cache.NewHTTPCache(cache.DevDefaultConfiguration)
server.Use(httpcache.Handle)
// ...
}
```

### Goyave middleware
To use Souin as goyave middleware, you can refer to the [Goyave plugin integration folder](https://github.com/darkweak/souin/tree/master/plugins/goyave) to discover how to configure it.
You just have to start Goyave, define a new goyave router and tell to the router instance to use the Handle method as GlobalMiddleware like below:
Expand Down Expand Up @@ -597,7 +640,7 @@ experimental:
plugins:
souin:
moduleName: github.com/darkweak/souin
version: v1.6.6
version: v1.6.7
```
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 Expand Up @@ -778,3 +821,4 @@ Thanks to these users for contributing or helping this project in any way
* [Kevin Pollet](https://github.com/kevinpollet)
* [Choelzl](https://github.com/choelzl)
* [Menci](https://github.com/menci)
* [Duy Nguyen](https://github.com/duy-nguyen-devops)
7 changes: 5 additions & 2 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ type URL struct {

// CacheProvider config
type CacheProvider struct {
URL string `json:"url" yaml:"url"`
Path string `json:"path" yaml:"path"`
// URL to connect to the storage system.
URL string `json:"url" yaml:"url"`
// Path to the configuration file.
Path string `json:"path" yaml:"path"`
// Declare the cache provider directly in the Souin configuration.
Configuration interface{} `json:"configuration" yaml:"configuration"`
}

Expand Down
Loading

0 comments on commit 3464dd0

Please sign in to comment.