Skip to content

Commit

Permalink
feat(native): add tiny-http Rust server (#20)
Browse files Browse the repository at this point in the history
Reviewed-by: Razvan Deaconescu <[email protected]>
Approved-by: Razvan Deaconescu <[email protected]>
  • Loading branch information
razvand authored Mar 14, 2024
2 parents 39ddbcf + 0ee1438 commit 62751d5
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions native/http-rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Makefile.uk
46 changes: 46 additions & 0 deletions native/http-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions native/http-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "http-rs"
version = "0.1.0"
edition = "2021"

[dependencies]
tiny_http = "0.12"
21 changes: 21 additions & 0 deletions native/http-rs/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
spec: v0.6

name: http-rs

unikraft: stable

libraries:
musl: stable
lwip:
version: stable
kconfig:
CONFIG_LWIP_UKNETDEV: 'y'
CONFIG_LWIP_TCP: 'y'
CONFIG_LWIP_THREADS: 'y'
CONFIG_LWIP_SOCKET: 'y'
CONFIG_LWIP_AUTOIFACE: 'y'
CONFIG_LWIP_IPV4: 'y'
CONFIG_LWIP_DHCP: 'y'

targets:
- qemu/x86_64
43 changes: 43 additions & 0 deletions native/http-rs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Rust Tiny-HTTP Server

This directory contains a simple HTTP server written in Rust using [tiny-http](https://github.com/tiny-http/tiny-http) running with Unikraft.
The image opens up a simple HTTP server and provides a simple HTML response for each request.

To run this example, install [Unikraft's companion command-line toolchain `kraft`](https://unikraft.org/docs/cli) and [the `nightly` Rust toolchain channel through Rustup](https://www.rust-lang.org/tools/install).

## Build

To build the image, clone this repository and `cd` into this directory.
You can then build the project with:

```bash
KRAFTKIT_TARGET=http-rs cargo +nightly build -Z build-std=std,panic_abort --target x86_64-unikraft-linux-musl
```

In the above command, `KRAFTKIT_TARGET=http-rs` makes the name of the application known to `kraftld`.
Rust for Unikraft currently only supports the `qemu/x86_64` target.
If a `Kraftfile` contains more targets, the correct one can be selected through the `KRAFTKIT_PLAT` and `KRAFTKIT_ARCH` environment variables.

## Run

In order to run the locally built image, use `kraft`:

```bash
kraft run --rm --plat qemu --arch x86_64 -p 8080:8080 .
```

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.

## Learn more

- [How to build unikernels](https://unikraft.org/docs/cli/building)
- [How to run unikernels locally](https://unikraft.org/docs/cli/running)
- [The `Kraftfile` specification](https://unikraft.org/docs/cli/reference/kraftfile/latest)
- [`*-unikraft-linux-musl`—The `rustc` book](https://doc.rust-lang.org/rustc/platform-support/unikraft-linux-musl.html)
16 changes: 16 additions & 0 deletions native/http-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use tiny_http::{Server, Response};

fn main() -> std::io::Result<()> {
let server = Server::http("0.0.0.0:8080").unwrap();
let port = server.server_addr().to_ip().unwrap().port();
println!("Now listening on port {port}");

for request in server.incoming_requests() {
println!("Received {request:?}");

let response = Response::from_string("Hello, world!\r\n");
request.respond(response)?;
}

Ok(())
}

0 comments on commit 62751d5

Please sign in to comment.