diff --git a/bank_core/src/lib.rs b/bank_core/src/lib.rs index 4f13060..26859cd 100644 --- a/bank_core/src/lib.rs +++ b/bank_core/src/lib.rs @@ -8,6 +8,13 @@ pub use schemas::*; mod error; mod util; pub use error::*; +use serde::{Deserialize, Serialize}; pub use util::*; -make_schemas!((Credentials); (ApiError, TokenResponse), [account::schemas, chat::schemas, transaction::schemas, user::schemas]); +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, +} diff --git a/openapi-def.yaml b/openapi-def.yaml index 784530a..98be9a5 100644 --- a/openapi-def.yaml +++ b/openapi-def.yaml @@ -10,6 +10,18 @@ tags: - name: Transactions - name: Chats paths: + /motd: + get: + operationId: motd + responses: + 200: + description: Motd + content: + application/json: + schema: + $ref: '#/components/schemas/Motd' + default: + $ref: '#/components/responses/Default' /api/login: post: operationId: login diff --git a/src/api/auth.rs b/src/api/auth.rs index d23ad08..762f6fe 100644 --- a/src/api/auth.rs +++ b/src/api/auth.rs @@ -105,7 +105,7 @@ impl<'a> Claims<'a> { fn invalid_username_or_password() -> ApiError<'static> { ApiError::new( StatusCode::FORBIDDEN, - "invalid_username_or_password", + "auth.invalid_credentials", "Invalid username or password", ) } diff --git a/src/api/mod.rs b/src/api/mod.rs index 5767366..e566222 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -5,9 +5,10 @@ use axum::{ extract::{FromRequest, FromRequestParts, Request}, http::{StatusCode, request::Parts}, response::IntoResponse, + routing::get, }; -use bank_core::{ApiError, make_schemas, pagination::RequestPagination}; +use bank_core::{ApiError, Motd, make_schemas, pagination::RequestPagination}; use jsonwebtoken::{DecodingKey, EncodingKey}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use tracing_error::SpanTrace; @@ -269,6 +270,7 @@ pub struct AppState { pub decoding_key: DecodingKey, pub sockets: Arc, pub interop: Option, + pub motd: Motd, } pub struct InteropState { @@ -290,6 +292,7 @@ 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()) @@ -298,6 +301,10 @@ pub fn router() -> Router> { .nest("/socket", socket::router()) } +pub async fn motd(EState(state): State) -> Json { + Json(state.motd.clone()) +} + make_schemas!((); (ApiError, _ValidationErrors), [ bank_core::schemas, transactions::schemas]); #[derive(Deserialize)] diff --git a/src/config.rs b/src/config.rs index 3c836a7..7bab15b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,8 @@ pub struct Config { pub jwt_key: String, #[serde(default)] pub interop: Option, + #[serde(default = "default_motd")] + pub motd: String, } #[derive(Deserialize)] @@ -22,6 +24,10 @@ 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 38d01f6..2c8b0ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,7 @@ async fn main() { decoding_key, sockets: Arc::new(Sockets::new()), interop, + motd: bank_core::Motd { text: config.motd }, })); let listener = TcpListener::bind(socket_addr).await.unwrap();