Skip to content

Commit

Permalink
Remove rustfmt.toml and use defaults (#436)
Browse files Browse the repository at this point in the history
* Remove rustfmt.toml and use defaults

* Use allow(dead_code) with not used variables
  • Loading branch information
ndelvalle authored Jul 14, 2024
1 parent acb3ad8 commit c78396c
Show file tree
Hide file tree
Showing 25 changed files with 942 additions and 945 deletions.
6 changes: 0 additions & 6 deletions rustfmt.toml

This file was deleted.

70 changes: 35 additions & 35 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
use axum::http::header;
use axum::Router;
use tower_http::{
compression::CompressionLayer, cors::CorsLayer, propagate_header::PropagateHeaderLayer,
sensitive_headers::SetSensitiveHeadersLayer, trace,
compression::CompressionLayer, cors::CorsLayer, propagate_header::PropagateHeaderLayer,
sensitive_headers::SetSensitiveHeadersLayer, trace,
};

use crate::logger;
use crate::models;
use crate::routes;

pub async fn create_app() -> Router {
logger::setup();
logger::setup();

models::sync_indexes()
.await
.expect("Failed to sync database indexes");
models::sync_indexes()
.await
.expect("Failed to sync database indexes");

Router::new()
.merge(routes::status::create_route())
.merge(routes::user::create_route())
.merge(Router::new().nest(
"/v1",
// All public v1 routes will be nested here.
Router::new().merge(routes::cat::create_route()),
))
// High level logging of requests and responses
.layer(
trace::TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().include_headers(true))
.on_request(trace::DefaultOnRequest::new().level(tracing::Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
)
// Mark the `Authorization` request header as sensitive so it doesn't
// show in logs.
.layer(SetSensitiveHeadersLayer::new(std::iter::once(
header::AUTHORIZATION,
)))
// Compress responses
.layer(CompressionLayer::new())
// Propagate `X-Request-Id`s from requests to responses
.layer(PropagateHeaderLayer::new(header::HeaderName::from_static(
"x-request-id",
)))
// CORS configuration. This should probably be more restrictive in
// production.
.layer(CorsLayer::permissive())
Router::new()
.merge(routes::status::create_route())
.merge(routes::user::create_route())
.merge(Router::new().nest(
"/v1",
// All public v1 routes will be nested here.
Router::new().merge(routes::cat::create_route()),
))
// High level logging of requests and responses
.layer(
trace::TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().include_headers(true))
.on_request(trace::DefaultOnRequest::new().level(tracing::Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
)
// Mark the `Authorization` request header as sensitive so it doesn't
// show in logs.
.layer(SetSensitiveHeadersLayer::new(std::iter::once(
header::AUTHORIZATION,
)))
// Compress responses
.layer(CompressionLayer::new())
// Propagate `X-Request-Id`s from requests to responses
.layer(PropagateHeaderLayer::new(header::HeaderName::from_static(
"x-request-id",
)))
// CORS configuration. This should probably be more restrictive in
// production.
.layer(CorsLayer::permissive())
}
18 changes: 9 additions & 9 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use crate::settings::SETTINGS;
static CONNECTION: OnceCell<Database> = OnceCell::const_new();

pub async fn connection() -> &'static Database {
CONNECTION
.get_or_init(|| async {
let db_uri = SETTINGS.database.uri.as_str();
let db_name = SETTINGS.database.name.as_str();
CONNECTION
.get_or_init(|| async {
let db_uri = SETTINGS.database.uri.as_str();
let db_name = SETTINGS.database.name.as_str();

mongodb::Client::with_uri_str(db_uri)
mongodb::Client::with_uri_str(db_uri)
.await
.expect("Failed to initialize MongoDB connection")
.database(db_name)
})
.await
.expect("Failed to initialize MongoDB connection")
.database(db_name)
})
.await
}
120 changes: 62 additions & 58 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,87 +11,91 @@ use wither::WitherError;
#[derive(thiserror::Error, Debug)]
#[error("...")]
pub enum Error {
#[error("{0}")]
Wither(#[from] WitherError),
#[error("{0}")]
Wither(#[from] WitherError),

#[error("{0}")]
Mongo(#[from] MongoError),
#[error("{0}")]
Mongo(#[from] MongoError),

#[error("Error parsing ObjectID {0}")]
ParseObjectID(String),
#[error("Error parsing ObjectID {0}")]
ParseObjectID(String),

#[error("{0}")]
SerializeMongoResponse(#[from] bson::de::Error),
#[error("{0}")]
SerializeMongoResponse(#[from] bson::de::Error),

#[error("{0}")]
Authenticate(#[from] AuthenticateError),
#[error("{0}")]
Authenticate(#[from] AuthenticateError),

#[error("{0}")]
BadRequest(#[from] BadRequest),
#[error("{0}")]
BadRequest(#[from] BadRequest),

#[error("{0}")]
NotFound(#[from] NotFound),
#[error("{0}")]
NotFound(#[from] NotFound),

#[error("{0}")]
RunSyncTask(#[from] JoinError),
#[error("{0}")]
RunSyncTask(#[from] JoinError),

#[error("{0}")]
HashPassword(#[from] BcryptError),
#[error("{0}")]
HashPassword(#[from] BcryptError),
}

impl Error {
fn get_codes(&self) -> (StatusCode, u16) {
match *self {
// 4XX Errors
Error::ParseObjectID(_) => (StatusCode::BAD_REQUEST, 40001),
Error::BadRequest(_) => (StatusCode::BAD_REQUEST, 40002),
Error::NotFound(_) => (StatusCode::NOT_FOUND, 40003),
Error::Authenticate(AuthenticateError::WrongCredentials) => (StatusCode::UNAUTHORIZED, 40004),
Error::Authenticate(AuthenticateError::InvalidToken) => (StatusCode::UNAUTHORIZED, 40005),
Error::Authenticate(AuthenticateError::Locked) => (StatusCode::LOCKED, 40006),

// 5XX Errors
Error::Authenticate(AuthenticateError::TokenCreation) => {
(StatusCode::INTERNAL_SERVER_ERROR, 5001)
}
Error::Wither(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5002),
Error::Mongo(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5003),
Error::SerializeMongoResponse(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5004),
Error::RunSyncTask(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5005),
Error::HashPassword(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5006),
fn get_codes(&self) -> (StatusCode, u16) {
match *self {
// 4XX Errors
Error::ParseObjectID(_) => (StatusCode::BAD_REQUEST, 40001),
Error::BadRequest(_) => (StatusCode::BAD_REQUEST, 40002),
Error::NotFound(_) => (StatusCode::NOT_FOUND, 40003),
Error::Authenticate(AuthenticateError::WrongCredentials) => {
(StatusCode::UNAUTHORIZED, 40004)
}
Error::Authenticate(AuthenticateError::InvalidToken) => {
(StatusCode::UNAUTHORIZED, 40005)
}
Error::Authenticate(AuthenticateError::Locked) => (StatusCode::LOCKED, 40006),

// 5XX Errors
Error::Authenticate(AuthenticateError::TokenCreation) => {
(StatusCode::INTERNAL_SERVER_ERROR, 5001)
}
Error::Wither(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5002),
Error::Mongo(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5003),
Error::SerializeMongoResponse(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5004),
Error::RunSyncTask(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5005),
Error::HashPassword(_) => (StatusCode::INTERNAL_SERVER_ERROR, 5006),
}
}
}

pub fn bad_request() -> Self {
Error::BadRequest(BadRequest {})
}
pub fn bad_request() -> Self {
Error::BadRequest(BadRequest {})
}

pub fn not_found() -> Self {
Error::NotFound(NotFound {})
}
pub fn not_found() -> Self {
Error::NotFound(NotFound {})
}
}

impl IntoResponse for Error {
fn into_response(self) -> Response {
let (status_code, code) = self.get_codes();
let message = self.to_string();
let body = Json(json!({ "code": code, "message": message }));
fn into_response(self) -> Response {
let (status_code, code) = self.get_codes();
let message = self.to_string();
let body = Json(json!({ "code": code, "message": message }));

(status_code, body).into_response()
}
(status_code, body).into_response()
}
}

#[derive(thiserror::Error, Debug)]
#[error("...")]
pub enum AuthenticateError {
#[error("Wrong authentication credentials")]
WrongCredentials,
#[error("Failed to create authentication token")]
TokenCreation,
#[error("Invalid authentication credentials")]
InvalidToken,
#[error("User is locked")]
Locked,
#[error("Wrong authentication credentials")]
WrongCredentials,
#[error("Failed to create authentication token")]
TokenCreation,
#[error("Invalid authentication credentials")]
InvalidToken,
#[error("User is locked")]
Locked,
}

#[derive(thiserror::Error, Debug)]
Expand Down
14 changes: 7 additions & 7 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::env;
use crate::settings::SETTINGS;

pub fn setup() {
if env::var_os("RUST_LOG").is_none() {
let app_name = env::var("CARGO_PKG_NAME").unwrap();
let level = SETTINGS.logger.level.as_str();
let env = format!("{app_name }={level},tower_http={level}");
if env::var_os("RUST_LOG").is_none() {
let app_name = env::var("CARGO_PKG_NAME").unwrap();
let level = SETTINGS.logger.level.as_str();
let env = format!("{app_name }={level},tower_http={level}");

env::set_var("RUST_LOG", env);
}
env::set_var("RUST_LOG", env);
}

tracing_subscriber::fmt::init();
tracing_subscriber::fmt::init();
}
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use settings::SETTINGS;

#[tokio::main]
async fn main() {
let app = app::create_app().await;
let app = app::create_app().await;

let port = SETTINGS.server.port;
let address = SocketAddr::from(([127, 0, 0, 1], port));
let port = SETTINGS.server.port;
let address = SocketAddr::from(([127, 0, 0, 1], port));

info!("Server listening on {}", &address);
axum::Server::bind(&address)
.serve(app.into_make_service())
.await
.expect("Failed to start server");
info!("Server listening on {}", &address);
axum::Server::bind(&address)
.serve(app.into_make_service())
.await
.expect("Failed to start server");
}
64 changes: 32 additions & 32 deletions src/models/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,48 @@ impl ModelExt for Cat {}
#[derive(Debug, Clone, Serialize, Deserialize, WitherModel, Validate)]
#[model(index(keys = r#"doc!{ "user": 1, "created_at": 1 }"#))]
pub struct Cat {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub user: ObjectId,
pub name: String,
pub updated_at: Date,
pub created_at: Date,
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub user: ObjectId,
pub name: String,
pub updated_at: Date,
pub created_at: Date,
}

impl Cat {
pub fn new(user: ObjectId, name: String) -> Self {
let now = date::now();
Self {
id: None,
user,
name,
updated_at: now,
created_at: now,
pub fn new(user: ObjectId, name: String) -> Self {
let now = date::now();
Self {
id: None,
user,
name,
updated_at: now,
created_at: now,
}
}
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PublicCat {
#[serde(alias = "_id", serialize_with = "serialize_object_id_as_hex_string")]
pub id: ObjectId,
#[serde(serialize_with = "serialize_object_id_as_hex_string")]
pub user: ObjectId,
pub name: String,
#[serde(with = "bson_datetime_as_rfc3339_string")]
pub updated_at: Date,
#[serde(with = "bson_datetime_as_rfc3339_string")]
pub created_at: Date,
#[serde(alias = "_id", serialize_with = "serialize_object_id_as_hex_string")]
pub id: ObjectId,
#[serde(serialize_with = "serialize_object_id_as_hex_string")]
pub user: ObjectId,
pub name: String,
#[serde(with = "bson_datetime_as_rfc3339_string")]
pub updated_at: Date,
#[serde(with = "bson_datetime_as_rfc3339_string")]
pub created_at: Date,
}

impl From<Cat> for PublicCat {
fn from(cat: Cat) -> Self {
Self {
id: cat.id.unwrap(),
user: cat.user,
name: cat.name.clone(),
updated_at: cat.updated_at,
created_at: cat.created_at,
fn from(cat: Cat) -> Self {
Self {
id: cat.id.unwrap(),
user: cat.user,
name: cat.name.clone(),
updated_at: cat.updated_at,
created_at: cat.created_at,
}
}
}
}
Loading

0 comments on commit c78396c

Please sign in to comment.