spikard 0.4.0-x64-mingw-ucrt
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +1 -0
- data/README.md +659 -0
- data/ext/spikard_rb/Cargo.toml +17 -0
- data/ext/spikard_rb/extconf.rb +10 -0
- data/ext/spikard_rb/src/lib.rs +6 -0
- data/lib/spikard/app.rb +405 -0
- data/lib/spikard/background.rb +27 -0
- data/lib/spikard/config.rb +396 -0
- data/lib/spikard/converters.rb +13 -0
- data/lib/spikard/handler_wrapper.rb +113 -0
- data/lib/spikard/provide.rb +214 -0
- data/lib/spikard/response.rb +173 -0
- data/lib/spikard/schema.rb +243 -0
- data/lib/spikard/sse.rb +111 -0
- data/lib/spikard/streaming_response.rb +44 -0
- data/lib/spikard/testing.rb +221 -0
- data/lib/spikard/upload_file.rb +131 -0
- data/lib/spikard/version.rb +5 -0
- data/lib/spikard/websocket.rb +59 -0
- data/lib/spikard.rb +43 -0
- data/sig/spikard.rbs +366 -0
- data/vendor/bundle/ruby/3.4.0/gems/diff-lcs-1.6.2/mise.toml +5 -0
- data/vendor/bundle/ruby/3.4.0/gems/rake-compiler-dock-1.10.0/build/buildkitd.toml +2 -0
- data/vendor/crates/spikard-bindings-shared/Cargo.toml +63 -0
- data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +139 -0
- data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +561 -0
- data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +194 -0
- data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +246 -0
- data/vendor/crates/spikard-bindings-shared/src/error_response.rs +403 -0
- data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +274 -0
- data/vendor/crates/spikard-bindings-shared/src/lib.rs +25 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +298 -0
- data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +637 -0
- data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +309 -0
- data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +248 -0
- data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +355 -0
- data/vendor/crates/spikard-bindings-shared/tests/comprehensive_coverage.rs +502 -0
- data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +389 -0
- data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +413 -0
- data/vendor/crates/spikard-core/Cargo.toml +40 -0
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -0
- data/vendor/crates/spikard-core/src/debug.rs +63 -0
- data/vendor/crates/spikard-core/src/di/container.rs +726 -0
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -0
- data/vendor/crates/spikard-core/src/di/error.rs +118 -0
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -0
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -0
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -0
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -0
- data/vendor/crates/spikard-core/src/di/value.rs +283 -0
- data/vendor/crates/spikard-core/src/errors.rs +39 -0
- data/vendor/crates/spikard-core/src/http.rs +153 -0
- data/vendor/crates/spikard-core/src/lib.rs +29 -0
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -0
- data/vendor/crates/spikard-core/src/metadata.rs +397 -0
- data/vendor/crates/spikard-core/src/parameters.rs +723 -0
- data/vendor/crates/spikard-core/src/problem.rs +310 -0
- data/vendor/crates/spikard-core/src/request_data.rs +189 -0
- data/vendor/crates/spikard-core/src/router.rs +249 -0
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -0
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -0
- data/vendor/crates/spikard-core/src/validation/error_mapper.rs +689 -0
- data/vendor/crates/spikard-core/src/validation/mod.rs +459 -0
- data/vendor/crates/spikard-http/Cargo.toml +58 -0
- data/vendor/crates/spikard-http/examples/sse-notifications.rs +147 -0
- data/vendor/crates/spikard-http/examples/websocket-chat.rs +91 -0
- data/vendor/crates/spikard-http/src/auth.rs +247 -0
- data/vendor/crates/spikard-http/src/background.rs +1562 -0
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -0
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -0
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -0
- data/vendor/crates/spikard-http/src/cors.rs +490 -0
- data/vendor/crates/spikard-http/src/debug.rs +63 -0
- data/vendor/crates/spikard-http/src/di_handler.rs +1878 -0
- data/vendor/crates/spikard-http/src/handler_response.rs +532 -0
- data/vendor/crates/spikard-http/src/handler_trait.rs +861 -0
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -0
- data/vendor/crates/spikard-http/src/lib.rs +524 -0
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -0
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -0
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -0
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +930 -0
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +541 -0
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -0
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -0
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +535 -0
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +867 -0
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +678 -0
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -0
- data/vendor/crates/spikard-http/src/response.rs +399 -0
- data/vendor/crates/spikard-http/src/server/handler.rs +1557 -0
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -0
- data/vendor/crates/spikard-http/src/server/mod.rs +806 -0
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +630 -0
- data/vendor/crates/spikard-http/src/server/routing_factory.rs +497 -0
- data/vendor/crates/spikard-http/src/sse.rs +961 -0
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -0
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -0
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -0
- data/vendor/crates/spikard-http/src/testing.rs +377 -0
- data/vendor/crates/spikard-http/src/websocket.rs +831 -0
- data/vendor/crates/spikard-http/tests/background_behavior.rs +918 -0
- data/vendor/crates/spikard-http/tests/common/handlers.rs +308 -0
- data/vendor/crates/spikard-http/tests/common/mod.rs +21 -0
- data/vendor/crates/spikard-http/tests/di_integration.rs +202 -0
- data/vendor/crates/spikard-http/tests/doc_snippets.rs +4 -0
- data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +1135 -0
- data/vendor/crates/spikard-http/tests/multipart_behavior.rs +688 -0
- data/vendor/crates/spikard-http/tests/server_config_builder.rs +324 -0
- data/vendor/crates/spikard-http/tests/sse_behavior.rs +728 -0
- data/vendor/crates/spikard-http/tests/websocket_behavior.rs +724 -0
- data/vendor/crates/spikard-rb/Cargo.toml +43 -0
- data/vendor/crates/spikard-rb/build.rs +199 -0
- data/vendor/crates/spikard-rb/src/background.rs +63 -0
- data/vendor/crates/spikard-rb/src/config/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/config/server_config.rs +283 -0
- data/vendor/crates/spikard-rb/src/conversion.rs +459 -0
- data/vendor/crates/spikard-rb/src/di/builder.rs +105 -0
- data/vendor/crates/spikard-rb/src/di/mod.rs +413 -0
- data/vendor/crates/spikard-rb/src/handler.rs +612 -0
- data/vendor/crates/spikard-rb/src/integration/mod.rs +3 -0
- data/vendor/crates/spikard-rb/src/lib.rs +1857 -0
- data/vendor/crates/spikard-rb/src/lifecycle.rs +275 -0
- data/vendor/crates/spikard-rb/src/metadata/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +427 -0
- data/vendor/crates/spikard-rb/src/runtime/mod.rs +5 -0
- data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +326 -0
- data/vendor/crates/spikard-rb/src/server.rs +283 -0
- data/vendor/crates/spikard-rb/src/sse.rs +231 -0
- data/vendor/crates/spikard-rb/src/testing/client.rs +404 -0
- data/vendor/crates/spikard-rb/src/testing/mod.rs +7 -0
- data/vendor/crates/spikard-rb/src/testing/sse.rs +143 -0
- data/vendor/crates/spikard-rb/src/testing/websocket.rs +221 -0
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -0
- data/vendor/crates/spikard-rb/tests/magnus_ffi_tests.rs +14 -0
- metadata +213 -0
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
//! Configuration extraction trait and implementation for language bindings
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides a trait-based abstraction for extracting ServerConfig and related
|
|
4
|
+
//! configuration structs from language-specific objects (Python dicts, JavaScript objects, etc.)
|
|
5
|
+
//! without duplicating extraction logic across bindings.
|
|
6
|
+
//!
|
|
7
|
+
//! The `ConfigSource` trait allows language bindings to implement a unified interface for
|
|
8
|
+
//! reading configuration, while `ConfigExtractor` provides the actual extraction logic that
|
|
9
|
+
//! works with any `ConfigSource` implementation.
|
|
10
|
+
|
|
11
|
+
use spikard_http::{
|
|
12
|
+
ApiKeyConfig, BackgroundTaskConfig, CompressionConfig, ContactInfo, JwtConfig, LicenseInfo, OpenApiConfig,
|
|
13
|
+
RateLimitConfig, SecuritySchemeInfo, ServerConfig, ServerInfo, StaticFilesConfig,
|
|
14
|
+
};
|
|
15
|
+
use std::collections::HashMap;
|
|
16
|
+
|
|
17
|
+
/// Trait for reading configuration from language-specific objects
|
|
18
|
+
///
|
|
19
|
+
/// Bindings implement this trait to provide unified access to configuration values
|
|
20
|
+
/// regardless of the language-specific representation (PyDict, JavaScript Object, etc.).
|
|
21
|
+
pub trait ConfigSource {
|
|
22
|
+
/// Get a boolean value from the source
|
|
23
|
+
fn get_bool(&self, key: &str) -> Option<bool>;
|
|
24
|
+
|
|
25
|
+
/// Get a u64 value from the source
|
|
26
|
+
fn get_u64(&self, key: &str) -> Option<u64>;
|
|
27
|
+
|
|
28
|
+
/// Get a u16 value from the source
|
|
29
|
+
fn get_u16(&self, key: &str) -> Option<u16>;
|
|
30
|
+
|
|
31
|
+
/// Get a string value from the source
|
|
32
|
+
fn get_string(&self, key: &str) -> Option<String>;
|
|
33
|
+
|
|
34
|
+
/// Get a vector of strings from the source
|
|
35
|
+
fn get_vec_string(&self, key: &str) -> Option<Vec<String>>;
|
|
36
|
+
|
|
37
|
+
/// Get a nested ConfigSource for nested objects
|
|
38
|
+
fn get_nested(&self, key: &str) -> Option<Box<dyn ConfigSource + '_>>;
|
|
39
|
+
|
|
40
|
+
/// Check if a key exists in the source
|
|
41
|
+
fn has_key(&self, key: &str) -> bool;
|
|
42
|
+
|
|
43
|
+
/// Get array length (for collection iteration)
|
|
44
|
+
fn get_array_length(&self, _key: &str) -> Option<usize> {
|
|
45
|
+
None
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// Get element at index from array
|
|
49
|
+
fn get_array_element(&self, _key: &str, _index: usize) -> Option<Box<dyn ConfigSource + '_>> {
|
|
50
|
+
None
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Get u32 value from the source (helper for common case)
|
|
54
|
+
fn get_u32(&self, key: &str) -> Option<u32> {
|
|
55
|
+
self.get_u64(key).and_then(|v| u32::try_from(v).ok())
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/// Get usize value from the source (helper)
|
|
59
|
+
fn get_usize(&self, key: &str) -> Option<usize> {
|
|
60
|
+
self.get_u64(key).and_then(|v| usize::try_from(v).ok())
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// Configuration extractor that works with any ConfigSource
|
|
65
|
+
pub struct ConfigExtractor;
|
|
66
|
+
|
|
67
|
+
impl ConfigExtractor {
|
|
68
|
+
/// Extract a complete ServerConfig from a ConfigSource
|
|
69
|
+
pub fn extract_server_config(source: &dyn ConfigSource) -> Result<ServerConfig, String> {
|
|
70
|
+
let host = source
|
|
71
|
+
.get_string("host")
|
|
72
|
+
.or_else(|| source.get_string("Host"))
|
|
73
|
+
.unwrap_or_else(|| "127.0.0.1".to_string());
|
|
74
|
+
|
|
75
|
+
let port = source
|
|
76
|
+
.get_u16("port")
|
|
77
|
+
.or_else(|| source.get_u32("port").map(|p| p as u16))
|
|
78
|
+
.unwrap_or(8000);
|
|
79
|
+
|
|
80
|
+
let workers = source
|
|
81
|
+
.get_usize("workers")
|
|
82
|
+
.or_else(|| source.get_u32("workers").map(|w| w as usize))
|
|
83
|
+
.unwrap_or(1);
|
|
84
|
+
|
|
85
|
+
let enable_request_id = source.get_bool("enable_request_id").unwrap_or(true);
|
|
86
|
+
|
|
87
|
+
let max_body_size = source.get_usize("max_body_size");
|
|
88
|
+
|
|
89
|
+
let request_timeout = source.get_u64("request_timeout");
|
|
90
|
+
|
|
91
|
+
let graceful_shutdown = source.get_bool("graceful_shutdown").unwrap_or(true);
|
|
92
|
+
|
|
93
|
+
let shutdown_timeout = source.get_u64("shutdown_timeout").unwrap_or(30);
|
|
94
|
+
|
|
95
|
+
let compression = source
|
|
96
|
+
.get_nested("compression")
|
|
97
|
+
.and_then(|cfg| Self::extract_compression_config(cfg.as_ref()).ok());
|
|
98
|
+
|
|
99
|
+
let rate_limit = source
|
|
100
|
+
.get_nested("rate_limit")
|
|
101
|
+
.and_then(|cfg| Self::extract_rate_limit_config(cfg.as_ref()).ok());
|
|
102
|
+
|
|
103
|
+
let jwt_auth = source
|
|
104
|
+
.get_nested("jwt_auth")
|
|
105
|
+
.and_then(|cfg| Self::extract_jwt_config(cfg.as_ref()).ok());
|
|
106
|
+
|
|
107
|
+
let api_key_auth = source
|
|
108
|
+
.get_nested("api_key_auth")
|
|
109
|
+
.and_then(|cfg| Self::extract_api_key_config(cfg.as_ref()).ok());
|
|
110
|
+
|
|
111
|
+
let static_files = Self::extract_static_files_config(source)?;
|
|
112
|
+
|
|
113
|
+
let openapi = source
|
|
114
|
+
.get_nested("openapi")
|
|
115
|
+
.and_then(|cfg| Self::extract_openapi_config(cfg.as_ref()).ok());
|
|
116
|
+
|
|
117
|
+
Ok(ServerConfig {
|
|
118
|
+
host,
|
|
119
|
+
port,
|
|
120
|
+
workers,
|
|
121
|
+
enable_request_id,
|
|
122
|
+
max_body_size,
|
|
123
|
+
request_timeout,
|
|
124
|
+
compression,
|
|
125
|
+
rate_limit,
|
|
126
|
+
jwt_auth,
|
|
127
|
+
api_key_auth,
|
|
128
|
+
static_files,
|
|
129
|
+
graceful_shutdown,
|
|
130
|
+
shutdown_timeout,
|
|
131
|
+
background_tasks: BackgroundTaskConfig::default(),
|
|
132
|
+
openapi,
|
|
133
|
+
lifecycle_hooks: None,
|
|
134
|
+
di_container: None,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Extract CompressionConfig from a ConfigSource
|
|
139
|
+
pub fn extract_compression_config(source: &dyn ConfigSource) -> Result<CompressionConfig, String> {
|
|
140
|
+
let gzip = source.get_bool("gzip").unwrap_or(true);
|
|
141
|
+
let brotli = source.get_bool("brotli").unwrap_or(true);
|
|
142
|
+
let min_size = source
|
|
143
|
+
.get_usize("min_size")
|
|
144
|
+
.or_else(|| source.get_u32("min_size").map(|s| s as usize))
|
|
145
|
+
.unwrap_or(1024);
|
|
146
|
+
let quality = source.get_u32("quality").unwrap_or(6);
|
|
147
|
+
|
|
148
|
+
Ok(CompressionConfig {
|
|
149
|
+
gzip,
|
|
150
|
+
brotli,
|
|
151
|
+
min_size,
|
|
152
|
+
quality,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Extract RateLimitConfig from a ConfigSource
|
|
157
|
+
pub fn extract_rate_limit_config(source: &dyn ConfigSource) -> Result<RateLimitConfig, String> {
|
|
158
|
+
let per_second = source.get_u64("per_second").ok_or("Rate limit requires 'per_second'")?;
|
|
159
|
+
|
|
160
|
+
let burst = source.get_u32("burst").ok_or("Rate limit requires 'burst' as u32")?;
|
|
161
|
+
|
|
162
|
+
let ip_based = source.get_bool("ip_based").unwrap_or(true);
|
|
163
|
+
|
|
164
|
+
Ok(RateLimitConfig {
|
|
165
|
+
per_second,
|
|
166
|
+
burst,
|
|
167
|
+
ip_based,
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// Extract JwtConfig from a ConfigSource
|
|
172
|
+
pub fn extract_jwt_config(source: &dyn ConfigSource) -> Result<JwtConfig, String> {
|
|
173
|
+
let secret = source.get_string("secret").ok_or("JWT auth requires 'secret'")?;
|
|
174
|
+
|
|
175
|
+
let algorithm = source.get_string("algorithm").unwrap_or_else(|| "HS256".to_string());
|
|
176
|
+
|
|
177
|
+
let audience = source.get_vec_string("audience");
|
|
178
|
+
|
|
179
|
+
let issuer = source.get_string("issuer");
|
|
180
|
+
|
|
181
|
+
let leeway = source.get_u64("leeway").unwrap_or(0);
|
|
182
|
+
|
|
183
|
+
Ok(JwtConfig {
|
|
184
|
+
secret,
|
|
185
|
+
algorithm,
|
|
186
|
+
audience,
|
|
187
|
+
issuer,
|
|
188
|
+
leeway,
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// Extract ApiKeyConfig from a ConfigSource
|
|
193
|
+
pub fn extract_api_key_config(source: &dyn ConfigSource) -> Result<ApiKeyConfig, String> {
|
|
194
|
+
let keys = source
|
|
195
|
+
.get_vec_string("keys")
|
|
196
|
+
.ok_or("API Key auth requires 'keys' as Vec<String>)")?;
|
|
197
|
+
|
|
198
|
+
let header_name = source
|
|
199
|
+
.get_string("header_name")
|
|
200
|
+
.unwrap_or_else(|| "X-API-Key".to_string());
|
|
201
|
+
|
|
202
|
+
Ok(ApiKeyConfig { keys, header_name })
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/// Extract static files configuration list from a ConfigSource
|
|
206
|
+
pub fn extract_static_files_config(source: &dyn ConfigSource) -> Result<Vec<StaticFilesConfig>, String> {
|
|
207
|
+
let length = source.get_array_length("static_files").unwrap_or(0);
|
|
208
|
+
if length == 0 {
|
|
209
|
+
return Ok(Vec::new());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let mut configs = Vec::new();
|
|
213
|
+
for i in 0..length {
|
|
214
|
+
let sf_source = source
|
|
215
|
+
.get_array_element("static_files", i)
|
|
216
|
+
.ok_or("Failed to get static files array element")?;
|
|
217
|
+
|
|
218
|
+
let directory = sf_source
|
|
219
|
+
.get_string("directory")
|
|
220
|
+
.ok_or("Static files requires 'directory'")?;
|
|
221
|
+
|
|
222
|
+
let route_prefix = sf_source
|
|
223
|
+
.get_string("route_prefix")
|
|
224
|
+
.ok_or("Static files requires 'route_prefix'")?;
|
|
225
|
+
|
|
226
|
+
let index_file = sf_source.get_bool("index_file").unwrap_or(true);
|
|
227
|
+
|
|
228
|
+
let cache_control = sf_source.get_string("cache_control");
|
|
229
|
+
|
|
230
|
+
configs.push(StaticFilesConfig {
|
|
231
|
+
directory,
|
|
232
|
+
route_prefix,
|
|
233
|
+
index_file,
|
|
234
|
+
cache_control,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
Ok(configs)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/// Extract OpenApiConfig from a ConfigSource
|
|
242
|
+
pub fn extract_openapi_config(source: &dyn ConfigSource) -> Result<OpenApiConfig, String> {
|
|
243
|
+
let enabled = source.get_bool("enabled").unwrap_or(false);
|
|
244
|
+
let title = source.get_string("title").unwrap_or_else(|| "API".to_string());
|
|
245
|
+
let version = source.get_string("version").unwrap_or_else(|| "1.0.0".to_string());
|
|
246
|
+
let description = source.get_string("description");
|
|
247
|
+
let swagger_ui_path = source
|
|
248
|
+
.get_string("swagger_ui_path")
|
|
249
|
+
.unwrap_or_else(|| "/docs".to_string());
|
|
250
|
+
let redoc_path = source.get_string("redoc_path").unwrap_or_else(|| "/redoc".to_string());
|
|
251
|
+
let openapi_json_path = source
|
|
252
|
+
.get_string("openapi_json_path")
|
|
253
|
+
.unwrap_or_else(|| "/openapi.json".to_string());
|
|
254
|
+
|
|
255
|
+
let contact = source
|
|
256
|
+
.get_nested("contact")
|
|
257
|
+
.map(|cfg| {
|
|
258
|
+
let name = cfg.get_string("name");
|
|
259
|
+
let email = cfg.get_string("email");
|
|
260
|
+
let url = cfg.get_string("url");
|
|
261
|
+
ContactInfo { name, email, url }
|
|
262
|
+
})
|
|
263
|
+
.filter(|c| c.name.is_some() || c.email.is_some() || c.url.is_some());
|
|
264
|
+
|
|
265
|
+
let license = source.get_nested("license").and_then(|cfg| {
|
|
266
|
+
let name = cfg.get_string("name")?;
|
|
267
|
+
let url = cfg.get_string("url");
|
|
268
|
+
Some(LicenseInfo { name, url })
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
let servers = Self::extract_servers_config(source)?;
|
|
272
|
+
|
|
273
|
+
let security_schemes = Self::extract_security_schemes_config(source)?;
|
|
274
|
+
|
|
275
|
+
Ok(OpenApiConfig {
|
|
276
|
+
enabled,
|
|
277
|
+
title,
|
|
278
|
+
version,
|
|
279
|
+
description,
|
|
280
|
+
swagger_ui_path,
|
|
281
|
+
redoc_path,
|
|
282
|
+
openapi_json_path,
|
|
283
|
+
contact,
|
|
284
|
+
license,
|
|
285
|
+
servers,
|
|
286
|
+
security_schemes,
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/// Extract servers list from OpenAPI config
|
|
291
|
+
fn extract_servers_config(source: &dyn ConfigSource) -> Result<Vec<ServerInfo>, String> {
|
|
292
|
+
let length = source.get_array_length("servers").unwrap_or(0);
|
|
293
|
+
if length == 0 {
|
|
294
|
+
return Ok(Vec::new());
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
let mut servers = Vec::new();
|
|
298
|
+
for i in 0..length {
|
|
299
|
+
let server_source = source
|
|
300
|
+
.get_array_element("servers", i)
|
|
301
|
+
.ok_or("Failed to get servers array element")?;
|
|
302
|
+
|
|
303
|
+
let url = server_source.get_string("url").ok_or("Server requires 'url'")?;
|
|
304
|
+
|
|
305
|
+
let description = server_source.get_string("description");
|
|
306
|
+
|
|
307
|
+
servers.push(ServerInfo { url, description });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
Ok(servers)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/// Extract security schemes from OpenAPI config
|
|
314
|
+
fn extract_security_schemes_config(
|
|
315
|
+
_source: &dyn ConfigSource,
|
|
316
|
+
) -> Result<HashMap<String, SecuritySchemeInfo>, String> {
|
|
317
|
+
// TODO: Implement when bindings support iterating HashMap-like structures
|
|
318
|
+
// For now, return empty map as default
|
|
319
|
+
Ok(HashMap::new())
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
#[cfg(test)]
|
|
324
|
+
mod tests {
|
|
325
|
+
use super::*;
|
|
326
|
+
|
|
327
|
+
struct MockConfigSource {
|
|
328
|
+
data: HashMap<String, String>,
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
impl MockConfigSource {
|
|
332
|
+
fn new() -> Self {
|
|
333
|
+
Self { data: HashMap::new() }
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
fn with(mut self, key: &str, value: String) -> Self {
|
|
337
|
+
self.data.insert(key.to_string(), value);
|
|
338
|
+
self
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
impl ConfigSource for MockConfigSource {
|
|
343
|
+
fn get_bool(&self, key: &str) -> Option<bool> {
|
|
344
|
+
self.data.get(key).and_then(|v| match v.as_str() {
|
|
345
|
+
"true" => Some(true),
|
|
346
|
+
"false" => Some(false),
|
|
347
|
+
_ => v.parse().ok(),
|
|
348
|
+
})
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
fn get_u64(&self, key: &str) -> Option<u64> {
|
|
352
|
+
self.data.get(key).and_then(|v| v.parse().ok())
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
fn get_u16(&self, key: &str) -> Option<u16> {
|
|
356
|
+
self.data.get(key).and_then(|v| v.parse().ok())
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
fn get_string(&self, key: &str) -> Option<String> {
|
|
360
|
+
self.data.get(key).cloned()
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
fn get_vec_string(&self, key: &str) -> Option<Vec<String>> {
|
|
364
|
+
self.data
|
|
365
|
+
.get(key)
|
|
366
|
+
.map(|s| s.split(',').map(|t| t.trim().to_string()).collect())
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
fn get_nested(&self, _key: &str) -> Option<Box<dyn ConfigSource + '_>> {
|
|
370
|
+
None
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
fn has_key(&self, key: &str) -> bool {
|
|
374
|
+
self.data.contains_key(key)
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
#[test]
|
|
379
|
+
fn test_compression_config_extraction() {
|
|
380
|
+
let source = MockConfigSource::new()
|
|
381
|
+
.with("gzip", "true".to_string())
|
|
382
|
+
.with("brotli", "false".to_string())
|
|
383
|
+
.with("min_size", "2048".to_string())
|
|
384
|
+
.with("quality", "9".to_string());
|
|
385
|
+
|
|
386
|
+
let config = ConfigExtractor::extract_compression_config(&source).unwrap();
|
|
387
|
+
assert!(config.gzip);
|
|
388
|
+
assert!(!config.brotli);
|
|
389
|
+
assert_eq!(config.min_size, 2048);
|
|
390
|
+
assert_eq!(config.quality, 9);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
#[test]
|
|
394
|
+
fn test_compression_config_defaults() {
|
|
395
|
+
let source = MockConfigSource::new();
|
|
396
|
+
|
|
397
|
+
let config = ConfigExtractor::extract_compression_config(&source).unwrap();
|
|
398
|
+
assert!(config.gzip);
|
|
399
|
+
assert!(config.brotli);
|
|
400
|
+
assert_eq!(config.min_size, 1024);
|
|
401
|
+
assert_eq!(config.quality, 6);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
#[test]
|
|
405
|
+
fn test_jwt_config_extraction() {
|
|
406
|
+
let source = MockConfigSource::new()
|
|
407
|
+
.with("secret", "my-secret".to_string())
|
|
408
|
+
.with("algorithm", "HS512".to_string())
|
|
409
|
+
.with("leeway", "30".to_string());
|
|
410
|
+
|
|
411
|
+
let config = ConfigExtractor::extract_jwt_config(&source).unwrap();
|
|
412
|
+
assert_eq!(config.secret, "my-secret");
|
|
413
|
+
assert_eq!(config.algorithm, "HS512");
|
|
414
|
+
assert_eq!(config.leeway, 30);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
#[test]
|
|
418
|
+
fn test_jwt_config_missing_secret() {
|
|
419
|
+
let source = MockConfigSource::new();
|
|
420
|
+
let result = ConfigExtractor::extract_jwt_config(&source);
|
|
421
|
+
assert!(result.is_err());
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
#[test]
|
|
425
|
+
fn test_api_key_config_extraction() {
|
|
426
|
+
let source = MockConfigSource::new()
|
|
427
|
+
.with("keys", "key1,key2,key3".to_string())
|
|
428
|
+
.with("header_name", "Authorization".to_string());
|
|
429
|
+
|
|
430
|
+
let config = ConfigExtractor::extract_api_key_config(&source).unwrap();
|
|
431
|
+
assert_eq!(config.keys, vec!["key1", "key2", "key3"]);
|
|
432
|
+
assert_eq!(config.header_name, "Authorization");
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
#[test]
|
|
436
|
+
fn test_api_key_config_defaults() {
|
|
437
|
+
let source = MockConfigSource::new().with("keys", "only-key".to_string());
|
|
438
|
+
|
|
439
|
+
let config = ConfigExtractor::extract_api_key_config(&source).unwrap();
|
|
440
|
+
assert_eq!(config.keys, vec!["only-key"]);
|
|
441
|
+
assert_eq!(config.header_name, "X-API-Key");
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
#[test]
|
|
445
|
+
fn test_rate_limit_config_extraction() {
|
|
446
|
+
let source = MockConfigSource::new()
|
|
447
|
+
.with("per_second", "100".to_string())
|
|
448
|
+
.with("burst", "50".to_string())
|
|
449
|
+
.with("ip_based", "false".to_string());
|
|
450
|
+
|
|
451
|
+
let config = ConfigExtractor::extract_rate_limit_config(&source).unwrap();
|
|
452
|
+
assert_eq!(config.per_second, 100);
|
|
453
|
+
assert_eq!(config.burst, 50);
|
|
454
|
+
assert!(!config.ip_based);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
#[test]
|
|
458
|
+
fn test_rate_limit_config_missing_required() {
|
|
459
|
+
let source = MockConfigSource::new().with("per_second", "100".to_string());
|
|
460
|
+
|
|
461
|
+
let result = ConfigExtractor::extract_rate_limit_config(&source);
|
|
462
|
+
assert!(result.is_err());
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
#[test]
|
|
466
|
+
fn test_openapi_config_extraction() {
|
|
467
|
+
let source = MockConfigSource::new()
|
|
468
|
+
.with("enabled", "true".to_string())
|
|
469
|
+
.with("title", "Test API".to_string())
|
|
470
|
+
.with("version", "2.0.0".to_string())
|
|
471
|
+
.with("description", "A test API".to_string())
|
|
472
|
+
.with("swagger_ui_path", "/api-docs".to_string())
|
|
473
|
+
.with("redoc_path", "/api-redoc".to_string())
|
|
474
|
+
.with("openapi_json_path", "/api.json".to_string());
|
|
475
|
+
|
|
476
|
+
let config = ConfigExtractor::extract_openapi_config(&source).unwrap();
|
|
477
|
+
assert!(config.enabled);
|
|
478
|
+
assert_eq!(config.title, "Test API");
|
|
479
|
+
assert_eq!(config.version, "2.0.0");
|
|
480
|
+
assert_eq!(config.description, Some("A test API".to_string()));
|
|
481
|
+
assert_eq!(config.swagger_ui_path, "/api-docs");
|
|
482
|
+
assert_eq!(config.redoc_path, "/api-redoc");
|
|
483
|
+
assert_eq!(config.openapi_json_path, "/api.json");
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
#[test]
|
|
487
|
+
fn test_openapi_config_defaults() {
|
|
488
|
+
let source = MockConfigSource::new();
|
|
489
|
+
|
|
490
|
+
let config = ConfigExtractor::extract_openapi_config(&source).unwrap();
|
|
491
|
+
assert!(!config.enabled);
|
|
492
|
+
assert_eq!(config.title, "API");
|
|
493
|
+
assert_eq!(config.version, "1.0.0");
|
|
494
|
+
assert_eq!(config.description, None);
|
|
495
|
+
assert_eq!(config.swagger_ui_path, "/docs");
|
|
496
|
+
assert_eq!(config.redoc_path, "/redoc");
|
|
497
|
+
assert_eq!(config.openapi_json_path, "/openapi.json");
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
#[test]
|
|
501
|
+
fn test_static_files_config_empty() {
|
|
502
|
+
let source = MockConfigSource::new();
|
|
503
|
+
|
|
504
|
+
let configs = ConfigExtractor::extract_static_files_config(&source).unwrap();
|
|
505
|
+
assert_eq!(configs.len(), 0);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
#[test]
|
|
509
|
+
fn test_server_config_extraction() {
|
|
510
|
+
let source = MockConfigSource::new()
|
|
511
|
+
.with("host", "0.0.0.0".to_string())
|
|
512
|
+
.with("port", "3000".to_string())
|
|
513
|
+
.with("workers", "4".to_string())
|
|
514
|
+
.with("enable_request_id", "false".to_string())
|
|
515
|
+
.with("max_body_size", "5242880".to_string())
|
|
516
|
+
.with("request_timeout", "60".to_string())
|
|
517
|
+
.with("graceful_shutdown", "false".to_string())
|
|
518
|
+
.with("shutdown_timeout", "10".to_string());
|
|
519
|
+
|
|
520
|
+
let config = ConfigExtractor::extract_server_config(&source).unwrap();
|
|
521
|
+
assert_eq!(config.host, "0.0.0.0");
|
|
522
|
+
assert_eq!(config.port, 3000);
|
|
523
|
+
assert_eq!(config.workers, 4);
|
|
524
|
+
assert!(!config.enable_request_id);
|
|
525
|
+
assert_eq!(config.max_body_size, Some(5242880));
|
|
526
|
+
assert_eq!(config.request_timeout, Some(60));
|
|
527
|
+
assert!(!config.graceful_shutdown);
|
|
528
|
+
assert_eq!(config.shutdown_timeout, 10);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
#[test]
|
|
532
|
+
fn test_server_config_defaults() {
|
|
533
|
+
let source = MockConfigSource::new();
|
|
534
|
+
|
|
535
|
+
let config = ConfigExtractor::extract_server_config(&source).unwrap();
|
|
536
|
+
assert_eq!(config.host, "127.0.0.1");
|
|
537
|
+
assert_eq!(config.port, 8000);
|
|
538
|
+
assert_eq!(config.workers, 1);
|
|
539
|
+
assert!(config.enable_request_id);
|
|
540
|
+
assert_eq!(config.max_body_size, None);
|
|
541
|
+
assert_eq!(config.request_timeout, None);
|
|
542
|
+
assert!(config.graceful_shutdown);
|
|
543
|
+
assert_eq!(config.shutdown_timeout, 30);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
#[test]
|
|
547
|
+
fn test_servers_config_empty() {
|
|
548
|
+
let source = MockConfigSource::new();
|
|
549
|
+
|
|
550
|
+
let servers = ConfigExtractor::extract_servers_config(&source).unwrap();
|
|
551
|
+
assert_eq!(servers.len(), 0);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
#[test]
|
|
555
|
+
fn test_security_schemes_config_empty() {
|
|
556
|
+
let source = MockConfigSource::new();
|
|
557
|
+
|
|
558
|
+
let schemes = ConfigExtractor::extract_security_schemes_config(&source).unwrap();
|
|
559
|
+
assert_eq!(schemes.len(), 0);
|
|
560
|
+
}
|
|
561
|
+
}
|