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 #9 from agravelot/ci/linter-happy
Browse files Browse the repository at this point in the history
  • Loading branch information
agravelot authored May 5, 2021
2 parents 8f06825 + 7b11924 commit de58320
Show file tree
Hide file tree
Showing 27 changed files with 250 additions and 176 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
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
2 changes: 1 addition & 1 deletion .traefik.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
displayName: Image optimizer
type: middleware

import: github.com/agravelot/image_optimizer
import: github.com/agravelot/imageopti

summary: 'Image optimizer middleware is a middleware designed to optimize image responses on the fly.'

Expand Down
11 changes: 8 additions & 3 deletions cache/factory.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
// Package cache provide caching systems for images.
package cache

import (
"fmt"
"sync"
"time"

"github.com/agravelot/image_optimizer/config"
"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)
// opt, err := redis.ParseURL(conf.Redis.URL)
// if err != nil {
// return nil, err
// }
Expand All @@ -28,7 +33,7 @@ func New(conf config.Config) (Cache, error) {
// }

if conf.Cache == "file" {
return newFileCache(conf.File.Path, 100*time.Second)
return newFileCache(conf.File.Path, defaultCacheExpiry)
}

if conf.Cache == "memory" {
Expand Down
51 changes: 11 additions & 40 deletions cache/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/agravelot/image_optimizer/cache"
"github.com/agravelot/image_optimizer/config"
"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
Expand All @@ -31,47 +32,17 @@ func TestNew(t *testing.T) {
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"}},
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) {
Expand Down
8 changes: 4 additions & 4 deletions cache/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ func BenchmarkFileCache_Get(b *testing.B) {
}
}

func createTempDir(t testing.TB) string {
func createTempDir(tb testing.TB) string {
dir, err := ioutil.TempDir("./", "example")
if err != nil {
t.Fatal(err)
tb.Fatal(err)
}

t.Cleanup(func() {
tb.Cleanup(func() {
if err = os.RemoveAll(dir); err != nil {
t.Fatal(err)
tb.Fatal(err)
}
})

Expand Down
4 changes: 4 additions & 0 deletions cache/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"time"
)

// MemoryCache in-memory cache system struct.
type MemoryCache struct {
mtx sync.RWMutex
m map[string][]byte
}

// Get return cached image with given key.
func (c *MemoryCache) Get(key string) ([]byte, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
Expand All @@ -19,9 +21,11 @@ func (c *MemoryCache) Get(key string) ([]byte, error) {
if !ok {
return nil, fmt.Errorf("no result found with key = %s", key)
}

return v, nil
}

// Set add a value into in-memory with custom expiry.
func (c *MemoryCache) Set(key string, v []byte, expiry time.Duration) error {
c.mtx.Lock()
defer c.mtx.Unlock()
Expand Down
8 changes: 7 additions & 1 deletion cache/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ func TestMemoryCache_Get(t *testing.T) {
type fields struct {
m map[string][]byte
}

type args struct {
key string
}

tests := []struct {
name string
fields fields
Expand Down Expand Up @@ -62,10 +64,12 @@ func TestMemoryCache_Set(t *testing.T) {
type fields struct {
m map[string][]byte
}

type args struct {
key string
v []byte
}

tests := []struct {
name string
fields fields
Expand Down Expand Up @@ -108,7 +112,7 @@ func TestMemoryCache_Set(t *testing.T) {

time.Sleep(1 * time.Second)

v, err = c.Get(tt.args.key)
_, err = c.Get(tt.args.key)
if err == nil {
t.Errorf("value must be deleted after expiry")
}
Expand Down Expand Up @@ -174,9 +178,11 @@ func TestMemoryCache_delete(t *testing.T) {
type fields struct {
m map[string][]byte
}

type args struct {
key string
}

tests := []struct {
name string
fields fields
Expand Down
6 changes: 4 additions & 2 deletions cache/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"time"
)

type NoneCache struct {
}
// NoneCache dummy cache system.
type NoneCache struct{}

// Get always return nil with not found error.
func (c *NoneCache) Get(key string) ([]byte, error) {
return nil, fmt.Errorf("no result found with key = %s", key)
}

// Set always return nil.
func (c *NoneCache) Set(_ string, _ []byte, _ time.Duration) error {
return nil
}
10 changes: 4 additions & 6 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package cache

import "time"

// var ctx = context.Background()

type RedisCache struct {
// client *redis.Client
}
// RedisCache hold redis client.
type RedisCache struct{} // client *redis.Client

// Get return cached media with given key from redis.
func (c *RedisCache) Get(key string) ([]byte, error) {

// v, err := c.client.Get(ctx, key).Bytes()

// if err == redis.Nil {
Expand All @@ -23,6 +20,7 @@ func (c *RedisCache) Get(key string) ([]byte, error) {
return []byte("unsafe not supported by yaegi"), nil
}

// Set add a new image in cache with custom expiry.
func (c *RedisCache) Set(key string, v []byte, expiry time.Duration) error {
// return c.client.Set(ctx, key, v, expiry).Err()
return nil
Expand Down
1 change: 1 addition & 0 deletions cache/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
)

// Tokenize generate unique key for request caching strategy.
func Tokenize(req *http.Request) (string, error) {
width := req.URL.Query().Get("w")

Expand Down
2 changes: 2 additions & 0 deletions cache/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ func TestTokenize(t *testing.T) {
if err != nil {
t.Fatal(err)
}

return req
}

type args struct {
req *http.Request
}

tests := []struct {
name string
args args
Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Package config provide configurations structs for imageopti middleware.
package config

// ImaginaryProcessorConfig define imaginary image processor configurations.
type ImaginaryProcessorConfig struct {
Url string `json:"url" yaml:"url" toml:"url"`
URL string `json:"url" yaml:"url" toml:"url"`
}

// RedisCacheConfig define redis cache system configurations.
type RedisCacheConfig struct {
Url string `json:"url" yaml:"url" toml:"url"`
URL string `json:"url" yaml:"url" toml:"url"`
}

// FileCacheConfig define file cache system configurations.
type FileCacheConfig struct {
Path string `json:"path" yaml:"path" toml:"path"`
}
Expand Down
10 changes: 5 additions & 5 deletions demo/frontend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ services:
labels:
- "traefik.enable=true"
- "traefik.http.routers.demo.rule=Host(`demo.localhost`)"
- "traefik.http.middlewares.image_optimizer.plugin.dev.config.processor=imaginary"
- "traefik.http.middlewares.image_optimizer.plugin.dev.config.imaginary.url=http://imaginary:9000"
- "traefik.http.middlewares.image_optimizer.plugin.dev.config.cache=file"
- "traefik.http.middlewares.image_optimizer.plugin.dev.config.file.path=/root"
- "traefik.http.routers.demo.middlewares=image_optimizer"
- "traefik.http.middlewares.imageopti.plugin.dev.config.processor=imaginary"
- "traefik.http.middlewares.imageopti.plugin.dev.config.imaginary.url=http://imaginary:9000"
- "traefik.http.middlewares.imageopti.plugin.dev.config.cache=file"
- "traefik.http.middlewares.imageopti.plugin.dev.config.file.path=/root"
- "traefik.http.routers.demo.middlewares=imageopti"

networks:
traefik-net:
Expand Down
4 changes: 2 additions & 2 deletions demo/gateway/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ services:
- "--entryPoints.metrics.address=:8082"
- "--metrics.prometheus.entryPoint=metrics"
- "--experimental.devPlugin.goPath=/plugins"
- "--experimental.devPlugin.moduleName=github.com/agravelot/image_optimizer"
- "--experimental.devPlugin.moduleName=github.com/agravelot/imageopti"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- "/home/agravelot/Lab/traefik-image-optimization:/plugins/src/github.com/agravelot/image_optimizer:rw,delegated"
- "/home/agravelot/Lab/traefik-image-optimization:/plugins/src/github.com/agravelot/imageopti:rw,delegated"
# - "./gateway/traefik.yml:/etc/traefik/traefik.yml:ro,delegated"
- "letsencrypt:/letsencrypt:rw,delegated"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
Expand Down
2 changes: 1 addition & 1 deletion demo/gateway/traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ global:
experimental:
devPlugin:
goPath: /plugins
moduleName: image_optimizer
moduleName: imageopti

entryPoints:
web:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/agravelot/image_optimizer
module github.com/agravelot/imageopti

go 1.16
Loading

0 comments on commit de58320

Please sign in to comment.