wip: populating rest of swagger

This commit is contained in:
Jędrzej Stuczyński
2023-09-21 16:16:25 +01:00
parent a1aff09b91
commit eda8b6bf85
12 changed files with 92 additions and 36 deletions
+15 -3
View File
@@ -8,11 +8,12 @@ use clap::{crate_name, crate_version, Parser};
use colored::Colorize;
use lazy_static::lazy_static;
use log::error;
use nym_bin_common::bin_info;
use nym_bin_common::logging::{maybe_print_banner, setup_logging};
use nym_bin_common::output_format::OutputFormat;
use nym_bin_common::{bin_info, bin_info_owned};
use nym_network_defaults::setup_env;
use std::error::Error;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
mod commands;
mod config;
@@ -49,10 +50,21 @@ struct Cli {
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
setup_logging();
let config = nym_node::http::Config::default();
let config = nym_node::http::Config {
landing: Default::default(),
policy: Default::default(),
api: nym_node::http::api::Config {
v1_config: nym_node::http::api::v1::Config {
build_information: bin_info_owned!(),
gateway: Default::default(),
mixnide: Default::default(),
network_requester: Default::default(),
},
},
};
let mut router = nym_node::http::NymNodeRouter::new(config);
let address = "127.0.0.1:12345".parse().unwrap();
let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 12345);
let server = router.build_server(&address)?;
server.await?;
+3
View File
@@ -11,4 +11,7 @@ pub enum NymNodeError {
bind_address: SocketAddr,
source: hyper::Error,
},
#[error("unimplemented")]
Unimplemented,
}
+5 -2
View File
@@ -20,9 +20,12 @@ pub async fn logger<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
let host = header_map(req.headers().get(HOST), "Unknown Host".to_string());
info!("[{host}] requester {method}: {uri} via {agent}");
let res = next.run(req).await;
let status = res.status();
next.run(req).await
info!("[{host}] {method} '{uri}': {status} / agent: {agent}");
res
}
fn header_map(header: Option<&HeaderValue>, msg: String) -> String {
+1 -1
View File
@@ -5,7 +5,7 @@ pub mod middleware;
pub mod router;
pub mod state;
pub use router::{Config, NymNodeRouter};
pub use router::{Config, NymNodeRouter, api, landing_page, policy};
// pub struct Config {
// router_config: router::Config,
+1 -1
View File
@@ -16,7 +16,7 @@ pub(crate) mod routes {
pub(crate) const SWAGGER: &str = "/swagger";
}
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Config {
pub v1_config: v1::Config,
}
@@ -4,30 +4,28 @@
use crate::http::router::api::{FormattedResponse, OutputParams};
use crate::http::state::AppState;
use axum::extract::{Query, State};
use axum::response::IntoResponse;
use nym_bin_common::build_information::BinaryBuildInformationOwned;
/// Description of the path
/// Returns build metadata of the binary running the API
#[utoipa::path(
get,
path = "/build-info",
context_path = "/api/v1",
responses(
(status=200, content(
(status = 200, content(
("application/json" = BinaryBuildInformationOwned),
("application/yaml" = BinaryBuildInformationOwned)
))
),
params(OutputParams)
)]
pub async fn build_info(
pub(crate) async fn build_info(
State(state): State<AppState>,
Query(output): Query<OutputParams>,
) -> FormattedResponse<BinaryBuildInformationOwned> {
) -> BuildInfoResponse {
let output = output.output.unwrap_or_default();
// todo!()
// TODO: get rid of the clone since it's getting serialized anyway
output.to_response(state.build_information.clone())
}
pub struct BuildInfoResponse(BinaryBuildInformationOwned);
pub type BuildInfoResponse = FormattedResponse<BinaryBuildInformationOwned>;
@@ -2,13 +2,18 @@
// SPDX-License-Identifier: Apache-2.0
use crate::http::state::AppState;
use axum::http::StatusCode;
use axum::routing::get;
use axum::Router;
pub mod root;
pub(crate) mod routes {
pub(crate) const ROOT: &str = "/";
}
#[derive(Debug, Clone, Default)]
pub struct Config {}
pub(crate) fn routes(_config: Config) -> Router<AppState> {
Router::new().route("/", get(|| async { StatusCode::NOT_IMPLEMENTED }))
Router::new().route(routes::ROOT, get(root::root_gateway))
}
@@ -0,0 +1,24 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::error::NymNodeError;
use crate::http::router::api::OutputParams;
use axum::extract::Query;
use axum::http::StatusCode;
use axum::response::IntoResponse;
#[utoipa::path(
get,
path = "",
context_path = "/api/v1/gateway",
responses(
(status = 501, description = "the endpoint hasn't been implemented yet")
),
params(OutputParams)
)]
pub(crate) async fn root_gateway(Query(_output): Query<OutputParams>) -> impl IntoResponse {
(
StatusCode::NOT_IMPLEMENTED,
NymNodeError::Unimplemented.to_string(),
)
}
+3 -2
View File
@@ -21,11 +21,12 @@ pub(crate) mod routes {
pub(crate) const NETWORK_REQUESTER: &str = "/network-requester";
pub(crate) const BUILD_INFO: &str = "/build-info";
pub(crate) const ROLES: &str = "/roles";
pub(crate) const SWAGGER: &str = "/swagger";
}
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Config {
pub build_information: Option<BinaryBuildInformationOwned>,
pub build_information: BinaryBuildInformationOwned,
pub gateway: gateway::Config,
pub mixnide: mixnode::Config,
pub network_requester: network_requester::Config,
+18 -2
View File
@@ -11,15 +11,31 @@ use utoipa_swagger_ui::SwaggerUi;
#[derive(OpenApi)]
#[openapi(
info(title = "NymNode API"),
paths(api::v1::build_info::build_info,),
paths(api::v1::build_info::build_info, api::v1::gateway::root::root_gateway,),
components(schemas(api::Output, api::OutputParams, BinaryBuildInformationOwned))
)]
/*
#[derive(OpenApi)]
#[openapi(
info(title = "NymNode API"),
paths(
api::v1::build_info::build_info,
),
components(
schemas(
api::Output,
api::OutputParams,
BinaryBuildInformationOwned
)
)
)]
*/
pub(crate) struct ApiDoc;
pub(crate) fn route() -> Router<AppState> {
// provide absolute path to the openapi.json
let config = utoipa_swagger_ui::Config::from("/api/v1/api-docs/openapi.json");
SwaggerUi::new("/swagger")
SwaggerUi::new(super::routes::SWAGGER)
.url("/api-docs/openapi.json", ApiDoc::openapi())
.config(config)
.into()
+5 -9
View File
@@ -12,7 +12,7 @@ use std::net::SocketAddr;
pub mod api;
pub mod landing_page;
mod policy;
pub mod policy;
pub(crate) mod routes {
pub(crate) const LANDING_PAGE: &str = "/";
@@ -24,7 +24,7 @@ pub(crate) mod routes {
// TODO: can it be made nicer?
pub type NymNodeHTTPServer = Server<AddrIncoming, IntoMakeService<Router>>;
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Config {
pub landing: landing_page::Config,
pub policy: policy::Config,
@@ -32,24 +32,20 @@ pub struct Config {
}
pub struct NymNodeRouter {
// landing_page_assets: Option<PathBuf>,
inner: Router,
}
impl NymNodeRouter {
pub fn new(config: Config) -> NymNodeRouter {
/*
.merge(SwaggerUi::new("/swagger-ui")
.url("/api-docs/openapi.json", ApiDoc::openapi()));
*/
use utoipa::OpenApi;
let state = AppState::new(config.api.v1_config.build_information.clone());
NymNodeRouter {
inner: Router::new()
.nest(routes::LANDING_PAGE, landing_page::routes(config.landing))
.nest(routes::POLICY, policy::routes(config.policy))
.nest(routes::API, api::routes(config.api))
.layer(axum::middleware::from_fn(logging::logger))
.with_state(AppState::new_dummy()),
.with_state(state),
}
}
+5 -7
View File
@@ -1,26 +1,24 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use nym_bin_common::bin_info_owned;
use nym_bin_common::build_information::BinaryBuildInformationOwned;
use std::ops::Deref;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct AppState {
pub(crate) struct AppState {
inner: Arc<AppStateInner>,
}
impl AppState {
pub fn new_dummy() -> Self {
pub fn new(build_information: BinaryBuildInformationOwned) -> Self {
AppState {
inner: Arc::new(AppStateInner {
build_information: bin_info_owned!(),
}),
inner: Arc::new(AppStateInner { build_information }),
}
}
}
// TODO: that's kinda abusing it...
impl Deref for AppState {
type Target = AppStateInner;
@@ -30,7 +28,7 @@ impl Deref for AppState {
}
#[derive(Debug)]
pub struct AppStateInner {
pub(crate) struct AppStateInner {
// TODO: split it based on routes?
pub(crate) build_information: BinaryBuildInformationOwned,
}