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
// globals.rs
//! # Global Configuration Module
//!
//! This module provides global variables for the StackQL server configuration.
//! It manages the global host, port, and connection string settings using `OnceCell` for safe, single initialization.
//!
//! ## Features
//! - Stores global server configuration values (`host`, `port`, `connection_string`) using `OnceCell`.
//! - Provides initialization functions to set global values (`init_globals`).
//! - Exposes getter functions for retrieving configured global values from other modules.
//!
//! ## Example Usage
//! ```rust
//! use crate::globals::{init_globals, server_host, server_port, connection_string};
//!
//! fn setup() {
//! init_globals("localhost".to_string(), 5444);
//! println!("Host: {}", server_host());
//! println!("Port: {}", server_port());
//! println!("Connection String: {}", connection_string());
//! }
//! ```
use OnceCell;
use crate;
// ============================
// Global Static Variables
// ============================
/// Stores the global server host.
///
/// The server host is initialized via the `init_globals` function and is only set once per application lifetime.
static STACKQL_SERVER_HOST: = new;
/// Stores the global server port.
///
/// The server port is initialized via the `init_globals` function and is only set once per application lifetime.
static STACKQL_SERVER_PORT: = new;
/// Stores the global connection string used for database connections.
///
/// This string is generated using the `init_globals` function based on the provided host and port.
static STACKQL_CONNECTION_STRING: = new;
// ============================
// Initialization Function
// ============================
/// Initializes the global variables for host, port, and connection string.
///
/// This function must be called once before accessing global values via getter functions.
/// It uses `OnceCell` to ensure each value is only initialized once.
///
/// # Arguments
/// - `host` - The server host address as a `String`.
/// - `port` - The server port as a `u16`.
///
/// # Example
/// ```rust
/// use crate::globals::init_globals;
/// init_globals("localhost".to_string(), 5444);
/// ```
// ============================
// Getter Functions
// ============================
/// Retrieves the configured global server host.
///
/// If the host is not set via `init_globals`, it returns the default value from `app`.
///
/// # Returns
/// - `&'static str` - The configured server host or the default host.
///
/// # Example
/// ```rust
/// use crate::globals::{init_globals, server_host};
/// init_globals("localhost".to_string(), 5444);
/// assert_eq!(server_host(), "localhost");
/// ```
/// Retrieves the configured global server port.
///
/// If the port is not set via `init_globals`, it returns the default value from `app`.
///
/// # Returns
/// - `u16` - The configured server port or the default port.
///
/// # Example
/// ```rust
/// use crate::globals::{init_globals, server_port};
/// init_globals("localhost".to_string(), 5444);
/// assert_eq!(server_port(), 5444);
/// ```