[mixnode] replace rocket with axum (#4071)

* axum-equivalent mixnode http api routes

* replaced all rocket routes with axum equivalents

* removed '_axum' suffix from the routes
This commit is contained in:
Jędrzej Stuczyński
2023-10-30 12:55:42 +00:00
committed by GitHub
parent a209b87a41
commit da9d743f39
20 changed files with 159 additions and 92 deletions
+10 -5
View File
@@ -1,9 +1,14 @@
use crate::node::node_description::NodeDescription;
use rocket::serde::json::Json;
use rocket::State;
use axum::extract::Query;
use nym_node::http::api::{FormattedResponse, OutputParams};
/// Returns a description of the node and why someone might want to delegate stake to it.
#[get("/description")]
pub(crate) fn description(description: &State<NodeDescription>) -> Json<NodeDescription> {
Json(description.inner().clone())
pub(crate) async fn description(
description: NodeDescription,
Query(output): Query<OutputParams>,
) -> MixnodeDescriptionResponse {
let output = output.output.unwrap_or_default();
output.to_response(description)
}
pub type MixnodeDescriptionResponse = FormattedResponse<NodeDescription>;
+9 -7
View File
@@ -1,10 +1,11 @@
use axum::extract::Query;
use cupid::TopologyType;
use rocket::serde::{json::Json, Serialize};
use nym_node::http::api::{FormattedResponse, OutputParams};
use serde::Serialize;
use sysinfo::{System, SystemExt};
#[derive(Serialize, Debug)]
#[serde(crate = "rocket::serde")]
pub(crate) struct Hardware {
pub struct Hardware {
ram: String,
num_cores: usize,
crypto_hardware: Option<CryptoHardware>,
@@ -12,7 +13,6 @@ pub(crate) struct Hardware {
#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Debug)]
#[serde(crate = "rocket::serde")]
pub(crate) struct CryptoHardware {
aesni: bool,
avx2: bool,
@@ -24,11 +24,13 @@ pub(crate) struct CryptoHardware {
}
/// Provides hardware information which Nym can use to optimize mixnet speed over time (memory, crypto hardware, CPU, cores, etc).
#[get("/hardware")]
pub(crate) fn hardware() -> Json<Option<Hardware>> {
Json(hardware_info())
pub(crate) async fn hardware(Query(output): Query<OutputParams>) -> MixnodeHardwareResponse {
let output = output.output.unwrap_or_default();
output.to_response(hardware_info())
}
pub type MixnodeHardwareResponse = FormattedResponse<Option<Hardware>>;
/// Gives back a summary report of whatever system hardware info we can get for this platform.
fn hardware_info() -> Option<Hardware> {
let crypto_hardware = hardware_info_from_cupid();
+8 -4
View File
@@ -1,11 +1,15 @@
pub(crate) mod description;
pub(crate) mod hardware;
pub(crate) mod state;
pub(crate) mod stats;
pub(crate) mod verloc;
use rocket::Request;
use axum::http::{StatusCode, Uri};
use axum::response::IntoResponse;
#[catch(404)]
pub(crate) fn not_found(req: &Request<'_>) -> String {
format!("I couldn't find '{}'. Try something else?", req.uri())
pub(crate) async fn not_found(uri: Uri) -> impl IntoResponse {
(
StatusCode::NOT_FOUND,
format!("I couldn't find '{uri}'. Try something else?"),
)
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::node::http::verloc::VerlocState;
use crate::node::node_statistics::SharedNodeStats;
use axum::extract::FromRef;
// this is a temporary thing for the transition period
#[derive(Clone)]
pub(crate) struct MixnodeAppState {
pub(crate) verloc: VerlocState,
pub(crate) stats: SharedNodeStats,
}
impl FromRef<MixnodeAppState> for VerlocState {
fn from_ref(app_state: &MixnodeAppState) -> Self {
app_state.verloc.clone()
}
}
impl FromRef<MixnodeAppState> for SharedNodeStats {
fn from_ref(app_state: &MixnodeAppState) -> Self {
app_state.stats.clone()
}
}
+25 -16
View File
@@ -1,29 +1,38 @@
use crate::node::node_statistics::{NodeStats, NodeStatsSimple, SharedNodeStats};
use rocket::serde::json::Json;
use rocket::State;
use serde::Serialize;
use axum::extract::{Query, State};
use nym_node::http::api::{FormattedResponse, Output};
use serde::{Deserialize, Serialize};
#[derive(Serialize)]
#[serde(untagged)]
pub(crate) enum NodeStatsResponse {
pub enum NodeStatsResponse {
Full(NodeStats),
Simple(NodeStatsSimple),
}
/// Returns a running stats of the node.
#[get("/stats?<debug>")]
pub(crate) async fn stats(
stats: &State<SharedNodeStats>,
debug: Option<bool>,
) -> Json<NodeStatsResponse> {
Query(params): Query<StatsQueryParams>,
State(stats): State<SharedNodeStats>,
) -> MixnodeStatsResponse {
let output = params.output.unwrap_or_default();
let snapshot_data = stats.clone_data().await;
// there's no point in returning the entire hashmap of sending destinations in regular mode
if let Some(debug) = debug {
if debug {
return Json(NodeStatsResponse::Full(snapshot_data));
}
}
Json(NodeStatsResponse::Simple(snapshot_data.simplify()))
let response = if params.debug {
NodeStatsResponse::Full(snapshot_data)
} else {
NodeStatsResponse::Simple(snapshot_data.simplify())
};
output.to_response(response)
}
pub type MixnodeStatsResponse = FormattedResponse<NodeStatsResponse>;
#[derive(Default, Debug, Serialize, Deserialize, Copy, Clone)]
// #[derive(Default, Debug, Serialize, Deserialize, Copy, Clone, IntoParams, ToSchema)]
#[serde(default)]
pub(crate) struct StatsQueryParams {
debug: bool,
pub output: Option<Output>,
}
+11 -6
View File
@@ -1,7 +1,8 @@
use axum::extract::{Query, State};
use nym_mixnode_common::verloc::{AtomicVerlocResult, VerlocResult};
use rocket::serde::json::Json;
use rocket::State;
use nym_node::http::api::{FormattedResponse, OutputParams};
#[derive(Clone)]
pub(crate) struct VerlocState {
shared: AtomicVerlocResult,
}
@@ -16,8 +17,12 @@ impl VerlocState {
/// Provides verifiable location (verloc) measurements for this mixnode - a list of the
/// round-trip times, in milliseconds, for all other mixnodes that this node knows about.
#[get("/verloc")]
pub(crate) async fn verloc(state: &State<VerlocState>) -> Json<VerlocResult> {
// since it's impossible to get a mutable reference to the state, we can't cache any results outside the lock : (
Json(state.shared.clone_data().await)
pub(crate) async fn verloc(
State(verloc): State<VerlocState>,
Query(output): Query<OutputParams>,
) -> MixnodeVerlocResponse {
let output = output.output.unwrap_or_default();
output.to_response(verloc.shared.clone_data().await)
}
pub type MixnodeVerlocResponse = FormattedResponse<VerlocResult>;