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

Support badger full configuration from Souin configuration file #101

Merged
merged 3 commits into from
Aug 17, 2021
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion api/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import (
"encoding/json"
"github.com/dgrijalva/jwt-go"
"github.com/golang-jwt/jwt"
"net/http"
"time"
)
Expand Down
22 changes: 20 additions & 2 deletions cache/providers/badgerProvider.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package providers

import (
"encoding/json"
"fmt"
t "github.com/darkweak/souin/configurationtypes"
badger "github.com/dgraph-io/badger/v3"
"github.com/imdario/mergo"
"net/http"
"regexp"
"time"
Expand All @@ -15,8 +17,24 @@ type Badger struct {
}

// BadgerConnectionFactory function create new Badger instance
func BadgerConnectionFactory(_ t.AbstractConfigurationInterface) (*Badger, error) {
db, _ := badger.Open(badger.DefaultOptions("").WithInMemory(true))
func BadgerConnectionFactory(c t.AbstractConfigurationInterface) (*Badger, error) {
badgerConfiguration := c.GetDefaultCache().GetBadger()
badgerOptions := badger.DefaultOptions(badgerConfiguration.Path)
if badgerConfiguration.Configuration != nil {
var parsedBadger badger.Options
if b, e := json.Marshal(badgerConfiguration.Configuration); e == nil {
if e = json.Unmarshal(b, &parsedBadger); e != nil {
fmt.Println("Impossible to parse the configuration for the default provider (Badger)")
}
}

if err := mergo.Merge(&badgerOptions, parsedBadger, mergo.WithOverride); err != nil {
fmt.Println("An error occurred during the badgerOptions merge from the default options with your configuration.")
}
} else {
badgerOptions = badgerOptions.WithInMemory(true)
}
db, _ := badger.Open(badgerOptions)

return &Badger{db}, nil
}
Expand Down
15 changes: 15 additions & 0 deletions cache/providers/badgerProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func getBadgerClientAndMatchedURL(key string) (types.AbstractProviderInterface,
)
}

// This test ensure that Badger options are override by the Souin configuration
func TestCustomBadgerConnectionFactory(t *testing.T) {
c := tests.MockConfiguration(tests.BadgerConfiguration)
r, err := BadgerConnectionFactory(c)

if nil != err {
errors.GenerateError(t, "Shouldn't have panic")
}

if nil == r {
errors.GenerateError(t, "Badger should be instanciated")
}
}

func TestBadgerConnectionFactory(t *testing.T) {
c := tests.MockConfiguration(tests.BaseConfiguration)
r, err := BadgerConnectionFactory(c)
Expand All @@ -46,6 +60,7 @@ func TestBadgerConnectionFactory(t *testing.T) {
func TestIShouldBeAbleToReadAndWriteDataInBadger(t *testing.T) {
client, matchedURL := getBadgerClientAndMatchedURL("Test")

fmt.Println(client, matchedURL)
client.Set("Test", []byte(BADGERVALUE), matchedURL, time.Duration(20)*time.Second)
time.Sleep(1 * time.Second)

Expand Down
7 changes: 7 additions & 0 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type CacheProvider struct {

//DefaultCache configuration
type DefaultCache struct {
Badger CacheProvider `json:"badger" yaml:"badger"`
Distributed bool `json:"distributed" yaml:"distributed"`
Headers []string `json:"headers" yaml:"headers"`
Olric CacheProvider `json:"olric" yaml:"olric"`
Expand All @@ -77,6 +78,11 @@ type DefaultCache struct {
TTL Duration `json:"ttl" yaml:"ttl"`
}

// GetBadger returns the Badger configuration
func (d *DefaultCache) GetBadger() CacheProvider {
return d.Badger
}

// GetDistributed returns if it uses Olric or not as provider
func (d *DefaultCache) GetDistributed() bool {
return d.Distributed
Expand Down Expand Up @@ -104,6 +110,7 @@ func (d *DefaultCache) GetTTL() time.Duration {

// DefaultCacheInterface interface
type DefaultCacheInterface interface {
GetBadger() CacheProvider
GetDistributed() bool
GetOlric() CacheProvider
GetHeaders() []string
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ require (
github.com/buraksezer/olric v0.3.11
github.com/dgraph-io/badger/v3 v3.2103.1
github.com/dgraph-io/ristretto v0.1.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fsnotify/fsnotify v1.4.9
github.com/go-chi/stampede v0.4.5
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/uuid v1.3.0
github.com/imdario/mergo v0.3.12
github.com/pquerna/cachecontrol v0.1.0
go.uber.org/zap v1.19.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ github.com/dgraph-io/badger/v3 v3.2103.1 h1:zaX53IRg7ycxVlkd5pYdCeFp1FynD6qBGQoQ
github.com/dgraph-io/badger/v3 v3.2103.1/go.mod h1:dULbq6ehJ5K0cGW/1TQ9iSfUk0gbSiToDWmWmTsJ53E=
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
Expand All @@ -47,6 +45,8 @@ github.com/go-chi/stampede v0.4.5 h1:/qEiOLkpBstcdVnlYfg7TEWfL4It3ruXYDKpJkmkZMQ
github.com/go-chi/stampede v0.4.5/go.mod h1:26lupLoxsX2qfPW6QCOEj8Gb1/KWpbH5XysNAoNojsU=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
Expand Down Expand Up @@ -86,6 +86,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/memberlist v0.1.5 h1:AYBsgJOW9gab/toO5tEB8lWetVgDKZycqkebJ8xxpqM=
github.com/hashicorp/memberlist v0.1.5/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
6 changes: 6 additions & 0 deletions plugins/caddy/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (

// DefaultCache the struct
type DefaultCache struct {
Badger configurationtypes.CacheProvider
Distributed bool
Headers []string
Olric configurationtypes.CacheProvider
Regex configurationtypes.Regex
TTL time.Duration
}

// GetBadger returns the Badger configuration
func (d *DefaultCache) GetBadger() configurationtypes.CacheProvider {
return d.Badger
}

// GetDistributed returns if it uses Olric or not as provider
func (d *DefaultCache) GetDistributed() bool {
return d.Distributed
Expand Down
2 changes: 1 addition & 1 deletion plugins/caddy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/darkweak/souin/plugins/caddy
go 1.15

require (
github.com/caddyserver/caddy/v2 v2.4.0-beta.2
github.com/caddyserver/caddy/v2 v2.4.3
github.com/darkweak/souin v1.5.2
go.uber.org/zap v1.19.0
)
Expand Down
Loading