elif-openapi 0.2.1

OpenAPI 3.0 specification generation for elif.rs framework
Documentation
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
/*!
Swagger UI integration for interactive API documentation.

This module provides functionality to serve interactive Swagger UI documentation
for OpenAPI specifications.
*/

use crate::{
    error::{OpenApiError, OpenApiResult},
    specification::OpenApiSpec,
};
use axum::{
    body::Body,
    extract::{Path, State},
    http::{header, StatusCode},
    response::{Html, Json, Response},
    routing::get,
    Router,
};
use std::sync::Arc;
use tower_http::cors::CorsLayer;

/// Application state for the Swagger UI server
#[derive(Clone)]
pub struct SwaggerState {
    /// OpenAPI specification
    pub spec: Arc<OpenApiSpec>,
    /// Configuration
    pub config: SwaggerConfig,
}

/// Swagger UI server for serving interactive API documentation
pub struct SwaggerUi {
    /// OpenAPI specification
    spec: Arc<OpenApiSpec>,
    /// Configuration
    config: SwaggerConfig,
}

/// Configuration for Swagger UI
#[derive(Debug, Clone)]
pub struct SwaggerConfig {
    /// Server host
    pub host: String,
    /// Server port
    pub port: u16,
    /// Page title
    pub title: String,
    /// Custom CSS
    pub custom_css: Option<String>,
    /// Custom JavaScript
    pub custom_js: Option<String>,
    /// OAuth configuration
    pub oauth: Option<OAuthConfig>,
}

/// OAuth configuration for Swagger UI
#[derive(Debug, Clone)]
pub struct OAuthConfig {
    pub client_id: String,
    pub client_secret: Option<String>,
    pub realm: Option<String>,
    pub app_name: String,
    pub scopes: Vec<String>,
}

impl SwaggerUi {
    /// Create new Swagger UI server
    pub fn new(spec: OpenApiSpec, config: SwaggerConfig) -> Self {
        Self {
            spec: Arc::new(spec),
            config,
        }
    }

    /// Get the specification
    pub fn specification(&self) -> Option<&OpenApiSpec> {
        Some(&self.spec)
    }

    /// Get the configuration  
    pub fn config(&self) -> &SwaggerConfig {
        &self.config
    }

    /// Start the Swagger UI server using axum
    pub async fn serve(&self) -> OpenApiResult<()> {
        let state = SwaggerState {
            spec: Arc::clone(&self.spec),
            config: self.config.clone(),
        };

        // Build the router
        let app = Router::new()
            .route("/", get(serve_index))
            .route("/api-spec.json", get(serve_spec))
            .route("/static/*path", get(serve_static))
            .layer(CorsLayer::permissive())
            .with_state(state);

        // Bind to the configured address
        let addr = format!("{}:{}", self.config.host, self.config.port);
        let listener = tokio::net::TcpListener::bind(&addr)
            .await
            .map_err(|e| OpenApiError::generic(format!("Failed to bind to {}: {}", addr, e)))?;

        println!("🚀 Swagger UI server running at http://{}", addr);
        println!("📖 API documentation available at http://{}/", addr);

        // Start the server
        axum::serve(listener, app)
            .await
            .map_err(|e| OpenApiError::generic(format!("Server error: {}", e)))?;

        Ok(())
    }
}

// Axum handlers for Swagger UI routes

/// Serve the main Swagger UI index page
async fn serve_index(State(state): State<SwaggerState>) -> Html<String> {
    let html = SwaggerUi::generate_index_html(&state.config);
    Html(html)
}

/// Serve the OpenAPI specification JSON
async fn serve_spec(
    State(state): State<SwaggerState>,
) -> Result<Json<OpenApiSpec>, (StatusCode, String)> {
    Ok(Json((*state.spec).clone()))
}

/// Serve static assets (CSS, JS)
async fn serve_static(
    Path(path): Path<String>,
) -> Result<Response<Body>, (StatusCode, &'static str)> {
    let (content_type, body) = match path.as_str() {
        "swagger-ui-bundle.js" => (
            "application/javascript",
            "// Swagger UI Bundle - placeholder for CDN content",
        ),
        "swagger-ui-standalone-preset.js" => (
            "application/javascript",
            "// Swagger UI Preset - placeholder for CDN content",
        ),
        "swagger-ui.css" => (
            "text/css",
            "/* Swagger UI CSS - placeholder for CDN content */",
        ),
        _ => return Err((StatusCode::NOT_FOUND, "Not Found")),
    };

    let response = Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, content_type)
        .body(Body::from(body))
        .map_err(|_| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to build response",
            )
        })?;

    Ok(response)
}

impl SwaggerUi {
    /// Generate the main HTML page
    fn generate_index_html(config: &SwaggerConfig) -> String {
        let oauth_config = if let Some(oauth) = &config.oauth {
            format!(
                r#"
                ui.initOAuth({{
                    clientId: "{}",
                    realm: "{}",
                    appName: "{}",
                    scopes: [{}]
                }});
                "#,
                oauth.client_id,
                oauth.realm.as_deref().unwrap_or(""),
                oauth.app_name,
                oauth
                    .scopes
                    .iter()
                    .map(|s| format!(r#""{}""#, s))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        } else {
            String::new()
        };

        let custom_css = config.custom_css.as_deref().unwrap_or("");
        let custom_js = config.custom_js.as_deref().unwrap_or("");

        format!(
            r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{}</title>
    <link rel="stylesheet" type="text/css" href="/static/swagger-ui.css" />
    <style>
        html {{
            box-sizing: border-box;
            overflow: -moz-scrollbars-vertical;
            overflow-y: scroll;
        }}

        *, *:before, *:after {{
            box-sizing: inherit;
        }}

        body {{
            margin:0;
            background: #fafafa;
        }}

        {}
    </style>
</head>
<body>
    <div id="swagger-ui"></div>
    
    <script src="/static/swagger-ui-bundle.js"></script>
    <script src="/static/swagger-ui-standalone-preset.js"></script>
    <script>
        window.onload = function() {{
            const ui = SwaggerUIBundle({{
                url: '/api-spec.json',
                dom_id: '#swagger-ui',
                deepLinking: true,
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIStandalonePreset
                ],
                plugins: [
                    SwaggerUIBundle.plugins.DownloadUrl
                ],
                layout: "StandaloneLayout",
                validatorUrl: null,
                tryItOutEnabled: true,
                filter: true,
                supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
                onComplete: function() {{
                    console.log("Swagger UI loaded successfully");
                }},
                onFailure: function(error) {{
                    console.error("Swagger UI failed to load:", error);
                }}
            }});

            {}

            window.ui = ui;
        }};

        {}
    </script>
</body>
</html>"#,
            config.title, custom_css, oauth_config, custom_js
        )
    }

    /// Generate static Swagger UI HTML file
    pub fn generate_static_html(
        spec: &OpenApiSpec,
        config: &SwaggerConfig,
    ) -> OpenApiResult<String> {
        let spec_json = serde_json::to_string(spec)
            .map_err(|e| OpenApiError::export_error(format!("Failed to serialize spec: {}", e)))?;

        let oauth_config = if let Some(oauth) = &config.oauth {
            format!(
                r#"
                ui.initOAuth({{
                    clientId: "{}",
                    realm: "{}",
                    appName: "{}",
                    scopes: [{}]
                }});
                "#,
                oauth.client_id,
                oauth.realm.as_deref().unwrap_or(""),
                oauth.app_name,
                oauth
                    .scopes
                    .iter()
                    .map(|s| format!(r#""{}""#, s))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        } else {
            String::new()
        };

        let custom_css = config.custom_css.as_deref().unwrap_or("");
        let custom_js = config.custom_js.as_deref().unwrap_or("");

        Ok(format!(
            r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{}</title>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui.css" />
    <style>
        html {{
            box-sizing: border-box;
            overflow: -moz-scrollbars-vertical;
            overflow-y: scroll;
        }}

        *, *:before, *:after {{
            box-sizing: inherit;
        }}

        body {{
            margin:0;
            background: #fafafa;
        }}

        {}
    </style>
</head>
<body>
    <div id="swagger-ui"></div>
    
    <script src="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui-bundle.js"></script>
    <script src="https://unpkg.com/swagger-ui-dist@5.9.0/swagger-ui-standalone-preset.js"></script>
    <script>
        const spec = {};

        window.onload = function() {{
            const ui = SwaggerUIBundle({{
                spec: spec,
                dom_id: '#swagger-ui',
                deepLinking: true,
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIStandalonePreset
                ],
                plugins: [
                    SwaggerUIBundle.plugins.DownloadUrl
                ],
                layout: "StandaloneLayout",
                validatorUrl: null,
                tryItOutEnabled: true,
                filter: true,
                supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
                onComplete: function() {{
                    console.log("Swagger UI loaded successfully");
                }},
                onFailure: function(error) {{
                    console.error("Swagger UI failed to load:", error);
                }}
            }});

            {}

            window.ui = ui;
        }};

        {}
    </script>
</body>
</html>"#,
            config.title, custom_css, spec_json, oauth_config, custom_js
        ))
    }
}

impl Default for SwaggerConfig {
    fn default() -> Self {
        Self {
            host: "127.0.0.1".to_string(),
            port: 8080,
            title: "API Documentation".to_string(),
            custom_css: None,
            custom_js: None,
            oauth: None,
        }
    }
}

impl SwaggerConfig {
    /// Create new configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Set server host and port
    pub fn with_server(mut self, host: &str, port: u16) -> Self {
        self.host = host.to_string();
        self.port = port;
        self
    }

    /// Set page title
    pub fn with_title(mut self, title: &str) -> Self {
        self.title = title.to_string();
        self
    }

    /// Add custom CSS
    pub fn with_custom_css(mut self, css: &str) -> Self {
        self.custom_css = Some(css.to_string());
        self
    }

    /// Add custom JavaScript
    pub fn with_custom_js(mut self, js: &str) -> Self {
        self.custom_js = Some(js.to_string());
        self
    }

    /// Configure OAuth
    pub fn with_oauth(mut self, oauth: OAuthConfig) -> Self {
        self.oauth = Some(oauth);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::specification::OpenApiSpec;

    #[test]
    fn test_swagger_config_creation() {
        let config = SwaggerConfig::new()
            .with_server("localhost", 3000)
            .with_title("My API");

        assert_eq!(config.host, "localhost");
        assert_eq!(config.port, 3000);
        assert_eq!(config.title, "My API");
    }

    #[test]
    fn test_static_html_generation() {
        let spec = OpenApiSpec::new("Test API", "1.0.0");
        let config = SwaggerConfig::new().with_title("Test API Documentation");

        let html = SwaggerUi::generate_static_html(&spec, &config).unwrap();

        assert!(html.contains("Test API Documentation"));
        assert!(html.contains("swagger-ui"));
        assert!(html.contains("SwaggerUIBundle"));
    }

    #[test]
    fn test_swagger_ui_creation() {
        let spec = OpenApiSpec::new("Test API", "1.0.0");
        let config = SwaggerConfig::default();

        let swagger_ui = SwaggerUi::new(spec, config);
        assert_eq!(swagger_ui.config.host, "127.0.0.1");
        assert_eq!(swagger_ui.config.port, 8080);
    }
}