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

Commit

Permalink
Merge pull request #2 from agravelot/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
agravelot authored May 4, 2021
2 parents 3c10c37 + 438209e commit 83a7ace
Show file tree
Hide file tree
Showing 44 changed files with 2,737 additions and 320 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
.DS_Store
cover.html
profile.cov
10 changes: 4 additions & 6 deletions .traefik.yml
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}}'

21 changes: 19 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: lint test vendor clean
.PHONY: lint test vendor clean coverage demo

export GO111MODULE=on

Expand All @@ -8,6 +8,8 @@ lint:
golangci-lint run

test:
rm -fr .cache | true
mkdir .cache
go test -v -cover ./...

yaegi_test:
Expand All @@ -17,4 +19,19 @@ vendor:
go mod vendor

clean:
rm -rf ./vendor
rm -rf ./vendor

coverage:
rm profile.cov cover.html && go test -v -coverpkg=./... -coverprofile=profile.cov ./... && go tool cover -html=profile.cov -o cover.html

demo-init:
docker network create traefik-net | true

demo-up: demo-init
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

demo-restart: demo-init
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

demo-logs: demo-up
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
46 changes: 46 additions & 0 deletions cache/factory.go
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)
}
91 changes: 91 additions & 0 deletions cache/factory_test.go
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)
}
})
}
}
Loading

0 comments on commit 83a7ace

Please sign in to comment.