Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework to forward by default, and update readme #19

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,47 @@

[![Deploy to Fastly](https://deploy.edgecompute.app/button)](https://deploy.edgecompute.app/deploy)

Learn about Fastly Compute with Fanout using a basic starter that demonstrates basic Fanout handlers.
Install this starter kit to use Fanout. It routes incoming requests through the Fanout GRIP proxy and on to an origin. It also provides some endpoints for testing subscriptions without an origin.

There is no need to modify this kit. It is production-ready for typical Fanout usage that coordinates with an origin. However, if you would like to implement Fanout logic at the edge, this kit is also a good starting point for that and you can modify it to fit your needs. See the test endpoints for inspiration.

**For more details about this and other starter kits for Compute, see the [Fastly Documentation Hub](https://www.fastly.com/documentation/solutions/starters/)**.

## Setup

The app expects a configured backend named "self" that points back to app itself. For example, if the service has a domain `foo.edgecompute.app`, then you'll need to create a backend on the service named "self" with the destination host set to `foo.edgecompute.app` and port 443. Also set 'Override Host' to the same host value.
The app expects a configured backend named "origin" where Fanout-capable requests should be forwarded to.

Additionally, for the test endpoints to work, the app expects a configured backend named "self" that points back to app itself. For example, if the service has a domain `foo.edgecompute.app`, then you'll need to create a backend on the service named "self" with the destination host set to `foo.edgecompute.app` and port 443. Also set "Override Host" to the same host value.

## Endpoints
## Test Endpoints

The app exposes the following endpoints to clients:
For requests made to domains ending in `.edgecompute.app`, the app will handle requests to the following endpoints without forwarding to the origin:

* `/ws`: bi-directional WebSocket
* `/stream`: HTTP streaming of `text/plain`
* `/sse`: SSE (streaming of `text/event-stream`)
* `/response`: Long-polling
* `/test/websocket`: bi-directional WebSocket
* `/test/stream`: HTTP streaming of `text/plain`
* `/test/sse`: SSE (streaming of `text/event-stream`)
* `/test/long-poll`: Long-polling

Connecting to any endpoint will subscribe the connection to channel "test". The WebSocket endpoint echos back any messages it receives from the client.
Connecting to any of these endpoints will subscribe the connection to channel "test". The WebSocket endpoint echos back any messages it receives from the client.

Data can be sent to the connections via the GRIP publish endpoint at `https://fanout.fastly.com/{service-id}/publish/`. For example, here's a curl command to send a WebSocket message:
Data can be sent to the connections via the GRIP publish endpoint at `https://api.fastly.com/service/{service-id}/publish/`. For example, here's a curl command to send a WebSocket message:

```sh
curl \
--user {service-id}:{secret} \
-H "Authorization: Bearer {fastly-api-token}" \
-d '{"items":[{"channel":"test","formats":{"ws-message":{"content":"hello"}}}]}' \
https://fanout.fastly.com/{service-id}/publish/
https://api.fastly.com/service/{service-id}/publish/
```

## How it works

For each call, the app is actually invoked twice.
Non-test requests are simply forwarded through the Fanout proxy and on to the origin.

For test requests, the app is actually invoked twice.

1. Initially, a client request arrives at the app without having been routed through the Fanout proxy yet. The app checks for this via the presence of a `Grip-Sig` header. If that header is not present, the app calls `req.handoff_fanout("self")` and exits. This tells the subsystem that the connection should be routed through Fanout, and is used for HTTP requests controlled by GRIP.

2. Since `self` refers to the same app, a second request is made to the same app, this time coming through Fanout. The app checks for this, and then handles the request accordingly (in `handle()`).
2. Since `self` refers to the same app, a second request is made to the same app, this time coming through Fanout. The app checks for this, and then handles the request accordingly (in `handle_test()`).

## Note

Expand Down
60 changes: 41 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,59 @@ fn handle_fanout_ws(mut req: Request, chan: &str) -> Response {
resp
}

fn handle_fanout(req: Request, chan: &str) -> Response {
fn handle_test(req: Request, chan: &str) -> Response {
match req.get_url().path() {
"/stream/long-poll" => fanout_util::grip_response("text/plain", "response", chan),
"/stream/plain" => fanout_util::grip_response("text/plain", "stream", chan),
"/stream/sse" => fanout_util::grip_response("text/event-stream", "stream", chan),
"/stream/websocket" => handle_fanout_ws(req, chan),
_ => Response::from_status(StatusCode::BAD_REQUEST).with_body("Invalid Fanout request\n"),
"/test/long-poll" => fanout_util::grip_response("text/plain", "response", chan),
"/test/stream" => fanout_util::grip_response("text/plain", "stream", chan),
"/test/sse" => fanout_util::grip_response("text/event-stream", "stream", chan),
"/test/websocket" => handle_fanout_ws(req, chan),
_ => Response::from_status(StatusCode::NOT_FOUND).with_body("No such test endpoint\n"),
}
}

fn is_tls(req: &Request) -> bool {
req.get_url().scheme().eq_ignore_ascii_case("https")
}

fn main() -> Result<(), Error> {
// Log service version
println!(
"FASTLY_SERVICE_VERSION: {}",
std::env::var("FASTLY_SERVICE_VERSION").unwrap_or_else(|_| String::new())
);
let req = Request::from_client();

// Request is a stream request - from client, or from fanout
if req.get_path().starts_with("/stream/") {
return Ok(if req.get_header_str("Grip-Sig").is_some() {
// Request is from Fanout
handle_fanout(req, "test").send_to_client()
} else {
// Not from fanout, hand it off to Fanout to manage
req.handoff_fanout("self")?
});
let mut req = Request::from_client();

let host = match req.get_url().host_str() {
Some(s) => s.to_string(),
None => {
return Ok(Response::from_status(StatusCode::NOT_FOUND)
.with_body("Unknown host\n")
.send_to_client());
}
};

let path = req.get_path().to_string();

if let Some(addr) = req.get_client_ip_addr() {
req.set_header("X-Forwarded-For", addr.to_string());
}

if is_tls(&req) {
req.set_header("X-Forwarded-Proto", "https");
}

// Forward all non-stream requests to the primary backend
let be_resp = req.send("primary_backend");
// Request is a test request - from client, or from Fanout
if host.ends_with(".edgecompute.app") && path.starts_with("/test/") {
if req.get_header_str("Grip-Sig").is_some() {
// Request is from Fanout, handle it here
return Ok(handle_test(req, "test").send_to_client());
} else {
// Not from Fanout, route it through Fanout first
return Ok(req.handoff_fanout("self")?);
}
}

Ok(be_resp?.send_to_client())
// Forward all non-test requests to the origin
Ok(req.handoff_fanout("origin")?)
}
Loading