Skip to content
This repository was archived by the owner on Jul 4, 2021. It is now read-only.

Commit 83a7ace

Browse files
authored
Merge pull request #2 from agravelot/develop
2 parents 3c10c37 + 438209e commit 83a7ace

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2737
-320
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea/
22
.DS_Store
3+
cover.html
4+
profile.cov

.traefik.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
displayName: Demo Plugin
1+
displayName: Image optimizer
22
type: middleware
33

4-
import: github.com/traefik/plugindemo
4+
import: github.com/agravelot/image_optimizer
55

6-
summary: '[Demo] Add Request Header'
6+
summary: 'Image optimizer middleware is a middleware designed to optimize image responses on the fly.'
77

88
testData:
9-
Headers:
10-
X-Demo: test
11-
X-URL: '{{URL}}'
9+

Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: lint test vendor clean
1+
.PHONY: lint test vendor clean coverage demo
22

33
export GO111MODULE=on
44

@@ -8,6 +8,8 @@ lint:
88
golangci-lint run
99

1010
test:
11+
rm -fr .cache | true
12+
mkdir .cache
1113
go test -v -cover ./...
1214

1315
yaegi_test:
@@ -17,4 +19,19 @@ vendor:
1719
go mod vendor
1820

1921
clean:
20-
rm -rf ./vendor
22+
rm -rf ./vendor
23+
24+
coverage:
25+
rm profile.cov cover.html && go test -v -coverpkg=./... -coverprofile=profile.cov ./... && go tool cover -html=profile.cov -o cover.html
26+
27+
demo-init:
28+
docker network create traefik-net | true
29+
30+
demo-up: demo-init
31+
docker-compose --env-file=demo/.env -f demo/gateway/docker-compose.yml -f demo/frontend/docker-compose.yml -f demo/imaginary/docker-compose.yml up -d
32+
33+
demo-restart: demo-init
34+
docker-compose --env-file=demo/.env -f demo/gateway/docker-compose.yml -f demo/frontend/docker-compose.yml -f demo/imaginary/docker-compose.yml restart
35+
36+
demo-logs: demo-up
37+
docker-compose --env-file=demo/.env -f demo/gateway/docker-compose.yml -f demo/frontend/docker-compose.yml -f demo/imaginary/docker-compose.yml logs -f gateway imaginary

cache/factory.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cache
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"time"
7+
8+
"github.com/agravelot/image_optimizer/config"
9+
)
10+
11+
type Cache interface {
12+
Get(key string) ([]byte, error)
13+
Set(key string, val []byte, expiry time.Duration) error
14+
}
15+
16+
func New(conf config.Config) (Cache, error) {
17+
// if conf.Processor == "redis" {
18+
// opt, err := redis.ParseURL(conf.Redis.Url)
19+
// if err != nil {
20+
// return nil, err
21+
// }
22+
23+
// client := redis.NewClient(opt)
24+
25+
// return &RedisCache{
26+
// client: client,
27+
// }, nil
28+
// }
29+
30+
if conf.Cache == "file" {
31+
return newFileCache(conf.File.Path, 100*time.Second)
32+
}
33+
34+
if conf.Cache == "memory" {
35+
return &MemoryCache{
36+
m: map[string][]byte{},
37+
mtx: sync.RWMutex{},
38+
}, nil
39+
}
40+
41+
if conf.Cache == "none" || conf.Cache == "" {
42+
return &NoneCache{}, nil
43+
}
44+
45+
return nil, fmt.Errorf("unable to resolve given cache %s", conf.Cache)
46+
}

cache/factory_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cache_test
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/agravelot/image_optimizer/cache"
8+
"github.com/agravelot/image_optimizer/config"
9+
)
10+
11+
func TestNew(t *testing.T) {
12+
type args struct {
13+
conf config.Config
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
want cache.Cache
19+
wantErr bool
20+
}{
21+
{
22+
name: "should be able to memory cache",
23+
args: args{config.Config{Processor: "none", Cache: "memory"}},
24+
want: &cache.MemoryCache{},
25+
wantErr: false,
26+
},
27+
{
28+
name: "should be able to memory cache",
29+
args: args{config.Config{Processor: "none", Cache: "none"}},
30+
want: &cache.NoneCache{},
31+
wantErr: false,
32+
},
33+
{
34+
name: "should not be able to init cache without valid driver",
35+
args: args{config.Config{Processor: "imaginary", Imaginary: config.ImaginaryProcessorConfig{Url: "http://localhost"}, Cache: "unsupported"}},
36+
want: nil,
37+
wantErr: true,
38+
},
39+
// {
40+
// name: "should not be able to init imaginary without valid url",
41+
// args: args{config.Config{Processor: "imaginary", Imaginary: config.ImaginaryConfig{Url: "localhost"}, Cache: "memory"}},
42+
// want: nil,
43+
// wantErr: true,
44+
// },
45+
// {
46+
// name: "should not be able to init imaginary without valid url 2 ",
47+
// args: args{config.Config{Processor: "imaginary", Imaginary: config.ImaginaryConfig{Url: "htt://localhost"}}},
48+
// want: nil,
49+
// wantErr: true,
50+
// },
51+
// {
52+
// name: "should not be able to init imaginary without url",
53+
// args: args{config.Config{Processor: "imaginary"}},
54+
// want: nil,
55+
// wantErr: true,
56+
// },
57+
// {
58+
// name: "should be able to return local optimizer",
59+
// args: args{config.Config{Processor: "local"}},
60+
// want: &processor.LocalProcessor{},
61+
// wantErr: false,
62+
// },
63+
// {
64+
// name: "should return error with unsupported processor",
65+
// args: args{config.Config{Processor: "unsupported"}},
66+
// want: nil,
67+
// wantErr: true,
68+
// },
69+
// {
70+
// name: "should return error with empty processor",
71+
// args: args{config.Config{Processor: "unsupported"}},
72+
// want: nil,
73+
// wantErr: true,
74+
// },
75+
}
76+
for _, tt := range tests {
77+
t.Run(tt.name, func(t *testing.T) {
78+
got, err := cache.New(tt.args.conf)
79+
if (err != nil) != tt.wantErr {
80+
t.Fatalf("New() error = %v, wantErr %v", err, tt.wantErr)
81+
}
82+
83+
typeGot := reflect.TypeOf(got)
84+
typeWanted := reflect.TypeOf(tt.want)
85+
86+
if typeGot != typeWanted {
87+
t.Errorf("New() = %v, want %v", typeGot, typeWanted)
88+
}
89+
})
90+
}
91+
}

0 commit comments

Comments
 (0)