Skip to content

Commit

Permalink
chore: Rename/refactor (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
szinn authored Aug 15, 2024
1 parent 99dd2b8 commit 18ca71a
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 66 deletions.
38 changes: 25 additions & 13 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ resolver = "2"
members = [
"crates/rust-arch",
"crates/arch-api",
"crates/arch-core",
"crates/arch-core-models",
"crates/arch-db",
"crates/arch-domain-api",
"crates/arch-domain-core",
"crates/arch-domain-models",
"crates/arch-utils",
]

Expand All @@ -21,8 +22,10 @@ rust-version = "1.80"

[workspace.dependencies]
arch-api = { path = "crates/arch-api" }
arch-core = { path = "crates/arch-core" }
arch-db = { path = "crates/arch-db" }
arch-domain-api = { path = "crates/arch-domain-api" }
arch-domain-core = { path = "crates/arch-domain-core" }
arch-domain-models = { path = "crates/arch-domain-models" }
arch-utils = { path = "crates/arch-utils" }

async-trait = "0.1.81"
Expand Down
3 changes: 2 additions & 1 deletion crates/arch-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ rust-version.workspace = true
publish = false

[dependencies]
arch-core.workspace = true
arch-domain-api.workspace = true
arch-utils.workspace = true

thiserror.workspace = true
tokio.workspace = true
Expand Down
7 changes: 3 additions & 4 deletions crates/arch-api/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{net::SocketAddr, sync::Arc};

use arch_core::ArchService;

use arch_domain_api::ArchApi;
use axum::response::{IntoResponse, Response};
use hyper::StatusCode;
use tokio::net::TcpListener;
Expand All @@ -12,13 +11,13 @@ use crate::ApiError;
pub(crate) mod handlers;
pub(crate) mod health;

pub async fn start_server(port: u16, arch_service: Arc<ArchService>, subsys: SubsystemHandle) -> Result<(), ApiError> {
pub async fn start_server(port: u16, arch_api: Arc<ArchApi>, subsys: SubsystemHandle) -> Result<(), ApiError> {
tracing::trace!("Starting http service");

let addr: SocketAddr = format!("0.0.0.0:{}", port).parse().map_err(|_| ApiError::BadPort(port))?;
let listener = TcpListener::bind(addr).await.unwrap();

let routes = handlers::get_routes(arch_service.clone());
let routes = handlers::get_routes(arch_api.clone());

tracing::info!("Listening on port {}", port);
loop {
Expand Down
6 changes: 3 additions & 3 deletions crates/arch-api/src/http/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ApiError;
use std::{net::SocketAddr, sync::Arc, time::Duration};

use arch_core::ArchService;
use arch_domain_api::ArchApi;
use axum::{extract::Request, routing::get, Router};
use hyper::body::Incoming;
use hyper_util::rt::TokioIo;
Expand All @@ -12,10 +12,10 @@ use tower_http::timeout::TimeoutLayer;

use super::health;

pub fn get_routes(arch_service: Arc<ArchService>) -> Router<()> {
pub fn get_routes(arch_api: Arc<ArchApi>) -> Router<()> {
axum::Router::new()
.route("/api/v1/health", get(health::health))
.with_state(arch_service.health_service.clone())
.with_state(arch_api.health_api.clone())
.layer(TimeoutLayer::new(Duration::from_secs(2)))
}

Expand Down
11 changes: 5 additions & 6 deletions crates/arch-api/src/http/health.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::sync::Arc;

use arch_core::HealthService;
use arch_domain_api::HealthApi;
use arch_utils::arcbox::ArcBox;
use axum::{extract::State, response::IntoResponse};
use hyper::StatusCode;

#[tracing::instrument(level = "trace", skip(health_service))]
pub(crate) async fn health(State(health_service): State<Arc<Box<dyn HealthService>>>) -> impl IntoResponse {
match health_service.is_healthy().await {
#[tracing::instrument(level = "trace", skip(health_api))]
pub(crate) async fn health(State(health_api): State<ArcBox<dyn HealthApi>>) -> impl IntoResponse {
match health_api.is_healthy().await {
true => StatusCode::OK,
false => StatusCode::BAD_REQUEST,
}
Expand Down
23 changes: 0 additions & 23 deletions crates/arch-core/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "arch-core"
name = "arch-domain-api"
version.workspace = true
edition.workspace = true
authors.workspace = true
Expand All @@ -16,4 +16,3 @@ arch-utils.workspace = true

async-trait.workspace = true
thiserror.workspace = true
tracing.workspace = true
File renamed without changes.
6 changes: 6 additions & 0 deletions crates/arch-domain-api/src/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use async_trait::async_trait;

#[async_trait]
pub trait HealthApi: Send + Sync {
async fn is_healthy(&self) -> bool;
}
11 changes: 11 additions & 0 deletions crates/arch-domain-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod health;

pub mod error;
pub use error::Error;

use arch_utils::arcbox::ArcBox;
pub use health::HealthApi;

pub struct ArchApi {
pub health_api: ArcBox<dyn HealthApi>,
}
20 changes: 20 additions & 0 deletions crates/arch-domain-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "arch-domain-core"
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
readme.workspace = true
rust-version.workspace = true
publish = false

[dependencies]
arch-db.workspace = true
arch-domain-api.workspace = true
arch-utils.workspace = true

async-trait.workspace = true
thiserror.workspace = true
tracing.workspace = true
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
use std::sync::Arc;

use arch_db::DatabaseRepository;
use arch_domain_api::HealthApi;
use async_trait::async_trait;

#[async_trait]
pub trait HealthService: Send + Sync {
async fn is_healthy(&self) -> bool;
}

#[derive(Clone)]
pub(crate) struct HealthServiceImpl {
pub(crate) struct HealthService {
repository: Arc<DatabaseRepository>,
}

impl HealthServiceImpl {
impl HealthService {
pub(crate) fn new(repository: Arc<DatabaseRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl HealthService for HealthServiceImpl {
impl HealthApi for HealthService {
#[tracing::instrument(level = "trace", skip(self))]
async fn is_healthy(&self) -> bool {
let db_healthy = !self.repository.database.get_postgres_connection_pool().is_closed();
Expand Down
16 changes: 16 additions & 0 deletions crates/arch-domain-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::sync::Arc;

use arch_db::DatabaseRepository;
use arch_domain_api::{ArchApi, Error, HealthApi};
use arch_utils::{arcbox, arcbox::ArcBox};
use health::HealthService;

mod health;

#[tracing::instrument(level = "trace", skip(database))]
pub async fn create_service(database: Arc<DatabaseRepository>) -> Result<ArchApi, Error> {
let health_service = HealthService::new(database.clone());
let health_api: ArcBox<dyn HealthApi> = arcbox!(health_service);

Ok(ArchApi { health_api })
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "arch-core-models"
name = "arch-domain-models"
version.workspace = true
edition.workspace = true
authors.workspace = true
Expand All @@ -8,5 +8,6 @@ homepage.workspace = true
repository.workspace = true
readme.workspace = true
rust-version.workspace = true
publish = false

[dependencies]
File renamed without changes.
2 changes: 1 addition & 1 deletion crates/rust-arch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ config = "0.14.0"
log = "0.4.22"

arch-api.workspace = true
arch-core.workspace = true
arch-domain-core.workspace = true
arch-db.workspace = true

serde.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-arch/src/bin/rust-arch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{sync::Arc, time::Duration};

use arch_api::http::start_server;
use arch_core::create_service;

use anyhow::{Context, Result};
use arch_db::create_database_connection;
use arch_domain_core::create_service;
use rust_arch::{
args::{self, Args},
config::RustArchConfig,
Expand Down

0 comments on commit 18ca71a

Please sign in to comment.