mirror of
https://git.dirksys.ovh/dirk/bankserver.git
synced 2025-12-20 11:09:21 +01:00
refactor motd endpoint and add bank meta endpoint
This commit is contained in:
parent
27520f3337
commit
9b1368dd9c
@ -1,5 +1,6 @@
|
|||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod chat;
|
pub mod chat;
|
||||||
|
pub mod meta;
|
||||||
pub mod pagination;
|
pub mod pagination;
|
||||||
mod schemas;
|
mod schemas;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
@ -11,10 +12,4 @@ pub use error::*;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
pub use util::*;
|
pub use util::*;
|
||||||
|
|
||||||
make_schemas!((Credentials); (ApiError, TokenResponse, Motd), [account::schemas, chat::schemas, transaction::schemas, user::schemas]);
|
make_schemas!((Credentials); (ApiError, TokenResponse), [account::schemas, chat::schemas, transaction::schemas, user::schemas, meta::schemas]);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
||||||
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
|
|
||||||
pub struct Motd {
|
|
||||||
pub text: String,
|
|
||||||
}
|
|
||||||
|
|||||||
18
bank_core/src/meta.rs
Normal file
18
bank_core/src/meta.rs
Normal 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));
|
||||||
@ -5,14 +5,17 @@ info:
|
|||||||
version: 0.0.1
|
version: 0.0.1
|
||||||
tags:
|
tags:
|
||||||
- name: Authentication
|
- name: Authentication
|
||||||
|
- name: Meta
|
||||||
- name: Users
|
- name: Users
|
||||||
- name: Accounts
|
- name: Accounts
|
||||||
- name: Transactions
|
- name: Transactions
|
||||||
- name: Chats
|
- name: Chats
|
||||||
paths:
|
paths:
|
||||||
/api/motd:
|
/api/meta/motd:
|
||||||
get:
|
get:
|
||||||
operationId: motd
|
operationId: meta-motd
|
||||||
|
tags:
|
||||||
|
- Meta
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Motd
|
description: Motd
|
||||||
@ -22,6 +25,20 @@ paths:
|
|||||||
$ref: '#/components/schemas/Motd'
|
$ref: '#/components/schemas/Motd'
|
||||||
default:
|
default:
|
||||||
$ref: '#/components/responses/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:
|
/api/login:
|
||||||
post:
|
post:
|
||||||
operationId: login
|
operationId: login
|
||||||
|
|||||||
20
src/api/meta.rs
Normal file
20
src/api/meta.rs
Normal 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())
|
||||||
|
}
|
||||||
@ -8,7 +8,7 @@ use axum::{
|
|||||||
routing::get,
|
routing::get,
|
||||||
};
|
};
|
||||||
|
|
||||||
use bank_core::{ApiError, Motd, make_schemas, pagination::RequestPagination};
|
use bank_core::{ApiError, make_schemas, pagination::RequestPagination};
|
||||||
use jsonwebtoken::{DecodingKey, EncodingKey};
|
use jsonwebtoken::{DecodingKey, EncodingKey};
|
||||||
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||||
use tracing_error::SpanTrace;
|
use tracing_error::SpanTrace;
|
||||||
@ -22,12 +22,15 @@ mod auth;
|
|||||||
mod chats;
|
mod chats;
|
||||||
mod docs;
|
mod docs;
|
||||||
mod interop;
|
mod interop;
|
||||||
|
mod meta;
|
||||||
mod socket;
|
mod socket;
|
||||||
mod transactions;
|
mod transactions;
|
||||||
mod user;
|
mod user;
|
||||||
|
|
||||||
pub use interop::router as interop_router;
|
pub use interop::router as interop_router;
|
||||||
|
|
||||||
|
use crate::config::Meta;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default)]
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
pub struct Json<T>(T);
|
pub struct Json<T>(T);
|
||||||
|
|
||||||
@ -270,7 +273,7 @@ pub struct AppState {
|
|||||||
pub decoding_key: DecodingKey,
|
pub decoding_key: DecodingKey,
|
||||||
pub sockets: Arc<Sockets>,
|
pub sockets: Arc<Sockets>,
|
||||||
pub interop: Option<InteropState>,
|
pub interop: Option<InteropState>,
|
||||||
pub motd: Motd,
|
pub meta: Meta,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InteropState {
|
pub struct InteropState {
|
||||||
@ -292,17 +295,13 @@ pub type State = axum::extract::State<Arc<AppState>>;
|
|||||||
pub fn router() -> Router<Arc<AppState>> {
|
pub fn router() -> Router<Arc<AppState>> {
|
||||||
Router::new()
|
Router::new()
|
||||||
.merge(auth::router())
|
.merge(auth::router())
|
||||||
.route("/motd", get(motd))
|
|
||||||
.nest("/users", user::router())
|
.nest("/users", user::router())
|
||||||
.nest("/docs", docs::router())
|
.nest("/docs", docs::router())
|
||||||
.nest("/accounts", account::router())
|
.nest("/accounts", account::router())
|
||||||
.nest("/transactions", transactions::router())
|
.nest("/transactions", transactions::router())
|
||||||
.nest("/chats", chats::router())
|
.nest("/chats", chats::router())
|
||||||
.nest("/socket", socket::router())
|
.nest("/socket", socket::router())
|
||||||
}
|
.nest("/meta", meta::router())
|
||||||
|
|
||||||
pub async fn motd(EState(state): State) -> Json<Motd> {
|
|
||||||
Json(state.motd.clone())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
make_schemas!((); (ApiError, _ValidationErrors), [ bank_core::schemas, transactions::schemas]);
|
make_schemas!((); (ApiError, _ValidationErrors), [ bank_core::schemas, transactions::schemas]);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||||
|
|
||||||
|
use bank_core::meta::{Bank, Motd};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -13,8 +14,36 @@ pub struct Config {
|
|||||||
pub jwt_key: String,
|
pub jwt_key: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub interop: Option<InteropConfig>,
|
pub interop: Option<InteropConfig>,
|
||||||
|
#[serde(default = "default_meta")]
|
||||||
|
pub meta: Meta,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Meta {
|
||||||
#[serde(default = "default_motd")]
|
#[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)]
|
#[derive(Deserialize)]
|
||||||
@ -24,10 +53,6 @@ pub struct InteropConfig {
|
|||||||
pub prefix: String,
|
pub prefix: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_motd() -> String {
|
|
||||||
"bankserver-rs".into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_host() -> IpAddr {
|
fn default_host() -> IpAddr {
|
||||||
IpAddr::V6(Ipv6Addr::LOCALHOST)
|
IpAddr::V6(Ipv6Addr::LOCALHOST)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,7 +60,7 @@ async fn main() {
|
|||||||
decoding_key,
|
decoding_key,
|
||||||
sockets: Arc::new(Sockets::new()),
|
sockets: Arc::new(Sockets::new()),
|
||||||
interop,
|
interop,
|
||||||
motd: bank_core::Motd { text: config.motd },
|
meta: config.meta,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let listener = TcpListener::bind(socket_addr).await.unwrap();
|
let listener = TcpListener::bind(socket_addr).await.unwrap();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user