Saying WCGI: WebAssembly + CGI
Welcome to the way forward for server-side improvement with WebAssembly!
Right this moment we’re introducing WCGI, a know-how that marries the facility of WebAssembly with the
versatility and ease of CGI.
Listed here are a few of WCGI’s highlights:
- You possibly can reuse your current CGI purposes by compiling them to WASI
(AssemblyScript, C, C++, Go, PHP, Python, …) - Ship ultra-small packages that can solely comprise your enterprise logic
and static belongings, no HTTP stack or cumbersome Docker containers - Fully sandboxed execution: WebAssembly code runs in a sandbox, with one
remoted occasion per request
Image working WordPress and never having to fret about attackers breaking into
your system. With WCGI, now this a actuality!
Strive WordPress domestically with Wasmer
# Set up wasmer beta 2
curl https://get.wasmer.io -sSfL | sh -s "v3.2.0-beta.2"
# Execute WordPress
mkdir db
wasmer run-unstable wasmer/wcgi-wordpress-demo --mapdir=/db:db
We’re trialing the brand new runner structure through
wasmer run-unstable
.
Sooner or later you’d merely usewasmer run
The Why Behind WCGI
When venturing into serverless options at Wasmer, we confronted a vital query:
ought to we create our personal framework and threat locking builders right into a walled
backyard, or ought to we undertake an open commonplace that enables them to make the most of current
code?
CGI’s alignment with the aim of executing a program per HTTP request makes it a
compelling selection. Apparently, CGI can outperform many different options (reminiscent of WSGI in Python/Ruby or NodeJS) by way of scalability and latency in serverless environments.
Moreover, it additionally intently mirrors the employees proposal from the Winter
Community
Group.
Contemplate the problem of working PHP applications on servers. We now have two main
choices:
- Wrap the PHP interpreter with a layer that devices every HTTP name
- Use the prevailing
php-cgi
program and easily compile it to Wasm
Possibility 2 just isn’t solely sooner, nevertheless it additionally allows any internet software on Wasmer
extra effectively.
By embracing WCGI, these searching for to realize better effectivity, safety, and adaptability in server-side improvement can actually profit from this method.
Making a WCGI Utility with Rust
To create a WCGI software utilizing Rust, first add the cgi
crate as a
dependency in your Cargo.toml
file:
$ cargo add cgi
Subsequent, write your Rust server, like the instance beneath:
// src/lib.rs
use cgi::{http::StatusCode, Request, Response};
fn important() {
cgi::deal with(handler);
}
fn handler(request: Request) -> Response {
let who = String::from_utf8_lossy(request.physique());
let who = if who.trim().is_empty() {
"World"
} else {
who.trim()
};
cgi::text_response(StatusCode::OK, format!("Howdy, {who}!"))
}
After writing your implementation, compile it to the wasm32-wasi
goal.
You might want to put in the WASI goal should you haven’t already (rustup goal add wasm32-wasi
).
$ cargo construct --goal wasm32-wasi --launch
Create a wasmer.toml
file that describes your bundle and comprises a WCGI
command that the wasmer
CLI will execute.
[package]
title = "wasmer/wcgi-rust-template"
model = "0.1.0"
description = "A template for WCGI purposes"
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/wasmerio/wcgi-rust-template"
[[module]]
title = "server"
supply = "goal/wasm32-wasi/launch/wcgi-rust-template.wasm"
abi = "wasi"
[[command]]
title = "server"
runner = "wcgi"
module = "wcgi"
annotations = { wcgi = { dialect = "rfc-3875" } }
Now you can begin the server and browse to
http://localhost:8000/ to see it in motion:
$ wasmer run-unstable .
Making a WCGI Utility in PHP
PHP-CGI was first launched by Rafael Fernández López.
Because of his unique model of php-cgi
compiled to WebAssembly & WASI,
Wasmer can now run PHP web sites utilizing WCGI.
First, create a brand new repository and replica
php-cgi.wasm
into it. Then, create an app/
listing and add some PHP code to it.
$ mkdir app
$ echo '<? print("Howdy, World!"); ?>' > app/index.php
Now we have to create a wasmer.toml
file.
It is a bit longer than the Rust one as a result of we have to set some atmosphere
variables that inform php-cgi
which script to invoke. We additionally need the app/
folder to be bundled with the bundle after we publish it.
[package]
title = "wasmer/wcgi-php-template"
model = "0.1.0"
description = "A PHP template for WCGI purposes"
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/wasmerio/wcgi-php-template"
[[module]]
title = "php"
supply = "php-cgi.wasm"
abi = "wasi"
[[command]]
title = "php"
runner = "wcgi"
module = "php"
[command.annotations.wcgi]
dialect = "rfc-3875"
[command.annotations.wasi]
atom = "php"
env = ["DOCUMENT_ROOT=/app", "SCRIPT_FILENAME=/app/index.php"]
[fs]
"app" = "app"
Now you can run this with wasmer run-unstable
:
$ wasmer run-unstable .
Opening up our internet browser reveals the “Howdy, World” message as anticipated.
You too can use the wasmer
CLI’s --mapdir
flag to make particular directories
in your host machine obtainable to the WebAssembly software.
That is actually helpful throughout improvement as a result of it means you possibly can mount your
app/
listing and make adjustments on the fly with no need to restart the WCGI
server.
First, begin the WCGI server and inform it that the /app
listing contained in the
WebAssembly software corresponds to the ./app
listing in your host.
$ wasmer run-unstable --mapdir /app:./app .
When you open http://localhost:8000/ up in your browser,
it is best to see the “Howdy, World!” from earlier than.
Subsequent, modify app/index.php
to print phpinfo()
, hit save, and refresh your
browser.
This bundle can be obtainable on WAPM.
$ wasmer run-unstable wasmer/wcgi-php-template
You’ll find in Github the supply code for the WordPress Wasmer pacakage, and the PHP WCGI template.
We will not wait to see what you create subsequent with WCGI!
In Abstract…
WCGI represents a refined method to server-side improvement, integrating the
flexibility, safety, and efficiency of WebAssembly. This modern
know-how has the potential to reshape the panorama of serverless
purposes, offering builders with a strong and versatile answer for
their tasks.
Watch this house ????