mirror of
https://github.com/moghtech/komodo.git
synced 2026-04-30 14:25:22 -05:00
periphery use AppError
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2178,7 +2178,7 @@ dependencies = [
|
|||||||
"run_command",
|
"run_command",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serror",
|
"serror_axum",
|
||||||
"simple_logger",
|
"simple_logger",
|
||||||
"svi",
|
"svi",
|
||||||
"sysinfo",
|
"sysinfo",
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ termination_signal.workspace = true
|
|||||||
run_command.workspace = true
|
run_command.workspace = true
|
||||||
svi.workspace = true
|
svi.workspace = true
|
||||||
resolver_api.workspace = true
|
resolver_api.workspace = true
|
||||||
serror.workspace = true
|
serror_axum.workspace = true
|
||||||
# external
|
# external
|
||||||
tokio.workspace = true
|
tokio.workspace = true
|
||||||
axum.workspace = true
|
axum.workspace = true
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use async_timing_util::unix_timestamp_ms;
|
use async_timing_util::unix_timestamp_ms;
|
||||||
use axum::http::StatusCode;
|
|
||||||
use axum_extra::{headers::ContentType, TypedHeader};
|
|
||||||
use monitor_client::entities::update::Log;
|
use monitor_client::entities::update::Log;
|
||||||
use run_command::{async_run_command, CommandOutput};
|
use run_command::{async_run_command, CommandOutput};
|
||||||
use serror::serialize_error;
|
|
||||||
|
|
||||||
use crate::state::State;
|
use crate::state::State;
|
||||||
|
|
||||||
@@ -71,13 +68,3 @@ pub fn output_into_log(
|
|||||||
end_ts: unix_timestamp_ms() as i64,
|
end_ts: unix_timestamp_ms() as i64,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_response_error(
|
|
||||||
e: anyhow::Error,
|
|
||||||
) -> (StatusCode, TypedHeader<ContentType>, String) {
|
|
||||||
(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
TypedHeader(ContentType::json()),
|
|
||||||
serialize_error(e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,13 +4,11 @@ extern crate log;
|
|||||||
use std::{net::SocketAddr, sync::Arc, time::Instant};
|
use std::{net::SocketAddr, sync::Arc, time::Instant};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use axum::{
|
use axum::{middleware, routing::post, Extension, Json, Router};
|
||||||
http::StatusCode, middleware, routing::post, Extension, Json,
|
|
||||||
Router,
|
|
||||||
};
|
|
||||||
|
|
||||||
use axum_extra::{headers::ContentType, TypedHeader};
|
use axum_extra::{headers::ContentType, TypedHeader};
|
||||||
use resolver_api::Resolver;
|
use resolver_api::Resolver;
|
||||||
|
use serror_axum::AppResult;
|
||||||
use termination_signal::tokio::immediate_term_handle;
|
use termination_signal::tokio::immediate_term_handle;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -23,11 +21,6 @@ mod state;
|
|||||||
use requests::PeripheryRequest;
|
use requests::PeripheryRequest;
|
||||||
use state::State;
|
use state::State;
|
||||||
|
|
||||||
use crate::helpers::into_response_error;
|
|
||||||
|
|
||||||
type ResponseResult<T> =
|
|
||||||
Result<T, (StatusCode, TypedHeader<ContentType>, String)>;
|
|
||||||
|
|
||||||
async fn app() -> anyhow::Result<()> {
|
async fn app() -> anyhow::Result<()> {
|
||||||
let state = State::load().await?;
|
let state = State::load().await?;
|
||||||
|
|
||||||
@@ -45,22 +38,21 @@ async fn app() -> anyhow::Result<()> {
|
|||||||
let req_id = Uuid::new_v4();
|
let req_id = Uuid::new_v4();
|
||||||
info!("request {req_id} | {request:?}");
|
info!("request {req_id} | {request:?}");
|
||||||
let res = tokio::spawn(async move {
|
let res = tokio::spawn(async move {
|
||||||
state.resolve_request(request, ()).await
|
let res = state.resolve_request(request, ()).await;
|
||||||
|
if let Err(e) = &res {
|
||||||
|
debug!("request {req_id} ERROR: {e:#?}");
|
||||||
|
}
|
||||||
|
let elapsed = timer.elapsed();
|
||||||
|
info!("request {req_id} | resolve time: {elapsed:?}");
|
||||||
|
res
|
||||||
})
|
})
|
||||||
.await
|
.await;
|
||||||
.context("failed in spawned request handler");
|
|
||||||
if let Err(e) = &res {
|
if let Err(e) = &res {
|
||||||
debug!("request {req_id} SPAWN ERROR: {e:#?}");
|
debug!("request {req_id} SPAWN ERROR: {e:#?}");
|
||||||
}
|
}
|
||||||
let res = res.map_err(into_response_error)?;
|
let res = res??;
|
||||||
if let Err(e) = &res {
|
|
||||||
debug!("request {req_id} ERROR: {e:#?}");
|
|
||||||
}
|
|
||||||
let res = res.map_err(into_response_error)?;
|
|
||||||
let elapsed = timer.elapsed();
|
|
||||||
info!("request {req_id} | resolve time: {elapsed:?}");
|
|
||||||
debug!("request {req_id} RESPONSE: {res}");
|
debug!("request {req_id} RESPONSE: {res}");
|
||||||
ResponseResult::Ok((TypedHeader(ContentType::json()), res))
|
AppResult::Ok((TypedHeader(ContentType::json()), res))
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user