Crates.io | esi_fastly |
lib.rs | esi_fastly |
version | 0.1.3 |
source | src |
created_at | 2021-03-12 00:14:10.689362 |
updated_at | 2021-03-12 19:50:24.015461 |
description | A Fastly Compute@Edge interface for the esi crate |
homepage | |
repository | https://github.com/kailan/esi |
max_upload_size | |
id | 367492 |
size | 3,664 |
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.
<esi:include>
(+ alt
, onerror="continue"
)<esi:comment>
<esi:remove>
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.
[dependencies]
esi_fastly = "^0.1"
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)
}
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.
[dependencies]
esi = "^0.1"
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!()
}
The source and documentation for this project are released under the MIT License.