Skip to content

Commit 72142e9

Browse files
committed
Add docker support
1 parent 0f8c839 commit 72142e9

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Cargo.lock
1515

1616
log.dot
1717
output.png
18+
19+
docker-compose.yaml

Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM rust:latest AS build
2+
3+
WORKDIR /build
4+
5+
RUN USER=root cargo init --bin . --name bot
6+
7+
COPY Cargo.toml Cargo.lock .
8+
9+
RUN cargo build --release
10+
RUN rm -rf src
11+
12+
COPY src ./src
13+
14+
RUN ls -lha
15+
16+
RUN cargo build --release
17+
18+
19+
20+
FROM debian:bookworm-slim
21+
22+
WORKDIR /app
23+
24+
# Copy the binary from the build image to the new image
25+
COPY --from=build /build/target/release/bot-tic-tac-toe-rust /app/bot
26+
27+
COPY entrypoint.sh /app/entrypoint.sh
28+
RUN chmod +x /app/entrypoint.sh
29+
30+
EXPOSE 8080
31+
32+
# Run the binary
33+
CMD ["/app/entrypoint.sh"]

entrypoint.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
if [[ -z "$SECRET" ]]; then echo "SECRET is not set"; exit 1; fi
4+
5+
echo "Starting server on port $PORT"
6+
7+
/app/bot -p 8080 -s ${SECRET}
8+

src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ pub fn handle_status(_body: String, config: &AppData) -> String {
8686
.minify();
8787
}
8888

89-
pub fn handle_finish(_body: String, _config: &AppData) -> String {
89+
pub fn handle_finish(_body: String, config: &AppData) -> String {
9090
info!("Handling finish request");
9191

92-
return r#"{
92+
return format!(r#"{{
9393
"status": "ok",
94+
"game": "tic-tac-toe",
95+
"version": "{}",
96+
"secret": "{}",
9497
"message": "Game finished!"
95-
}"#
96-
.to_string()
97-
.minify();
98+
}}"#, config.version, config.secret).minify();
9899
}
99100

100101
pub fn handle_error(body: String, _config: &AppData) -> String {
@@ -407,7 +408,7 @@ async fn main() -> std::io::Result<()> {
407408
.service(hello)
408409
.service(handle_request)
409410
})
410-
.bind(("127.0.0.1", port))?
411+
.bind(("0.0.0.0", port))?
411412
.run()
412413
.await
413414
}

0 commit comments

Comments
 (0)