Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to postgresql #477

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/secrets.dev.yaml
**/values.dev.yaml
/bin
/target
LICENSE
README.md
42 changes: 23 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,44 @@ version = "0.1.0"
authors = ["ndelvalle <[email protected]>"]
edition = "2021"

[[bin]]
name ="rustapi"
path ="./src/main.rs"

[dependencies]
async-trait = "0.1.81"
axum = { version = "0.7.5" }
axum-extra = { version = "0.9.3", features = ["typed-header"] }
bcrypt = "0.15.1"
# Investigate if wither::bson can be used instead and activate this feature.
bson = { version = "2.10.0", features = ["serde_with", "chrono-0_4"] }
bytes = "1.7.2"
chrono = "0.4.38"
config = "0.14.0"
futures = "0.3.30"
jsonwebtoken = "9.3.0"
mime = "0.3.17"
once_cell = "1.20.0"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
serde_derive = "1.0.152"
# Wait for wither to relase a new version.
# https://github.com/thedodd/wither/pull/89#issuecomment-1023644443
wither = { git = "https://github.com/thedodd/wither", rev = "52fd503" }
futures = "0.3.30"
serde_json = "1.0.120"
thiserror = "1.0.63"
axum = { version = "0.7.5" }
tokio = { version = "1.39.1", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tower-http = { version = "0.6", features = [
"trace",
"compression-br",
"propagate-header",
"sensitive-headers",
"cors",
] }
chrono = "0.4.38"
async-trait = "0.1.81"
# Investigate if wither::bson can be used instead and activate this feature.
bson = { version = "2.10.0", features = ["serde_with", "chrono-0_4"] }
jsonwebtoken = "9.3.0"
once_cell = "1.20.0"
bcrypt = "0.15.1"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
validator = { version = "0.18.1", features = ["derive"] }
mime = "0.3.17"
bytes = "1.7.2"
axum-extra = { version = "0.9.3", features = ["typed-header"] }
# Wait for wither to relase a new version.
# https://github.com/thedodd/wither/pull/89#issuecomment-1023644443
wither = { git = "https://github.com/thedodd/wither", rev = "52fd503" }

[dev-dependencies]
assert-json-diff = "2.0.2"
reqwest = { version = "0.12.4", features = ["json"] }
pretty_assertions = "1.4.1"
reqwest = { version = "0.12.4", features = ["json"] }
71 changes: 71 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG RUST_VERSION=1.79.0
ARG APP_NAME=rustapi

################################################################################
# Create a stage for building the application.

FROM rust:${RUST_VERSION}-alpine AS build
ARG APP_NAME
WORKDIR /app

# Install host build dependencies.
RUN apk add --no-cache clang lld musl-dev git

# Build the application.
# Leverage a cache mount to /usr/local/cargo/registry/
# for downloaded dependencies, a cache mount to /usr/local/cargo/git/db
# for git repository dependencies, and a cache mount to /app/target/ for
# compiled dependencies which will speed up subsequent builds.
# Leverage a bind mount to the src directory to avoid having to copy the
# source code into the container. Once built, copy the executable to an
# output directory before the cache mounted /app/target is unmounted.
RUN --mount=type=bind,source=src,target=src \
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/git/db \
--mount=type=cache,target=/usr/local/cargo/registry/ \
cargo build --locked --release && \
cp ./target/release/$APP_NAME /bin/server

################################################################################
# Create a new stage for running the application that contains the minimal
# runtime dependencies for the application. This often uses a different base
# image from the build stage where the necessary files are copied from the build
# stage.
#
# The example below uses the alpine image as the foundation for running the app.
# By specifying the "3.18" tag, it will use version 3.18 of alpine. If
# reproducability is important, consider using a digest
# (e.g., alpine@sha256:664888ac9cfd28068e062c991ebcff4b4c7307dc8dd4df9e728bedde5c449d91).
FROM alpine:3.18 AS final

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser

# Copy the executable from the "build" stage.
COPY --from=build /bin/server /bin/

# Expose the port that the application listens on.
EXPOSE 3000

# What the container should run when it is started.
CMD ["/bin/server"]
38 changes: 38 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3"

services:
app:
build:
context: .
target: final
ports:
- 3000:3000
environment:
- PORT=3000
- DB_CONNECTION_URL = postgres://rustapi:RAa6dHNblVoJboG6fyFlbCvlYe7@database

depends_on:
database:
condition: service_started
# the DB
database:
image: postgres:15-alpine
restart: always
shm_size: 100mb
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=rustapi.db
- POSTGRES_USER=rustapi
- POSTGRES_PASSWORD=RAa6dHNblVoJboG6fyFlbCvlYe7
expose:
- 5432
healthcheck:
test: [ "CMD", "pg_isready -d rustapi.db rustapi"]
interval: 10s
timeout: 5s
retries: 5
volumes:
db-data:


4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::SocketAddr;
use std::net::{Ipv4Addr, SocketAddrV4};
use tokio::net::TcpListener;
use tracing::info;

Expand All @@ -25,7 +25,7 @@ use settings::SETTINGS;
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let port = SETTINGS.server.port;
let address = SocketAddr::from(([127, 0, 0, 1], port));
let address = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port);

let app = app::create_app().await;

Expand Down
Loading