Skip to content

Commit

Permalink
Merge pull request #2 from darkweak/feat/middleware/traefik-support
Browse files Browse the repository at this point in the history
feat(middleware): Support Traefik and bump version
  • Loading branch information
darkweak committed Sep 15, 2022
2 parents e47be45 + cdfee22 commit 2baa414
Show file tree
Hide file tree
Showing 25 changed files with 644 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .traefik.yml
@@ -0,0 +1,6 @@
displayName: ESI
type: middleware
import: github.com/darkweak/go-esi/middleware/traefik
summary: 'Pure implementation of the ESI process'

testData: {}
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -45,13 +45,42 @@ func functionToParseESITags(b []byte, r *http.Request) []byte {

## Available as middleware
- [x] Caddy
- [x] Træfik

### Caddy middleware
```bash
xcaddy build --with github.com/darkweak/go-esi/middleware/caddy
```
Refer to the [sample Caddyfile](https://github.com/darkweak/go-esi/blob/master/middleware/caddy/Caddyfile) to know how to configure that.

### Træfik middleware
```bash
# anywhere/traefik.yml
experimental:
plugins:
souin:
moduleName: github.com/darkweak/go-esi
version: v0.0.4
```
```bash
# anywhere/dynamic-configuration
http:
routers:
whoami:
middlewares:
- esi
service: whoami
rule: Host(`domain.com`)
middlewares:
esi:
plugin:
esi:
# We don't care about the configuration but we have ot declare that block
# due to shitty træfik empty configuration handle.
disable: false
```
Refer to the [sample Caddyfile](https://github.com/darkweak/go-esi/blob/master/middleware/caddy/Caddyfile) to know how to configure that.

## TODO
- [x] choose tag
- [x] comment tag
Expand Down
5 changes: 3 additions & 2 deletions esi/include.go
Expand Up @@ -53,11 +53,12 @@ func (i *includeTag) process(b []byte, req *http.Request) ([]byte, int) {
}

rq, _ := http.NewRequest(http.MethodGet, i.src, nil)
response, err := clientPool.Get().(*http.Client).Do(rq)
client := &http.Client{}
response, err := client.Do(rq)

if err != nil || response.StatusCode >= 400 {
rq, _ = http.NewRequest(http.MethodGet, i.src, nil)
response, err = clientPool.Get().(*http.Client).Do(rq)
response, err = client.Do(rq)

if err != nil || response.StatusCode >= 400 {
return nil, len(b)
Expand Down
7 changes: 0 additions & 7 deletions esi/type.go
Expand Up @@ -2,15 +2,8 @@ package esi

import (
"net/http"
"sync"
)

var clientPool = &sync.Pool{
New: func() any {
return &http.Client{}
},
}

type (
tag interface {
process([]byte, *http.Request) ([]byte, int)
Expand Down
4 changes: 2 additions & 2 deletions middleware/caddy/go.mod
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/caddyserver/caddy/v2 v2.5.2
github.com/darkweak/go-esi v0.0.3
github.com/darkweak/go-esi v0.0.4
)

require (
Expand Down Expand Up @@ -111,4 +111,4 @@ require (
howett.net/plist v1.0.0 // indirect
)

replace github.com/darkweak/go-esi v0.0.3 => ../..
replace github.com/darkweak/go-esi v0.0.4 => ../..
20 changes: 20 additions & 0 deletions middleware/traefik/docker-compose.yml
@@ -0,0 +1,20 @@
version: '3.8'

services:
traefik:
image: traefik:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ../..:/plugins-local/src/github.com/darkweak/go-esi
- ./traefik.yml:/traefik.yml
- ./esi-configuration.yml:/esi-configuration.yml
environment:
GOPATH: /plugins-local
ports:
- 80:80
- 8080:8080

whoami:
image: traefik/whoami
labels:
- traefik.http.routers.whoami.rule=Host(`domain.com`)
24 changes: 24 additions & 0 deletions middleware/traefik/esi-configuration.yml
@@ -0,0 +1,24 @@
http:
routers:
whoami:
middlewares:
- esi
entrypoints:
- http
service: whoami
rule: Host(`domain.com`)

services:
whoami:
loadBalancer:
servers:
- url: http://whoami
passHostHeader: false

middlewares:
esi:
plugin:
esi:
# We don't care about the configuration but we have ot declare that block
# due to shitty træfik empty configuration handle.
disable: false
35 changes: 35 additions & 0 deletions middleware/traefik/esi.go
@@ -0,0 +1,35 @@
package traefik

import (
"context"
"net/http"

"github.com/darkweak/go-esi/esi"
)

// Config the plugin configuration.
type Config struct{}

// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}

// Demo a Demo plugin.
type Demo struct {
next http.Handler
name string
}

// New created a new Demo plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
return &Demo{
next: next,
name: name,
}, nil
}

func (a *Demo) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
esi.Parse([]byte{}, req)
a.next.ServeHTTP(rw, req)
}
7 changes: 7 additions & 0 deletions middleware/traefik/go.mod
@@ -0,0 +1,7 @@
module github.com/darkweak/go-esi/middleware/traefik

go 1.18

require github.com/darkweak/go-esi v0.0.4

replace github.com/darkweak/go-esi v0.0.4 => ../..
Empty file added middleware/traefik/go.sum
Empty file.
22 changes: 22 additions & 0 deletions middleware/traefik/traefik.yml
@@ -0,0 +1,22 @@
providers:
file:
filename: /esi-configuration.yml
watch: true

api:
dashboard: true
debug: true
insecure: true

pilot:
token: 12926953-a7d1-4223-a092-d7dd95a91fa3

experimental:
localPlugins:
esi:
moduleName: github.com/darkweak/go-esi

log:
level: DEBUG

accessLog: {}
21 changes: 21 additions & 0 deletions middleware/traefik/vendor/github.com/darkweak/go-esi/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions middleware/traefik/vendor/github.com/darkweak/go-esi/esi/choose.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions middleware/traefik/vendor/github.com/darkweak/go-esi/esi/escape.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2baa414

Please sign in to comment.