Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaux committed Jul 4, 2021
1 parent d38dca5 commit a2c9759
Show file tree
Hide file tree
Showing 48 changed files with 2,888 additions and 323 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
go-version: [ 1.15, 1.x ]
go-version: [ 1.16, 1.x ]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
name: Main Process
runs-on: ubuntu-latest
env:
GO_VERSION: 1.15
GOLANGCI_LINT_VERSION: v1.33.0
GO_VERSION: 1.16
GOLANGCI_LINT_VERSION: v1.39.0
YAEGI_VERSION: v0.9.8
CGO_ENABLED: 0
defaults:
Expand Down
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
7 changes: 7 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
"testpackage",
"paralleltest",
"tparallel",
"scopelint",
"godox",
"funlen", # Re-enable it
"gocognit", # Re-enable it
"cyclop", # Re-enable it
"exhaustivestruct",
"nilerr",
]

[issues]
Expand Down
13 changes: 7 additions & 6 deletions .traefik.yml
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

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
51 changes: 51 additions & 0 deletions cache/factory.go
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)
}
62 changes: 62 additions & 0 deletions cache/factory_test.go
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)
}
})
}
}
Loading

0 comments on commit a2c9759

Please sign in to comment.