molten_api/state.rs
1//! Defines the shared application state for the Molten API.
2//!
3//! This module provides the `AppState` struct, which holds common resources
4//! such as the database connection and service clients, making them
5//! accessible to all request handlers.
6use molten_service::{DocumentService, FormService, WorkflowService};
7use sea_orm::DatabaseConnection;
8use std::sync::Arc;
9
10/// The shared state accessible by all request handlers.
11/// We wrap it in Arc for cheap cloning across threads.
12#[derive(Clone)]
13pub struct AppState {
14 /// Database Connection
15 pub db: DatabaseConnection,
16 /// Smart pointer to document orchestration service
17 pub document_service: Arc<DocumentService>,
18 /// Smart pointer to form orchestration service
19 pub form_service: Arc<FormService>,
20 /// Smart pointer to workflow orchestration service
21 pub workflow_service: Arc<WorkflowService>,
22}
23
24impl AppState {
25 /// Creates a new instance of `AppState`, initializing all shared services
26 /// with the provided database connection.
27 ///
28 /// # Arguments
29 /// * `db` - A `DatabaseConnection` to be used by the services.
30 ///
31 /// # Returns
32 /// A new `AppState` instance.
33 pub fn new(db: DatabaseConnection) -> Self {
34 let document_service = DocumentService::new(db.clone());
35 let form_service = FormService::new(db.clone());
36 let workflow_service = WorkflowService::new(db.clone());
37 Self {
38 db,
39 document_service: Arc::new(document_service),
40 form_service: Arc::new(form_service),
41 workflow_service: Arc::new(workflow_service),
42 }
43 }
44}