Skip to content

Commit

Permalink
fix: cleanup structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zk-steve committed Mar 6, 2024
1 parent dbd449d commit f5b6fa1
Show file tree
Hide file tree
Showing 39 changed files with 304 additions and 4,918 deletions.
2,741 changes: 0 additions & 2,741 deletions Cargo.lock

This file was deleted.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[workspace]
resolver = "2"
members = ["src/public"]
members = [
"src/public",
]
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Using the `rust-musl-builder` as base image, instead of
# the official Rust toolchain
FROM clux/muslrust:stable AS chef
FROM --platform=linux/amd64 clux/muslrust:stable AS chef
USER root
RUN cargo install cargo-chef
WORKDIR /app
Expand All @@ -21,13 +21,13 @@ RUN cargo build --release --target x86_64-unknown-linux-musl --bin cli

FROM scratch AS prod
WORKDIR /user
COPY config/00-default.toml 00-default.toml
COPY src/public/config/00-default.toml 00-default.toml
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/cli /usr/local/bin/rust-server
ENTRYPOINT ["/usr/local/bin/rust-server", "--config-path=*.toml"]

FROM alpine AS dev
WORKDIR /user
COPY config/00-default.toml 00-default.toml
COPY src/public/config/00-default.toml 00-default.toml
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/cli /usr/local/bin/rust-server
COPY --from=bunyan /root/.cargo/bin/bunyan /usr/local/bin/
ENTRYPOINT ["/bin/sh"]
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POSTGRES_DIR="./library/src/adapter/repositories/postgres"
POSTGRES_DIR="./src/adapter/src/repositories/postgres"
DATABASE_URL="postgres://postgres:[email protected]:5432/postgres"

PKG_NAME=rust-api-server
Expand Down Expand Up @@ -35,5 +35,5 @@ build:
build-dev:
BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker build --ssh default -t $(PKG_NAME):$(BUILD_VERSION) --target=dev .

profiling:
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./config/* -c deploy/local/custom.toml
profiling-public:
CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph --root -- -c ./src/public/config/* -c ./deploy/local/custom.toml
7 changes: 3 additions & 4 deletions src/adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ version = "0.1.0"
edition = "2021"

[dependencies]
rust-core = { path = "../core" }
rust_core = { path = "../core" }
async-trait = { version = "0.1.77" }
tokio = { version = "1.2", features = ["full"] }
diesel = { version = "2.1.0", features = [
tokio = { version = "1.36.0", features = ["full"] }
diesel = { version = "2.1.4", features = [
"postgres",
"postgres_backend",
"uuid",
Expand All @@ -16,4 +16,3 @@ serde = { version = "1.0", features = ["derive"] }
deadpool-diesel = { version = "0.5.0", features = ["postgres", "serde"] }
diesel_migrations = "2.1.0"
testcontainers-modules = { version = "0.3.4", features = ["postgres"] }
futures = "*"
9 changes: 5 additions & 4 deletions src/adapter/src/repositories/in_memory/question.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use async_trait::async_trait;
use rust_core::common::errors::Error;
use rust_core::common::errors::Error::NotFound;
use rust_core::entities::question::{QuestionEntity, QuestionId};
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use tokio::sync::RwLock;

use rust_core::common::errors::Error;
use rust_core::common::errors::Error::NotFound;
use rust_core::entities::question::{QuestionEntity, QuestionId};
use rust_core::entities::question_filter::QuestionFilter;
use rust_core::ports::question::QuestionPort;

Expand Down
1 change: 1 addition & 0 deletions src/adapter/src/repositories/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod in_memory;
pub mod postgres;
pub mod repository_test;
12 changes: 0 additions & 12 deletions src/adapter/src/repositories/postgres/models/id.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/adapter/src/repositories/postgres/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod id;
pub mod question;
8 changes: 4 additions & 4 deletions src/adapter/src/repositories/postgres/models/question.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::time::SystemTime;

use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
use rust_core::entities::question::QuestionEntity;
use serde::Serialize;
use std::time::SystemTime;

use super::id::ToId;
use rust_core::entities::question::QuestionEntity;

#[derive(Debug, Queryable, Serialize, Selectable, Insertable, AsChangeset, Identifiable)]
#[diesel(table_name = super::super::schema::questions)]
Expand All @@ -23,7 +23,7 @@ pub struct QuestionModel {
impl QuestionModel {
pub fn from(entity: QuestionEntity) -> Self {
QuestionModel {
id: entity.id.to_id(),
id: entity.id.to_string().parse().unwrap(),
title: entity.title,
content: entity.content,
tags: entity.tags.map(|v| v.into_iter().map(Some).collect()),
Expand Down
21 changes: 10 additions & 11 deletions src/adapter/src/repositories/postgres/question_db.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::repositories::postgres::models::question::QuestionModel;
use crate::repositories::postgres::schema::questions::dsl::questions;
use crate::repositories::postgres::schema::questions::id;
use async_trait::async_trait;
use rust_core::common::errors::Error;
use rust_core::entities::question::{QuestionEntity, QuestionId};
use rust_core::entities::question_filter::QuestionFilter;
use rust_core::ports::question::QuestionPort;

use deadpool_diesel::postgres::Pool;
use diesel::{delete, insert_into, QueryDsl, RunQueryDsl, SelectableHelper};
use diesel::{update, ExpressionMethods};
use diesel_migrations::{embed_migrations, EmbeddedMigrations};

use super::models::id::ToId;
use rust_core::common::errors::Error;
use rust_core::entities::question::{QuestionEntity, QuestionId};
use rust_core::entities::question_filter::QuestionFilter;
use rust_core::ports::question::QuestionPort;

use crate::repositories::postgres::models::question::QuestionModel;
use crate::repositories::postgres::schema::questions::dsl::questions;
use crate::repositories::postgres::schema::questions::id;

// NOTE: path relative to Cargo.toml
pub const MIGRATIONS: EmbeddedMigrations =
Expand Down Expand Up @@ -73,7 +72,7 @@ impl QuestionPort for QuestionDBRepository {
}

async fn delete(&self, question_id: &QuestionId) -> Result<(), Error> {
let question_id = question_id.to_id();
let question_id: i32 = question_id.to_string().parse().unwrap();
self.db
.get()
.await
Expand All @@ -93,7 +92,7 @@ impl QuestionPort for QuestionDBRepository {
}

async fn get(&self, question_id: &QuestionId) -> Result<QuestionEntity, Error> {
let question_id = question_id.to_id();
let question_id: i32 = question_id.to_string().parse().unwrap();
self.db
.get()
.await
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#[cfg(test)]
mod tests {
use adapter::repositories::{
in_memory::question::QuestionInMemoryRepository,
postgres::question_db::{QuestionDBRepository, MIGRATIONS},
use std::collections::HashMap;
use std::{str::FromStr, sync::Arc};

use deadpool_diesel::{
postgres::{Pool, Runtime},
Manager,
};
use diesel_migrations::MigrationHarness;
use testcontainers_modules::{postgres::Postgres, testcontainers::clients::Cli};

use rust_core::{
entities::{
Expand All @@ -14,14 +19,8 @@ mod tests {
ports::question::QuestionPort,
};

use deadpool_diesel::{
postgres::{Pool, Runtime},
Manager,
};
use diesel_migrations::MigrationHarness;
use std::collections::HashMap;
use std::{str::FromStr, sync::Arc};
use testcontainers_modules::{postgres::Postgres, testcontainers::clients::Cli};
use crate::repositories::in_memory::question::QuestionInMemoryRepository;
use crate::repositories::postgres::question_db::{QuestionDBRepository, MIGRATIONS};

struct DatabaseConfig {
url: String,
Expand Down
1 change: 0 additions & 1 deletion src/adapter/tests/mod.rs

This file was deleted.

17 changes: 17 additions & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "common"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
opentelemetry-semantic-conventions = { version = "0.14.0" }
config = "0.14.0"
glob = "0.3.1"
tracing = "0.1"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing-opentelemetry = "0.23.0"
tracing-bunyan-formatter = "0.3.9"
opentelemetry = { version = "0.22.0" }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.15.0", features = ["tonic"] }
2 changes: 2 additions & 0 deletions src/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod loggers;
pub mod options;
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions src/common/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use config::ConfigError::Message;
use config::{Config, ConfigError, Environment, File};
use glob::glob;
use serde::Deserialize;

pub fn parse_options<'de, T: Deserialize<'de>>(
config_paths: Vec<String>,
) -> Result<T, ConfigError> {
let mut config = Config::builder();

for path in &config_paths {
let paths = glob(path).map_err(|e| Message(e.to_string()))?;
for entry in paths {
let entry = entry.map_err(|e| Message(e.to_string()))?;
config = config.add_source(File::from(entry));
}
}

let config = config
.add_source(
Environment::default()
.separator("__")
.list_separator(",")
.ignore_empty(true),
)
.build()?;

config.try_deserialize()
}

/// Represents logging configuration.
#[derive(Debug, Deserialize, Clone)]
pub struct Log {
/// Log level.
pub level: String,
}

/// Default logging configuration.
pub fn default_log() -> Log {
Log {
level: "INFO".to_string(),
}
}
2 changes: 1 addition & 1 deletion src/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "rust-core"
name = "rust_core"
version = "0.1.0"
edition = "2021"

Expand Down
6 changes: 4 additions & 2 deletions src/core/src/entities/pagination_entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::common::errors::Error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::common::errors::Error;

/// Represents pagination parameters.
#[derive(Debug, Serialize, Deserialize)]
pub struct PaginationEntity {
Expand Down
13 changes: 11 additions & 2 deletions src/core/src/entities/question.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::entities::entity::Entity;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::io::{Error, ErrorKind};
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::entities::entity::Entity;

/// Identifier for a question.
#[derive(Debug, Serialize, Deserialize, Eq, Hash, PartialEq, Clone)]
pub struct QuestionId(pub String);
Expand Down Expand Up @@ -44,6 +47,12 @@ impl FromStr for QuestionId {
}
}

impl fmt::Display for QuestionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

/// Represents a question entity.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct QuestionEntity {
Expand Down
6 changes: 4 additions & 2 deletions src/core/src/entities/question_filter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::common::errors::Error;
use crate::entities::filter_entity::FilterEntity;
use crate::entities::pagination_entity::PaginationEntity;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents filters for querying questions.
#[derive(Debug, Serialize, Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/ports/question.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use async_trait::async_trait;

use crate::common::errors::Error;
use crate::entities::question::{QuestionEntity, QuestionId};
use crate::entities::question_filter::QuestionFilter;
use async_trait::async_trait;

#[async_trait]
pub trait QuestionPort {
Expand Down
Loading

0 comments on commit f5b6fa1

Please sign in to comment.