Skip to content

Commit dfdfaa8

Browse files
authored
feat(middleware): roadrunner support & performance update (#11)
* feat(middlewares): implement roadrunner * update golangci-lint * update tests * feat: improve performances and refacto with rr support * feat: finish roadrunner * fix: github actions tests setup * fix: run detached caddy on the 8080 HTTP port * fix: middleware caddy unit tests * feat: documentation update to include roadrunner * temporary downgrade to be able to build * feat: bump version
1 parent 9298fae commit dfdfaa8

Some content is hidden

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

45 files changed

+2890
-1678
lines changed

.github/workflows/middlewares.yml

+19-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ jobs:
1414
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
1515
-
1616
name: Install Go
17-
uses: actions/setup-go@v2
17+
uses: actions/setup-go@v3
1818
with:
19-
go-version: 1.18
19+
go-version: 1.19
2020
-
2121
name: Checkout code
22-
uses: actions/checkout@v2
22+
uses: actions/checkout@v3
2323
-
2424
name: Install xcaddy
2525
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
@@ -32,3 +32,19 @@ jobs:
3232
-
3333
name: Run detached caddy
3434
run: cd middleware/caddy && ./caddy run &
35+
36+
build-roadrunner-validator:
37+
name: Check that go-esi build as roadrunner middleware
38+
runs-on: ubuntu-latest
39+
steps:
40+
-
41+
name: Install Go
42+
uses: actions/setup-go@v3
43+
with:
44+
go-version: 1.19
45+
-
46+
name: Checkout code
47+
uses: actions/checkout@v3
48+
-
49+
name: Run Roadrunner tests
50+
run: cd middleware/roadrunner && go test -v ./...

.github/workflows/non-regression.yml

+26-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@ on:
66

77
jobs:
88
lint-and-tests:
9-
name: Validate Go code linting
9+
name: lint and static tests
1010
runs-on: ubuntu-latest
1111
steps:
12-
- name: Checkout code
13-
uses: actions/checkout@v2
14-
- name: Install Go
15-
uses: actions/setup-go@v2
12+
-
13+
name: Add domain.com host to /etc/hosts
14+
run: |
15+
sudo echo "127.0.0.1 domain.com" | sudo tee -a /etc/hosts
16+
-
17+
name: checkout code
18+
uses: actions/checkout@v3
19+
-
20+
name: install Go
21+
uses: actions/setup-go@v3
1622
with:
17-
go-version: 1.18
18-
- name: golangci-lint
19-
uses: golangci/golangci-lint-action@v2
20-
- name: tests
23+
go-version: 1.19
24+
-
25+
name: golangci-lint
26+
uses: golangci/golangci-lint-action@v3
27+
-
28+
name: install xcaddy
29+
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
30+
-
31+
name: build caddy binary
32+
run: cd middleware/caddy && make build && cd -
33+
-
34+
name: run caddy binary as detached mode
35+
run: cd middleware/caddy && make run &
36+
-
37+
name: tests
2138
run: go test -v -race ./...

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Set up Go
1414
uses: actions/setup-go@v2
1515
with:
16-
go-version: 1.18
16+
go-version: 1.19
1717
-
1818
name: Checkout
1919
uses: actions/checkout@v2

.golangci.yml

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
run:
22
timeout: 30s
33
issues-exit-code: 1
4+
5+
linters:
6+
enable-all: true
7+
disable:
8+
- bodyclose
9+
- cyclop
10+
- exhaustivestruct
11+
- exhaustruct
12+
- forbidigo
13+
- gochecknoglobals
14+
- gci
15+
- golint
16+
- gomnd
17+
- ifshort
18+
- ireturn
19+
- nestif
20+
- nonamedreturns
21+
- nosnakecase
22+
- revive
23+
- testpackage
24+
- stylecheck
25+
- varnamelen

Makefile

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
.PHONY: lint run-caddy run-server run-traefik
1+
.PHONY: bump-version lint run-caddy run-roadrunner run-server run-traefik vendor
2+
MIDDLEWARES_LIST=caddy roadrunner server traefik
3+
4+
bump-version:
5+
test $(from)
6+
test $(to)
7+
sed -i '' 's/version: $(from)/version: $(to)/' README.md
8+
for middleware in $(MIDDLEWARES_LIST) ; do \
9+
sed -i '' 's/github.com\/darkweak\/go-esi $(from)/github.com\/darkweak\/go-esi $(to)/' middleware/$$middleware/go.mod ; \
10+
done
211

312
lint: ## Run golangci-lint to ensure the code quality
413
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint golangci-lint run
514

615
run-caddy: ## Build and run caddy binary
716
cd middleware/caddy && $(MAKE) build && $(MAKE) run
817

18+
run-roadrunner: ## Build and run roadrunner
19+
cd middleware/roadrunner && $(MAKE) build && $(MAKE) run
20+
921
run-server: ## Run server main.go
1022
go run middleware/server/main.go
1123

12-
run-traefik: ## Run server main.go
24+
run-traefik: ## Build and run træfik
1325
cd middleware/traefik && $(MAKE) build && $(MAKE) run
26+
27+
vendor: ## Generate and prepare vendors for each plugin
28+
go mod tidy && go mod download
29+
for middleware in $(MIDDLEWARES_LIST) ; do \
30+
cd middleware/$$middleware && ($(MAKE) build || true) && cd -; done

README.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,60 @@ func functionToParseESITags(b []byte, r *http.Request) []byte {
4646
## Available as middleware
4747
- [x] Caddy
4848
- [x] Træfik
49+
- [x] Roadrunner
4950

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

57+
### Roadrunner middleware
58+
To use the `go-esi` processor as Roadrunner middleware, you just have to follow the steps below.
59+
You have to build your `rr` binary with the `go-esi` dependency.
60+
```toml
61+
[velox]
62+
build_args = ['-trimpath', '-ldflags', '-s -X github.com/roadrunner-server/roadrunner/v2/internal/meta.version=v2.12.0 -X github.com/roadrunner-server/roadrunner/v2/internal/meta.buildTime=10:00:00']
63+
64+
[roadrunner]
65+
ref = "v2.12.3"
66+
67+
[github]
68+
[github.token]
69+
token = "GH_TOKEN"
70+
71+
[github.plugins]
72+
logger = { ref = "v3.2.0", owner = "roadrunner-server", repository = "logger" }
73+
esi = { ref = "master", owner = "darkweak", repository = "go-esi", folder = "middleware/roadrunner", replace = "/opt/middleware/roadrunner" }
74+
server = { ref = "v3.2.0", owner = "roadrunner-server", repository = "server" }
75+
gzip = { ref = "v3.2.0", owner = "roadrunner-server", repository = "gzip" }
76+
http = { ref = "v3.2.0", owner = "roadrunner-server", repository = "http" }
77+
78+
[log]
79+
level = "debug"
80+
mode = "development"
81+
```
82+
83+
After that, you'll be able to set enable and add the esi processor to the middleware chain.
84+
```yaml
85+
# .rr.yaml
86+
http:
87+
# Other http sub keys
88+
esi: {}
89+
middleware:
90+
- headers
91+
- gzip
92+
- esi
93+
```
94+
5695
### Træfik middleware
5796
```yaml
5897
# anywhere/traefik.yml
5998
experimental:
6099
plugins:
61100
souin:
62101
moduleName: github.com/darkweak/go-esi
63-
version: v0.0.5
102+
version: v0.0.6
64103
```
65104
```yaml
66105
# anywhere/dynamic-configuration

esi/choose.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ type chooseTag struct {
1919

2020
// Input (e.g.
2121
// <esi:choose>
22-
// <esi:when test="$(HTTP_COOKIE{group})=='Advanced'">
23-
// <esi:include src="http://www.example.com/advanced.html"/>
24-
// </esi:when>
25-
// <esi:when test="$(HTTP_COOKIE{group})=='Basic User'">
26-
// <esi:include src="http://www.example.com/basic.html"/>
27-
// </esi:when>
28-
// <esi:otherwise>
29-
// <esi:include src="http://www.example.com/new_user.html"/>
30-
// </esi:otherwise>
22+
//
23+
// <esi:when test="$(HTTP_COOKIE{group})=='Advanced'">
24+
// <esi:include src="http://www.example.com/advanced.html"/>
25+
// </esi:when>
26+
// <esi:when test="$(HTTP_COOKIE{group})=='Basic User'">
27+
// <esi:include src="http://www.example.com/basic.html"/>
28+
// </esi:when>
29+
// <esi:otherwise>
30+
// <esi:include src="http://www.example.com/new_user.html"/>
31+
// </esi:otherwise>
32+
//
3133
// </esi:choose>
3234
// ).
3335
func (c *chooseTag) Process(b []byte, req *http.Request) ([]byte, int) {
@@ -44,6 +46,7 @@ func (c *chooseTag) Process(b []byte, req *http.Request) ([]byte, int) {
4446
for _, v := range tagIdxs {
4547
if validateTest(v[1], req) {
4648
res = Parse(v[2], req)
49+
4750
return res, c.length
4851
}
4952
}
@@ -64,5 +67,6 @@ func (*chooseTag) GetClosePosition(b []byte) int {
6467
if idx := closeChoose.FindIndex(b); idx != nil {
6568
return idx[1]
6669
}
70+
6771
return 0
6872
}

esi/comment.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
const comment = "comment"
99

10-
var closeComment = regexp.MustCompile("/>")
10+
var closeComment = regexp.MustCompile("/>((\n| +)+)?")
1111

1212
type commentTag struct {
1313
*baseTag
@@ -31,5 +31,6 @@ func (*commentTag) GetClosePosition(b []byte) int {
3131
if idx := closeComment.FindIndex(b); idx != nil {
3232
return idx[1]
3333
}
34+
3435
return 0
3536
}

esi/escape.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const escape = "<!--esi"
99

1010
var (
1111
escapeRg = regexp.MustCompile("<!--esi")
12-
closeEscape = regexp.MustCompile("-->")
12+
closeEscape = regexp.MustCompile("((\n| +)+)?-->")
13+
startEscape = regexp.MustCompile("((\n| +)+)?")
1314
)
1415

1516
type escapeTag struct {
@@ -23,8 +24,13 @@ func (e *escapeTag) Process(b []byte, req *http.Request) ([]byte, int) {
2324
return nil, len(b)
2425
}
2526

27+
startPosition := 0
28+
if startIdx := startEscape.FindIndex(b); startIdx != nil {
29+
startPosition = startIdx[1]
30+
}
31+
2632
e.length = closeIdx[1]
27-
b = b[:closeIdx[0]]
33+
b = b[startPosition:closeIdx[0]]
2834

2935
return b, e.length
3036
}
@@ -37,5 +43,6 @@ func (*escapeTag) GetClosePosition(b []byte) int {
3743
if idx := closeEscape.FindIndex(b); idx != nil {
3844
return idx[1]
3945
}
46+
4047
return 0
4148
}

esi/esi.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ func CanProcess(b []byte) bool {
5656
}
5757

5858
func ReadToTag(next []byte, pointer int) (startTagPosition, esiPointer int, t Tag) {
59-
tagIdx := esi.FindIndex(next)
6059
var isEscapeTag bool
6160

61+
tagIdx := esi.FindIndex(next)
62+
6263
if escIdx := escapeRg.FindIndex(next); escIdx != nil && (tagIdx == nil || escIdx[0] < tagIdx[0]) {
6364
tagIdx = escIdx
6465
tagIdx[1] = escIdx[0]

0 commit comments

Comments
 (0)