a0fea6edb4
* Migrate nym-api HTTP server from rocket to axum (#4698) Migrate endpoints to Axum * Squashed after PR review Initial WIP - bootstrap axum server with same data as rocket - start axum server alongside rocket - add routes for circulating-supply, contract-cache, network - write simple bash validation that migrated APIs return 200 - mark rocket parts of code as deprecated - start more complicated routes: WIP Init storage always Add coconut routes Add api-status routes Expand tests WIP Migrate unstable APIs with query params Update bash tests Add node-status routes Redirect / to /swagger Update API tests Implement graceful shutdown rustfmt Fix clippy * Add ecash routes after rebase * PR feedback - add CORS layer - move logger to common crate - remove global log filters for nym-api and axum * Serve OpenAPI for all endpoints (#4761) * Playing around with swagger * Generate OpenAPI for /status routes * Phase out static_routes as strings - also nest routers in a clearer way * Generate OpenAPI for /network routes * Generate OpenAPI for /api-status routes * Generate OpenAPI for "nym nodes" routes * Fix some network-monitor routes * Generate OpenAPI for /ecash routes * Add utoipa feature to /common mods * Add OpenAPI for unstable routes * Fix MixNodeDetails field in models * Introduce axum feature flag (#4775) * Add Axum bind_address to config * Introduce axum feature flag * Add comment to template.rs * Add Github action to build wtih `axum` feature * Refactor server start & shutdown (#4777) * Clippy: don't forget axum feature * Refactor router so it's safer * Implement graceful shutdown * Nicer pattern matching * Better Result syntax
94 lines
2.4 KiB
Rust
94 lines
2.4 KiB
Rust
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use axum::http::{header, HeaderValue, StatusCode};
|
|
use axum::response::{IntoResponse, Response};
|
|
use axum::Json;
|
|
use bytes::{BufMut, BytesMut};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
pub mod logging;
|
|
|
|
#[derive(Debug, Clone)]
|
|
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
|
|
pub enum FormattedResponse<T> {
|
|
Json(Json<T>),
|
|
Yaml(Yaml<T>),
|
|
}
|
|
|
|
impl<T> IntoResponse for FormattedResponse<T>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
fn into_response(self) -> Response {
|
|
match self {
|
|
FormattedResponse::Json(json_response) => json_response.into_response(),
|
|
FormattedResponse::Yaml(yaml_response) => yaml_response.into_response(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Debug, Serialize, Deserialize, Copy, Clone)]
|
|
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum Output {
|
|
#[default]
|
|
Json,
|
|
Yaml,
|
|
}
|
|
|
|
#[derive(Default, Debug, Serialize, Deserialize, Copy, Clone)]
|
|
#[cfg_attr(feature = "utoipa", derive(utoipa::IntoParams, utoipa::ToSchema))]
|
|
#[serde(default)]
|
|
pub struct OutputParams {
|
|
pub output: Option<Output>,
|
|
}
|
|
|
|
impl Output {
|
|
pub fn to_response<T: Serialize>(self, data: T) -> FormattedResponse<T> {
|
|
match self {
|
|
Output::Json => FormattedResponse::Json(Json(data)),
|
|
Output::Yaml => FormattedResponse::Yaml(Yaml(data)),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
#[must_use]
|
|
pub struct Yaml<T>(pub T);
|
|
|
|
impl<T> From<T> for Yaml<T> {
|
|
fn from(inner: T) -> Self {
|
|
Self(inner)
|
|
}
|
|
}
|
|
|
|
impl<T> IntoResponse for Yaml<T>
|
|
where
|
|
T: Serialize,
|
|
{
|
|
// replicates axum's Json
|
|
fn into_response(self) -> Response {
|
|
let mut buf = BytesMut::with_capacity(128).writer();
|
|
match serde_yaml::to_writer(&mut buf, &self.0) {
|
|
Ok(()) => (
|
|
[(
|
|
header::CONTENT_TYPE,
|
|
HeaderValue::from_static("application/yaml"),
|
|
)],
|
|
buf.into_inner().freeze(),
|
|
)
|
|
.into_response(),
|
|
Err(err) => (
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
[(
|
|
header::CONTENT_TYPE,
|
|
HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref()),
|
|
)],
|
|
err.to_string(),
|
|
)
|
|
.into_response(),
|
|
}
|
|
}
|
|
}
|