From 9b1368dd9cd16e6b513d3154d4a2042dc771ea21 Mon Sep 17 00:00:00 2001 From: DSeeLP <46624152+DSeeLP@users.noreply.github.com> Date: Thu, 17 Apr 2025 09:07:31 +0200 Subject: [PATCH] refactor motd endpoint and add bank meta endpoint --- bank_core/src/lib.rs | 9 ++------- bank_core/src/meta.rs | 18 ++++++++++++++++++ openapi-def.yaml | 21 +++++++++++++++++++-- src/api/meta.rs | 20 ++++++++++++++++++++ src/api/mod.rs | 13 ++++++------- src/config.rs | 35 ++++++++++++++++++++++++++++++----- src/main.rs | 2 +- 7 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 bank_core/src/meta.rs create mode 100644 src/api/meta.rs diff --git a/bank_core/src/lib.rs b/bank_core/src/lib.rs index 26859cd..da5b004 100644 --- a/bank_core/src/lib.rs +++ b/bank_core/src/lib.rs @@ -1,5 +1,6 @@ pub mod account; pub mod chat; +pub mod meta; pub mod pagination; mod schemas; pub mod transaction; @@ -11,10 +12,4 @@ pub use error::*; use serde::{Deserialize, Serialize}; pub use util::*; -make_schemas!((Credentials); (ApiError, TokenResponse, Motd), [account::schemas, chat::schemas, transaction::schemas, user::schemas]); - -#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] -#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))] -pub struct Motd { - pub text: String, -} +make_schemas!((Credentials); (ApiError, TokenResponse), [account::schemas, chat::schemas, transaction::schemas, user::schemas, meta::schemas]); diff --git a/bank_core/src/meta.rs b/bank_core/src/meta.rs new file mode 100644 index 0000000..4025cde --- /dev/null +++ b/bank_core/src/meta.rs @@ -0,0 +1,18 @@ +use serde::{Deserialize, Serialize}; + +use crate::make_schemas; + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] +#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))] +pub struct Motd { + pub text: String, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] +#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))] +pub struct Bank { + pub name: String, + pub currency: String, +} + +make_schemas!((); (Motd, Bank)); diff --git a/openapi-def.yaml b/openapi-def.yaml index d4721be..d9cb668 100644 --- a/openapi-def.yaml +++ b/openapi-def.yaml @@ -5,14 +5,17 @@ info: version: 0.0.1 tags: - name: Authentication + - name: Meta - name: Users - name: Accounts - name: Transactions - name: Chats paths: - /api/motd: + /api/meta/motd: get: - operationId: motd + operationId: meta-motd + tags: + - Meta responses: 200: description: Motd @@ -22,6 +25,20 @@ paths: $ref: '#/components/schemas/Motd' default: $ref: '#/components/responses/Default' + /api/meta/bank: + get: + operationId: meta-bank + tags: + - Meta + responses: + 200: + description: Bank + content: + application/json: + schema: + $ref: '#/components/schemas/Bank' + default: + $ref: '#/components/responses/Default' /api/login: post: operationId: login diff --git a/src/api/meta.rs b/src/api/meta.rs new file mode 100644 index 0000000..f3255f5 --- /dev/null +++ b/src/api/meta.rs @@ -0,0 +1,20 @@ +use std::sync::Arc; + +use axum::{Router, routing::get}; +use bank_core::meta::{Bank, Motd}; + +use super::{AppState, EState, Json, State}; + +pub fn router() -> Router> { + Router::new() + .route("/motd", get(motd)) + .route("/bank", get(bank)) +} + +pub async fn motd(EState(state): State) -> Json { + Json(state.meta.motd.clone()) +} + +pub async fn bank(EState(state): State) -> Json { + Json(state.meta.bank.clone()) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index e566222..c69dbf0 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -8,7 +8,7 @@ use axum::{ routing::get, }; -use bank_core::{ApiError, Motd, make_schemas, pagination::RequestPagination}; +use bank_core::{ApiError, make_schemas, pagination::RequestPagination}; use jsonwebtoken::{DecodingKey, EncodingKey}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use tracing_error::SpanTrace; @@ -22,12 +22,15 @@ mod auth; mod chats; mod docs; mod interop; +mod meta; mod socket; mod transactions; mod user; pub use interop::router as interop_router; +use crate::config::Meta; + #[derive(Debug, Clone, Copy, Default)] pub struct Json(T); @@ -270,7 +273,7 @@ pub struct AppState { pub decoding_key: DecodingKey, pub sockets: Arc, pub interop: Option, - pub motd: Motd, + pub meta: Meta, } pub struct InteropState { @@ -292,17 +295,13 @@ pub type State = axum::extract::State>; pub fn router() -> Router> { Router::new() .merge(auth::router()) - .route("/motd", get(motd)) .nest("/users", user::router()) .nest("/docs", docs::router()) .nest("/accounts", account::router()) .nest("/transactions", transactions::router()) .nest("/chats", chats::router()) .nest("/socket", socket::router()) -} - -pub async fn motd(EState(state): State) -> Json { - Json(state.motd.clone()) + .nest("/meta", meta::router()) } make_schemas!((); (ApiError, _ValidationErrors), [ bank_core::schemas, transactions::schemas]); diff --git a/src/config.rs b/src/config.rs index 7bab15b..2c9c614 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use std::net::{IpAddr, Ipv6Addr, SocketAddr}; +use bank_core::meta::{Bank, Motd}; use serde::Deserialize; #[derive(Deserialize)] @@ -13,8 +14,36 @@ pub struct Config { pub jwt_key: String, #[serde(default)] pub interop: Option, + #[serde(default = "default_meta")] + pub meta: Meta, +} + +#[derive(Deserialize)] +pub struct Meta { #[serde(default = "default_motd")] - pub motd: String, + pub motd: Motd, + #[serde(default = "default_bank")] + pub bank: Bank, +} + +fn default_meta() -> Meta { + Meta { + motd: default_motd(), + bank: default_bank(), + } +} + +fn default_motd() -> Motd { + Motd { + text: "bankserver-rs".into(), + } +} + +fn default_bank() -> Bank { + Bank { + name: "BankServer".into(), + currency: "CURR".into(), + } } #[derive(Deserialize)] @@ -24,10 +53,6 @@ pub struct InteropConfig { pub prefix: String, } -fn default_motd() -> String { - "bankserver-rs".into() -} - fn default_host() -> IpAddr { IpAddr::V6(Ipv6Addr::LOCALHOST) } diff --git a/src/main.rs b/src/main.rs index d0d16b7..31b00f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,7 @@ async fn main() { decoding_key, sockets: Arc::new(Sockets::new()), interop, - motd: bank_core::Motd { text: config.motd }, + meta: config.meta, })); let listener = TcpListener::bind(socket_addr).await.unwrap();