Skip to content

Commit

Permalink
Merge pull request #1 from darkweak/fix/middleware/reset-and-put-back…
Browse files Browse the repository at this point in the history
…-buffer

fix(middleware): Reset and put back buffer
  • Loading branch information
darkweak committed Sep 12, 2022
2 parents 083d71e + 336df7e commit e47be45
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and validate go-esi as plugins
name: Build and validate go-esi as middleware

on:
- pull_request
Expand All @@ -25,10 +25,10 @@ jobs:
run: go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
name: Build go-esi as caddy module
run: cd plugins/caddy && xcaddy build --with github.com/darkweak/go-esi/middleware/caddy=./ --with github.com/darkweak/go-esi@latest=../..
run: cd middleware/caddy && xcaddy build --with github.com/darkweak/go-esi/middleware/caddy=./ --with github.com/darkweak/go-esi@latest=../..
-
name: Run Caddy tests
run: cd plugins/caddy && go test -v ./...
run: cd middleware/caddy && go test -v ./...
-
name: Run detached caddy
run: cd plugins/caddy && ./caddy run &
run: cd middleware/caddy && ./caddy run &
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ go-esi

go-esi is the implementation of the non-standard ESI (Edge-Side-Include) specification from the w3. With that you'll be able to use the ESI tags and process them in your favorite golang servers.

## What are the ESI tags
The ESI tags were introduced by Akamai to add some dynamic tags and only re-render these parts on the server-side.
The goal of that is to render only specific parts. For example, we want to render a full e-commerce webpage but only the cart is user-dependent. So we could render the "static" parts and store with a predefined TTL (e.g. 60 minutes), and only the cart would be requested to render the block.

There are multiple `esi` tags that we can use but the most used is the `esi:include` because that's the one to request another resource.

We can have many `esi:include` tags in a single response, and each `esi:include` tags can itself have one or more `esi:include` tags.

![esi page example](https://github.com/darkweak/go-esi/blob/master/docs/esi_2.jpg?sanitize=true)

We can have multiple `esi:include` tags in the page to request another resource and add its content to the main page.

![esi process example](https://github.com/darkweak/go-esi/blob/master/docs/esi_1.jpg?sanitize=true)

## References
https://www.w3.org/TR/esi-lang/

Expand Down
Binary file added docs/esi_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/esi_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 5 additions & 6 deletions middleware/caddy/esi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
Expand All @@ -23,10 +22,7 @@ var bufPool *sync.Pool = &sync.Pool{
func init() {
caddy.RegisterModule(ESI{})
httpcaddyfile.RegisterGlobalOption("esi", func(h *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
return httpcaddyfile.App{
Name: "http.handlers.esi",
Value: caddyconfig.JSON(ESI{}, nil),
}, nil
return &ESI{}, nil
})
httpcaddyfile.RegisterHandlerDirective("esi", func(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
return &ESI{}, nil
Expand All @@ -46,7 +42,10 @@ func (ESI) CaddyModule() caddy.ModuleInfo {

// ServeHTTP implements caddyhttp.MiddlewareHandler
func (e *ESI) ServeHTTP(rw http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
cw := newWriter(bufPool.Get().(*bytes.Buffer), rw)
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
cw := newWriter(buf, rw)
next.ServeHTTP(cw, r)

b := esi.Parse(cw.buf.Bytes(), r)
Expand Down
4 changes: 2 additions & 2 deletions middleware/caddy/go.mod
Original file line number Diff line number Diff line change
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.1
github.com/darkweak/go-esi v0.0.3
)

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.1 => ../..
replace github.com/darkweak/go-esi v0.0.3 => ../..

0 comments on commit e47be45

Please sign in to comment.