Skip to content

Commit

Permalink
feat: initial share implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgehermo9 committed Oct 9, 2024
1 parent d3e660f commit eaf1789
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 45 deletions.
126 changes: 122 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ sqlx = { version = "0.8.2", features = [
chrono = { version = "0.4.38", features = ["serde"] }
http-serde = "2.1.1"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
1 change: 1 addition & 0 deletions crates/server/src/dto.rs → crates/server/src/dtos.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod error_object;
pub mod share_dto;
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ impl ErrorObject {
trace_id: Uuid::now_v7(),
}
}

pub fn new_internal_error() -> Self {
Self::new(
"Internal server error".to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
)
}
}

impl IntoResponse for ErrorObject {
Expand Down
22 changes: 22 additions & 0 deletions crates/server/src/dtos/share_dto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::model::share::Share;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShareDTO {
pub id: Uuid,
pub json: String,
pub query: String,
}

impl From<Share> for ShareDTO {
fn from(share: Share) -> Self {
Self {
id: share.id,
json: share.json,
query: share.query,
}
}
}
2 changes: 1 addition & 1 deletion crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::Router;
use sqlx::PgPool;
use state::AppState;

pub mod dto;
pub mod dtos;
pub mod model;
pub mod routes;
pub mod services;
Expand Down
18 changes: 15 additions & 3 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use std::{env, net::SocketAddr};

use sqlx::postgres::PgPoolOptions;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[tokio::main]
async fn main() {
// TODO: configure tracing
// TODO: background vacuum job

let env_filter = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();
tracing_subscriber::registry()
.with(fmt::layer())
.with(env_filter)
.init();

let port = env::var("PORT").unwrap_or_else(|_| "3000".to_string());
let addr = SocketAddr::from(([0, 0, 0, 0], port.parse().unwrap()));

Expand All @@ -20,8 +31,9 @@ async fn main() {

let max_share_expiration_time_secs = env::var("MAX_SHARE_EXPIRATION_TIME_SECS")
.map(|s| s.parse().unwrap())
// Defaults to 1 week
.unwrap_or(24 * 7);
.unwrap_or(24 * 7)
* 60
* 60;

assert!(
max_share_expiration_time_secs > 0,
Expand Down
35 changes: 33 additions & 2 deletions crates/server/src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use axum::Router;

use crate::AppState;
use axum::{http::StatusCode, Router};

mod shares;

Expand All @@ -9,3 +8,35 @@ pub fn router(app_state: AppState) -> Router {
.nest(shares::SHARES_CONTEXT, shares::router())
.with_state(app_state)
}

// use macro and not a function so the logs are generated from the caller's module path
macro_rules! build_error_response {
($error:expr) => {{
let original_error = format!("{:?}", $error);
let original_error_message = $error.to_string();
let error_object = ErrorObject::from($error);

if error_object.code.is_server_error() {
tracing::error!(
status_code = error_object.code.as_u16(),
trace_id = %error_object.trace_id,
timestamp = error_object.timestamp.to_rfc3339(),
original_error = original_error,
original_error_message = original_error_message,
"Returned HTTP server error response"
);
} else {
tracing::info!(
status_code = error_object.code.as_u16(),
trace_id = %error_object.trace_id,
timestamp = error_object.timestamp.to_rfc3339(),
original_error = original_error,
original_error_message = original_error_message,
"Returned HTTP error response"
);
}
error_object.into_response()
}};
}

pub(crate) use build_error_response;
Loading

0 comments on commit eaf1789

Please sign in to comment.