kratos

package module
v0.0.0-...-d399288 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 7 Imported by: 0

README

Kratos middleware: Souin

This is a distributed HTTP cache module for Kratos based on Souin cache.

Features

Example

There is the example about the Souin initialization.

import (
	httpcache "github.com/darkweak/souin/plugins/kratos"
	kratos_http "github.com/go-kratos/kratos/v2/transport/http"
)

func main() {
	kratos_http.NewServer(
		kratos_http.Filter(
			httpcache.NewHTTPCacheFilter(httpcache.DevDefaultConfiguration),
		),
	)
}

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 Kratos Configuration structure into the New method (you can use the DefaultConfiguration variable to have a built-in production ready configuration).
See the full detailled configuration names here.

You can also use the configuration file to configuration the HTTP cache. Refer to the code block below:

server: #...
data: #...
# HTTP cache part
httpcache:
  api:
    souin: {}
  default_cache:
    regex:
      exclude: /excluded
    ttl: 5s
  log_level: debug

After that you have to edit your server instanciation to use the HTTP cache configuration parser

import (
	httpcache "github.com/darkweak/souin/plugins/kratos"
	kratos_http "github.com/go-kratos/kratos/v2/transport/http"
)

func main() {
  c := config.New(
		config.WithSource(file.NewSource("examples/configuration.yml")),
		config.WithDecoder(func(kv *config.KeyValue, v map[string]interface{}) error {
			return yaml.Unmarshal(kv.Value, v)
		}),
	)
	if err := c.Load(); err != nil {
		panic(err)
	}

	server := kratos_http.NewServer(
		kratos_http.Filter(
			httpcache.NewHTTPCacheFilter(httpcache.ParseConfiguration(c)),
		),
	)
  // ...
}

Other resources

You can find an example for a docker-compose stack inside the examples folder.
See the Souin configuration for the full configuration, and its associated development kratos middleware

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPCacheFilter

func NewHTTPCacheFilter(c middleware.BaseConfiguration) kratos_http.FilterFunc

NewHTTPCacheFilter, allows the user to set up an HTTP cache system, RFC-7234 compliant and supports the tag based cache purge, distributed and not-distributed storage, key generation tweaking. Use it with httpcache.NewHTTPCacheFilter(httpcache.ParseConfiguration(config))

func ParseConfiguration

func ParseConfiguration(c config.Config) middleware.BaseConfiguration

ParseConfiguration parse the Kratos configuration into a valid HTTP cache configuration object.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL