refactor motd endpoint and add bank meta endpoint

This commit is contained in:
DSeeLP 2025-04-17 09:07:31 +02:00
parent 27520f3337
commit 9b1368dd9c
7 changed files with 96 additions and 22 deletions

View File

@ -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]);

18
bank_core/src/meta.rs Normal file
View File

@ -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));

View File

@ -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

20
src/api/meta.rs Normal file
View File

@ -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<Arc<AppState>> {
Router::new()
.route("/motd", get(motd))
.route("/bank", get(bank))
}
pub async fn motd(EState(state): State) -> Json<Motd> {
Json(state.meta.motd.clone())
}
pub async fn bank(EState(state): State) -> Json<Bank> {
Json(state.meta.bank.clone())
}

View File

@ -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>(T);
@ -270,7 +273,7 @@ pub struct AppState {
pub decoding_key: DecodingKey,
pub sockets: Arc<Sockets>,
pub interop: Option<InteropState>,
pub motd: Motd,
pub meta: Meta,
}
pub struct InteropState {
@ -292,17 +295,13 @@ pub type State = axum::extract::State<Arc<AppState>>;
pub fn router() -> Router<Arc<AppState>> {
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<Motd> {
Json(state.motd.clone())
.nest("/meta", meta::router())
}
make_schemas!((); (ApiError, _ValidationErrors), [ bank_core::schemas, transactions::schemas]);

View File

@ -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<InteropConfig>,
#[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)
}

View File

@ -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();