From c1b9273e0cb3f20e5173202bf9fa588a69e7d6f2 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Thu, 13 Jun 2024 14:42:42 +0200 Subject: [PATCH] Deploy with docker-compose (#1) Deploy using docker-compose and GitHub Actions. Created a simple get endpoint to check if server is running on / Updated address from 127.0.0.1 to 0.0.0.0 --- .dockerignore | 6 ++++++ .github/workflows/build.yaml | 4 +--- .github/workflows/deploy.yml | 16 ++++++++++++++++ Dockerfile | 29 +++++++++++++++++++++++++++++ docker-compose.yaml | 9 +++++++++ src/main.rs | 7 ++++--- src/routing/index.rs | 11 +++++++++++ src/routing/mod.rs | 3 ++- 8 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/deploy.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yaml create mode 100644 src/routing/index.rs diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..8ef0081 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.github +spec +http +target +README.md +.gitignore diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 528c26c..bd7d7a5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,10 +1,8 @@ -name: Rust +name: Build & test on: push: - branches: [ master ] pull_request: - branches: [ master ] env: CARGO_TERM_COLOR: always diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..22326cf --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,16 @@ +name: Deploy image to Docker Hub and pull it + +on: + push: + branches: [ master ] + +jobs: + deploy: + name: Build and run + runs-on: self-hosted + + steps: + - name: Check out the repo + uses: actions/checkout@v4 + - name: Run + run: docker-compose up -d --build diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0895a77 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Creates a new cargo project, copies the Cargo.toml and Cargo.lock files to the new project, +# builds the project, and then copies the built binary to a new image. + +FROM rust:1.78 as build +LABEL authors="Martin Berg Alstad" + +RUN USER=root cargo new --bin simplify_truths +WORKDIR /simplify_truths + +COPY ./Cargo.lock ./Cargo.lock +COPY ./Cargo.toml ./Cargo.toml + +# this build step will cache your dependencies +RUN cargo build --release +RUN rm src/*.rs + +COPY ./src ./src + +RUN rm ./target/release/deps/simplify_truths* +RUN cargo build --release + +FROM debian + +# copy the build artifact from the build stage +COPY --from=build /simplify_truths/target/release/simplify_truths . + +EXPOSE 8000 + +CMD ["./simplify_truths"] diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..9c30d18 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,9 @@ +version: '3' +services: + server: + restart: always + build: + context: . + dockerfile: Dockerfile + ports: + - "8000:8000" diff --git a/src/main.rs b/src/main.rs index 00f3525..da23707 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::net::SocketAddr; use tokio::net::TcpListener; -use crate::routing::{simplify, table}; +use crate::routing::{index, simplify, table}; mod expressions; mod parsing; @@ -13,7 +13,7 @@ mod utils; #[tokio::main] async fn main() { - let addr = SocketAddr::from(([127, 0, 0, 1], config::PORT)); + let addr = SocketAddr::from(([0, 0, 0, 0], config::PORT)); let listener = TcpListener::bind(&addr) .await .unwrap(); @@ -21,7 +21,8 @@ async fn main() { println!("Listening on: {}", listener.local_addr().unwrap()); let routes = simplify::router() - .merge(table::router()); + .merge(table::router()) + .merge(index::router()); axum::serve(listener, routes).await.unwrap(); } diff --git a/src/routing/index.rs b/src/routing/index.rs new file mode 100644 index 0000000..16cc8e6 --- /dev/null +++ b/src/routing/index.rs @@ -0,0 +1,11 @@ +use axum::Router; +use axum::routing::get; + +pub fn router() -> Router { + Router::new() + .route("/", get(index)) +} + +async fn index() -> &'static str { + "Welcome to the Simplify Truths API!\n" +} diff --git a/src/routing/mod.rs b/src/routing/mod.rs index 8f9a770..0a968e6 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -1,2 +1,3 @@ pub(crate) mod simplify; -pub(crate) mod table; \ No newline at end of file +pub(crate) mod table; +pub(crate) mod index; \ No newline at end of file