-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
73 additions
and
251 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
server.o | ||
server |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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 |
---|---|---|
@@ -1,35 +1,23 @@ | ||
# Use a Rust image based on Alpine | ||
FROM rust:1.81.0-alpine3.20 as builder | ||
FROM alpine:3.20.3 as builder | ||
Check warning on line 1 in Dockerfile GitHub Actions / build-and-push-imageThe 'as' keyword should match the case of the 'from' keyword
|
||
|
||
# Install build dependencies | ||
RUN apk add --no-cache musl-dev | ||
RUN apk add --no-cache zig upx | ||
|
||
# Set the working directory | ||
WORKDIR /usr/src/app | ||
|
||
# Copy the entire project | ||
COPY . . | ||
|
||
# Determine the system architecture and set the appropriate Rust target | ||
RUN arch=$(uname -m) && \ | ||
case "$arch" in \ | ||
"x86_64") echo "x86_64-unknown-linux-musl" > /tmp/target ;; \ | ||
"aarch64") echo "aarch64-unknown-linux-musl" > /tmp/target ;; \ | ||
*) echo "Unsupported architecture: $arch" && exit 1 ;; \ | ||
esac | ||
|
||
# Add the target to rustup and build the project | ||
RUN rustup target add $(cat /tmp/target) && \ | ||
cargo build --release --target $(cat /tmp/target) | ||
|
||
# Copy the binary to a known location | ||
RUN cp target/$(cat /tmp/target)/release/hello-world-http /usr/local/bin/hello-world-http | ||
RUN \ | ||
zig build-exe -lc -static -O ReleaseSmall ./server.zig && \ | ||
upx -9 ./server | ||
|
||
# Start a new stage for a minimal runtime container | ||
FROM scratch | ||
|
||
# Copy the built executable from the builder stage | ||
COPY --from=builder /usr/local/bin/hello-world-http /hello-world-http | ||
COPY --from=builder /usr/src/app/server /server | ||
|
||
# Set the startup command | ||
CMD ["/hello-world-http"] | ||
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,3 @@ | ||
|
||
all: | ||
zig build-exe -lc -static -O ReleaseSmall ./server.zig |
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,38 @@ | ||
const std = @import("std"); | ||
const net = std.net; | ||
const print = std.debug.print; | ||
|
||
pub fn main() !void { | ||
const allocator = std.heap.c_allocator; | ||
|
||
// Get the HOST environment variable | ||
const host = try std.process.getEnvVarOwned(allocator, "HOST"); | ||
defer allocator.free(host); | ||
|
||
// Get the PORT environment variable | ||
const port_str = try std.process.getEnvVarOwned(allocator, "PORT"); | ||
defer allocator.free(port_str); | ||
|
||
// Parse the port string into a u16 integer | ||
const port_number = try std.fmt.parseInt(u16, port_str, 10); | ||
|
||
// Parse the IP address and port | ||
const addr = try net.Address.parseIp(host, port_number); | ||
|
||
var server = try addr.listen(.{ .reuse_port = true }); | ||
defer server.deinit(); | ||
|
||
print("Server Listening on {s}:{}\n", .{ host, port_number }); | ||
|
||
const response = "HTTP/1.1 200 OK\r\n" ++ "Content-Length: 11\r\n" ++ "Content-Type: text/plain\r\n" ++ "Connection: close\r\n" ++ "\r\n" ++ "hello world"; | ||
|
||
while (server.accept()) |client| { | ||
defer client.stream.close(); | ||
|
||
print("Connection received from {}\n", .{client.address}); | ||
|
||
_ = try client.stream.write(response); | ||
} else |err| { | ||
print("Error accepting connection: {}\n", .{err}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.