Skip to content

Commit 3209009

Browse files
committed
Souin minimal with Traefik plugin support (#18)
* POC minimal version * Refacto & performance update * Update tests * Implement Træfik plugin based on minimal version (#16) Add vendor for traefik plugin
1 parent 04496b6 commit 3209009

404 files changed

Lines changed: 175206 additions & 961 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
docker-compose.yml
44
Dockerfile
55
/.idea/
6-
/vendor/
76
.DS_Store
87
traefik.json

.traefik.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
displayName: Souin
2+
type: middleware
3+
import: github.com/darkweak/souin/plugins/traefik
4+
summary: 'Souin is a powerfull cache system as fast as Varnish but easier to configure'
5+
6+
# Refer to https://github.com/darkweak/Souin/configuration/configuration.sample.yml for full configuration
7+
testData:
8+
default_cache:
9+
headers:
10+
- Authorization
11+
regex:
12+
exclude: 'ARegexHere'
13+
ttl: 1000
14+
urls:
15+
'domain.com/':
16+
ttl: 1000
17+
headers:
18+
- Authorization

Dockerfile-dev

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
FROM golang:1.13-alpine AS souin
1+
FROM golang:1.15-alpine AS souin
22

33
RUN apk update && apk upgrade && \
44
apk add --no-cache bash git openssh gcc libc-dev
55
ENV GOPATH /app
66
RUN go get -u \
77
golang.org/x/lint/golint \
8-
github.com/allegro/bigcache \
98
github.com/dgraph-io/ristretto \
109
github.com/fsnotify/fsnotify \
11-
github.com/go-redis/redis \
12-
gopkg.in/yaml.v2
10+
gopkg.in/yaml.v3
1311

1412
RUN mkdir -p /app/src/github.com/darkweak/souin
1513
RUN mkdir -p /ssl
1614
ADD ./*.go /app/src/github.com/darkweak/souin/
1715
ADD ./cache /app/src/github.com/darkweak/souin/cache
16+
ADD ./configuration /app/src/github.com/darkweak/souin/configuration
17+
ADD ./configuration_types /app/src/github.com/darkweak/souin/configuration_types
18+
ADD ./default /app/src/github.com/darkweak/souin/default
1819
ADD ./errors /app/src/github.com/darkweak/souin/errors
20+
ADD ./helpers /app/src/github.com/darkweak/souin/helpers
1921
ADD ./providers /app/src/github.com/darkweak/souin/providers
20-
ADD ./default/server.* /app/src/github.com/darkweak/souin/
21-
ADD ./configuration/configuration.yml /
22-
ADD ./configuration /app/src/github.com/darkweak/souin/configuration
2322

2423
WORKDIR /app/src/github.com/darkweak/souin
2524

Dockerfile-prod

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.13-alpine AS builder
1+
FROM golang:1.15-alpine AS builder
22

33
RUN apk update && apk upgrade && \
44
apk add --no-cache bash git openssh gcc libc-dev
@@ -7,22 +7,21 @@ ENV GOOS linux
77
ENV GOARCH arm64
88
RUN go get -u \
99
golang.org/x/lint/golint \
10-
github.com/allegro/bigcache \
1110
github.com/dgraph-io/ristretto \
1211
github.com/fsnotify/fsnotify \
13-
github.com/go-redis/redis \
14-
gopkg.in/yaml.v2
12+
gopkg.in/yaml.v3
1513

1614
RUN mkdir -p /app/src/github.com/darkweak/cmd
1715
RUN mkdir -p /app/src/github.com/darkweak/souin
1816
RUN mkdir -p /ssl
1917
ADD ./*.go /app/src/github.com/darkweak/souin/
2018
ADD ./cache /app/src/github.com/darkweak/souin/cache
19+
ADD ./configuration /app/src/github.com/darkweak/souin/configuration
20+
ADD ./configuration_types /app/src/github.com/darkweak/souin/configuration_types
21+
ADD ./default /app/src/github.com/darkweak/souin/default
2122
ADD ./errors /app/src/github.com/darkweak/souin/errors
23+
ADD ./helpers /app/src/github.com/darkweak/souin/helpers
2224
ADD ./providers /app/src/github.com/darkweak/souin/providers
23-
ADD ./default/server.* /app/src/github.com/darkweak/souin/
24-
ADD ./configuration/configuration.yml /
25-
ADD ./configuration/* /app/src/github.com/darkweak/souin/configuration/
2625

2726
WORKDIR /app/src/github.com/darkweak/souin
2827

@@ -36,8 +35,11 @@ FROM alpine:latest AS souin
3635
COPY --from=builder /app/cmd/souin .
3736
COPY --from=builder /app/src/github.com/darkweak/souin/configuration .
3837
COPY --from=builder /ssl/ .
39-
COPY --from=builder /app/src/github.com/darkweak/souin/server.crt .
40-
COPY --from=builder /app/src/github.com/darkweak/souin/server.key .
38+
COPY --from=builder /app/src/github.com/darkweak/souin/default .
39+
RUN mkdir -p configuration
40+
RUN mv *.yml configuration/
41+
RUN mkdir -p default
42+
RUN mv server.* default/
4143
RUN chmod +x ./souin
4244

4345
CMD ["./souin"]

README.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,15 @@ ssl_providers: # The {providers}.json to use
6161
urls:
6262
'https:\/\/domain.com\/first-.+': # First regex route configuration
6363
ttl: 1000 # Override default TTL
64-
providers: # Providers list (each item must be in default providers list)
65-
- redis
66-
- memory
67-
- ristretto
6864
'https:\/\/domain.com\/second-route': # Second regex route configuration
6965
ttl: 10 # Override default TTL
7066
headers: # Override default headers
7167
- Authorization
72-
providers: # Providers list (each item must be in default providers list)
73-
- memory
7468
'https?:\/\/mysubdomain\.domain\.com': # Third regex route configuration
7569
ttl: 50
7670
headers: # Override default headers
7771
- Authorization
7872
- 'Content-Type'
79-
providers: # Providers list (each item must be in default providers list)
80-
- redis
8173
```
8274

8375
| Key | Description | Value example |
@@ -123,9 +115,6 @@ x-networks: &networks
123115
services:
124116
traefik:
125117
image: traefik:v2.0
126-
ports:
127-
- "81:80" # Note the 81 to 80 port declaration
128-
- "444:443" # Note the 444 to 443 port declaration
129118
command: --providers.docker
130119
volumes:
131120
- /var/run/docker.sock:/var/run/docker.sock

cache/providers/abstract.go

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package providers
2+
3+
import (
4+
"github.com/darkweak/souin/configuration_types"
5+
"github.com/darkweak/souin/cache/types"
6+
)
7+
8+
// InitializeProvider allow to generate the providers array according to the configuration
9+
func InitializeProvider(configuration configuration_types.AbstractConfigurationInterface) types.AbstractProviderInterface {
10+
r, _ := RistrettoConnectionFactory(configuration)
11+
return r
12+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package providers
2+
3+
import (
4+
"testing"
5+
"github.com/darkweak/souin/configuration"
6+
"github.com/darkweak/souin/configuration_types"
7+
"github.com/darkweak/souin/errors"
8+
"github.com/darkweak/souin/helpers"
9+
"regexp"
10+
"log"
11+
)
12+
13+
func MockConfiguration() configuration_types.AbstractConfigurationInterface {
14+
var config configuration.Configuration
15+
e := config.Parse([]byte(`
16+
default_cache:
17+
headers:
18+
- Authorization
19+
port:
20+
web: 80
21+
tls: 443
22+
regex:
23+
exclude: 'ARegexHere'
24+
ttl: 1000
25+
reverse_proxy_url: 'http://traefik'
26+
ssl_providers:
27+
- traefik
28+
urls:
29+
'domain.com/':
30+
ttl: 1000
31+
headers:
32+
- Authorization
33+
'mysubdomain.domain.com':
34+
ttl: 50
35+
headers:
36+
- Authorization
37+
- 'Content-Type'
38+
`))
39+
if e != nil {
40+
log.Fatal(e)
41+
}
42+
return &config
43+
}
44+
45+
func MockInitializeRegexp(configurationInstance configuration_types.AbstractConfigurationInterface) regexp.Regexp {
46+
u := ""
47+
for k := range configurationInstance.GetUrls() {
48+
if "" != u {
49+
u += "|"
50+
}
51+
u += "(" + k + ")"
52+
}
53+
54+
return *regexp.MustCompile(u)
55+
}
56+
57+
func TestInitializeProvider(t *testing.T) {
58+
c := MockConfiguration()
59+
p := InitializeProvider(c)
60+
err := p.Init()
61+
if nil != err {
62+
errors.GenerateError(t, "Init shouldn't crash")
63+
}
64+
}
65+
66+
func TestPathnameNotInExcludeRegex(t *testing.T) {
67+
config := MockConfiguration()
68+
if helpers.PathnameNotInExcludeRegex(config.GetDefaultCache().Regex.Exclude, config) {
69+
errors.GenerateError(t, "Pathname should be in regex")
70+
}
71+
if helpers.PathnameNotInExcludeRegex(config.GetDefaultCache().Regex.Exclude+"/A", config) {
72+
errors.GenerateError(t, "Pathname should be in regex")
73+
}
74+
if !helpers.PathnameNotInExcludeRegex("/BadPath", config) {
75+
errors.GenerateError(t, "Pathname shouldn't be in regex")
76+
}
77+
}

cache/providers/abstract_test.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)