-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(native): add tiny-http Rust server (#20)
Reviewed-by: Razvan Deaconescu <[email protected]> Approved-by: Razvan Deaconescu <[email protected]>
- Loading branch information
Showing
6 changed files
with
135 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Makefile.uk |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
[package] | ||
name = "http-rs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tiny_http = "0.12" |
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,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 |
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,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) |
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 @@ | ||
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(()) | ||
} |