-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
570 additions
and
648 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2023 Nostr Development Kit Devs | ||
// Distributed under the MIT software license | ||
|
||
use axum::{ | ||
extract::{rejection::JsonRejection, FromRequest}, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use serde_json::json; | ||
|
||
#[derive(FromRequest)] | ||
#[from_request(via(axum::Json), rejection(AppError))] | ||
pub struct AppJson<T>(pub T); | ||
|
||
impl<T> IntoResponse for AppJson<T> | ||
where | ||
axum::Json<T>: IntoResponse, | ||
{ | ||
fn into_response(self) -> Response { | ||
axum::Json(self.0).into_response() | ||
} | ||
} | ||
|
||
pub enum AppError { | ||
// The request body contained invalid JSON | ||
JsonRejection(JsonRejection), | ||
} | ||
|
||
impl IntoResponse for AppError { | ||
fn into_response(self) -> Response { | ||
let (status, message) = match self { | ||
AppError::JsonRejection(rejection) => (rejection.status(), rejection.body_text()), | ||
}; | ||
|
||
( | ||
status, | ||
AppJson(json!({ | ||
"success": false, | ||
"code": status.as_u16(), | ||
"message": message, | ||
"data": {} | ||
})), | ||
) | ||
.into_response() | ||
} | ||
} | ||
|
||
impl From<JsonRejection> for AppError { | ||
fn from(rejection: JsonRejection) -> Self { | ||
Self::JsonRejection(rejection) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
// Copyright (c) 2023 Nostr Development Kit Devs | ||
// Distributed under the MIT software license | ||
|
||
use env_logger::{Builder, Env}; | ||
use log::Level; | ||
use tracing::Level; | ||
|
||
use super::Config; | ||
|
||
pub fn init(config: &Config) { | ||
let log_level: Level = if cfg!(debug_assertions) && config.log_level != Level::Trace { | ||
Level::Debug | ||
let log_level: Level = if cfg!(debug_assertions) && config.log_level != Level::TRACE { | ||
Level::DEBUG | ||
} else { | ||
config.log_level | ||
}; | ||
|
||
Builder::from_env(Env::default().default_filter_or(log_level.to_string())).init(); | ||
tracing_subscriber::fmt().with_max_level(log_level).init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters