Skip to content

Commit

Permalink
Merge pull request #104 from slowlydev/feature/dockerize-and-health-c…
Browse files Browse the repository at this point in the history
…heck

Ffeature/dockerize-and-health-check
  • Loading branch information
slowlydev authored May 19, 2024
2 parents 5c5b4fa + ad96c4b commit 006b46d
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 137 deletions.
15 changes: 15 additions & 0 deletions api-backend/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM rust:1-alpine as build

WORKDIR /usr/src/app
COPY . .
COPY ./.sqlx .

RUN apk add --no-cache musl-dev pkgconfig openssl libressl-dev
ENV SQLX_OFFLINE true
RUN cargo build --release

FROM alpine

COPY --from=build /usr/src/app/target/release/api-backend /usr/local/bin/api-backend

CMD ["api-backend"]
5 changes: 5 additions & 0 deletions api-backend/src/endpoints/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use axum::{http::StatusCode, response::IntoResponse};

pub async fn check() -> Result<impl IntoResponse, StatusCode> {
Ok(StatusCode::OK)
}
4 changes: 3 additions & 1 deletion api-backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use tracing::info;
mod env;
mod log;
mod endpoints {
pub mod health;
pub mod schedule;
}

Expand All @@ -15,7 +16,8 @@ async fn main() {

let app = Router::new()
.route("/api/schedule", get(endpoints::schedule::get))
.route("/api/schedule/next", get(endpoints::schedule::get_next));
.route("/api/schedule/next", get(endpoints::schedule::get_next))
.route("/api/health", get(endpoints::health::check));

let default_addr = "0.0.0.0:5000".to_string();
let addr = std::env::var("BACKEND_ADDRESS").unwrap_or(default_addr);
Expand Down
29 changes: 29 additions & 0 deletions dash/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM node:20-alpine AS base

WORKDIR /app
COPY package*.json ./
COPY yarn.lock ./


RUN corepack enable yarn

RUN yarn install --frozen-lockfile

COPY . .

RUN yarn build

FROM node:20-alpine AS runtime

WORKDIR /app

COPY --from=base /app/.next ./.next
COPY --from=base /app/node_modules ./node_modules
COPY --from=base /app/package.json ./package.json
COPY --from=base /app/public ./public

ENV NODE_ENV=production

EXPOSE 3000

CMD ["yarn", "start"]
6 changes: 6 additions & 0 deletions dash/dockeringore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.git
.results
scripts
*dockerfile*
node_modules
.env
55 changes: 55 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
version: "3"

services:
dash:
build: ./dash/
ports:
- 3000:3000
restart: always
profiles:
- full
depends_on:
- api-backend
- live-backend
environment:
- NEXT_PUBLIC_LIVE_SOCKET_URL=http://live-backend:4000
- NEXT_PUBLIC_API_URL=http://api-backend:4001

api-backend:
build: ./api-backend/
ports:
- 4001:4001
restart: always
profiles:
- full
- backends
environment:
- BACKEND_ADDRESS=127.0.0.1:4001
- LOG_LEVEL=info

live-backend:
build: ./live-backend/
ports:
- 4000:4000
restart: always
profiles:
- full
- backends
depends_on:
- timescaledb
environment:
- DATABASE_URL=postgres://postgres:password@timescaledb:5432/postgres
- RUST_LOG="live_backend=trace,debug,info"
- BACKEND_ADDRESS=0.0.0.0:4000
- ORIGIN=http://localhost:3000

timescaledb:
image: timescale/timescaledb:latest-pg16
ports:
- 5432:5432
restart: always
profiles:
- full
- backends
environment:
- POSTGRES_PASSWORD=password
136 changes: 0 additions & 136 deletions live-backend/backend.sh

This file was deleted.

15 changes: 15 additions & 0 deletions live-backend/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM rust:1-alpine as build

WORKDIR /usr/src/app
COPY . .
COPY ./.sqlx .

RUN apk add --no-cache musl-dev pkgconfig openssl libressl-dev
ENV SQLX_OFFLINE true
RUN cargo build --release

FROM alpine

COPY --from=build /usr/src/app/target/release/live-backend /usr/local/bin/live-backend

CMD ["live-backend"]
2 changes: 2 additions & 0 deletions live-backend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::sync::broadcast;
use crate::LiveState;

mod cors;
mod health;
mod history;
pub mod live;
mod recap;
Expand Down Expand Up @@ -35,6 +36,7 @@ pub async fn init(
.route("/api/sse", get(live::sse_handler))
.route("/api/range-buffer", get(recap::range))
.route("/api/history/driver/:id", get(history::get_driver))
.route("/api/health", get(health::check))
.layer(cors)
.with_state(app_state);

Expand Down
19 changes: 19 additions & 0 deletions live-backend/src/server/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::sync::Arc;

use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use serde_json::json;

use super::AppState;

pub async fn check(State(state): State<Arc<AppState>>) -> Result<impl IntoResponse, StatusCode> {
let db_connected = sqlx::query("SELECT 1").fetch_one(&state.db).await.is_ok();

if db_connected {
Ok((StatusCode::OK, Json(json!({ "db": true }))))
} else {
Ok((
StatusCode::SERVICE_UNAVAILABLE,
Json(json!({ "db": false })),
))
}
}

0 comments on commit 006b46d

Please sign in to comment.