Skip to content

Commit

Permalink
Add health check service with HTTP port configuration and update depe…
Browse files Browse the repository at this point in the history
…ndencies (#6)

* Add health check service with HTTP port configuration and update dependencies

* fix ci
  • Loading branch information
tomkapa authored Jan 28, 2025
1 parent 7238b3f commit 5ca28d2
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 30 deletions.
25 changes: 6 additions & 19 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ jobs:

test:
name: Test
needs: [ fmt, clippy ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -69,6 +68,7 @@ jobs:
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -93,29 +93,16 @@ jobs:
type=ref,event=branch
type=ref,event=tag
type=semver,pattern={{version}}
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx
restore-keys: |
${{ runner.os }}-buildx
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
provenance: true
sbom: true
# platforms: linux/amd64
# provenance: true
# sbom: true
push: ${{ github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

- name: Update Cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
cache-from: type=gha
cache-to: type=gha,mode=max
104 changes: 99 additions & 5 deletions Cargo.lock

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

12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ name = "tomka"
[dependencies]
anyhow = "1.0.94"
tracing = "0.1.41"
tracing-subscriber = {version = "0.3.19", features = ["env-filter", "registry"]}
tracing-subscriber = { version = "0.3.19", features = [
"env-filter",
"registry",
] }
tracing-log = "0.2.0"
tracing-bunyan-formatter = "0.3.10"
thiserror = "2.0.11"
tonic = {version = "0.12.3"}
tonic = { version = "0.12.3" }
prost = "0.13.4"
tokio = { version = "1.43.0", features = ["rt-multi-thread", "macros"]}
tokio = { version = "1.43.0", features = ["rt-multi-thread", "macros"] }
crossbeam = "0.8.4"
futures = "0.3.31"
async-stream = "0.3.6"
serde = "1.0.217"
serde-aux = "4.5.0"
config = "0.15.6"
axum = "0.8.1"

[build-dependencies]
tonic-build = { version = "0.12.3"}
tonic-build = { version = "0.12.3" }
3 changes: 2 additions & 1 deletion configuration/base.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
application:
host: 0.0.0.0
grpc_port: 8080
grpc_port: 8080
http_port: 8081
3 changes: 3 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub struct ApplicationSettings {
pub host: String,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub grpc_port: u16,
/// The port for the HTTP server. It's used for health checks.
#[serde(deserialize_with = "deserialize_number_from_string")]
pub http_port: u16,
}

/// The possible runtime environment for our application.
Expand Down
42 changes: 42 additions & 0 deletions src/health_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::configuration::Settings;
use axum::{routing::get, Router};
use tokio::net::TcpListener;

async fn health_check_handler() -> &'static str {
"healthy"
}

pub struct HealthService {
port: u16,
server: axum::serve::Serve<TcpListener, Router, Router>,
}

impl HealthService {
pub async fn build(settings: Settings) -> anyhow::Result<Self> {
let address = format!(
"{}:{}",
settings.application.host, settings.application.http_port
);
let listener = TcpListener::bind(address).await?;
let addr = listener.local_addr()?;

let app = Router::new().route("/", get(health_check_handler));

// Build the server
let server = axum::serve(listener, app);
Ok(HealthService {
port: addr.port(),
server,
})
}

pub fn port(&self) -> u16 {
self.port
}

pub async fn run(self) -> anyhow::Result<()> {
tracing::info!("Health check server running on port {}", self.port);
self.server.await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod configuration;
pub mod health_check;
pub mod protobuf;
pub mod queue;
pub mod service;
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use tomka::configuration::Settings;
use tomka::health_check::HealthService;
use tomka::startup::Application;
use tomka::telemetry::{get_subscriber, init_subscriber};

Expand All @@ -8,10 +9,14 @@ async fn main() -> anyhow::Result<()> {
init_subscriber(subscriber);

let settings = Settings::load().expect("Failed to load configuration");
let application = Application::build(settings).await?;
let application = Application::build(settings.clone()).await?;
let application_task = tokio::spawn(application.run());

let health_check = HealthService::build(settings).await?;
let health_check_task = tokio::spawn(health_check.run());
tokio::select! {
_ = application_task => {},
_ = health_check_task => {},
}

Ok(())
Expand Down

0 comments on commit 5ca28d2

Please sign in to comment.