Skip to content
Merged
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
59 changes: 59 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async-graphql-axum = "7.0.13"
axum = "0.7.9"
axum-extra = { version = "0.9.3", features = ["typed-header"] }
chrono = "0.4.39"
clap = { version = "4.5.23", features = ["cargo", "derive", "env"] }
clap = { version = "4.5.23", features = ["cargo", "derive", "env", "string"] }
futures = "0.3.31"
opentelemetry = "0.27.1"
opentelemetry-otlp = "0.27.0"
Expand All @@ -38,3 +38,6 @@ httpmock = { version = "0.7.0", default-features = false }
rstest = "0.23.0"
serde_json = "1.0.133"
tempfile = "3.14.0"

[build-dependencies]
built = { version = "0.7.5", features = ["git2", "chrono"] }
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ RUN rustup target add x86_64-unknown-linux-musl && \
apt-get install -y musl-tools musl-dev && \
update-ca-certificates

WORKDIR /build

COPY ./Cargo.toml ./Cargo.toml
COPY ./Cargo.lock ./Cargo.lock
COPY ./build.rs ./build.rs
COPY ./.env ./.env
COPY ./src ./src
COPY ./.sqlx ./.sqlx
Expand All @@ -20,7 +23,7 @@ LABEL org.opencontainers.image.source=https://github.com/DiamondLightSource/numt
LABEL org.opencontainers.image.description="Central co-ordinator for scan numbers and file locations"
LABEL org.opencontainers.image.licenses=Apache-2.0

COPY --from=build ./target/x86_64-unknown-linux-musl/release/numtracker /app/numtracker
COPY --from=build /build/target/x86_64-unknown-linux-musl/release/numtracker /app/numtracker

CMD ["serve"]
ENTRYPOINT ["/app/numtracker"]
5 changes: 4 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
fn main() {
println!("cargo:rerun-if-changed=migrations");
println!("cargo::rerun-if-changed=migrations");
built::write_built_file().expect("Failed to write build time information");
// Force the application to be rebuilt after committing to ensure build info is up to date
println!("cargo::rerun-if-changed=.git/refs");
}
42 changes: 42 additions & 0 deletions src/build_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Compile time build information provided by built

use chrono::Local;
use serde::Serialize;

include!(concat!(env!("OUT_DIR"), "/built.rs"));

/// User friendly label for marking a build as debug or not
pub const DEBUG_LABEL: &str = if DEBUG { " (debug)" } else { "" };
/// User friendly label for indicating repo state
pub const DIRTY_LABEL: &str = match GIT_DIRTY {
Some(true) => " (+unstaged changes)",
_ => "",
};

pub fn build_info() -> String {
format!(
concat!("- {}{}\n", "Built: {}\n", "Commit: {}{}"),
PKG_VERSION,
DEBUG_LABEL,
BUILT_TIME_UTC,
GIT_COMMIT_HASH.unwrap_or("Unknown"),
DIRTY_LABEL
)
}

#[derive(Debug, Clone, Serialize)]
pub struct ServerStatus {
version: String,
start_time: String,
build: String,
}

impl ServerStatus {
pub fn new() -> Self {
Self {
version: PKG_VERSION.into(),
start_time: Local::now().to_rfc3339(),
build: GIT_COMMIT_HASH.unwrap_or("Unknown").into(),
}
}
}
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use tracing::Level;
use url::Url;

#[derive(Debug, Parser)]
#[clap(version)]
#[clap(long_version = crate::build_info::build_info())]
pub struct Cli {
#[clap(short, long, default_value = "numtracker.db", env = "NUMTRACKER_DB")]
pub(crate) db: PathBuf,
Expand Down
6 changes: 4 additions & 2 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use auth::{AuthError, PolicyCheck};
use axum::response::{Html, IntoResponse};
use axum::routing::{get, post};
use axum::{Extension, Router};
use axum::{Extension, Json, Router};
use axum_extra::headers::authorization::Bearer;
use axum_extra::headers::Authorization;
use axum_extra::TypedHeader;
use chrono::{Datelike, Local};
use tokio::net::TcpListener;
use tracing::{info, instrument, trace, warn};

use crate::build_info::ServerStatus;
use crate::cli::ServeOptions;
use crate::db_service::{
BeamlineConfiguration, BeamlineConfigurationUpdate, SqliteScanPathService,
Expand All @@ -54,6 +55,7 @@ use crate::template::{FieldSource, PathTemplate};
mod auth;

pub async fn serve_graphql(db: &Path, opts: ServeOptions) {
let server_status = Json(ServerStatus::new());
let db = SqliteScanPathService::connect(db)
.await
.expect("Unable to open DB");
Expand All @@ -71,7 +73,7 @@ pub async fn serve_graphql(db: &Path, opts: ServeOptions) {
let app = Router::new()
// status check endpoint allows external processes to monitor status of server without
// making graphql queries
.route("/status", get(|| async {}))
.route("/status", get(server_status))
.route("/graphql", post(graphql_handler))
.route("/graphiql", get(graphiql))
.layer(Extension(schema));
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::error::Error;
use cli::{Cli, Command};
use tracing::debug;

mod build_info;
mod cli;
mod db_service;
mod graphql;
Expand Down
Loading