Skip to content

Commit

Permalink
[wip] docker setup intent for easy running
Browse files Browse the repository at this point in the history
  • Loading branch information
dalton-oliveira committed Nov 23, 2023
1 parent 587fb6a commit 35b9660
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 112 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
.git
.gitignore
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build

on:
push:
branches:
- "feature/docker"

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: code checkout
uses: actions/checkout@v2

- name: install the gcloud cli
uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ secrets.GOOGLE_PROJECT }}
service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
export_default_credentials: true

- name: build and push the docker image
env:
GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }}
run: |
gcloud auth configure-docker us-central1-docker.pkg.dev
docker build -t us-central1-docker.pkg.dev/$GOOGLE_PROJECT/snake-docker/snake:latest .
docker push us-central1-docker.pkg.dev/$GOOGLE_PROJECT/snake-docker/snake:latest
93 changes: 10 additions & 83 deletions Cargo.lock

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

6 changes: 0 additions & 6 deletions Cross.toml

This file was deleted.

29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM messense/rust-musl-cross:x86_64-musl as chef
RUN cargo install cargo-chef wasm-bindgen-cli
RUN rustup target add wasm32-unknown-unknown
RUN apt update && apt install wget
RUN wget https://dmej8g5cpdyqd.cloudfront.net/downloads/noip-duc_3.0.0-beta.7.tar.gz && tar xf noip-duc_3.0.0-beta.7.tar.gz
# RUN ls noip-duc_3.0.0-beta.7/binaries -lah
# RUN noip-duc_3.0.0-beta.7/binaries/noip-duc_3.0.0-beta.7_x86_64-musl

WORKDIR /snake
FROM chef AS planner
# Copy source code from previous stage
COPY . .
# Generate info for caching dependencies
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /snake/recipe.json recipe.json
# Build & cache dependencies
RUN cargo chef cook --release --recipe-path recipe.json
# Copy source code from previous stage
COPY . .
# Build application
RUN bash build.sh

# Create a new stage with a minimal image
FROM scratch
COPY --from=builder /snake/target/x86_64-unknown-linux-musl/release/snake-web /snake-web
ENTRYPOINT ["/snake-web"]
EXPOSE 80
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3"
services:
api:
image: snake:latest
build:
context: .
dockerfile: Dockerfile
ports:
- "80:80"
environment:
PORT_BIND: 80
2 changes: 1 addition & 1 deletion snake-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition.workspace = true
[dependencies]
snake = { path = "../core" }
tokio = { version = "1.32.0", features = ["macros"] }
salvo = { version = "0.58.0", features = ["websocket", "serve-static", "openssl"] }
salvo = { version = "0.58.0", features = ["websocket", "serve-static"] }
once_cell = "1"
futures-util = { version = "0.3", default-features = false }
rust-embed = "8.0.0"
Expand Down
19 changes: 4 additions & 15 deletions snake-web/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use once_cell::sync::Lazy;
use salvo::conn::openssl::{Keycert, OpensslConfig};

use rust_embed::RustEmbed;
use salvo::prelude::*;
Expand Down Expand Up @@ -27,20 +26,10 @@ async fn main() {

let port = std::env::var("PORT_BIND").unwrap_or_else(|_| PORT_BIND.to_owned());
let bind_address = format!("0.0.0.0:{port}");
if !port.ends_with("80") {
let config = OpensslConfig::new(
Keycert::new()
.with_cert(include_bytes!("../../certs/cert.pem").as_ref())
.with_key(include_bytes!("../../certs/privKey.pem").as_ref()),
);
println!("serving at https://{}", bind_address.clone());
let acceptor = TcpListener::new(bind_address).openssl(config).bind().await;
Server::new(acceptor).serve(router).await;
} else {
println!("serving at http://{}", bind_address.clone());
let acceptor = TcpListener::new(bind_address).bind().await;
Server::new(acceptor).serve(router).await
}

println!("serving at http://{}", bind_address.clone());
let acceptor = TcpListener::new(bind_address).bind().await;
Server::new(acceptor).serve(router).await
}

#[handler]
Expand Down
8 changes: 3 additions & 5 deletions snake-web/src/websocket_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,29 @@ impl WsGame {
let (mut ws_tx, ws_rx) = ws.split();

let (snake_id, direction) = self.add_user(&ws_tx).await;

let game = Arc::clone(&self.game);
let mut rx = self.rx.clone();
tokio::task::spawn(async move {
let notify = Message::binary(to_command(NOTIFY, encode(snake_id).unwrap()));
if let Err(_msg) = ws_tx.send(notify).await {
// ignoring all errors
RwLock::write(&game).await.remove_snake(snake_id);
}
loop {
if let Ok(()) = rx.changed().await {
let ping =
Message::binary(to_command(PING, encode(SystemTime::now()).unwrap()));
if let Err(_msg) = ws_tx.send(ping).await {
break;
// ignoring all errors
}
// let last = Instant::now();
let val = rx.borrow_and_update().to_owned();

let game_data = Message::binary(to_command(GAME_DATA, val));
if let Err(_msg) = ws_tx.send(game_data).await {
break;
// ignoring all errors
}
}
}
RwLock::write(&game).await.remove_snake(snake_id);
});

rx_commands(direction, snake_id, ws_rx, Arc::clone(&self.game));
Expand Down
2 changes: 1 addition & 1 deletion snake-web/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
canvas {
width: auto;
touch-action: pan-x pan-y;
touch-action: none;
}
</style>
<script src="https://hammerjs.github.io/dist/hammer.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion wasm-render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition.workspace = true
[dependencies]
js-sys = "0.3.64"
snake = { path = "../core" }
wasm-bindgen = "0.2.87"
wasm-bindgen = "0.2.88"
bincode = "2.0.0-rc.3"

[lib]
Expand Down

0 comments on commit 35b9660

Please sign in to comment.