forked from kailan/esi
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Short form esi:vars processing works. Added an example for testing.
- Loading branch information
Showing
7 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[target.wasm32-wasi] | ||
rustflags = ["-C", "debuginfo=2"] | ||
runner = "viceroy run -C fastly.toml -- " | ||
|
||
[build] | ||
target = "wasm32-wasi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "esi_vars_example" | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
fastly = "^0.11" | ||
esi = { path = "../../esi" } | ||
log = "^0.4" | ||
env_logger = "^0.11" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This file describes a Fastly Compute package. To learn more visit: | ||
# https://developer.fastly.com/reference/fastly-toml/ | ||
|
||
authors = ["[email protected]"] | ||
description = "" | ||
language = "rust" | ||
manifest_version = 2 | ||
name = "esi_example_minimal" | ||
service_id = "" | ||
|
||
[local_server] | ||
|
||
[local_server.backends] | ||
|
||
[local_server.backends.mock-s3] | ||
url = "https://mock-s3.edgecompute.app" | ||
override_host = "mock-s3.edgecompute.app" | ||
|
||
[scripts] | ||
build = "cargo build --bin esi_example_minimal --release --target wasm32-wasi --color always" | ||
|
||
[setup] | ||
|
||
[setup.backends] | ||
|
||
[setup.backends.mock-s3] | ||
address = "mock-s3.edgecompute.app" | ||
port = 443 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>My Variable Website</title> | ||
</head> | ||
<body> | ||
<header style="background: #f1f1f1; padding: 16px"> | ||
<h1>My Variable Website</h1> | ||
</header> | ||
<div class="layout" style="display: flex"> | ||
Assigning "YES" to variable...<br> | ||
<esi:assign name="test" value="YES" /> | ||
Check (short form): YES=<esi:vars name="test" /><br> | ||
Check (long form): YES=<esi:vars>$(test)</esi:vars><br> | ||
|
||
Updating variable to "PERHAPS"...<br> | ||
<esi:assign name="test" value="PERHAPS" /> | ||
Check (short form): PERHAPS=<esi:vars name="test" /><br> | ||
Check (long form): PERHAPS=<esi:vars>$(test)</esi:vars><br> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use fastly::{http::StatusCode, mime, Error, Request, Response}; | ||
use log::info; | ||
|
||
fn main() { | ||
env_logger::builder() | ||
.filter(None, log::LevelFilter::Trace) | ||
.init(); | ||
|
||
if let Err(err) = handle_request(Request::from_client()) { | ||
println!("returning error response"); | ||
|
||
Response::from_status(StatusCode::INTERNAL_SERVER_ERROR) | ||
.with_body(err.to_string()) | ||
.send_to_client(); | ||
} | ||
} | ||
|
||
fn handle_request(req: Request) -> Result<(), Error> { | ||
if req.get_path() != "/" { | ||
Response::from_status(StatusCode::NOT_FOUND).send_to_client(); | ||
return Ok(()); | ||
} | ||
|
||
// Generate synthetic test response from "index.html" file. | ||
// You probably want replace this with a backend call, e.g. `req.clone_without_body().send("origin_0")` | ||
let mut beresp = | ||
Response::from_body(include_str!("index.html")).with_content_type(mime::TEXT_HTML); | ||
|
||
// If the response is HTML, we can parse it for ESI tags. | ||
if beresp | ||
.get_content_type() | ||
.is_some_and(|c| c.subtype() == mime::HTML) | ||
{ | ||
let processor = esi::Processor::new(Some(req), esi::Configuration::default()); | ||
|
||
processor.process_response( | ||
&mut beresp, | ||
None, | ||
Some(&|req| { | ||
info!("Sending request {} {}", req.get_method(), req.get_path()); | ||
Ok(req.with_ttl(120).send_async("mock-s3")?.into()) | ||
}), | ||
Some(&|req, mut resp| { | ||
info!( | ||
"Received response for {} {}", | ||
req.get_method(), | ||
req.get_path() | ||
); | ||
if !resp.get_status().is_success() { | ||
// Override status so we still insert errors. | ||
resp.set_status(StatusCode::OK); | ||
} | ||
Ok(resp) | ||
}), | ||
)?; | ||
} else { | ||
// Otherwise, we can just return the response. | ||
beresp.send_to_client(); | ||
} | ||
|
||
Ok(()) | ||
} |