Skip to content

Commit

Permalink
Healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed May 31, 2024
1 parent 9792a8e commit 55e456d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ api_key = "G5CKNF3BTS2hRl60bpdYMNPqXvXsP-QZd2lrtmgctsk="
[server]
host = "127.0.0.1:3000"
disable_auth = false
server_address = "http://localhost:3000"

[database]
kind = "connection_string"
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub struct ServerConfig {

pub username: Option<String>,
pub password: Option<String>,

// Optional address to show in API explorer
pub server_address: Option<String>,
}

impl ServerConfig {
Expand Down Expand Up @@ -292,6 +295,7 @@ mod tests {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
username: None,
password: None,
server_address: None,
},
database: DatabaseConfig::connection_string(
"postgres://postgres:[email protected]:52804/database"
Expand Down Expand Up @@ -319,6 +323,7 @@ mod tests {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
username: None,
password: None,
server_address: None,
},
database: DatabaseConfig::Parts(DbParts {
host: "host".to_string(),
Expand Down
21 changes: 18 additions & 3 deletions src/new_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use poem::listener::{Acceptor, Listener, TcpListener};
use poem::web::LocalAddr;
use poem::Route;
use poem_openapi::{OpenApi, OpenApiService};
use poem_openapi::{ApiResponse, OpenApi, OpenApiService};

use crate::app::App;

Expand All @@ -20,8 +20,19 @@ impl ConsumerApi {}

struct ServiceApi;

#[derive(ApiResponse)]
enum ServiceResponse {
#[oai(status = 200)]
Healthy,
}

#[OpenApi]
impl ServiceApi {}
impl ServiceApi {
#[oai(path = "/", method = "get")]
async fn health(&self) -> ServiceResponse {
ServiceResponse::Healthy
}
}

pub struct ServerHandle {
pub local_addrs: Vec<LocalAddr>,
Expand All @@ -40,12 +51,16 @@ impl ServerHandle {
}

pub async fn spawn_server(app: Arc<App>) -> eyre::Result<ServerHandle> {
let api_service = OpenApiService::new(
let mut api_service = OpenApiService::new(
(AdminApi, ConsumerApi, ServiceApi),
"Tx Sitter",
version::version!(),
);

if let Some(server_address) = app.config.server.server_address.as_ref() {
api_service = api_service.server(server_address.clone());
}

let router = Route::new()
.nest("/explorer", api_service.rapidoc())
.nest("/schema.json", api_service.spec_endpoint())
Expand Down

0 comments on commit 55e456d

Please sign in to comment.