Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
|
||||
use axum::{
|
||||
extract::{MatchedPath, State},
|
||||
http::Request,
|
||||
routing::get,
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tower_http::trace::TraceLayer;
|
||||
|
||||
@@ -22,6 +28,7 @@ impl Api {
|
||||
pub async fn serve(&self) -> anyhow::Result<()> {
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.route("/discovery", get(discovery))
|
||||
.with_state(self.state.clone())
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
@@ -55,6 +62,28 @@ async fn root() -> &'static str {
|
||||
"Hello, churn!"
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Discovery {
|
||||
pub external_host: String,
|
||||
pub process_host: String,
|
||||
}
|
||||
|
||||
impl Discovery {
|
||||
pub async fn get_from_host(host: &str) -> anyhow::Result<Self> {
|
||||
let resp = reqwest::get(format!("{}/discovery", host.trim_end_matches('/'))).await?;
|
||||
let s: Self = resp.json().await?;
|
||||
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
async fn discovery(State(state): State<SharedState>) -> Json<Discovery> {
|
||||
Json(Discovery {
|
||||
external_host: state.config.external_host.clone(),
|
||||
process_host: state.config.process_host.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl notmad::Component for Api {
|
||||
async fn run(&self, _cancellation_token: CancellationToken) -> Result<(), notmad::MadError> {
|
||||
|
Reference in New Issue
Block a user