-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Rocket, Actix, Tokio and basic Rust examples. Signed-off-by: Felipe Huici <[email protected]>
- Loading branch information
1 parent
a83c14d
commit 7fa33a6
Showing
21 changed files
with
575 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "hello" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
actix-web = "4" |
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,15 @@ | ||
FROM rust:1.75.0-bookworm AS build | ||
|
||
RUN cargo new --bin app | ||
WORKDIR /app | ||
COPY Cargo.toml ./ | ||
COPY src ./src | ||
RUN cargo build --release | ||
|
||
FROM scratch | ||
|
||
COPY --from=build /app/target/release/hello /server | ||
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/ | ||
COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/ | ||
COPY --from=build /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/ | ||
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ |
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,7 @@ | ||
spec: v0.6 | ||
|
||
runtime: base:latest | ||
|
||
rootfs: ./Dockerfile | ||
|
||
cmd: ["/server"] |
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,64 @@ | ||
# Rust Actix Web Server | ||
|
||
This directory contains an [Actix](https://actix.rs/) web server running on Unikraft. | ||
|
||
## Set Up | ||
|
||
To run this example, [install Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli), clone this repository and `cd` into this directory. | ||
|
||
## Run and Use | ||
|
||
Use `kraft` to run the image and start a Unikraft instance: | ||
|
||
```bash | ||
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 | ||
``` | ||
|
||
If the `--plat` argument is left out, it defaults to `qemu`. | ||
If the `--arch` argument is left out, it defaults to your system's CPU architecture. | ||
|
||
Once executed, it will open port `8080` and wait for connections. | ||
To test it, you can use `curl`: | ||
|
||
```bash | ||
curl localhost:8080 | ||
``` | ||
|
||
You should see a "Hello, World!" message. | ||
|
||
## Inspect and Close | ||
|
||
To list information about the Unikraft instance, use: | ||
|
||
```bash | ||
kraft ps | ||
``` | ||
|
||
```text | ||
NAME KERNEL ARGS CREATED STATUS MEM PLAT | ||
admiring_ndakasi oci://unikraft.org/base:latest /server 1 minute ago running 64MiB 0.0.0.0:8080->8080/tcp qemu/x86_64 | ||
``` | ||
|
||
The instance name is `nostalgic_snowflake`. | ||
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run: | ||
|
||
```bash | ||
kraft rm nostalgic_snowflake | ||
``` | ||
|
||
Note that depending on how you modify this example your instance **may** need more memory to run. | ||
To do so, use the `kraft run`'s `-M` flag, for example: | ||
|
||
```bash | ||
kraft run -p 8080:8080 --plat qemu --arch x86_64 -M 512M | ||
``` | ||
|
||
## `kraft` and `sudo` | ||
|
||
Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior. | ||
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless). | ||
|
||
## Learn More | ||
|
||
- [How to run unikernels locally](https://unikraft.org/docs/cli/running) | ||
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit) |
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,28 @@ | ||
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; | ||
|
||
#[get("/")] | ||
async fn hello() -> impl Responder { | ||
HttpResponse::Ok().body("Hello, World!\r\n") | ||
} | ||
|
||
#[post("/echo")] | ||
async fn echo(req_body: String) -> impl Responder { | ||
HttpResponse::Ok().body(req_body) | ||
} | ||
|
||
async fn manual_hello() -> impl Responder { | ||
HttpResponse::Ok().body("Hey there!") | ||
} | ||
|
||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
HttpServer::new(|| { | ||
App::new() | ||
.service(hello) | ||
.service(echo) | ||
.route("/hey", web::get().to(manual_hello)) | ||
}) | ||
.bind(("0.0.0.0", 8080))? | ||
.run() | ||
.await | ||
} |
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,8 @@ | ||
[package] | ||
name = "hello" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
rocket = "0.5.0" |
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,17 @@ | ||
FROM rust:1.75.0-bookworm AS build | ||
|
||
RUN cargo new --bin app | ||
WORKDIR /app | ||
COPY Cargo.toml ./ | ||
COPY Rocket.toml ./ | ||
COPY src ./src | ||
RUN cargo build --release | ||
|
||
FROM scratch | ||
|
||
COPY ./Rocket.toml /Rocket.toml | ||
COPY --from=build /app/target/release/hello /server | ||
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/ | ||
COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/ | ||
COPY --from=build /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/ | ||
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ |
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,7 @@ | ||
spec: v0.6 | ||
|
||
runtime: base:latest | ||
|
||
rootfs: ./Dockerfile | ||
|
||
cmd: ["/server"] |
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,75 @@ | ||
# Rust/Rocket v0.5 | ||
|
||
This example was derived from [Rocket's "hello" example](https://github.com/rwf2/Rocket/tree/v0.5/examples/hello). | ||
|
||
## Set Up | ||
|
||
To run this example, [install Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli), clone this repository and `cd` into this directory. | ||
|
||
## Run and Use | ||
|
||
Use `kraft` to run the image and start a Unikraft instance: | ||
|
||
```bash | ||
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 | ||
``` | ||
|
||
If the `--plat` argument is left out, it defaults to `qemu`. | ||
If the `--arch` argument is left out, it defaults to your system's CPU architecture. | ||
|
||
Once executed, it will open port `8080` and wait for connections. | ||
To test it, you can use `curl`. | ||
Try one of these available endpoints: | ||
|
||
```bash | ||
Then try visiting one of the available paths: | ||
- https://<NAME>.<METRO>.kraft.cloud/hello/world | ||
- https://<NAME>.<METRO>.kraft.cloud/hello/мир | ||
- https://<NAME>.<METRO>.kraft.cloud/wave/Rocketeer/100 | ||
- https://<NAME>.<METRO>.kraft.cloud/?emoji | ||
- https://<NAME>.<METRO>.kraft.cloud/?name=Rocketeer | ||
- https://<NAME>.<METRO>.kraft.cloud/?lang=ру | ||
- https://<NAME>.<METRO>.kraft.cloud/?lang=ру&emoji | ||
- https://<NAME>.<METRO>.kraft.cloud/?emoji&lang=en | ||
- https://<NAME>.<METRO>.kraft.cloud/?name=Rocketeer&lang=en | ||
- https://<NAME>.<METRO>.kraft.cloud/?emoji&name=Rocketeer | ||
- https://<NAME>.<METRO>.kraft.cloud/?name=Rocketeer&lang=en&emoji | ||
- https://<NAME>.<METRO>.kraft.cloud/?lang=ru&emoji&name=Rocketeer | ||
``` | ||
|
||
## Inspect and Close | ||
|
||
To list information about the Unikraft instance, use: | ||
|
||
```bash | ||
kraft ps | ||
``` | ||
|
||
```text | ||
NAME KERNEL ARGS CREATED STATUS MEM PLAT | ||
admiring_ndakasi oci://unikraft.org/base:latest /server 1 minute ago running 64MiB 0.0.0.0:8080->8080/tcp qemu/x86_64 | ||
``` | ||
|
||
The instance name is `nostalgic_snowflake`. | ||
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run: | ||
|
||
```bash | ||
kraft rm nostalgic_snowflake | ||
``` | ||
|
||
Note that depending on how you modify this example your instance **may** need more memory to run. | ||
To do so, use the `kraft run`'s `-M` flag, for example: | ||
|
||
```bash | ||
kraft run -p 8080:8080 --plat qemu --arch x86_64 -M 512M | ||
``` | ||
|
||
## `kraft` and `sudo` | ||
|
||
Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior. | ||
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless). | ||
|
||
## Learn More | ||
|
||
- [How to run unikernels locally](https://unikraft.org/docs/cli/running) | ||
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit) |
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,3 @@ | ||
[default] | ||
address = "0.0.0.0" | ||
port = 8080 |
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,81 @@ | ||
#[macro_use] extern crate rocket; | ||
|
||
#[cfg(test)] mod tests; | ||
|
||
#[derive(FromFormField)] | ||
enum Lang { | ||
#[field(value = "en")] | ||
English, | ||
#[field(value = "ru")] | ||
#[field(value = "ру")] | ||
Russian | ||
} | ||
|
||
#[derive(FromForm)] | ||
struct Options<'r> { | ||
emoji: bool, | ||
name: Option<&'r str>, | ||
} | ||
|
||
// Try visiting: | ||
// http://127.0.0.1:8000/hello/world | ||
#[get("/world")] | ||
fn world() -> &'static str { | ||
"Hello, World!" | ||
} | ||
|
||
// Try visiting: | ||
// http://127.0.0.1:8000/hello/мир | ||
#[get("/мир")] | ||
fn mir() -> &'static str { | ||
"Привет, мир!" | ||
} | ||
|
||
// Try visiting: | ||
// http://127.0.0.1:8000/wave/Rocketeer/100 | ||
#[get("/<name>/<age>")] | ||
fn wave(name: &str, age: u8) -> String { | ||
format!("👋 Hello, {} year old named {}!", age, name) | ||
} | ||
|
||
// Note: without the `..` in `opt..`, we'd need to pass `opt.emoji`, `opt.name`. | ||
// | ||
// Try visiting: | ||
// http://127.0.0.1:8000/?emoji | ||
// http://127.0.0.1:8000/?name=Rocketeer | ||
// http://127.0.0.1:8000/?lang=ру | ||
// http://127.0.0.1:8000/?lang=ру&emoji | ||
// http://127.0.0.1:8000/?emoji&lang=en | ||
// http://127.0.0.1:8000/?name=Rocketeer&lang=en | ||
// http://127.0.0.1:8000/?emoji&name=Rocketeer | ||
// http://127.0.0.1:8000/?name=Rocketeer&lang=en&emoji | ||
// http://127.0.0.1:8000/?lang=ru&emoji&name=Rocketeer | ||
#[get("/?<lang>&<opt..>")] | ||
fn hello(lang: Option<Lang>, opt: Options<'_>) -> String { | ||
let mut greeting = String::new(); | ||
if opt.emoji { | ||
greeting.push_str("👋 "); | ||
} | ||
|
||
match lang { | ||
Some(Lang::Russian) => greeting.push_str("Привет"), | ||
Some(Lang::English) => greeting.push_str("Hello"), | ||
None => greeting.push_str("Hi"), | ||
} | ||
|
||
if let Some(name) = opt.name { | ||
greeting.push_str(", "); | ||
greeting.push_str(name); | ||
} | ||
|
||
greeting.push('!'); | ||
greeting | ||
} | ||
|
||
#[launch] | ||
fn rocket() -> _ { | ||
rocket::build() | ||
.mount("/", routes![hello]) | ||
.mount("/hello", routes![world, mir]) | ||
.mount("/wave", routes![wave]) | ||
} |
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,8 @@ | ||
[package] | ||
name = "http-tokio" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
||
[dependencies] | ||
tokio = {version = "1", features = ["rt-multi-thread", "net", "time", "macros", "io-util"] } |
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,16 @@ | ||
FROM rust:1.75.0-bookworm AS build | ||
|
||
WORKDIR /src | ||
|
||
COPY ./src /src/src | ||
COPY ./Cargo.toml /src/Cargo.toml | ||
|
||
RUN cargo build | ||
|
||
FROM scratch | ||
|
||
COPY --from=build src/target/debug/http-tokio /server | ||
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6 | ||
COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6 | ||
COPY --from=build /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/libgcc_s.so.1 | ||
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2 |
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,7 @@ | ||
spec: v0.6 | ||
|
||
runtime: base:latest | ||
|
||
rootfs: ./Dockerfile | ||
|
||
cmd: ["/server"] |
Oops, something went wrong.