Guides
Rust (Axum) HTTP server

Quickstart guide for a Rust HTTP server

In this quickstart guide, you'll learn how to deploy an HTTP server on the Wasmer Edge. We will be using Rust's popular Axum (opens in a new tab) framework to create a simple HTTP server.

Prerequisites

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

Deploying an HTTP Server

Install Wasmer

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

Initialize the local directory

With the CLI installed, let's create a new Axum application! We begin creating a new empty directory - after that, we will need to run a single command.

$ mkdir my-axum-server
$ cd my-axum-server
$ wasmer deploy --template=axum-starter
It seems you are trying to create a new app!
 Who should own this app? · <you> 
 What should be the name of the app? · <your-app-name> 
 
 Unpacked template
 
NOTE: The selected template has a `BUILD.md` file.
This means there are likely additional build 
steps that you need to perform before deploying
the app:
 
# Building the application
1. Install [WASIX](https://wasix.org/docs/language-guide/rust/installation)
2. Build the application: `cargo wasix build --release`
 
After taking the necessary steps to build your application, re-run `wasmer deploy`

You can see, as prompted by the command, that we'll need a couple more steps before deploying the server, namely actually building the application: we assume you installed the WASIX (opens in a new tab) toolchain. We need to build the HTTP server using cargo wasix:

$ cargo wasix build --release
...
Compiling wasmer-hello v0.1.0 (/.../Wasmer/edge-quickstart/wasmer-hello)
Finished dev [unoptimized + debuginfo] target(s) in 10.69s
info: Post-processing WebAssembly files
🚨

It might happen that your build fails. This is due to different libraries that needs to be used to achieve WASIX compatibility: one of the easiest "gotchas" is forgetting to update your lock file with cargo update.

Now, Your directory structure should look like this:

      • main.rs
    • Cargo.toml
    • wasmer.toml
    • app.yaml
    • LICENSE
    • README.md
    • BUILD.md
  • Once the application itself was correctly built, you can re-run wasmer deploy:

    $ wasmer deploy 
    It seems you are trying to create a new app!
     Who should own this app? · <you> 
     What should be the name of the app? · <your-app-name> 
     Unpacked template
     Do you want to deploy the app now? · yes
    Loading local package (manifest path: <current-working-directory>)
     Correctly built package locally
     Package correctly uploaded
     Succesfully pushed release to namespace <you> on the registry 
    Deploying app <your-app-name> (<you>) to Wasmer Edge...
     
    App <your-app-name> (<you>) was successfully deployed 🚀
    https://<your-app-name>-<you>.wasmer.app
     
     Unique URL: https://<app_id>.id.wasmer.app
     Dashboard:  https://wasmer.io/apps/<you>/<your-app-name>
     
    Waiting for new deployment to become available...
    (You can safely stop waiting now with CTRL-C)
    .
    𖥔 Deployment complete
     

    Let's check it:

    $ curl https://<your-app-name>-<you>.wasmer.app
    Hello, Axum ❤️ WASIX!

    Test the server locally

    We can test out server locally by running the following command:

    Terminal 1Terminal 2
    wasmer-hello
    $ wasmer run ./target/wasm32-wasmer-wasi/release/wasmer-hello.wasm --env PORT=3000
    Listening on http://127.0.0.1:3000
    curl
    $ curl http://localhost:3000
    Hello, Axum ❤️ WASMER!

    Conclusion

    Congratulations! You have successfully deployed an Axum server on Wasmer Edge 🚀.

    Tip: To make changes to your Axum server, simply modify the main.rs file in the app directory and run wasmer deploy again to deploy the changes.

    Resources

    wasmer-examples/axum-wasmer-starter