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 (opens in a new tab) 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 (opens in a new tab).
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 (opens in a new tab). 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-wasi
target:
rustup target add wasm32-wasi
This will install the wasm32-wasi
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.
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-wasi target.
cargo build --release --target wasm32-wasi
This will create a hello-cgi.wasm
file in the target/wasm32-wasi/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:
[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-wasi/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
your-namespace
: This is usually your username and also can be your organization name.
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
Running the CGI app
Now we can test our CGI app locally using the wasmer
command-line.
wasmer run .
The above command will start a local server on port 8000
.
To test the app, we can use curl
to send a request to the server.
curl http://localhost:8000
The root path will return Hello, World!
as we didn't specify any name in the body.
Hello, World!
curl -X POST -d "Wasmer" http://localhost:8000
The above command will send a POST
request with the body Wasmer
to the server.
Hello, Wasmer!
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>-<app-owner>.wasmer.app
Hello, World!
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