mycelium-api 8.3.1-rc.1

Provide API ports to the mycelium project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
mod callback_engines;
mod dispatchers;
mod dtos;
mod mcp;
mod middleware;
mod models;
mod modifiers;
mod openapi;
mod openapi_processor;
mod otel;
mod rest;
mod router;
mod rpc;
pub(crate) mod settings;

use crate::openapi_processor::initialize_tools_registry;

use actix_cors::Cors;
use actix_web::{
    dev::Service,
    middleware::{Logger, NormalizePath, TrailingSlash},
    web, App, HttpServer,
};
use actix_web_opentelemetry::RequestTracing;
use awc::{error::HeaderValue, Client};
use dispatchers::{
    email_dispatcher, services_health_dispatcher, webhook_dispatcher,
};
use models::config_handler::ConfigHandler;
use myc_adapters_shared_lib::models::{
    SharedAppModule, SharedClientImpl, SharedClientImplParameters,
    SharedClientProvider,
};
use myc_config::{
    init_vault_config_from_file, optional_config::OptionalConfig,
};
use myc_core::{
    domain::{
        dtos::callback::CallbackExecutor,
        entities::{
            GuestRoleRegistration, LocalMessageReading, LocalMessageWrite,
            RemoteMessageWrite, ServiceRead,
        },
    },
    use_cases::gateway::guest_roles::propagate_declared_roles_to_storage_engine,
};
use myc_diesel::repositories::{
    DieselDbPoolProvider, DieselDbPoolProviderParameters, SqlAppModule,
};
use myc_http_tools::settings::DEFAULT_REQUEST_ID_KEY;
use myc_kv::repositories::KVAppModule;
use myc_mem_db::repositories::{
    MemDbAppModule, MemDbPoolProvider, MemDbPoolProviderParameters,
};
use myc_notifier::{
    models::ClientProvider,
    repositories::{
        NotifierAppModule, NotifierClientImpl, NotifierClientImplParameters,
    },
};
use mycelium_base::utils::errors::MappedErrors;
use oauth2::http::HeaderName;
use openapi::ApiDoc;
use openssl::{
    pkey::PKey,
    ssl::{SslAcceptor, SslMethod},
    x509::X509,
};
use otel::initialize_otel;
use reqwest::header::{
    ACCEPT, ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_METHODS,
    ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_LENGTH, CONTENT_TYPE,
};
use rest::{
    index::heath_check_endpoints,
    manager::{
        account_endpoints as manager_account_endpoints,
        guest_role_endpoints as manager_guest_role_endpoints,
        tenant_endpoints as manager_tenant_endpoints,
    },
    openid::well_known_endpoints,
    role_scoped::configure as configure_standard_endpoints,
    service::tools_endpoints as service_tools_endpoints,
    shared::insert_role_header,
    staff::account_endpoints as staff_account_endpoints,
};
use router::route_request;
use settings::{ADMIN_API_SCOPE, TOOLS_API_SCOPE};
use shaku::HasComponent;
use std::{path::PathBuf, str::FromStr, sync::Arc, sync::Mutex};
use tracing::{info, trace, Instrument};
use tracing_actix_web::TracingLogger;
use utoipa::OpenApi;
use utoipa_redoc::{FileConfig, Redoc, Servable};
use utoipa_swagger_ui::{oauth, Config, SwaggerUi};
use uuid::Uuid;

#[tokio::main]
pub async fn main() -> std::io::Result<()> {
    // ? -----------------------------------------------------------------------
    // ? EXPORT THE UTOIPA_REDOC_CONFIG_FILE ENVIRONMENT VARIABLE
    //
    // The UTOIPA_REDOC_CONFIG_FILE environment variable should be exported
    // before the server starts. The variable should contain the path to the
    // redoc configuration file.
    //
    // ? -----------------------------------------------------------------------

    if let Err(err) = std::env::var("UTOIPA_REDOC_CONFIG_FILE") {
        trace!("Error on get env `UTOIPA_REDOC_CONFIG_FILE`: {err}");
        info!("Env variable `UTOIPA_REDOC_CONFIG_FILE` not set. Setting default value");

        unsafe {
            std::env::set_var(
                "UTOIPA_REDOC_CONFIG_FILE",
                "ports/api/src/openapi/redoc.config.json",
            );
        }
    }

    // ? -----------------------------------------------------------------------
    // ? INITIALIZE SERVICES CONFIGURATION
    //
    // All configurations for the core, ports, and adapters layers should exists
    // into the configuration file. Such file are loaded here.
    //
    // ? -----------------------------------------------------------------------
    info!("Initializing services configuration");

    let env_config_path = match std::env::var("SETTINGS_PATH") {
        Ok(path) => path,
        Err(err) => panic!("Error on get env `SETTINGS_PATH`: {err}"),
    };

    let config =
        match ConfigHandler::init_from_file(PathBuf::from(env_config_path)) {
            Ok(res) => res,
            Err(err) => panic!("Error on init config: {err}"),
        };

    let api_config = config.api.clone();

    // ? -----------------------------------------------------------------------
    // ? CONFIGURE LOGGING AND TELEMETRY
    //
    // The logging and telemetry configuration should be initialized before the
    // server starts. The configuration should be set to the server and the
    // server should be started.
    //
    // IMPORTANT: Does not remove the _guard variable from this context, because
    // it is used to keep the telemetry alive.
    //
    // ? -----------------------------------------------------------------------
    info!("Initializing Logging and Telemetry configuration");

    let _guard = initialize_otel(api_config.to_owned().logging)?;

    let span = tracing::Span::current();

    // ? -----------------------------------------------------------------------
    // ? INITIALIZE VAULT CONFIGURATION
    //
    // The vault configuration should be initialized before the server starts.
    // Vault configurations should be used to store sensitive data.
    //
    // ? -----------------------------------------------------------------------
    info!("Initializing Vault configs");

    init_vault_config_from_file(None, Some(config.vault.to_owned()))
        .instrument(span.to_owned())
        .await;

    // ? -----------------------------------------------------------------------
    // ? CONFIGURE INTERNAL DEPENDENCIES
    // ? -----------------------------------------------------------------------
    info!("Initialize internal dependencies");

    let (sql_module, shared_module, notifier_module, kv_module, mem_module) =
        initialize_modules(&config.to_owned())
            .await
            .map_err(|err| {
                tracing::error!("Error initializing modules: {err}");

                std::io::Error::new(std::io::ErrorKind::Other, err)
            })?;

    // ? -----------------------------------------------------------------------
    // ? PROPAGATE DECLARED ROLES TO THE SQL DATABASE
    //
    // The roles propagation was defined as part of the solution for issue #127:
    // - https://github.com/LepistaBioinformatics/mycelium/issues/127
    //
    // ? -----------------------------------------------------------------------
    info!("Propagate declared roles to the SQL database");

    propagate_declared_roles_to_storage_engine(
        Box::new(mem_module.resolve_ref() as &dyn ServiceRead),
        Box::new(sql_module.resolve_ref() as &dyn GuestRoleRegistration),
    )
    .instrument(span.to_owned())
    .await
    .map_err(|err| {
        tracing::error!(
            "Error propagating declared roles to the SQL database: {err}"
        );

        std::io::Error::new(std::io::ErrorKind::Other, err)
    })?;

    // ? -----------------------------------------------------------------------
    // ? INITIALIZE THE TOOLS REGISTRY
    //
    // The tools registry should be initialized before the server starts. The
    // registry should be used to store the tools for the tools endpoints.
    //
    // ? -----------------------------------------------------------------------
    info!("Initializing tools registry");

    let tools_registry_schema = initialize_tools_registry(mem_module.clone())
        .instrument(span.to_owned())
        .await
        .map_err(|err| {
            tracing::error!("Error initializing tools registry: {err}");

            std::io::Error::new(std::io::ErrorKind::Other, err)
        })?;

    // ? -----------------------------------------------------------------------
    // ? FIRE THE EMAIL DISPATCHER
    //
    // The email dispatcher should be fired to allow emails to be sent.
    // Dispatching will occur in a separate thread.
    //
    // ? -----------------------------------------------------------------------
    info!("Fire email dispatcher");

    email_dispatcher(
        config.queue.to_owned(),
        unsafe {
            Arc::from_raw(*Arc::new(
                sql_module.resolve_ref() as &dyn LocalMessageReading
            ))
        },
        unsafe {
            Arc::from_raw(*Arc::new(
                sql_module.resolve_ref() as &dyn LocalMessageWrite
            ))
        },
        unsafe {
            Arc::from_raw(*Arc::new(
                notifier_module.resolve_ref() as &dyn RemoteMessageWrite
            ))
        },
    )
    .instrument(span.to_owned())
    .await;

    // ? -----------------------------------------------------------------------
    // ? FIRE THE WEBHOOK DISPATCHER
    //
    // The webhook dispatcher should be fired to allow webhooks to be dispatched.
    // Dispatching will occur in a separate thread.
    //
    // ? -----------------------------------------------------------------------
    info!("Fire webhook dispatcher");

    webhook_dispatcher(config.core.to_owned(), sql_module.clone())
        .instrument(span.to_owned())
        .await;

    // ? -----------------------------------------------------------------------
    // ? FIRE THE SERVICES HEALTH DISPATCHER
    //
    // The services health dispatcher should be fired to allow the services
    // health to be checked.
    //
    // ? -----------------------------------------------------------------------
    info!("Fire services health dispatcher");

    services_health_dispatcher(config.api.clone(), mem_module.clone())
        .instrument(span.to_owned())
        .await;

    // ? -----------------------------------------------------------------------
    // ? CONFIGURE THE SERVER
    // ? -----------------------------------------------------------------------
    info!("Startup the server configuration");

    let server = HttpServer::new(move || {
        //
        // Here we should clone the config to avoid borrowing issues
        //
        let allowed_origins = config.api.allowed_origins.clone();
        let forward_api_config = config.api.clone();
        let auth_config = config.auth.clone();
        let token_config = config.core.account_life_cycle.clone();

        //
        // Configure the CORS policy
        //
        let cors = Cors::default()
            .allowed_origin_fn(move |origin, _| {
                allowed_origins
                    .contains(&origin.to_str().unwrap_or("").to_string())
            })
            .expose_headers(vec![
                ACCESS_CONTROL_ALLOW_CREDENTIALS,
                ACCESS_CONTROL_ALLOW_METHODS,
                ACCESS_CONTROL_ALLOW_ORIGIN,
                CONTENT_LENGTH,
                CONTENT_TYPE,
                ACCEPT,
                HeaderName::from_str(DEFAULT_REQUEST_ID_KEY).unwrap(),
            ])
            .allow_any_header()
            .allow_any_method()
            .max_age(3600);

        trace!("Configured Cors: {:?}", cors);

        // ? -------------------------------------------------------------------
        // ? OpenRPC discovery (server URLs from config / env)
        // ? -------------------------------------------------------------------
        let openrpc_spec_config =
            rpc::openrpc::OpenRpcSpecConfig::from_api_config(&config.api);

        // ? -------------------------------------------------------------------
        // ? Create the basis for the application
        // ? -------------------------------------------------------------------
        let base_app = App::new()
            //
            // Configure CORS policies
            //
            .wrap(cors)
            //
            // Normalize path
            //
            .wrap(NormalizePath::new(TrailingSlash::MergeOnly))
            //
            // Configure tracing and logging
            //
            .wrap(RequestTracing::default())
            .wrap(TracingLogger::default())
            //
            // Inject configuration
            //
            .app_data(web::Data::new(openrpc_spec_config))
            .app_data(web::Data::new(tools_registry_schema.clone()))
            .app_data(web::Data::new(token_config).clone())
            .app_data(web::Data::new(auth_config.to_owned()).clone())
            //
            // Inject modules
            //
            .app_data(web::Data::from(sql_module.clone()))
            .app_data(web::Data::from(shared_module.clone()))
            .app_data(web::Data::from(notifier_module.clone()))
            .app_data(web::Data::from(kv_module.clone()))
            .app_data(web::Data::from(mem_module.clone()))
            //
            // Index endpoints
            //
            // Index endpoints allow to check the status of the service
            //
            .service(
                web::scope("/health")
                    .configure(heath_check_endpoints::configure),
            )
            //
            // The well known openid configuration path
            //
            // This path is used to get the well known openid configuration
            // from the auth0 server.
            //
            .configure(well_known_endpoints::configure)
            //
            // Configure tools routes
            //
            // These endpoints allow users to identify the status of public
            // services.
            //
            .service(
                web::scope(TOOLS_API_SCOPE)
                    .configure(service_tools_endpoints::configure),
            )
            //
            // Configure API documentation
            //
            .service(Redoc::with_url_and_config(
                "/doc/redoc",
                ApiDoc::openapi(),
                FileConfig,
            ))
            .service(
                SwaggerUi::new("/doc/swagger/{_:.*}")
                    .url("/doc/openapi.json", ApiDoc::openapi())
                    .oauth(
                        oauth::Config::new()
                            .client_id("client-id")
                            .scopes(vec![String::from("openid")])
                            .use_pkce_with_authorization_code_grant(true),
                    )
                    .config(
                        Config::default()
                            .filter(true)
                            .show_extensions(true)
                            .persist_authorization(true)
                            .show_common_extensions(true)
                            .request_snippets_enabled(true),
                    ),
            )
            //
            // Configure anti-log elements
            //
            // Filter docs and common routes from the logs.
            //
            .wrap(
                Logger::default()
                    .exclude_regex("/health/*")
                    .exclude_regex("/doc/swagger/*")
                    .exclude_regex("/doc/redoc/*"),
            );

        // ? -------------------------------------------------------------------
        // ? CREATE THE ADMIN SCOPE
        //
        // Here you can find endpoints for the mycelium management (admin
        // scope). There include super users endpoints endpoints and role scoped
        // endpoints.
        //
        // ? -------------------------------------------------------------------
        let admin_scope = web::scope(ADMIN_API_SCOPE)
            //
            // Super Users
            //
            // Super user endpoints allow to perform manage the staff and
            // manager users actions, including determine new staffs and
            // managers.
            //
            .service(
                web::scope(rest::shared::UrlScope::Staffs.str())
                    //
                    // Inject a header to be collected by the
                    // MyceliumProfileData extractor.
                    //
                    // An empty role header was injected to allow only the
                    // super users with Staff status to access the staff
                    // endpoints.
                    //
                    .wrap_fn(|req, srv| {
                        let req = insert_role_header(req, vec![]);

                        srv.call(req)
                    })
                    //
                    // Configure endpoints
                    //
                    .configure(staff_account_endpoints::configure),
            )
            //
            // Manager Users
            //
            .service(
                web::scope(rest::shared::UrlScope::Managers.str())
                    //
                    // Inject a header to be collected by the
                    // MyceliumProfileData extractor.
                    //
                    // An empty role header was injected to allow only the
                    // super users with Managers status to access the
                    // managers endpoints.
                    //
                    .wrap_fn(|req, srv| {
                        let req = insert_role_header(req, vec![]);

                        srv.call(req)
                    })
                    //
                    // Configure endpoints
                    //
                    .configure(manager_tenant_endpoints::configure)
                    .configure(manager_guest_role_endpoints::configure)
                    .configure(manager_account_endpoints::configure),
            )
            //
            // JSON-RPC (single + batch at _adm/rpc)
            //
            .service(
                web::scope("rpc")
                    .wrap_fn(|req, srv| {
                        let req = insert_role_header(req, vec![]);

                        srv.call(req)
                    })
                    .configure(rpc::configure),
            )
            //
            // Role Scoped Endpoints
            //
            .configure(configure_standard_endpoints);

        // ? -------------------------------------------------------------------
        // ? CONFIGURE INTERNAL AUTHENTICATION
        // ? -------------------------------------------------------------------
        let final_app = match auth_config.internal {
            OptionalConfig::Enabled(config) => {
                //
                // Configure OAuth2 Scope
                //
                info!("Configuring Mycelium Internal authentication");
                base_app.app_data(web::Data::new(config.clone()))
            }
            _ => base_app,
        };

        // ? -------------------------------------------------------------------
        // ? CREATE THE GATEWAY SCOPE
        // ? -------------------------------------------------------------------
        final_app
            //
            // Configure admin routes
            //
            .service(admin_scope)
            //
            // Configure MCP server
            //
            // MCP (Model Context Protocol) exposes downstream service
            // APIs as AI-agent-invocable tools via JSON-RPC 2.0.
            // tools/call uses HTTP loopback through the gateway router
            // so auth, RBAC, and secret injection are fully reused.
            //
            .service(web::scope("/mcp").configure(mcp::endpoints::configure))
            //
            // Configure gateway routes
            //
            .app_data(web::Data::new(Client::new()))
            .app_data(web::Data::new(forward_api_config.to_owned()).clone())
            .wrap_fn(|mut req, srv| {
                req.headers_mut().insert(
                    HeaderName::from_str(DEFAULT_REQUEST_ID_KEY).unwrap(),
                    HeaderValue::from_str(Uuid::new_v4().to_string().as_str())
                        .unwrap(),
                );

                srv.call(req)
            })
            .default_service(web::to(route_request))
    });

    // ? -----------------------------------------------------------------------
    // ? FIRE THE SERVER
    // ? -----------------------------------------------------------------------

    let address = (
        api_config.to_owned().service_ip,
        api_config.to_owned().service_port,
    );

    info!("Listening on Address and Port: {:?}: ", address);

    // ? -----------------------------------------------------------------------
    // ? WITH TLS IF CONFIGURED
    // ? -----------------------------------------------------------------------
    if let OptionalConfig::Enabled(tls_config) = api_config.to_owned().tls {
        let api_config = api_config.clone();

        info!("Load TLS cert and key");

        // To create a self-signed temporary cert for testing:
        //
        // openssl req
        //     -x509 \
        //     -newkey rsa:4096 \
        //     -nodes \
        //     -keyout key.pem \
        //     -out cert.pem \
        //     -days 365 \
        //     -subj '/CN=localhost'
        //
        let mut builder =
            SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();

        //
        // Read the certificate content
        //
        let cert_pem = match tls_config.tls_cert.async_get_or_error().await {
            Ok(path) => path,
            Err(err) => panic!("Error on get TLS cert path: {err}"),
        };

        let cert = X509::from_pem(cert_pem.as_bytes())?;

        //
        // Read the certificate key
        //
        let key_pem = match tls_config.tls_key.async_get_or_error().await {
            Ok(path) => path,
            Err(err) => panic!("Error on get TLS key path: {err}"),
        };

        let key = PKey::private_key_from_pem(key_pem.as_bytes())?;

        //
        // Set the certificate and key
        //
        builder.set_certificate(&cert).unwrap();
        builder.set_private_key(&key).unwrap();

        info!("Fire the server with TLS");
        return server
            .bind_openssl(format!("{}:{}", address.0, address.1), builder)?
            .workers(api_config.service_workers.to_owned() as usize)
            .run()
            .await;
    }

    // ? -----------------------------------------------------------------------
    // ? WITHOUT TLS OTHERWISE
    // ? -----------------------------------------------------------------------
    info!("Fire the server without TLS");
    server
        .bind(address)?
        .workers(api_config.service_workers as usize)
        .run()
        .await
}

/// Initialize the modules for the application.
///
/// This function initializes the modules for the application based on the
/// configuration provided. The modules are
/// - SQL module
/// - Shared module
/// - Notifier module
/// - KV module
/// - MemDb module
///
/// The function returns a tuple of the initialized modules.
///
async fn initialize_modules(
    config: &ConfigHandler,
) -> Result<
    (
        Arc<SqlAppModule>,
        Arc<SharedAppModule>,
        Arc<NotifierAppModule>,
        Arc<KVAppModule>,
        Arc<MemDbAppModule>,
    ),
    MappedErrors,
> {
    let sql_module = Arc::new(
        SqlAppModule::builder()
            .with_component_parameters::<DieselDbPoolProvider>(
                DieselDbPoolProviderParameters {
                    pool: DieselDbPoolProvider::new(
                        &match config
                            .diesel
                            .database_url
                            .async_get_or_error()
                            .await
                        {
                            Ok(url) => url,
                            Err(err) => {
                                panic!("Error on get database url: {err}")
                            }
                        }
                        .as_str(),
                    ),
                },
            )
            .build(),
    );

    let shared_provider =
        match SharedClientImpl::new(config.redis.to_owned()).await {
            Ok(provider) => provider,
            Err(err) => panic!("Error on initialize shared provider: {err}"),
        };

    let shared_module = Arc::new(
        SharedAppModule::builder()
            .with_component_parameters::<SharedClientImpl>(
                SharedClientImplParameters {
                    redis_client: shared_provider.get_redis_client(),
                    redis_config: shared_provider.get_redis_config(),
                },
            )
            .build(),
    );

    let notifier_provider = match NotifierClientImpl::new(
        config.queue.to_owned(),
        config.redis.to_owned(),
        config.smtp.to_owned(),
    )
    .await
    {
        Ok(provider) => provider,
        Err(err) => panic!("Error on initialize notifier provider: {err}"),
    };

    let notifier_module = Arc::new(
        NotifierAppModule::builder()
            .with_component_parameters::<SharedClientImpl>(
                SharedClientImplParameters {
                    redis_client: shared_provider.get_redis_client(),
                    redis_config: shared_provider.get_redis_config(),
                },
            )
            .with_component_parameters::<NotifierClientImpl>(
                NotifierClientImplParameters {
                    smtp_client: notifier_provider.get_smtp_client(),
                    queue_config: notifier_provider.get_queue_config(),
                },
            )
            .build(),
    );

    let kv_module = Arc::new(
        KVAppModule::builder()
            .with_component_parameters::<SharedClientImpl>(
                SharedClientImplParameters {
                    redis_client: shared_provider.get_redis_client(),
                    redis_config: shared_provider.get_redis_config(),
                },
            )
            .build(),
    );

    for service in config.api.services.clone() {
        trace!("Service: {:?}", service);
    }

    // ? -----------------------------------------------------------------------
    // ? CREATE CALLBACK ENGINES FROM CONFIGURED CALLBACKS
    //
    // Convert callbacks configuration into executable engines that can be
    // injected and executed later.
    //
    // ? -----------------------------------------------------------------------
    let callbacks = config.api.to_owned().callbacks.clone().unwrap_or_default();

    let mut engines: Vec<Arc<dyn CallbackExecutor>> = Vec::new();

    for callback in &callbacks {
        match callback_engines::create_engine_from_callback(callback) {
            Ok(engine) => {
                tracing::debug!(
                    "Created engine for callback '{}' (type: {:?})",
                    callback.name,
                    callback.callback_type
                );

                engines.push(engine);
            }
            Err(e) => {
                tracing::warn!(
                    "Failed to create engine for callback '{}': {e}",
                    callback.name,
                );
            }
        }
    }

    let mem_module = Arc::new(
        MemDbAppModule::builder()
            .with_component_parameters::<MemDbPoolProvider>(
                MemDbPoolProviderParameters {
                    services_db: Arc::new(Mutex::new(
                        config.api.services.clone(),
                    )),
                    callbacks_db: Arc::new(Mutex::new(callbacks)),
                    engines: Arc::new(Mutex::new(engines)),
                    mode: Arc::new(Mutex::new(
                        config.api.to_owned().callback_execution_mode.clone(),
                    )),
                },
            )
            .build(),
    );

    Ok((
        sql_module,
        shared_module,
        notifier_module,
        kv_module,
        mem_module,
    ))
}