Skip to content

Commit

Permalink
feat: added minimal health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
torbrenner committed Jun 12, 2024
1 parent 34b2006 commit 447e159
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
target/
Cargo.lock

*.priv.pem
docker-compose.override.yml
11 changes: 11 additions & 0 deletions src/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serde::Serialize;

#[derive(Serialize)]
pub enum Verdict {
Healthy,
}

#[derive(Serialize)]
pub enum BeamStatus {
Ok,
}
20 changes: 18 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{sync::Arc, convert::Infallible};

use axum::{Router, routing::{get, post}, extract::{Json, State, Path, Query}, response::{Sse, sse::Event, IntoResponse, Response}, http::{HeaderValue, HeaderName}};
use axum::{Router, routing::{get, post}, extract::{Json, State, Path, Query}, response::{Sse, sse::Event, IntoResponse}, http::HeaderValue};
use clap::Parser;
use futures::{Stream, TryStreamExt, StreamExt};
use health::{BeamStatus, Verdict};
use http::StatusCode;
use hyper::{Client, HeaderMap, header::{AUTHORIZATION, ACCEPT}, Uri, Request, Method, Body};
use hyper::{Client, header::{AUTHORIZATION, ACCEPT}, Uri, Request, Method, Body};
use serde::{Serialize, Deserialize};
use tracing::{debug, trace};
use url::Url;
Expand All @@ -14,6 +15,7 @@ use crate::logger::init_logger;

mod banner;
mod logger;
mod health;

#[derive(Parser, Clone)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -86,6 +88,12 @@ struct BeamTask {
ttl: String,
}

#[derive(Serialize)]
struct HealthOutput {
summary: Verdict,
beam: BeamStatus
}

async fn handle_create_beam_task(
State(args): State<Arc<Arguments>>,
Json(query): Json<LensQuery>
Expand All @@ -111,6 +119,7 @@ async fn main() {
debug!("Beam App: {}", &args.beam_app);
// TODO: Add check for reachability of beam-proxy
let app = Router::new()
.route("/health", get(handler_health))
.route("/beam", post(handle_create_beam_task))
.route("/beam/:task_id", get(handle_listen_to_beam_tasks))
.layer(axum::middleware::map_response(banner::set_server_header))
Expand All @@ -122,6 +131,13 @@ async fn main() {
.unwrap();
}

async fn handler_health() -> Json<HealthOutput> {
Json(HealthOutput {
summary: Verdict::Healthy,
beam: BeamStatus::Ok
})
}

async fn create_beam_task(args: &Arguments, query: &LensQuery) -> Result<impl IntoResponse, (StatusCode, &'static str)> {
let client = Client::new();
let url = format!("{}v1/tasks", args.beam_url);
Expand Down

0 comments on commit 447e159

Please sign in to comment.