Skip to content

Commit

Permalink
docker: set health check and time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Iov committed Jan 29, 2024
1 parent cfadb19 commit 5c9f8d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
17 changes: 16 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@ RUN source $HOME/.cargo/env && cargo auditable build --release

FROM docker.io/alpine:3.14
RUN apk add --no-cache \
libgcc
libgcc \
tzdata \
#IMPORTANT: in order for the Docker container to be able to perform the check, the image must provide `curl`.
# If changing or updating the base image's version, please ensure that `curl` is available!
curl && \
# ensure the UTC timezone is set
ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime

WORKDIR /opt/barad-dur
COPY --from=builder /app/target/release/barad-dur /usr/local/bin/barad-dur
COPY --from=builder /app/migrations /opt/barad-dur/migrations
CMD ["/usr/local/bin/barad-dur"]

ENV TZ=Etc/UTC
ARG service_port_number=8080
EXPOSE ${service_port_number}/tcp
ENV SERVICE_PORT=${service_port_number}
HEALTHCHECK --interval=3s --timeout=3s --retries=2 --start-period=5s \
CMD curl -fSs http://localhost:$SERVICE_PORT/health || exit 1

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ For configuration options look into `config.sample.yaml`.

Barad-dûr has import scripts for panopticon, which you can find in `misc/panopticon-import`, together with usage instructions.

## Healthcheck for Docker container
The service API implements the `/health` and `/ready` checks for the Docker containers.

```bash
curl -fSs http://localhost:8100/health || exit 1
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
16 changes: 14 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::process;

use anyhow::{Context, Result};
use axum::headers::{Header, HeaderName, UserAgent};
use axum::routing::put;
use axum::{extract, Extension, Router, Server, TypedHeader};
use axum::{routing::get, routing::put};
use axum::{extract, response::IntoResponse, Extension, Router, Server, TypedHeader};
use http::{HeaderValue, StatusCode};
use tokio::sync::mpsc;

Expand All @@ -14,6 +14,7 @@ pub async fn run_server(settings: ServerSettings, tx: mpsc::Sender<model::Report
Server::bind(&settings.host.parse::<SocketAddr>()?)
.serve(
Router::new()
.route("/health", get(health_check))
.route("/report-usage-stats/push", put(save_report))
.layer(Extension(tx))
.into_make_service_with_connect_info::<SocketAddr>(),
Expand All @@ -23,6 +24,17 @@ pub async fn run_server(settings: ServerSettings, tx: mpsc::Sender<model::Report
Ok(())
}

/// Healthcheck
// Returns 200 OK or 503 Service unhealthy for health checking
async fn health_check() -> impl IntoResponse {
// TODO consider performing some more sofisticated check here ;)

Check warning on line 30 in src/server.rs

View workflow job for this annotation

GitHub Actions / Formatting, lints, syntax, and typos

"sofisticated" should be "sophisticated".
let healthy = true;
match healthy{
true => (StatusCode::OK, "OK"),
false => (StatusCode::SERVICE_UNAVAILABLE, "Service unhealthy"),
};
}

pub struct XForwardedFor(IpAddr);

impl Header for XForwardedFor {
Expand Down

0 comments on commit 5c9f8d3

Please # to comment.