Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): Enhance caddy documentation #206

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.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-souin 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-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 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
Expand All @@ -18,6 +18,9 @@ build-and-run-caddy-json: ## Run caddy binary with the json configuration
build-and-run-chi: ## Run Chi with Souin as plugin
cd plugins/chi && $(MAKE) prepare

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

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

Expand Down Expand Up @@ -108,6 +111,7 @@ validate: lint tests down health-check-prod ## Run lint, tests and ensure prod c

vendor-plugins: ## Generate and prepare vendors for each plugin
cd plugins/chi && $(MAKE) vendor
cd plugins/dotweb && $(MAKE) vendor
cd plugins/echo && $(MAKE) vendor
cd plugins/fiber && $(MAKE) vendor
cd plugins/gin && $(MAKE) vendor
Expand Down
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
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. [Goyave middleware](#goyave-middleware)
8.8. [Skipper filter](#skipper-filter)
8.9. [Træfik plugin](#træfik-plugin)
8.10. [Tyk plugin](#tyk-plugin)
8.11. [Webgo middleware](#webgo-middleware)
8.12. [Prestashop plugin](#prestashop-plugin)
8.13. [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 +478,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
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
159 changes: 159 additions & 0 deletions docs/e2e/Souin E2E.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,161 @@
}
]
},
{
"name": "Dotweb",
"item": [
{
"name": "Default",
"event": [
{
"listen": "test",
"script": {
"exec": [
"utils.baseEndpoint(pm, `${utils.getVar(pm, 'dotweb_url')}/default`, '', true)"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "Cache-Control",
"value": "",
"type": "text"
}
],
"url": {
"raw": "{{dotweb_url}}/default",
"host": [
"{{dotweb_url}}"
],
"path": [
"default"
]
}
},
"response": []
},
{
"name": "Default no cache",
"event": [
{
"listen": "test",
"script": {
"exec": [
"utils.baseEndpoint(pm, `${utils.getVar(pm, 'dotweb_url')}/default`, 'no-cache')"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{dotweb_url}}/default",
"host": [
"{{dotweb_url}}"
],
"path": [
"default"
]
}
},
"response": []
},
{
"name": "Default no store",
"event": [
{
"listen": "test",
"script": {
"exec": [
"utils.baseEndpoint(pm, `${utils.getVar(pm, 'dotweb_url')}/default`, 'no-store')"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "Cache-Control",
"value": "no-store",
"type": "text"
}
],
"url": {
"raw": "{{dotweb_url}}/default",
"host": [
"{{dotweb_url}}"
],
"path": [
"default"
]
}
},
"response": []
},
{
"name": "Exclude regex",
"event": [
{
"listen": "test",
"script": {
"exec": [
"utils.baseEndpoint(pm, `${utils.getVar(pm, 'dotweb_url')}/test2`, '', false, true)"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "Cache-Control",
"value": "",
"type": "text"
}
],
"url": {
"raw": "{{dotweb_url}}/excluded",
"host": [
"{{dotweb_url}}"
],
"path": [
"excluded"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
},
{
"name": "Fiber",
"item": [
Expand Down Expand Up @@ -2193,6 +2348,10 @@
{
"key": "souin_url",
"value": "http://domain.com"
},
{
"key": "dotweb_url",
"value": "http://domain.com"
}
]
}
30 changes: 19 additions & 11 deletions plugins/caddy/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,27 @@ func init() {
httpcaddyfile.RegisterHandlerDirective(moduleName, parseCaddyfileHandlerDirective)
}

// SouinCaddyPlugin declaration.
// SouinCaddyPlugin allows the user to set up an HTTP cache system, RFC-7234 compliant.
// Supports the tag based cache purge, distributed and not-distributed storage.
type SouinCaddyPlugin struct {
plugins.SouinBasePlugin
Configuration *Configuration
logger *zap.Logger
LogLevel string `json:"log_level,omitempty"`
bufPool *sync.Pool
AllowedHTTPVerbs []string `json:"allowed_http_verbs,omitempty"`
Headers []string `json:"headers,omitempty"`
Badger configurationtypes.CacheProvider `json:"badger,omitempty"`
Olric configurationtypes.CacheProvider `json:"olric,omitempty"`
TTL configurationtypes.Duration `json:"ttl,omitempty"`
DefaultCacheControl string `json:"default_cache_control,omitempty"`
Configuration *Configuration
logger *zap.Logger
// Log level.
LogLevel string `json:"log_level,omitempty"`
bufPool *sync.Pool
// Allowed HTTP verbs to be cached by the system.
AllowedHTTPVerbs []string `json:"allowed_http_verbs,omitempty"`
// Headers to add to the cache key if they are present.
Headers []string `json:"headers,omitempty"`
// Configure the Badger cache storage.
Badger configurationtypes.CacheProvider `json:"badger,omitempty"`
// Enable the Olric distributed cache storage.
Olric configurationtypes.CacheProvider `json:"olric,omitempty"`
// Time to live for a key, using time.duration.
TTL configurationtypes.Duration `json:"ttl,omitempty"`
// The default Cache-Control header value if none set by the upstream server.
DefaultCacheControl string `json:"default_cache_control,omitempty"`
}

// CaddyModule returns the Caddy module information.
Expand Down
8 changes: 8 additions & 0 deletions plugins/dotweb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: prepare vendor

prepare: vendor ## Prepare dotweb plugin
cd examples && cp docker-compose.yml.test docker-compose.yml && docker-compose up -d

vendor: ## Update the go mods for the plugin
go mod tidy
go mod download
43 changes: 43 additions & 0 deletions plugins/dotweb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Dotweb middleware: Souin
================================

This is a distributed HTTP cache module for Dotweb based on [Souin](https://github.com/darkweak/souin) cache.

## Features

* [RFC 7234](https://httpwg.org/specs/rfc7234.html) compliant HTTP Cache.
* Sets [the `Cache-Status` HTTP Response Header](https://httpwg.org/http-extensions/draft-ietf-httpbis-cache-header.html)
* REST API to purge the cache and list stored resources.
* Builtin support for distributed cache.
* Tag-based invalidation.


## Example
There is the example about the Souin initialization.
```go
import (
"net/http"

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)
// ...

}
```
With that your application will be able to cache the responses if possible and returns at least the `Cache-Status` HTTP header with the different directives mentionned in the RFC specification.
You have to pass a Dotweb `Configuration` structure into the `NewHTTPCache` method (you can use the `DefaultConfiguration` variable to have a built-in production ready configuration).
See the full detailled configuration names [here](https://github.com/darkweak/souin#optional-configuration).

Other resources
---------------
You can find an example for a docker-compose stack inside the `examples` folder.
See the [Souin](https://github.com/darkweak/souin) configuration for the full configuration, and its associated [development dotweb middleware](https://github.com/darkweak/souin/blob/master/plugins/dotweb)
Loading