Skip to content

Commit

Permalink
Added docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Feb 17, 2024
1 parent b7365e1 commit 8536669
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion apps/legacy_web/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ chrono = { version = "0.4.22", features = ["serde"] }
directories = "5.0.1"
serde = { version = "1.0.195", features = ["derive"] }
axum-macros = "0.4.1"
anyhow = "1.0.79"
anyhow = "1.0.79"
color-eyre.workspace = true
28 changes: 28 additions & 0 deletions apps/legacy_web/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM debian:latest

# Path to the executable to run
ARG BIN_PATH

# Install OpenSSL - required for Axum applications
RUN apt-get update && apt-get install -y openssl ca-certificates && rm -rf /var/lib/apt/lists/*

# Create a new user and group to run the application
RUN groupadd -r app && useradd -r -g app app

RUN mkdir -p /opt/app && chown app:app /opt/app
# Set the working directory
WORKDIR /opt/app


# Switch from root to the "app" user
USER app
COPY --chown=app:app ${BIN_PATH:?missing} /opt/app/backend

# Expose the port the server listens on
EXPOSE 8080

ENV XDG_DATA_HOME="/opt/app/"
ENV RUST_BACKTRACE=1

# Run the application
CMD "/opt/app/backend"
24 changes: 24 additions & 0 deletions apps/legacy_web/backend/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes

TARGET="x86_64-unknown-linux-gnu"
BIN_NAME="backend"
REPO_ROOT="$(readlink -f "$(dirname "${0}")/../../..")"
BACKEND_DIR="$(readlink -f "$(dirname "${0}")")"
RELATIVE_RELEASE_DIR="target/${TARGET}/release"
echo "REPO_ROOT=${REPO_ROOT}"
echo "BACKEND_DIR=${BACKEND_DIR}"

cargo zigbuild --release --target "${TARGET}" --bin "${BIN_NAME}"

docker buildx build "${REPO_ROOT}/${RELATIVE_RELEASE_DIR}" \
--platform=linux/amd64 \
-f "${BACKEND_DIR}/Dockerfile" \
-t "${BIN_NAME}" \
--build-arg BIN_PATH="${BIN_NAME}"

echo "built docker image. You can run via:"
echo "\"docker run -it -p HOST_PORT:8080 ${BIN_NAME}\""
4 changes: 3 additions & 1 deletion apps/legacy_web/backend/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ impl Database {
if fs::metadata(&path).is_err() {
let mut dir_path = path.clone();
dir_path.pop();
fs::create_dir_all(&dir_path).unwrap();
fs::create_dir_all(&dir_path).unwrap_or_else(|_| {
panic!("failed to create database dir at {path:?}")
});
File::create(&path).unwrap();
std::fs::write(
path.as_path(),
Expand Down
2 changes: 1 addition & 1 deletion apps/legacy_web/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn main() {
.layer(DefaultBodyLimit::disable())
.layer(CorsLayer::permissive());

let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 3000));
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 8080));
tracing::debug!("starting server on port: {}", addr.port());
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
channel = "beta-2023-12-02" # See workspace Cargo.toml
components = ["rust-src"]
profile = "default"
targets = ["x86_64-pc-windows-msvc", "aarch64-linux-android"]
targets = ["x86_64-pc-windows-msvc", "aarch64-linux-android", "x86_64-unknown-linux-gnu"]

0 comments on commit 8536669

Please sign in to comment.