Add configuration loading and error handling
Introduces a new `config` module for loading application settings from environment variables and a `users.toml` file. An `error` module is added to handle application-specific errors gracefully. An `.env.example` file is created to show available configuration options, and `users.toml.example` provides a template for user data. The main function now initializes the configuration and prints the server port.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use serde_json::json;
|
||||
|
||||
pub enum AppError {
|
||||
Unauthorized,
|
||||
BadRequest(String),
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, message) = match &self {
|
||||
Self::Unauthorized => (StatusCode::UNAUTHORIZED, "Unauthorized".to_owned()),
|
||||
Self::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
|
||||
Self::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
|
||||
};
|
||||
|
||||
let body = axum::Json(json!({ "error": message }));
|
||||
(status, body).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for AppError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Self::Internal(err.to_string())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user