Skip to content

Commit

Permalink
feat: add swagger-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 10, 2023
1 parent d473f8e commit 19c9c7e
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 5 deletions.
147 changes: 147 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions moksha-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_with = "3.4.0"
thiserror = "1.0.50"
itertools = "0.12.0"
uuid = { version = "1.6.1", features = ["serde", "v4"] }
utoipa = { version = "4.1.0" }

[target.'cfg(target_family = "wasm")'.dependencies]
# getrandom is transitive dependency of rand
Expand Down
12 changes: 7 additions & 5 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use secp256k1::PublicKey;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::convert::TryFrom;
use utoipa::ToSchema;
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -120,7 +121,7 @@ pub struct KeyResponse {
pub keys: HashMap<u64, PublicKey>,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum CurrencyUnit {
Sat,
Expand All @@ -136,7 +137,7 @@ impl Display for CurrencyUnit {
}
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum PaymentMethod {
Bolt11,
Expand Down Expand Up @@ -260,19 +261,20 @@ pub struct PostMeltBolt11Response {
}

#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, ToSchema)]
pub struct MintInfoResponse {
pub name: Option<String>,
#[schema(value_type = String)]
pub pubkey: PublicKey,
pub version: Option<String>,
pub description: Option<String>,
pub description_long: Option<String>,
pub contact: Option<Vec<Vec<String>>>,
pub nuts: Vec<MintInfoNut>,
pub motd: Option<String>,
pub nuts: Vec<MintInfoNut>,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ToSchema)]
pub enum MintInfoNut {
/// Cryptography and Models
#[serde(rename = "0")]
Expand Down
3 changes: 3 additions & 0 deletions moksha-mint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ envy = "0.4.2"
fedimint-tonic-lnd = { version = "0.1.3", features = ["lightningrpc"] }
uuid = { version = "1.6.1", features = ["serde", "v4"] }

utoipa = { version = "4.1.0", features = ["axum_extras"] }
utoipa-swagger-ui = { version = "5.0.0", features = ["axum"] }

[dev-dependencies]
tempfile = "3.8.1"
tower = { version = "0.4", features = ["util"] }
Expand Down
18 changes: 18 additions & 0 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use axum::routing::{get_service, post};
use axum::{middleware, Router};
use axum::{routing::get, Json};
use moksha_core::keyset::{generate_hash, Keysets, V1Keysets};
use utoipa_swagger_ui::SwaggerUi;
use uuid::Uuid;

use crate::mint::Mint;
Expand All @@ -36,6 +37,8 @@ use tracing::{event, info, Level};
use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

use utoipa::OpenApi;

pub async fn run_server(
mint: Mint,
addr: SocketAddr,
Expand Down Expand Up @@ -73,6 +76,13 @@ pub async fn run_server(
Ok(())
}

#[derive(OpenApi)]
#[openapi(
paths(get_info,),
components(schemas(MintInfoResponse, MintInfoNut, CurrencyUnit, PaymentMethod))
)]
struct ApiDoc;

fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -> Router {
let legacy_routes = Router::new()
.route("/keys", get(get_legacy_keys))
Expand All @@ -84,6 +94,7 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -
.route("/info", get(get_legacy_info));

let routes = Router::new()
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
.route("/v1/keys", get(get_keys))
.route("/v1/keys/:id", get(get_keys_by_id))
.route("/v1/keysets", get(get_keysets))
Expand Down Expand Up @@ -496,6 +507,13 @@ async fn get_melt_quote_bolt11(
}
}

#[utoipa::path(
get,
path = "/v1/info",
responses(
(status = 200, description = "get mint info", body = [MintInfoResponse])
)
)]
async fn get_info(State(mint): State<Mint>) -> Result<Json<MintInfoResponse>, MokshaMintError> {
let mint_info = MintInfoResponse {
name: mint.mint_info.name,
Expand Down

0 comments on commit 19c9c7e

Please sign in to comment.