This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from agravelot/develop
- Loading branch information
Showing
44 changed files
with
2,737 additions
and
320 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
displayName: Demo Plugin | ||
displayName: Image optimizer | ||
type: middleware | ||
|
||
import: github.com/traefik/plugindemo | ||
import: github.com/agravelot/image_optimizer | ||
|
||
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}}' | ||
|
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,46 @@ | ||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/agravelot/image_optimizer/config" | ||
) | ||
|
||
type Cache interface { | ||
Get(key string) ([]byte, error) | ||
Set(key string, val []byte, expiry time.Duration) error | ||
} | ||
|
||
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, 100*time.Second) | ||
} | ||
|
||
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,91 @@ | ||
package cache_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/agravelot/image_optimizer/cache" | ||
"github.com/agravelot/image_optimizer/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, | ||
}, | ||
// { | ||
// name: "should not be able to init imaginary without valid url", | ||
// args: args{config.Config{Processor: "imaginary", Imaginary: config.ImaginaryConfig{Url: "localhost"}, Cache: "memory"}}, | ||
// want: nil, | ||
// wantErr: true, | ||
// }, | ||
// { | ||
// name: "should not be able to init imaginary without valid url 2 ", | ||
// args: args{config.Config{Processor: "imaginary", Imaginary: config.ImaginaryConfig{Url: "htt://localhost"}}}, | ||
// want: nil, | ||
// wantErr: true, | ||
// }, | ||
// { | ||
// name: "should not be able to init imaginary without url", | ||
// args: args{config.Config{Processor: "imaginary"}}, | ||
// want: nil, | ||
// wantErr: true, | ||
// }, | ||
// { | ||
// name: "should be able to return local optimizer", | ||
// args: args{config.Config{Processor: "local"}}, | ||
// want: &processor.LocalProcessor{}, | ||
// wantErr: false, | ||
// }, | ||
// { | ||
// name: "should return error with unsupported processor", | ||
// args: args{config.Config{Processor: "unsupported"}}, | ||
// want: nil, | ||
// wantErr: true, | ||
// }, | ||
// { | ||
// name: "should return error with empty processor", | ||
// args: args{config.Config{Processor: "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.