esi_fastly

Crates.ioesi_fastly
lib.rsesi_fastly
version0.1.3
sourcesrc
created_at2021-03-12 00:14:10.689362
updated_at2021-03-12 19:50:24.015461
descriptionA Fastly Compute@Edge interface for the esi crate
homepage
repositoryhttps://github.com/kailan/esi
max_upload_size
id367492
size3,664
Kailan Blanks (kailan)

documentation

README

esi

A barebones Rust implementation of Edge Side Includes. Compatible with Fastly Compute@Edge via the esi_fastly crate.

Goal is to fully implement the ESI Language Specification 1.0.

Supported Tags

  • <esi:include> (+ alt, onerror="continue")
  • <esi:comment>
  • <esi:remove>

Usage

Compute@Edge

The esi_fastly crate provides an implementation of a RequestHandler that will automatically pass requests to backends matching the request hostname. Make sure create a backend for every host that your application will serve.

Cargo.toml

[dependencies]
esi_fastly = "^0.1"

src/main.rs

use fastly::{Error, Request, Response};
use esi_fastly::process_esi;

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
    // Send request to backend.
    let beresp = req.send("backend")?;

    // Process and execute ESI tags within the response body.
    // Make sure you have backends defined for any included hosts.
    // Their names should match the hostname, e.g. "developer.fastly.com"
    let result = process_esi(req, beresp)?;

    // Return the updated response to the client.
    Ok(result)
}

Standalone Rust

To use the esi crate without a third-party ExecutionContext, you will have to implement one yourself. The example below shows a basic execution context with a request handler that uses the reqwest crate.

Cargo.toml

[dependencies]
esi = "^0.1"

src/main.rs

pub struct ReqwestHandler;

impl esi::ExecutionContext for ReqwestHandler {
    fn send_request(&self, url: &str) -> Result<String, esi::Error> {
        match reqwest::blocking::get(url) {
            Ok(resp) => Ok(resp.text().unwrap()),
            Err(err) => Err(esi::Error::from_message(&format!("{:?}", err)))
        }
    }
}
use esi::transform_esi_string;

let exec_context = ReqwestHandler {};

let response_body = send_request_to_backend();

match transform_esi_string(response_body, &exec_context) {
    Ok(body) => send_body_to_client(body),
    Err(err) => panic!()
}

License

The source and documentation for this project are released under the MIT License.

Commit count: 33

cargo fmt