Skip to content

Commit

Permalink
fix(docs): Remove "package", change PHP example
Browse files Browse the repository at this point in the history
  • Loading branch information
xdoardo committed May 20, 2024
1 parent 37aa265 commit 2cbe7b5
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pages/edge/quickstart/js-wintercg.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Waiting for new deployment to become available...

The `wasmer deploy --template=js-worker` command will prompt you for the following:

- **App owner**: This is the owner of the package. It can be your username
- **App owner**: This is the owner of the app. It can be your username
or an organization; if you're logged in, the command will prompt you to
choose from your namespaces: by default, it will be your username.
- **App name**: This is the name of your app. By default, it will be the name
Expand Down
26 changes: 6 additions & 20 deletions pages/edge/quickstart/php.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Waiting for new deployment to become available...

The `wasmer deploy --template=php-starter` command will prompt you for the following:

- **App owner**: This is the owner of the package. It can be your username
- **App owner**: This is the owner of the app. It can be your username
or an organization; if you're logged in, the command will prompt you to
choose from your namespaces: by default, it will be your username.
- **App name**: This is the name of your app. By default, it will be the name
Expand Down Expand Up @@ -93,18 +93,10 @@ To illustrate the lifecycle of an app, let's edit the `index.php` file
in the `app` folder:

```php copy filename="app/index.php"
<?php
// Enter your code here, enjoy!
$array = array("1" => "PHP code tester Sandbox Online",
"emoji" => "😀 😃 😄 😁 😆", 5 , 5 => 89009,
"Random number" => rand(100,999),
"PHP Version" => phpversion(), // <- Notice the comma!
"Hello" => "World" // <- New line
);

foreach( $array as $key => $value ){
echo $key."\t=>\t".$value."\n";
}
<?php
echo 'Hello World!';
var_dump($_SERVER);
?>
```

Now, simply run `wasmer deploy` again:
Expand Down Expand Up @@ -132,13 +124,7 @@ Waiting for new deployment to become available...
Let's check it again:
```shell
$ curl https://<your-app-name>-<you>.wasmer.app
1 => PHP code tester Sandbox Online
emoji => 😀 😃 😄 😁 😆
2 => 5
5 => 89009
Random number => 286
PHP Version => 8.3.4
Hello => World
Hello World! <more text..>
```

### Testing your PHP application locally
Expand Down
2 changes: 1 addition & 1 deletion pages/edge/quickstart/python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Waiting for new deployment to become available...

The `wasmer deploy --template=python-http-server` command will prompt you for the following:

- **App owner**: This is the owner of the package. It can be your username
- **App owner**: This is the owner of the app. It can be your username
or an organization; if you're logged in, the command will prompt you to
choose from your namespaces: by default, it will be your username.
- **App name**: This is the name of your app. By default, it will be the name
Expand Down
243 changes: 243 additions & 0 deletions pages/edge/quickstart/rust-http-server.mdx
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`.
2 changes: 1 addition & 1 deletion pages/edge/quickstart/static-site.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Waiting for new deployment to become available...

The `wasmer deploy --template=static-website` command will prompt you for the following:

- **App owner**: This is the owner of the package. It can be your username
- **App owner**: This is the owner of the app. It can be your username
or an organization; if you're logged in, the command will prompt you to
choose from your namespaces: by default, it will be your username.
- **App name**: This is the name of your app. By default, it will be the name
Expand Down

0 comments on commit 2cbe7b5

Please sign in to comment.