mirror of
https://github.com/moghtech/komodo.git
synced 2026-09-12 00:01:11 +00:00
add new ec2 instance types clean up testing config document the libraries a bit clean up main update sysinfo and otel update client resolver 3.0 resolver v3 prog clean up gitignore implement periphery resolver v3 clean up core read api v3 more prog execute api missing apis compiling 1.16.13 work on more granular traits prog on crud
77 lines
1.9 KiB
Rust
77 lines
1.9 KiB
Rust
use anyhow::Context;
|
|
use axum::http::{header::CONTENT_TYPE, HeaderValue, StatusCode};
|
|
use serde::Serialize;
|
|
use serror::serialize_error;
|
|
|
|
pub struct Response(pub axum::response::Response);
|
|
|
|
impl<T> From<T> for Response
|
|
where
|
|
T: Serialize,
|
|
{
|
|
fn from(value: T) -> Response {
|
|
let res = match serde_json::to_string(&value)
|
|
.context("failed to serialize response body")
|
|
{
|
|
Ok(body) => axum::response::Response::builder()
|
|
.header(
|
|
CONTENT_TYPE,
|
|
HeaderValue::from_static("application/json"),
|
|
)
|
|
.body(axum::body::Body::from(body))
|
|
.unwrap(),
|
|
Err(e) => axum::response::Response::builder()
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
.header(
|
|
CONTENT_TYPE,
|
|
HeaderValue::from_static("application/json"),
|
|
)
|
|
.body(axum::body::Body::from(serialize_error(&e)))
|
|
.unwrap(),
|
|
};
|
|
Response(res)
|
|
}
|
|
}
|
|
|
|
pub enum JsonString {
|
|
Ok(String),
|
|
Err(serde_json::Error),
|
|
}
|
|
|
|
impl<T> From<T> for JsonString
|
|
where
|
|
T: Serialize,
|
|
{
|
|
fn from(value: T) -> JsonString {
|
|
match serde_json::to_string(&value) {
|
|
Ok(body) => JsonString::Ok(body),
|
|
Err(e) => JsonString::Err(e),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl JsonString {
|
|
pub fn into_response(self) -> axum::response::Response {
|
|
match self {
|
|
JsonString::Ok(body) => axum::response::Response::builder()
|
|
.header(
|
|
CONTENT_TYPE,
|
|
HeaderValue::from_static("application/json"),
|
|
)
|
|
.body(axum::body::Body::from(body))
|
|
.unwrap(),
|
|
JsonString::Err(error) => axum::response::Response::builder()
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
.header(
|
|
CONTENT_TYPE,
|
|
HeaderValue::from_static("application/json"),
|
|
)
|
|
.body(axum::body::Body::from(serialize_error(
|
|
&anyhow::Error::from(error)
|
|
.context("failed to serialize response body"),
|
|
)))
|
|
.unwrap(),
|
|
}
|
|
}
|
|
}
|