Skip to Content
GuidesCGI apps on Edge

CGI apps on Wasmer Edge

In this tutorial, you will learn how to deploy a CGI app on Wasmer Edge. We will be using the Rust’s CGI crate  to create a CGI app.

We’ll just make a simple Hello, World! CGI app.

What is CGI?

CGI or Common Gateway Interface is a standard for running programs on a web server to generate dynamic content. CGI programs are often written in scripting languages like Perl, Python, or Ruby, but they can also be written in compiled languages like C, C++, or Rust.

CGI with Wasmer

CGI works using the standard input and output streams. Wasmer maps the standard input and output streams to the HTTP request and response. You can read more about WCGI in our article, Announcing WCGI .

Prerequisites

The project requires the following tools to be installed on your system:

Creating a CGI app

Install Wasmer

Click here for instructions on how to install Wasmer if you haven’t done it already!

Log in into Wasmer

Create a new account in Wasmer . Then, log in into the Wasmer CLI and follow the provided steps to provide the CLI access to your Wasmer account.

wasmer login

Install the wasm32-wasip1 target:

rustup target add wasm32-wasip1

This will install the wasm32-wasip1 target for the Rust compiler.

Initialize the project

We’ll initialize an empty rust project using the cargo command-line tool.

cargo init --bin hello-cgi && cd hello-cgi

Add the CGI crate

cargo add cgi

Writing the CGI app

We’ll write a simple CGI app that prints Hello, World! to the standard output. This app also accepts a name as a body of the request and prints Hello, {name}!

Note: This cgi app will be wired to WCGI and will be served on the root path.

src/main.rs
use cgi::{http::StatusCode, Request, Response}; fn main() { cgi::handle(handler); } fn handler(request: Request) -> Response { let who = String::from_utf8_lossy(request.body()); let who = if who.trim().is_empty() { "World" } else { who.trim() }; cgi::text_response(StatusCode::OK, format!("Hello, {who}!")) }

Building the CGI app

As you see above in the source property, in the module section. We use the release build in wasm32-wasip1 target.

cargo build --release --target wasm32-wasip1

This will create a hello-cgi.wasm file in the target/wasm32-wasip1/release directory.

Wiring it up

To run this wcgi app on wasmer, we first need to create a wasmer.toml configuration file. Create a new file wasmer.toml at root level, and copy the following:

wasmer.toml
[package] name = "<your-namespace>/<your-package-name>" # 👈 Update this version = "0.1.0" description = "An application sample for WCGI" license = "MIT OR Apache-2.0" [[module]] name = "server" source = "target/wasm32-wasip1/release/hello-cgi.wasm" abi = "wasi" [[command]] name = "server" module = "server" runner = "https://webc.org/runner/wcgi" # 👈 This is the runner we'll be using [command.annotations.wcgi] dialect = "rfc-3875" # This is the CGI dialect we'll be using
ℹ️
    1. your-namespace: This is usually your username and also can be your organization name.
    1. your-package-name: This is the name of your package that’ll be published to the wasmer-registry.

Your project should now look like this:

      • main.rs
    • Cargo.toml
    • wasmer.toml
ℹ️

The local wasmer run command can only execute WASI(X) packages, so a WCGI package cannot be run locally. WCGI packages still deploy and run on Wasmer Edge, so we test the app by deploying it.

Deploying the CGI app

In order to deploy the app, all we need to do is to run:

wasmer deploy

This will tigger a wizard which sets up the remaining configuration needed, similar to this:

It seems you are trying to create a new app! Who should own this app? · wasmer-user What should be the name of the app? · hello-cgi A package manifest was found in path /tmp/tmp.bDul0Ng4o3/hello-cgi/wasmer.toml. Use it for the app? · yes Do you want to deploy the app now? · yes Loading local package (manifest path: /tmp/tmp.bDul0Ng4o3/hello-cgi/.) Correctly built package locally Package correctly uploaded Succesfully pushed release to namespace wasmer-user on the registry Successfully tagged package wasmer-user/wcgi-hello@0.1.0 Deploying app hello-cgi (wasmer-user) to Wasmer Edge... App hello-cgi (wasmer-user) was successfully deployed 🚀 https://hello-cgi-wasmer-user.wasmer.app Unique URL: https://2j6h5i7cpmj6.id.wasmer.app Dashboard: https://wasmer.io/apps/wasmer-user/hello-cgi Waiting for new deployment to become available... (You can safely stop waiting now with CTRL-C) . 𖥔 Deployment complete

Note how there’s been a new file, app.yaml created. This file configures how to deploy your app to Wasmer edge.

Testing the deployed app

We can test the deployed app using curl.

curl https://hello-cgi-wasmer-user.wasmer.app

The deployment URL follows the format https://<app-name>.wasmer.app. If the name is already taken, a suffixed variant such as https://<app-name>-<app-owner>.wasmer.app is used instead.

Hello, World!

We can also send a POST request with a name in the body:

curl -X POST -d "Wasmer" https://hello-cgi-wasmer-user.wasmer.app
Hello, Wasmer!

Works as expected!

Conclusion

In this tutorial, we learned how to deploy a CGI app on Wasmer Edge.

References

Resources

We’ve have also created a template repository for you to get started with CGI apps on Wasmer Edge.

wasmerio/wcgi-rust-template

Next steps

Last updated on