refactor: adapters configured from config crate
This commit is contained in:
@@ -5,7 +5,6 @@ use anyhow::Context;
|
||||
use tokio::net::TcpListener;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
use application::config::AppConfig;
|
||||
use composition::Repositories;
|
||||
use export::ExportAdapter;
|
||||
use importer::ImporterDocumentParser;
|
||||
@@ -21,15 +20,15 @@ async fn main() -> anyhow::Result<()> {
|
||||
dotenvy::dotenv().ok();
|
||||
init_tracing();
|
||||
|
||||
let (state, ap_router) = wire_dependencies()
|
||||
let config = config::load().context("Failed to load configuration")?;
|
||||
|
||||
let (state, ap_router) = wire_dependencies(&config)
|
||||
.await
|
||||
.context("Failed to wire dependencies")?;
|
||||
|
||||
let app = openapi::serve(routes::build_router(state, ap_router));
|
||||
|
||||
let host = std::env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
|
||||
let port = std::env::var("PORT").unwrap_or_else(|_| "3000".to_string());
|
||||
let addr = format!("{}:{}", host, port);
|
||||
let addr = format!("{}:{}", config.server.host, config.server.port);
|
||||
let listener = TcpListener::bind(&addr).await?;
|
||||
tracing::info!("Listening on {}", addr);
|
||||
axum::serve(
|
||||
@@ -41,24 +40,24 @@ async fn main() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn wire_dependencies() -> anyhow::Result<(AppState, axum::Router)> {
|
||||
let app_config = AppConfig::from_env();
|
||||
async fn wire_dependencies(config: &config::AppConfig) -> anyhow::Result<(AppState, axum::Router)> {
|
||||
let app_config = application::config::AppConfig::from(config);
|
||||
let instance = domain::value_objects::InstanceIdentity::new(app_config.base_url.clone());
|
||||
let database_url = std::env::var("DATABASE_URL").context("DATABASE_URL must be set")?;
|
||||
let database_url = config.database.url.as_str();
|
||||
composition::factory::warn_if_database_backend_set();
|
||||
|
||||
let (auth_service, password_hasher) = composition::factory::build_auth_adapters()?;
|
||||
let metadata_client = composition::factory::build_metadata_client()?;
|
||||
let poster_fetcher = composition::factory::build_poster_fetcher()?;
|
||||
let object_storage = composition::factory::build_object_storage()?;
|
||||
let (auth_service, password_hasher) = composition::factory::build_auth_adapters(&config.auth)?;
|
||||
let metadata_client = composition::factory::build_metadata_client(&config.metadata)?;
|
||||
let poster_fetcher = composition::factory::build_poster_fetcher(&config.metadata)?;
|
||||
let object_storage = composition::factory::build_object_storage(&config.storage)?;
|
||||
|
||||
let db = composition::factory::build_database_adapters(&database_url, &instance).await?;
|
||||
let db = composition::factory::build_database_adapters(database_url, &instance).await?;
|
||||
#[cfg(feature = "federation")]
|
||||
let ap_content_repo = db.ap_content;
|
||||
let db_pool = db.db_pool;
|
||||
|
||||
// Wire up event channel, federation service, and ap_router
|
||||
let event_bus = EventBusBackend::from_env()?;
|
||||
let event_bus = EventBusBackend::from_config(&config.events)?;
|
||||
|
||||
#[cfg(feature = "federation")]
|
||||
let (
|
||||
@@ -78,7 +77,7 @@ async fn wire_dependencies() -> anyhow::Result<(AppState, axum::Router)> {
|
||||
}
|
||||
};
|
||||
|
||||
let ep = create_event_publisher(event_bus, &db_pool).await?;
|
||||
let ep = create_event_publisher(event_bus, &db_pool, config).await?;
|
||||
|
||||
let ap = activitypub::wire(activitypub::ActivityPubDeps {
|
||||
activity_repo: fed_repos.activity,
|
||||
@@ -100,6 +99,7 @@ async fn wire_dependencies() -> anyhow::Result<(AppState, axum::Router)> {
|
||||
follow_query: Arc::clone(&fed_repos.follow_query),
|
||||
instance: instance.clone(),
|
||||
allow_registration: app_config.allow_registration,
|
||||
federation: config.federation.clone(),
|
||||
event_publisher: Arc::clone(&ep),
|
||||
})
|
||||
.await?;
|
||||
@@ -136,7 +136,7 @@ async fn wire_dependencies() -> anyhow::Result<(AppState, axum::Router)> {
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "federation"))]
|
||||
let event_publisher_arc = create_event_publisher(event_bus, &db_pool).await?;
|
||||
let event_publisher_arc = create_event_publisher(event_bus, &db_pool, config).await?;
|
||||
#[cfg(not(feature = "federation"))]
|
||||
let ap_router = axum::Router::new();
|
||||
#[cfg(not(feature = "federation"))]
|
||||
@@ -252,9 +252,7 @@ async fn wire_dependencies() -> anyhow::Result<(AppState, axum::Router)> {
|
||||
|
||||
let state = AppState {
|
||||
app_ctx,
|
||||
rss_renderer: Arc::new(RssAdapter::new(
|
||||
std::env::var("BASE_URL").unwrap_or_else(|_| "http://localhost:3000".into()),
|
||||
)),
|
||||
rss_renderer: Arc::new(RssAdapter::new(config.server.base_url.clone())),
|
||||
};
|
||||
Ok((state, ap_router))
|
||||
}
|
||||
@@ -262,19 +260,20 @@ async fn wire_dependencies() -> anyhow::Result<(AppState, axum::Router)> {
|
||||
async fn create_event_publisher(
|
||||
event_bus: EventBusBackend,
|
||||
db_pool: &composition::DbPool,
|
||||
config: &config::AppConfig,
|
||||
) -> anyhow::Result<Arc<dyn EventPublisher>> {
|
||||
match event_bus {
|
||||
EventBusBackend::Db => {
|
||||
tracing::info!("event bus: DB queue");
|
||||
Ok(match db_pool {
|
||||
composition::DbPool::Sqlite(pool) => {
|
||||
sqlite::SqliteEventQueue::create_publisher(pool.clone()).await?
|
||||
sqlite::SqliteEventQueue::create_publisher(pool.clone(), &config.events).await?
|
||||
}
|
||||
})
|
||||
}
|
||||
#[cfg(feature = "nats")]
|
||||
EventBusBackend::Nats => {
|
||||
let cfg = nats::NatsConfig::from_env()
|
||||
let cfg = nats::NatsConfig::from_config(&config.nats)
|
||||
.context("EVENT_BUS_BACKEND=nats requires NATS_URL to be set")?;
|
||||
tracing::info!("event bus: NATS ({})", cfg.url);
|
||||
Ok(nats::create_publisher(cfg).await?)
|
||||
|
||||
Reference in New Issue
Block a user