Skip to content

Commit

Permalink
Merge branch 'main' into feature/share-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
daavidrgz committed Oct 26, 2024
2 parents 0b96e48 + eae3e6c commit c55c8b4
Show file tree
Hide file tree
Showing 95 changed files with 10,519 additions and 119 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
**/target/
**/node_modules/
**/.astro
crates/web/frontend/pkg
**Dockerfile
docker-compose.yml
Expand Down

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

This file was deleted.

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

This file was deleted.

4 changes: 3 additions & 1 deletion crates/server/justfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# If there is a .env file located at `crates/server/.env`, it will be loaded automatically
set dotenv-load

# https://github.com/launchbadge/sqlx/blob/main/sqlx-cli/README.md
# The DATABASE_URL env var must be set to run those commands
# If there is a .env file located at `crates/server/.env`, it will be loaded automatically
prepare-check:
cargo sqlx prepare --check --workspace
prepare: # To be able to build the project in offline mode
cargo sqlx prepare --workspace
migrate:
cargo sqlx migrate run
run:
cargo run -p gq-server
7 changes: 5 additions & 2 deletions crates/server/migrations/20241006174524_init.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
-- Add migration script here
CREATE TYPE data_type AS ENUM('json', 'yaml');

CREATE TABLE share (
id UUID PRIMARY KEY,
json text NOT NULL,
input_data text NOT NULL,
input_type data_type NOT NULL,
output_type data_type NOT NULL,
query text NOT NULL,
expires_at TIMESTAMPTZ NOT NULL
);
2 changes: 2 additions & 0 deletions crates/server/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod dtos;
pub mod routes;
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions crates/server/src/api/dtos/share_dto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::model::share::{DataType, Share};

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

impl From<Share> for ShareDTO {
fn from(share: Share) -> Self {
Self {
id: share.id,
input_data: share.input_data,
input_type: share.input_type.into(),
output_type: share.output_type.into(),
query: share.query,
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum DataTypeDTO {
Json,
Yaml,
}

impl From<DataType> for DataTypeDTO {
fn from(data_type: DataType) -> Self {
match data_type {
DataType::Json => Self::Json,
DataType::Yaml => Self::Yaml,
}
}
}

impl From<DataTypeDTO> for DataType {
fn from(data_type: DataTypeDTO) -> Self {
match data_type {
DataTypeDTO::Json => Self::Json,
DataTypeDTO::Yaml => Self::Yaml,
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::dtos::share_dto::ShareDTO;
use crate::routes;
use crate::services::share::GetShareError;
use crate::{
dtos::error_object::ErrorObject,
services::share::{CreateShareError, ShareService},
AppState,
};
use crate::api::dtos::error_object::ErrorObject;
use crate::api::dtos::share_dto::{DataTypeDTO, ShareDTO};
use crate::services::share::{CreateShareError, GetShareError, ShareService};

use crate::api::routes;
use crate::app_state::AppState;

use axum::{
extract::{Path, State},
http::{HeaderMap, StatusCode},
Expand All @@ -21,7 +20,9 @@ pub const SHARES_CONTEXT: &str = "/shares";
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CreateShareRequest {
json: String,
input_data: String,
input_type: DataTypeDTO,
output_type: DataTypeDTO,
query: String,
expiration_time_secs: i64,
}
Expand All @@ -47,8 +48,16 @@ async fn create_share(
State(share_service): State<ShareService>,
Json(request): Json<CreateShareRequest>,
) -> impl IntoResponse {
// TODO: refactor CreateShareRequest into a model CreateShareRequest and a CreateShareRequestDTO
// so the create_share method does not contain that much attributes
let create_result = share_service
.create_share(request.json, request.query, request.expiration_time_secs)
.create_share(
request.input_data,
request.input_type.into(),
request.output_type.into(),
request.query,
request.expiration_time_secs,
)
.await;

let share_id = match create_result {
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions crates/server/src/dtos/share_dto.rs

This file was deleted.

9 changes: 4 additions & 5 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use app_state::AppState;
use axum::Router;
use sqlx::PgPool;
use state::AppState;

pub mod dtos;
pub mod api;
pub mod app_state;
pub mod model;
pub mod routes;
pub mod services;
pub mod state;

pub fn app(db_connection: PgPool, max_share_expiration_time_secs: i64) -> Router {
let app_state = AppState::new(db_connection, max_share_expiration_time_secs);
routes::router(app_state)
api::routes::router(app_state)
}
14 changes: 9 additions & 5 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ async fn main() {

let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let database_connections = env::var("DATABASE_CONNECTIONS")
.map(|s| s.parse().expect("DATABASE_CONNECTIONS must be a number"))
.map(|s| {
s.parse()
.unwrap_or_else(|_| panic!("DATABASE_CONNECTIONS must be a number. Got {s}"))
})
.unwrap_or(5);
let db_connection = PgPoolOptions::new()
.max_connections(database_connections)
Expand All @@ -31,8 +34,9 @@ async fn main() {

let max_share_expiration_time_secs = env::var("MAX_SHARE_EXPIRATION_TIME_SECS")
.map(|s| {
s.parse()
.expect("MAX_SHARE_EXPIRATION_TIME_SECS must be a number")
s.parse().unwrap_or_else(|_| {
panic!("MAX_SHARE_EXPIRATION_TIME_SECS must be a number. Got {s}")
})
})
.unwrap_or(24 * 7)
* 60
Expand All @@ -51,9 +55,9 @@ async fn main() {
let app = gq_server::app(db_connection, max_share_expiration_time_secs);
let listener = tokio::net::TcpListener::bind(addr)
.await
.expect("Failed to bind address {addr}");
.unwrap_or_else(|_| panic!("Failed to bind address {addr}"));

tracing::info!("Server started. Listening on: {addr}");
tracing::info!("Server started. Listening on {addr}");
axum::serve(listener, app)
.await
.expect("Failed to start server");
Expand Down
Loading

0 comments on commit c55c8b4

Please sign in to comment.