Skip to main content

Build a CDB EdgeCompute HTTP application

CDB EdgeCompute HTTP applications respond to HTTP requests directly from the edge. The handler compiles to a WebAssembly binary and runs without servers or containers. Three language options are available — complete the toolchain setup first: Rust (Modern HTTP), Rust (Legacy HTTP), or JavaScript.

Write a handler

Each language uses a different entry point convention. Choose the tab that matches the toolchain set up earlier.

Replace src/lib.rs with:

use wstd::http::body::Body;
use wstd::http::{Request, Response};

#[wstd::http_server]
async fn main(request: Request<Body>) -> anyhow::Result<Response<Body>> {
    let url = request.uri().to_string();
    Ok(Response::builder()
        .status(200)
        .header("content-type", "text/plain;charset=UTF-8")
        .body(Body::from(format!("Request received: {url}")))?)
}

Replace src/lib.rs with:

use edgecompute::body::Body;
use edgecompute::http::{Request, Response, StatusCode, Error};

#[edgecompute::http]
fn main(request: Request<Body>) -> Result<Response<Body>, Error> {
    let url = request.uri().to_string();
    Response::builder()
        .status(StatusCode::OK)
        .header("content-type", "text/plain;charset=UTF-8")
        .body(Body::from(format!("Request received: {url}")))
}

Create src/index.js:

addEventListener('fetch', (event) => {
  const url = event.request.url;
  event.respondWith(new Response(`Request received: ${url}`));
});

Build

Compile the handler to a WebAssembly binary. The first build downloads dependencies and takes one to two minutes.

cargo build --release --target wasm32-wasip2

The binary is at ./target/wasm32-wasip2/release/hello_world.wasm.

cargo build --release --target wasm32-wasip1

The binary is at ./target/wasm32-wasip1/release/hello_world.wasm.

npx edgecompute-build --input src/index.js --output wasm/app.wasm

The binary is at ./wasm/app.wasm.

Deploy

  1. In the CDB Technical Web Portal, navigate to CDB EdgeCompute > HTTP Applications > Applications and click Create new application.
  2. Click Upload binary and select the .wasm file produced in the Build step.
  3. Enter a Name for the application.
  4. Click Save and deploy.

The application URL appears on the details page. Test it:

curl -i https://<app-name>-<id>.edgecompute.app/hello

Deploying requires two API calls: upload the binary to get its ID, then create the application. An API token is required.

export GCORE_API_KEY="{YOUR_API_KEY}"
Do not commit API keys to source control.

1. Upload the binary

curl -sX POST 'https://api.cdb-staging.cdn.orange.com/edgecompute/v1/binaries/raw' \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H 'Content-Type: application/octet-stream' \
  --data-binary "@./target/wasm32-wasip2/release/hello_world.wasm"
curl -sX POST 'https://api.cdb-staging.cdn.orange.com/edgecompute/v1/binaries/raw' \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H 'Content-Type: application/octet-stream' \
  --data-binary "@./target/wasm32-wasip1/release/hello_world.wasm"
curl -sX POST 'https://api.cdb-staging.cdn.orange.com/edgecompute/v1/binaries/raw' \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H 'Content-Type: application/octet-stream' \
  --data-binary "@./wasm/app.wasm"

The response contains the binary ID:

{"id": 4695, "api_type": "wasi-http", "status": 1}

2. Create the application

export BINARY_ID=4695

curl -sX POST 'https://api.cdb-staging.cdn.orange.com/edgecompute/v1/apps' \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H 'Content-Type: application/json' \
  -d "{\"name\": \"my-http-app\", \"binary\": $BINARY_ID, \"status\": 1}"

The response contains the application URL:

{"name": "my-http-app", "url": "https://my-http-app-1000503.edgecompute.app", "api_type": "wasi-http"}

3. Test

The URL becomes active within a few seconds:

curl -i https://my-http-app-1000503.edgecompute.app/hello