Skip to content

Commit 7fdf79a

Browse files
committed
feat(examples): Introduce Ruby3.4 httpserver example
Signed-off-by: adriannnv <guzunadrian.vasile@gmail.com>
1 parent 421a061 commit 7fdf79a

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.unikraft/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.unikraft/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM unikraft.org/ruby:3.4
2+
3+
# Simple Ruby HTTP server
4+
COPY ./server.rb /src/server.rb
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spec: v0.6
2+
3+
name: httpserver-ruby3.4
4+
5+
runtime: ruby:3.4
6+
7+
rootfs: ./Dockerfile
8+
9+
cmd: ["/usr/bin/ruby", "/src/server.rb"]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Ruby Web Server
2+
3+
This directory contains a [Ruby](https://www.ruby-lang.org/en/) web server running on Unikraft.
4+
5+
## Set Up
6+
7+
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.
8+
9+
## Run and Use
10+
11+
Use `kraft` to run the image and start a Unikraft instance:
12+
13+
```bash
14+
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 -M 256M .
15+
```
16+
17+
If the `--plat` argument is left out, it defaults to `qemu`.
18+
If the `--arch` argument is left out, it defaults to your system's CPU architecture.
19+
20+
Once executed, it will open port `8080` and wait for connections.
21+
To test it, you can use `curl`:
22+
23+
```bash
24+
curl localhost:8080
25+
```
26+
27+
You should see a "Bye, World!" message.
28+
29+
## Inspect and Close
30+
31+
To list information about the Unikraft instance, use:
32+
33+
```bash
34+
kraft ps
35+
```
36+
37+
```text
38+
NAME KERNEL ARGS CREATED STATUS MEM PORTS PLAT
39+
zealous_ramu oci://unikraft.org/ruby:3.4 /usr/bin/ruby /src/server.rb 14 minutes ago running 244M 0.0.0.0:8080->8080/tcp qemu/x86_64
40+
```
41+
42+
The instance name is `zealous_ramu`.
43+
To close the Unikraft instance, close the `kraft` process (e.g., via `Ctrl+c`) or run:
44+
45+
```bash
46+
kraft rm zealous_ramu
47+
```
48+
49+
Note that depending on how you modify this example your instance **may** need more memory to run.
50+
To do so, use the `kraft run`'s `-M` flag, for example:
51+
52+
```bash
53+
kraft run --rm -p 8080:8080 --plat qemu --arch x86_64 -M 512M .
54+
```
55+
56+
## `kraft` and `sudo`
57+
58+
Mixing invocations of `kraft` and `sudo` can lead to unexpected behavior.
59+
Read more about how to start `kraft` without `sudo` at [https://unikraft.org/sudoless](https://unikraft.org/sudoless).
60+
61+
## Learn More
62+
63+
- [How to run unikernels locally](https://unikraft.org/docs/cli/running)
64+
- [Building `Dockerfile` Images with `BuildKit`](https://unikraft.org/guides/building-dockerfile-images-with-buildkit)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# https://dev.to/leandronsp/web-basics-a-simple-http-server-in-ruby-2jj4
2+
3+
require 'socket'
4+
5+
socket = TCPServer.new(8080)
6+
7+
loop do
8+
client = socket.accept
9+
10+
request = client.gets
11+
12+
response = "HTTP/1.1 200 OK\r\n" \
13+
"Content-type: text/html\r\n" \
14+
"Connection: close\r\n" \
15+
"\r\n" \
16+
"Bye, World!"
17+
client.puts(response)
18+
19+
client.close
20+
end

0 commit comments

Comments
 (0)