Skip to content

Commit

Permalink
fix: Create db connection up a level (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
szinn authored Aug 13, 2024
1 parent 4764071 commit 6486b58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 6 additions & 6 deletions crates/arch-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use arch_db::create_database_connection;
use std::sync::Arc;

use arch_db::DatabaseRepository;
use arch_utils::{arcbox, arcbox::ArcBox};
use error::Error;
use health::HealthServiceImpl;
Expand All @@ -12,11 +14,9 @@ pub struct ArchService {
pub health_service: ArcBox<dyn HealthService>,
}

#[tracing::instrument(level = "trace", skip(database_url))]
pub async fn create_service(database_url: &str) -> Result<ArchService, Error> {
let repository = create_database_connection(database_url).await?;

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

Ok(ArchService { health_service })
Expand Down
7 changes: 6 additions & 1 deletion crates/rust-arch/src/bin/rust-arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use arch_api::http::start_server;
use arch_core::create_service;

use anyhow::{Context, Result};
use arch_db::create_database_connection;
use rust_arch::{
args::{self, Args},
config::RustArchConfig,
Expand All @@ -24,7 +25,11 @@ async fn main() -> Result<()> {
let git_revision = env!("BUILD_GIT_HASH");
tracing::info!("RustArch {}-{}", crate_version, git_revision);

let arch_service = Arc::new(create_service(&config.database.database_url).await.context("Couldn't create service")?);
let database = create_database_connection(&config.database.database_url)
.await
.context("Couldn't connect to database")?;

let arch_service = Arc::new(create_service(database).await.context("Couldn't create service")?);

let server = Toplevel::new(|s| async move {
s.start(SubsystemBuilder::new("http_api", |h| start_server(3000, arch_service, h)));
Expand Down

0 comments on commit 6486b58

Please sign in to comment.