From 65619653c645da5cdcfa8588dcb5a887c0abb8c3 Mon Sep 17 00:00:00 2001 From: Ben Grant Date: Tue, 16 May 2023 15:36:53 +1000 Subject: [PATCH] Set up dockerfile, and handle SIGINT signals --- api/.dockerignore | 2 ++ api/Cargo.lock | 4 ++-- api/Dockerfile | 31 +++++++++++++++++++++++++++++++ api/src/main.rs | 7 ++++++- 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 api/.dockerignore create mode 100644 api/Dockerfile diff --git a/api/.dockerignore b/api/.dockerignore new file mode 100644 index 0000000..c5dd462 --- /dev/null +++ b/api/.dockerignore @@ -0,0 +1,2 @@ +target +.env diff --git a/api/Cargo.lock b/api/Cargo.lock index 61a14d3..caf90f8 100644 --- a/api/Cargo.lock +++ b/api/Cargo.lock @@ -1122,7 +1122,7 @@ dependencies = [ [[package]] name = "google-cloud" version = "0.2.1" -source = "git+https://github.com/GRA0007/google-cloud-rs.git#5b2c3d6dcde9e58528c90c0a3016f123104c5fe3" +source = "git+https://github.com/GRA0007/google-cloud-rs.git#4a2db92efd57a896e14d18877458c6ae43418aec" dependencies = [ "chrono", "futures", @@ -1144,7 +1144,7 @@ dependencies = [ [[package]] name = "google-cloud-derive" version = "0.2.1" -source = "git+https://github.com/GRA0007/google-cloud-rs.git#5b2c3d6dcde9e58528c90c0a3016f123104c5fe3" +source = "git+https://github.com/GRA0007/google-cloud-rs.git#4a2db92efd57a896e14d18877458c6ae43418aec" dependencies = [ "darling", "quote", diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..4b611e3 --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,31 @@ +# This dockerfile builds the API and runs it on a minimal container with the Datastore adaptor + +FROM rust:latest as builder + +# Install CA Certs for Hyper +RUN apt-get install -y --no-install-recommends ca-certificates +RUN update-ca-certificates + +WORKDIR /usr/src/app +COPY . . +# Will build and cache the binary and dependent crates in release mode +RUN --mount=type=cache,target=/usr/local/cargo,from=rust:latest,source=/usr/local/cargo \ + --mount=type=cache,target=target \ + cargo build --release --features datastore-adaptor && mv ./target/release/crabfit-api ./api + +# Runtime image +FROM debian:bullseye-slim + +# Run as "app" user +RUN useradd -ms /bin/bash app + +USER app +WORKDIR /app + +# Get compiled binaries from builder's cargo install directory +COPY --from=builder /usr/src/app/api /app/api +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +# Run the app +EXPOSE 3000 +CMD ./api diff --git a/api/src/main.rs b/api/src/main.rs index 5459081..e2f46aa 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -89,7 +89,7 @@ async fn main() { .layer(rate_limit) .layer(TraceLayer::new_for_http()); - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); println!( "🦀 Crab Fit API listening at http://{} in {} mode", @@ -102,6 +102,11 @@ async fn main() { ); Server::bind(&addr) .serve(app.into_make_service_with_connect_info::()) + .with_graceful_shutdown(async { + tokio::signal::ctrl_c() + .await + .expect("Failed to install Ctrl+C handler") + }) .await .unwrap(); }