generated from shuding/nextra-docs-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(docs): Remove "package", change PHP example
- Loading branch information
Showing
5 changed files
with
252 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
import { Steps, Callout, FileTree } from "nextra-theme-docs"; | ||
import Install from "@components/install.mdx"; | ||
import Login from "@components/login.mdx"; | ||
import CliVersionCallout from "@components/deploy/CliVersionCallout.mdx"; | ||
|
||
# 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](https://github.com/tokio-rs/axum) framework to create a simple HTTP server. | ||
|
||
## Prerequisites | ||
|
||
The project requires the following tools to be installed on your system: | ||
|
||
- [Rust](https://www.rust-lang.org/tools/install) | ||
- [Wasmer CLI](/install) | ||
- [`WASIX`](http://wasix.org/docs/language-guide/rust/installation) | ||
|
||
## Deploying an HTTP Server | ||
|
||
<Steps> | ||
|
||
<Install /> | ||
|
||
<CliVersionCallout /> | ||
|
||
### 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. | ||
|
||
```bash | ||
$ 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`] toolchain. We need to build the HTTP server | ||
using `cargo wasix`: | ||
|
||
``` | ||
$ cargo wasix build | ||
... | ||
``` | ||
|
||
Now, Your directory structure should look like this: | ||
|
||
<FileTree> | ||
<FileTree.Folder name="wasmer-hello" defaultOpen> | ||
<FileTree.Folder name="src" defaultOpen> | ||
<FileTree.File name="main.rs" /> | ||
</FileTree.Folder> | ||
<FileTree.File name="Cargo.toml" /> | ||
</FileTree.Folder> | ||
</FileTree> | ||
|
||
Next, add the following dependencies to your `Cargo.toml` file: | ||
|
||
```toml copy filename="Cargo.toml" | ||
[package] | ||
name = "wasmer-hello" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
axum = { version = "=0.6.9", features = ["tokio", "json"] } | ||
serde = { version = "1.0.160", features = ["derive"] } | ||
tracing = "0.1.37" | ||
tracing-subscriber = { version = "0.3.16", features = ["fmt"] } | ||
|
||
# NOTE: We need to pin and replace some dependencies to achieve wasix compatibility. | ||
tokio = { version = "=1.24.2", default-features = false, features = ["full"] } | ||
parking_lot = { version = "=0.12.1", features = ["nightly"] } | ||
|
||
[patch.crates-io] | ||
tokio = { git = "https://github.com/wasix-org/tokio.git", branch = "wasix-1.24.2" } | ||
socket2 = { git = "https://github.com/wasix-org/socket2.git", branch = "v0.4.9" } | ||
libc = { git = "https://github.com/wasix-org/libc.git", branch = "master" } | ||
``` | ||
|
||
<Callout type="default" emoji="🚧"> | ||
We need to pin and replace some dependencies to achieve wasix compatibility. | ||
To learn more about these patches, click | ||
[here](http://wasix.org/docs/language-guide/rust/patched-repos). | ||
</Callout> | ||
|
||
After updating your dependencies, let's update the `src/main.rs` file with the following code: | ||
|
||
```rust copy filename="src/main.rs" | ||
use axum::{routing::get, Router}; | ||
use std::net::SocketAddr; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Building our application with a single Route | ||
let app = Router::new().route("/", get(handler)); | ||
|
||
let port = std::env::var("PORT").unwrap_or("80".to_string()); | ||
let port = port.parse::<u16>().unwrap_or_else(|_| { | ||
eprintln!("Invalid port number: {}", port); | ||
std::process::exit(1); | ||
}); | ||
// Run the server with hyper on http://127.0.0.1:3000 | ||
let addr = SocketAddr::from(([127, 0, 0, 1], port)); | ||
eprintln!("Listening on http://{}", addr); | ||
axum::Server::bind(&addr) | ||
.serve(app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
async fn handler() -> &'static str { | ||
"Hello, Axum ❤️ WASMER!" | ||
} | ||
``` | ||
|
||
> Note: We are using the `PORT` environment variable to set the port number for our server to listen on. This is for local development. On wasmer edge, the port number should be set to `80`. That's why it's the default value. | ||
> This is done so because wasmer edge expects the server to listen on port `80`. | ||
Now, let's build our project using `WASIX`: | ||
|
||
```shell copy | ||
$ cargo wasix build --release | ||
... | ||
Compiling serde_urlencoded v0.7.1 | ||
Compiling serde_path_to_error v0.1.11 | ||
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 | ||
``` | ||
|
||
<Callout type="default" emoji="🚨"> | ||
It might happen that your build fails. One of the easiest way to resolve this | ||
is updating your lock file with `cargo update`. | ||
</Callout> | ||
|
||
### Test the server locally | ||
|
||
We can test out server locally by running the following command: | ||
|
||
import TerminalLeft from "@components/deploy/quickstart/TerminalLeft.mdx"; | ||
import TerminalRight from "@components/deploy/quickstart/TerminalRight.mdx"; | ||
|
||
| Terminal 1 | Terminal 2 | | ||
| ---------------- | ----------------- | | ||
| <TerminalLeft /> | <TerminalRight /> | | ||
|
||
### Deploying to Wasmer Edge | ||
|
||
Now let's add our deployment configuration. | ||
|
||
For this we need two files: | ||
|
||
- `wasmer.toml` - This file contains the configuration for the Wasmer Registry. | ||
- `app.yaml` - This file contains the configuration for the Wasmer Edge deployment. | ||
|
||
```toml copy filename="wasmer.toml" | ||
[package] | ||
name = "<your-namespace>/wasmer-hello" | ||
version = "0.1.0" | ||
description = "Sample Axum server for Wasmer Edge" | ||
license = "MIT" | ||
wasmer-extra-flags = "--net --enable-threads --enable-bulk-memory" | ||
|
||
[[module]] | ||
name = "wasmer-hello" | ||
source = "./target/wasm32-wasmer-wasi/release/wasmer-hello.wasm" | ||
abi = "wasi" | ||
|
||
[[command]] | ||
name = "proxy" | ||
module = "wasmer-hello" | ||
runner = "https://webc.org/runner/wasi" | ||
|
||
``` | ||
|
||
```yaml copy filename="app.yaml" | ||
kind: wasmer.io/App.v0 | ||
name: <your-username>-hello | ||
description: Sample Axum server for Wasmer Edge | ||
package: <your-namespace>/<your-username>-hello | ||
``` | ||
<Callout type="info" emoji="ℹ️"> | ||
You can also check if your configuration is valid by running `wasmer publish | ||
--dry-run`. To learn more about the `wasmer.toml` file, click | ||
[here](../configuration/app-configuration). | ||
</Callout> | ||
|
||
Let's Deploy! | ||
|
||
```shell copy | ||
$ wasmer deploy | ||
Found local package in wasmer.toml | ||
Publish package '<your-namespace>/wasmer-hello'? yes | ||
Publishing package... | ||
[1/2] ⬆️ Uploading... | ||
[2/2] 📦 Publishing... | ||
Successfully published package `<your-namespace>/<your-username>-hello@0.1.0` | ||
Package '<your-namespace>/wasmer-hello@0.1.0' published successfully! | ||
Writing updates app.yaml... | ||
Publishing app... | ||
App published successfully! | ||
Access your app at: https://x20imtdf68qv.id.wasmer.app | ||
``` | ||
<Callout type="info" emoji="ℹ️"> | ||
`wasmer deploy` automatically publishes your package and bumps the minor | ||
version. You can check all the available options with `wasmer deploy --help` | ||
or click [here](/edge/cli) to see the full documentation. | ||
</Callout> | ||
|
||
Now you can access your axum server at the url provided in the output by the `wasmer deploy` command. | ||
|
||
</Steps> | ||
|
||
## Conclusion | ||
|
||
Congratulations! You have successfully deployed your first Axum server to Wasmer Edge. | ||
|
||
## Additional Resources | ||
|
||
To learn more about Wasmer Edge, click [here](https://wasmer.io/products/edge). | ||
|
||
Checkout our full tutorial [here](http://wasix.org/docs/language-guide/rust/tutorials/wasix-axum) for `wasix-axum`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters