Code Monkey home page Code Monkey logo

go-esi's Introduction

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

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

References

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

Install

go get -u github.com/darkweak/go-esi

Usage

import (
    // ...
    github.com/darkweak/go-esi/esi
)

//...

func functionToParseESITags(b []byte, r *http.Request) []byte {
    // Returns the parsed response.
    res := esi.Parse(b, r)

    //...
    return res
}

Available as middleware

  • Caddy
  • Træfik
  • Roadrunner

Caddy middleware

xcaddy build --with github.com/darkweak/go-esi/middleware/caddy

Refer to the sample Caddyfile to know how to use that.

Roadrunner middleware

To use the go-esi processor as Roadrunner middleware, you just have to follow the steps below.
You have to build your rr binary with the go-esi dependency.

[velox]
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']

[roadrunner]
ref = "v2.12.3"

[github]
    [github.token]
    token = "GH_TOKEN"

    [github.plugins]
    logger = { ref = "v3.2.0", owner = "roadrunner-server", repository = "logger" }
    esi = { ref = "master", owner = "darkweak", repository = "go-esi", folder = "middleware/roadrunner", replace = "/opt/middleware/roadrunner" }
    server = { ref = "v3.2.0", owner = "roadrunner-server", repository = "server" }
    gzip = { ref = "v3.2.0", owner = "roadrunner-server", repository = "gzip" }
    http = { ref = "v3.2.0", owner = "roadrunner-server", repository = "http" }

[log]
level = "debug"
mode = "development"

After that, you'll be able to set enable and add the esi processor to the middleware chain.

# .rr.yaml
http:
  # Other http sub keys
  esi: {}
  middleware:
    - headers
    - gzip
    - esi

Træfik middleware

# anywhere/traefik.yml
experimental:
  plugins:
    souin:
      moduleName: github.com/darkweak/go-esi
      version: v0.0.6
# anywhere/dynamic-configuration
http:
  routers:
    whoami:
      middlewares:
        - esi
      service: whoami
      rule: Host(`domain.com`)
  middlewares:
    esi:
      plugin:
        esi: {}

Refer to the sample traefik file to know how to use that.

TODO

  • choose tag
  • comment tag
  • escape tag
  • include tag
  • remove tag
  • otherwise tag
  • try tag
  • vars tag
  • when tag

go-esi's People

Contributors

darkweak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

go-esi's Issues

Feature request: parallel processing of esi tags

If I'm reading the code correctly, right now all the esi tags are parsed and processed sequentially. This means that if there are 5 esi:includes each taking 50 ms, it'd add up to 250 ms instead of potentially only taking 50 ms if they were all requested at once.

A remedy to this would be to first parse the input, then process all found tags all at once, and only then insert the results in tags' place in the body to be returned.

ESI include should pass some original req's headers into the new request

Specification says:

  1. Protocol Considerations
    When an ESI template is processed, a separate request will need to be made for each include encountered. Implementations may use the original request's headers (e.g., Cookie, User-Agent, etc.) when doing so. Additionally, response headers from fragments (e.g., Set-Cookie, Server, Cache-Control, Last-Modified) may be ignored, and should not influence the assembled page.

(emphasis mine)

When creating the child request, some of the headers need to be included for the child response to be constructed properly, for example Cookie / Authorization headers are required to know the identity of the user for which we're constructing the child response.

Security considerations

Some of these headers must NOT be forwarded in cross-origin scenarios. For example, Cookie and Authorization musn't be passed unless the child request's host, port and scheme all match. IMO the best approach is to have an allow list of headers in general and a separate list of same-origin headers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.