Use this file to discover all available pages before exploring further.
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: by uploading a custom binary file (built using the JavaScript SDK or Rust) or by using a preconfigured template. If you use a template, skip Stage 1.
In this example, we’ll create a simple app that responds with “HTTP 200” and the text “Hello world!” in the response’s body. You can find more examples in the Gcore repository.Create a main source file src/lib.rs with the following content:
The resulting Wasm code will be written to the myapp/target/wasm32-wasip1/release/myapp.wasm file.
A JavaScript code pattern closely resembles Service Worker API. You can also find multiple examples in the Gcore repository.The key aspect of the Wasm configuration is to set up the addEventListener that has to synchronously call event.respondWith with a callback. This callback can be asynchronous and this is where you’d usually include any custom code to generate a response.Here’s the sample app source:
async function app(event) { const request = event.request; return new Response(`You made a request to ${request.url}`);}addEventListener('fetch', (event) => { event.respondWith(app(event));});
Run the command npm create fastedge-app my-first-app.For first-time setup select the following options:
$ npm create fastedge-app my-first-app┌ create-fastedge-app│◇ Creating in provided directory: ./my-first-app│◇ Select programming language:│ TypeScript│◇ Language: typescript│◇ Select a template:│ http-base│◆ Do you want to continue?│ ● Yes / ○ No
For detailed steps on how to deploy a FastEdge app, refer to the relevant sections below:
In the Customer Portal. Follow the instructions if you created a custom Wasm using either the Rust or Javascript SDK, or if you want to create a FastEdge app from a preconfigured template.
Via command line: Follow the instructions if you want to deploy a custom Wasm using cURL and our API.
1. In the Gcore Customer Portal, navigate to FastEdge > HTTP Applications.2. In the top-right corner of the screen, click Create new application.
3. Click Upload binary.
4. Choose your custom binary file.
5. Enter a name for your application and provide a description if needed.6. (Optional) Click + Add response headers to 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 customize the behavior of your application, click + Add environment variables and enter your data as key-value pairs.
InfoIf you’re adding sensitive information or want to ensure that any data in the app’s configuration remains secure, click + Add Secret and use the Secrets Manager.
8. Check all the settings. If everything is configured correctly, 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 > HTTP Applications.2. In the top-right corner of the screen, click Create new application.
3. In the Create from a template section, select the preferred template.
4. Enter a name for your application and, optionally, update its description.5. Provide environment variables or any required configuration for your app. Note that the list of setup options depends on the selected template. For example, if you create a FastEdge app from a Markdown template, you need to add response headers, enter the base part of the origin URL, and add content from the <head> section of an HTML document.6. (Optional) If you want to add metadata to the configuration, click + Add environment variables and enter metadata as key-value pairs.
7. 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. Upload the Wasm binary to our edge servers by running the following API request from the repo’s root directory. Insert your permanent API token instead of the api_key:
In the response, you will receive the ID of the uploaded binary (<binary_id>). Make sure to save it, as it will be used in the following step.2. Create the app by running the following API request:
You can test the application after its deployment by clicking the application link on the deployment confirmation screen:
Additionally, you can inspect and adjust the configuration in the Customer Portal on the HTTP Applications page:1. In the Gcore Customer Portal, navigate to FastEdge > HTTP Applications > Applications.
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, the response for the application configured in Stage 1 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))}
InfoThe 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 stage 1.2. Upload it to the Gcore 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:
geoip-*: Client GeoIP data, such as asn, latitude, longitude, region, city, continent, country name, and country code
server_addr: PoP IP address
server_name: Application hostname
x-forwarded-for: Client IP address
pop-*: PoP GeoIP data, such as asn, latitude, longitude, region, city, continent, country name, and country code
You can add more functionality to your app. For example, instead of printing “You made a request to /”, the app can print all request headers and set a custom response header from the environment settings.
The application logic (e.g., location-aware redirection) assumes the use of the headers listed in the following steps. The headers may change in the future.
Run the following curl request: curl https://<app_name>.fastedge.gcore.dev/, where <app_name> is the name of your application indicated in the previous step.If everything is updated correctly, the response will be: