Skip to content

Commit

Permalink
feat(docker): Basic docker config, needs some extra work
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Sep 1, 2023
1 parent 004f835 commit 127f625
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 68 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.env
.dockerignore
spec.yaml
target/
deploy/
tests/
Dockerfile
scripts/
migrations/
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL="postgres://postgres:password@localhost:5069/newsletter"
SQLX_OFFLINE=true
93 changes: 32 additions & 61 deletions .github/workflows/general.yml
Original file line number Diff line number Diff line change
@@ -1,89 +1,61 @@
name: Rust

on: [push, pull_request]
on:
push:
branches:
pull_request:

env:
CARGO_TERM_COLOR: always
CARGO_TERM_COLOR: always
SQLX_VERSION: 0.6.2
SQLX_FEATURES: "rustls,postgres"

# A workflow run is made up of one or more jobs, which run in parallel by default
# Each job runs in a runner environment specified by runs-on
jobs:
# Unique identifier of our job (`job_id`)
test:
# Sets the name `Test` for the job, which is displayed in the GitHub UI
name: Test
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Service containers to run with the `test` container job
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres:14
# Environment variables scoped only for the `postgres` element
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
# When you map ports using the ports keyword, GitHub uses the --publish command to publish the container’s ports to the Docker host
# Opens tcp port 5432 on the host and service container
ports:
- 5432:5432
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
# The uses keyword specifies that this step will run v3 of the actions/checkout action.
# This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools).
# You should use the checkout action any time your workflow will run against the repository's code.
uses: actions/checkout@v3

# This GitHub Action installs a Rust toolchain using rustup. It is designed for one-line concise usage and good defaults.
- name: Install the Rust toolchain
uses: dtolnay/rust-toolchain@stable

# A GitHub Action that implements smart caching for rust/cargo projects with sensible defaults.
- name: Rust Cache Action
uses: Swatinem/rust-cache@v2
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
with:
# An additional cache key that is added alongside the automatic `job`-based cache key and can be used to further differentiate jobs. default: empty
key: sqlx-${{ env.SQLX_VERSION }}

- name: Install sqlx-cli
run:
cargo install sqlx-cli
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no-default-features
--locked
# The --locked flag can be used to force Cargo to use the packaged Cargo.lock file if it is available.
# This may be useful for ensuring reproducible builds, to use the exact same set of dependencies that were available when the package was published.
# It may also be useful if a newer version of a dependency is published that no longer builds on your system, or has other problems

- name: Install postgresql-client
run: sudo apt-get update && sudo apt-get install postgresql-client -y

run: cargo install sqlx-cli
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no-default-features
--locked
- name: Migrate database
run: SKIP_DOCKER=true ./scripts/init_db.sh

run: |
sudo apt-get install libpq-dev -y
SKIP_DOCKER=true ./scripts/init_db.sh
- name: Check sqlx-data.json is up-to-date
run: |
SQLX_OFFLINE=true && cargo sqlx prepare &&cargo sqlx prepare --check -- --bin zero2prod
- name: Run tests
run: cargo test

# `fmt` container job
fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
# Specific to dtolnay/rust-toolchain: Comma-separated string of additional components to install
components: rustfmt
- name: Enforce formatting
run: cargo fmt --check

# `clippy` container job
clippy:
name: Clippy
runs-on: ubuntu-latest
Expand All @@ -105,20 +77,18 @@ jobs:
with:
key: sqlx-${{ env.SQLX_VERSION }}
- name: Install sqlx-cli
run:
cargo install sqlx-cli
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no-default-features
--locked
- name: Install postgresql-client
run: sudo apt-get update && sudo apt-get install postgresql-client -y
run: cargo install sqlx-cli
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no-default-features
--locked
- name: Migrate database
run: SKIP_DOCKER=true ./scripts/init_db.sh
run: |
sudo apt-get install libpq-dev -y
SKIP_DOCKER=true ./scripts/init_db.sh
- name: Linting
run: cargo clippy -- -D warnings

# `coverage` container job
coverage:
name: Code coverage
runs-on: ubuntu-latest
Expand All @@ -135,19 +105,20 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- name: Install postgresql-client
- name: Install libpq
run: sudo apt-get update && sudo apt-get install postgresql-client -y
- uses: Swatinem/rust-cache@v2
with:
key: sqlx-${{ env.SQLX_VERSION }}
- name: Install tarpaulin
run: cargo install cargo-tarpaulin
- name: Install sqlx-cli
run:
cargo install sqlx-cli
run: cargo install sqlx-cli
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no-default-features
--locked
- name: Migrate database
run: SKIP_DOCKER=true ./scripts/init_db.sh
- name: Generate code coverage
run: cargo install cargo-tarpaulin && cargo tarpaulin --verbose --workspace
run: cargo tarpaulin --verbose --workspace
10 changes: 8 additions & 2 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = "1.0.115"
config = { version = "0.13", default-features = false, features = ["yaml"] }
sqlx = { version = "0.6", default-features = false, features = ["runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
uuid = { version = "1", features = ["v4"] }
sqlx = { version = "0.6", default-features = false, features = ["runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] }
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
Expand All @@ -30,3 +30,5 @@ tracing-actix-web = "0.7"

[dev-dependencies]
reqwest = { version = "0.11", features = ["json"] }


8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM rust:1.71.0
WORKDIR /app
RUN apt update && apt install lld clang -y
COPY . .
ENV SQLX_OFFLINE true
RUN cargo build --release

ENTRYPOINT ["./target/release/zero2prod"]
18 changes: 18 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"db": "PostgreSQL",
"bcfcfebc6f5e8ffbf97d97c5a209be78b46d703924482cf8b43842705fcb7714": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Uuid",
"Text",
"Text",
"Timestamptz"
]
}
},
"query": "\n INSERT INTO subscriptions (id, email, name, subscribed_at)\n VALUES ($1, $2, $3, $4)\n "
}
}
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@ use zero2prod::{configurations::get_configuration, startup::run, telemetry::get_
async fn main() -> Result<(), std::io::Error> {
// Logger setup
LogTracer::init().expect("Failed to set logger");

// Info or above will be logged if the RUST_LOG environment variable is not set
let subscriber = get_subscriber("zero2prod".into(), "info".into(), std::io::stdout);
set_global_default(subscriber).expect("Failed to set subscriber");

// We want to panic if we cannot read the configuration
let configuration = get_configuration().expect("Failed to read configurations");
let connection = PgPool::connect(
let connection = PgPool::connect_lazy(
configuration
.database
.get_connnection_string()
.expose_secret(),
)
.await
.expect("Failed to connect to Postgresf");
.expect("Failed to connect to Postgres");

// Bind the TCP listener socket address with the configuration port
let address = format!("127.0.0.1:{}", configuration.application_port);
let address = format!(
"{}:{}",
configuration.application.host, configuration.application.port
);

let listener = TcpListener::bind(address).expect("Failed to bind random port");
run(listener, connection)?.await
}

0 comments on commit 127f625

Please sign in to comment.