This guide describes how to create a FastEdge app. Check out our FastEdge overview article to learn more about the product.
You can create a FastEdge app in two ways: from a custom binary file or a pre-configured template. If you chose the last option, skip the Stage 1.
To get started, you need to create a .wasm file that you will later upload to the Gcore Customer Portal.
1. Install the Rust compiler and cargo (package manager):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
2. Add the Wasm compilation target to Rust compiler:
rustup target add wasm32-wasi
1. Initialize the directory structure:
cargo new myapp --lib
2. Create a directory:
mkdir myapp/.cargo
3. Set the Wasm compilation target for the project by creating a config file myapp/.cargo/config.toml
with the following content:
[build]
target = "wasm32-wasi"
4. Create the project manifest file myapp/Cargo.toml
with the following content:
[package]
name = "myapp"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
fastedge = "0.1"
For illustration, we’ll create a simple app that responds with "HTTP 200" and the text “Hello world!” in the response’s body.
Create a main source file src/lib.rs with the following content:
use fastedge::{
body::Body,
http::{Request, Response, StatusCode, Error},
};
#[fastedge::http]
fn main(_req: Request<Body>) -> Result<Response<Body>, Error> {
Response::builder()
.status(StatusCode::OK)
.body(Body::from("Hello world!\n"))
}
Produce the Wasm binary:
cargo build --release
The resulting Wasm code will be written to the myapp/target/wasm32-wasi/release/myapp.wasm
file.
1. In the Gcore Customer Portal, navigate to FastEdge > Create application.
2. Click Upload binary.
3. Choose your custom binary file.
4. Click Save binary to upload it.
5. Enter a name for your application and provide a description if needed.
6. (Optional) Add fixed headers to the responses. For example, you may include CORS (Cross-Origin Resource Sharing) headers in each response to ensure secure communication between origins.
7. (Optional) If you want to add metadata to the configuration, click Add parameters and enter metadata as key-value pairs.
8. In the top-right corner of the screen, click Save and deploy.
Your application has been successfully deployed. You can now test its configuration and adjust it as described in the following steps.
1. In the Gcore Customer Portal, navigate to FastEdge > Create application.
2. In the Launch from a template section, select the preferred template.
3. Enter a name for your application and update a description if needed.
4. Provide required environment variables for the application:
If you selected a GitHub template, enter the repository name and add your personal access token.
If you selected a Markdown template, enter the base part of the origin URL and add content from the <head>
section of an HTML document.
5. (Optional) Add metadata as key-value pairs.
6. Click Save and deploy.
Your application has been successfully deployed. You can now test its configuration and adjust it as described in the following steps.
You can test the application after its deployment by clicking the application link on the deployment confirmation screen:
You can also inspect and adjust the configuration from the Dashboards page:
1. In the Gcore Customer Portal, navigate to FastEdge > Dashboard.
2. Find the app you want to test and click its name to open it.
3. Click the app link next to the app status to view the response.
For example, for the application configured in the Stage 1, the response will be “Hello world!”
You can add more functionality to your app. For example, instead of printing “Hello world!”, the app can print all request headers and set a custom response header from the environment settings. Let’s see how to do that.
To print all request headers and develop a custom response header, replace the current content of the myapp/src/lib.rs
file with the following:
use fastedge::{
body::Body,
http::{Request, Response, StatusCode, Error},
};
use std::env;
#[fastedge::http]
fn main(req: Request<Body>) -> Result<Response<Body>, Error> {
// print headers
let mut body: String = "".to_string();
for (h, v) in req.headers() {
body.push_str(h.as_str());
body.push_str(": ");
match v.to_str() {
Err(_) => body.push_str("not a valid text"),
Ok(a) => body.push_str(a),
}
body.push_str("\n");
}
// get value for custom header from the env var
let value = match env::var("CUSTOM_HEADER").ok() {
None => return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from("App misconfigured\n")),
Some(val) => val
};
// build response with body and custom header
Response::builder()
.status(StatusCode::OK)
.header("Custom-Header", value)
.body(Body::from(body))
}
The headers listed in the following step are passed to the FastEdge application, which uses the header content for functionalities like geolocation-aware redirects.
Update the application on the edge servers:
1. Compile a new Wasm file as described in step 4.
2. Upload it to the Customer Portal as a custom binary file.
When you open the app, you’ll see all request headers from the environment settings. It will be similar to the following output:
If you’re having issues with your FastEdge application, check out the following sections for helpful tips and troubleshooting suggestions.
If your application is correctly configured and works as expected, FastEdge will return the expected status code like "200 OK".
However, in some exceptional situations, you might get the following status codes. Check out the description to understand the root cause and how to fix it.
Status code | Description |
---|---|
530 | Internal FastEdge error. |
531 | Application has exceeded the allowed memory limit. |
532 | Application has timed out. |
533 | Application has crashed. |
Was this article helpful?