generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
2,888 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.idea/ | ||
.DS_Store | ||
cover.html | ||
profile.cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
displayName: Demo Plugin | ||
displayName: Image optimizer | ||
type: middleware | ||
|
||
import: github.com/traefik/plugindemo | ||
import: github.com/agravelot/imageopti | ||
|
||
summary: '[Demo] Add Request Header' | ||
summary: 'Image optimizer middleware is a middleware designed to optimize image responses on the fly.' | ||
|
||
testData: | ||
Headers: | ||
X-Demo: test | ||
X-URL: '{{URL}}' | ||
config: | ||
processor: none | ||
cache: none | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Package cache provide caching systems for images. | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/agravelot/imageopti/config" | ||
) | ||
|
||
// Cache Define cache system interface. | ||
type Cache interface { | ||
Get(key string) ([]byte, error) | ||
Set(key string, val []byte, expiry time.Duration) error | ||
} | ||
|
||
const defaultCacheExpiry = 100 * time.Second | ||
|
||
// New is the cache factory to instantiate a new instance of cache. | ||
func New(conf config.Config) (Cache, error) { | ||
// if conf.Processor == "redis" { | ||
// opt, err := redis.ParseURL(conf.Redis.URL) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// client := redis.NewClient(opt) | ||
|
||
// return &RedisCache{ | ||
// client: client, | ||
// }, nil | ||
// } | ||
|
||
if conf.Cache == "file" { | ||
return newFileCache(conf.File.Path, defaultCacheExpiry) | ||
} | ||
|
||
if conf.Cache == "memory" { | ||
return &MemoryCache{ | ||
m: map[string][]byte{}, | ||
mtx: sync.RWMutex{}, | ||
}, nil | ||
} | ||
|
||
if conf.Cache == "none" || conf.Cache == "" { | ||
return &NoneCache{}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("unable to resolve given cache %s", conf.Cache) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cache_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/agravelot/imageopti/cache" | ||
"github.com/agravelot/imageopti/config" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
type args struct { | ||
conf config.Config | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want cache.Cache | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "should be able to memory cache", | ||
args: args{config.Config{Processor: "none", Cache: "memory"}}, | ||
want: &cache.MemoryCache{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "should be able to memory cache", | ||
args: args{config.Config{Processor: "none", Cache: "none"}}, | ||
want: &cache.NoneCache{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "should not be able to init cache without valid driver", | ||
args: args{ | ||
config.Config{ | ||
Processor: "imaginary", | ||
Imaginary: config.ImaginaryProcessorConfig{URL: "http://localhost"}, | ||
Cache: "unsupported", | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := cache.New(tt.args.conf) | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("New() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
typeGot := reflect.TypeOf(got) | ||
typeWanted := reflect.TypeOf(tt.want) | ||
|
||
if typeGot != typeWanted { | ||
t.Errorf("New() = %v, want %v", typeGot, typeWanted) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.