-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e30bd9
commit fd7903b
Showing
5 changed files
with
93 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::state::CustomState; | ||
use anyhow::anyhow; | ||
use async_trait::async_trait; | ||
use axum::extract::FromRef; | ||
use roadster::app::context::{AppContext, AppContextWeak}; | ||
use roadster::app::RoadsterApp; | ||
use roadster::error::RoadsterResult; | ||
use roadster::health::check::{CheckResponse, HealthCheck, Status}; | ||
use roadster::util::empty::Empty; | ||
use std::time::Duration; | ||
|
||
pub type App = RoadsterApp<CustomState, Empty, Empty>; | ||
|
||
pub struct ExampleHealthCheck { | ||
// Prevent reference cycle because the `ExampleHealthCheck` is also stored in the `AppContext` | ||
context: AppContextWeak, | ||
} | ||
|
||
#[async_trait] | ||
impl HealthCheck for ExampleHealthCheck { | ||
fn name(&self) -> String { | ||
"example".to_string() | ||
} | ||
|
||
fn enabled(&self) -> bool { | ||
true | ||
} | ||
|
||
async fn check(&self) -> RoadsterResult<CheckResponse> { | ||
// Upgrade the `AppContext` in order to use it | ||
let _context = self | ||
.context | ||
.upgrade() | ||
.ok_or_else(|| anyhow!("Could not upgrade AppContextWeak"))?; | ||
|
||
Ok(CheckResponse::builder() | ||
.status(Status::Ok) | ||
.latency(Duration::from_secs(0)) | ||
.build()) | ||
} | ||
} | ||
|
||
pub fn build_app() -> App { | ||
RoadsterApp::builder() | ||
.state_provider(|context| { | ||
Ok(CustomState { | ||
context, | ||
custom_field: "Custom Field".to_string(), | ||
}) | ||
}) | ||
.add_health_check_provider(|registry, state| { | ||
// Downgrade the context before providing it to the `ExampleHealthCheck` | ||
let context = AppContext::from_ref(state).downgrade(); | ||
registry.register(ExampleHealthCheck { context }) | ||
}) | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,2 @@ | ||
use axum::extract::FromRef; | ||
use roadster::app::context::AppContext; | ||
|
||
#[derive(FromRef)] | ||
pub struct CustomState { | ||
custom_field: String, | ||
context: AppContext, | ||
} | ||
|
||
impl CustomState { | ||
pub fn new(custom_field: String, context: AppContext) -> Self { | ||
Self { | ||
custom_field, | ||
context, | ||
} | ||
} | ||
} | ||
pub mod app; | ||
pub mod state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use axum::extract::FromRef; | ||
use roadster::app::context::AppContext; | ||
|
||
#[derive(Clone, FromRef)] | ||
pub struct CustomState { | ||
pub context: AppContext, | ||
pub custom_field: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters