strike48-connector 0.3.9

Rust SDK for the Strike48 Connector 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
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
//! High-level API for serving apps to Strike48.
//!
//! This module provides ergonomic builders and macros for quickly spinning up
//! app connectors in Rust.
//!
//! # Example: Static Site
//!
//! ```rust,ignore
//! use strike48_connector::behaviors::serve::App;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     App::static_files("./dist")
//!         .display_name("My React App")
//!         .icon("hero-sparkles")
//!         .run()
//!         .await?;
//!     Ok(())
//! }
//! ```
//!
//! # Example: Dynamic App
//!
//! ```rust,ignore
//! use strike48_connector::behaviors::serve::App;
//! use strike48_connector::behaviors::app::{AppPageRequest, AppPageResponse};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     App::builder()
//!         .display_name("My Dashboard")
//!         .entry_path("/")
//!         .handler(|req: AppPageRequest| async move {
//!             match req.path.as_str() {
//!                 "/" => AppPageResponse::html("<h1>Welcome</h1>"),
//!                 _ => AppPageResponse::not_found(),
//!             }
//!         })
//!         .run()
//!         .await?;
//!     Ok(())
//! }
//! ```

use super::app::{
    AppConnector, AppManifest, AppPageRequest, AppPageResponse, NavigationConfig, StaticFileConfig,
};
use crate::connector::{ConnectorConfig, ConnectorRunner};
use crate::error::{ConnectorError, Result};
use crate::types::{ConnectorBehavior, PayloadEncoding};
use async_trait::async_trait;
use std::collections::HashMap;
use std::future::Future;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;

// =============================================================================
// App Builder
// =============================================================================

/// High-level builder for creating and running app connectors.
///
/// # Example
///
/// ```rust,ignore
/// use strike48_connector::behaviors::serve::App;
///
/// App::static_files("./public")
///     .display_name("My App")
///     .icon("hero-sparkles")
///     .run()
///     .await?;
/// ```
pub struct App {
    /// App manifest
    manifest: AppManifest,
    /// Static file configuration
    static_config: Option<StaticFileConfig>,
    /// Custom handler for page requests
    handler: Option<HandlerFn>,
    /// Connector configuration overrides
    connector_config: ConnectorConfig,
    /// Local development port (optional, for future use)
    #[allow(dead_code)]
    local_port: Option<u16>,
}

/// Type alias for page handler function.
type HandlerFn = Arc<
    dyn Fn(AppPageRequest) -> Pin<Box<dyn Future<Output = AppPageResponse> + Send>> + Send + Sync,
>;

impl App {
    /// Create a new App builder.
    pub fn builder() -> AppBuilder {
        AppBuilder::default()
    }

    /// Create an app that serves static files from a directory.
    ///
    /// This is the simplest way to serve a pre-built SPA or static site.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// App::static_files("./dist")
    ///     .display_name("My SPA")
    ///     .run()
    ///     .await?;
    /// ```
    pub fn static_files(dir: impl Into<PathBuf>) -> AppBuilder {
        let static_dir = dir.into();
        AppBuilder {
            static_dir: Some(static_dir),
            serve_index_for_spa: true,
            ..Default::default()
        }
    }

    /// Create an app from environment variables.
    ///
    /// Reads configuration from:
    /// - `STRIKE48_HOST`: Strike48 server address
    /// - `TENANT_ID`: Tenant identifier
    /// - `APP_NAME`: App name (optional)
    /// - `APP_STATIC_DIR`: Static files directory (optional)
    pub fn from_env() -> Result<AppBuilder> {
        let host = std::env::var("STRIKE48_HOST").map_err(|_| {
            ConnectorError::InvalidConfig("STRIKE48_HOST environment variable not set".to_string())
        })?;

        let tenant_id = std::env::var("TENANT_ID").map_err(|_| {
            ConnectorError::InvalidConfig("TENANT_ID environment variable not set".to_string())
        })?;

        let display_name = std::env::var("APP_NAME").ok();
        let static_dir = std::env::var("APP_STATIC_DIR").ok().map(PathBuf::from);
        let local_port = std::env::var("APP_LOCAL_PORT")
            .ok()
            .and_then(|p| p.parse().ok());

        Ok(AppBuilder {
            host: Some(host),
            tenant_id: Some(tenant_id),
            display_name,
            static_dir,
            local_port,
            ..Default::default()
        })
    }

    /// Run the app connector.
    pub async fn run(self) -> Result<()> {
        let connector = BuiltAppConnector {
            manifest: self.manifest,
            static_config: self.static_config,
            handler: self.handler,
            connector_type: self.connector_config.connector_type.clone(),
        };

        let runner = ConnectorRunner::new(self.connector_config, Arc::new(connector));
        runner.run().await
    }
}

// =============================================================================
// App Builder (Type-State Pattern)
// =============================================================================

/// Builder for constructing App instances.
#[derive(Default)]
pub struct AppBuilder {
    // Manifest fields
    display_name: Option<String>,
    entry_path: Option<String>,
    description: Option<String>,
    icon: Option<String>,
    version: Option<String>,
    routes: Option<Vec<String>>,
    navigation: Option<NavigationConfig>,
    api_access: bool,

    // Static file config
    static_dir: Option<PathBuf>,
    /// Serve index.html for SPA routes (for future use)
    #[allow(dead_code)]
    serve_index_for_spa: bool,

    // Handler
    handler: Option<HandlerFn>,

    // Connector config
    host: Option<String>,
    tenant_id: Option<String>,
    instance_id: Option<String>,
    connector_name: Option<String>,
    use_tls: Option<bool>,
    local_port: Option<u16>,
}

impl AppBuilder {
    /// Set the app display name (REQUIRED).
    ///
    /// This is the human-readable name shown in the UI and used to derive the
    /// default `connector_name` (as `"app-{display_name}"`) when no explicit
    /// `.connector_name()` is provided.
    pub fn display_name(mut self, name: impl Into<String>) -> Self {
        self.display_name = Some(name.into());
        self
    }

    /// Alias for [`display_name`](Self::display_name) for backwards compatibility.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.display_name = Some(name.into());
        self
    }

    /// Set the entry path (default: "/").
    pub fn entry_path(mut self, path: impl Into<String>) -> Self {
        self.entry_path = Some(path.into());
        self
    }

    /// Set the app description.
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set the app icon.
    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set the app version.
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Set the available routes.
    pub fn routes(mut self, routes: &[&str]) -> Self {
        self.routes = Some(routes.iter().map(|s| s.to_string()).collect());
        self
    }

    /// Set the navigation configuration.
    pub fn navigation(mut self, nav: NavigationConfig) -> Self {
        self.navigation = Some(nav);
        self
    }

    /// Declare that this app needs GraphQL API access on behalf of the user.
    pub fn api_access(mut self) -> Self {
        self.api_access = true;
        self
    }

    /// Set the static files directory.
    pub fn static_dir(mut self, dir: impl Into<PathBuf>) -> Self {
        self.static_dir = Some(dir.into());
        self
    }

    /// Set a custom page handler.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// .handler(|req| async move {
    ///     AppPageResponse::html("<h1>Hello</h1>")
    /// })
    /// ```
    pub fn handler<F, Fut>(mut self, f: F) -> Self
    where
        F: Fn(AppPageRequest) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = AppPageResponse> + Send + 'static,
    {
        self.handler = Some(Arc::new(move |req| Box::pin(f(req))));
        self
    }

    /// Set the Strike48 server host.
    pub fn host(mut self, host: impl Into<String>) -> Self {
        self.host = Some(host.into());
        self
    }

    /// Set the tenant ID.
    pub fn tenant_id(mut self, id: impl Into<String>) -> Self {
        self.tenant_id = Some(id.into());
        self
    }

    /// Set the instance ID.
    pub fn instance_id(mut self, id: impl Into<String>) -> Self {
        self.instance_id = Some(id.into());
        self
    }

    /// Override the connector name used for server registration.
    ///
    /// This determines the server-side `gateway_id`. By default it is
    /// derived as `"app-{display_name}"`. Use this to dynamically
    /// customise the gateway identity at runtime (e.g. append a hostname).
    ///
    /// Priority: `.connector_name()` > derived from `APP_NAME` env / `.display_name()`.
    pub fn connector_name(mut self, name: impl Into<String>) -> Self {
        self.connector_name = Some(name.into());
        self
    }

    /// Enable/disable TLS.
    pub fn use_tls(mut self, enabled: bool) -> Self {
        self.use_tls = Some(enabled);
        self
    }

    /// Set a local development port (serves app locally for testing).
    pub fn local_port(mut self, port: u16) -> Self {
        self.local_port = Some(port);
        self
    }

    /// Build the App.
    ///
    /// The connector name (which determines the server-side `gateway_id`) is
    /// resolved with the following priority:
    /// 1. `.connector_name()` builder method (highest — programmatic override)
    /// 2. Derived from display name: `APP_NAME` env var > `.display_name()` → `"app-{display_name}"`
    pub fn build(self) -> Result<App> {
        // APP_NAME env var overrides the builder-provided display name.
        let display_name = std::env::var("APP_NAME")
            .ok()
            .filter(|s| !s.is_empty())
            .or(self.display_name)
            .ok_or_else(|| {
                ConnectorError::InvalidConfig("App display_name is required".to_string())
            })?;

        let entry_path = self.entry_path.unwrap_or_else(|| "/".to_string());

        // Build manifest
        let mut manifest = AppManifest::new(&display_name, &entry_path);
        manifest.description = self.description;
        manifest.icon = self.icon;
        manifest.version = self.version;
        manifest.routes = self.routes;
        manifest.navigation = self.navigation;
        manifest.api_access = self.api_access;

        // Build static config
        let static_config = self.static_dir.map(StaticFileConfig::new);

        // Use from_env() as base — it already parses URL schemes (ws://, wss://,
        // grpc://, grpcs://) from MATRIX_HOST / STRIKE48_HOST and stores the
        // resolved host:port, use_tls, and transport_type.
        let env_config = ConnectorConfig::from_env();

        let (resolved_host, use_tls, transport_type) = if let Some(explicit_host) = self.host {
            // Builder had an explicit host — parse it for scheme detection
            match crate::url_parser::parse_url(&explicit_host) {
                Ok(parsed) => (
                    parsed.host_port(),
                    self.use_tls.unwrap_or(parsed.use_tls),
                    parsed.transport,
                ),
                Err(_) => (
                    explicit_host,
                    self.use_tls.unwrap_or(env_config.use_tls),
                    env_config.transport_type,
                ),
            }
        } else {
            // No explicit host — use env_config's already-parsed values
            (
                env_config.host,
                self.use_tls.unwrap_or(env_config.use_tls),
                env_config.transport_type,
            )
        };

        let tenant_id = self.tenant_id.unwrap_or(env_config.tenant_id);

        // Prefer explicit instance_id, then INSTANCE_ID env var (via env_config),
        // then fall back to a display_name-based auto-generated id.
        let instance_id = self.instance_id.unwrap_or_else(|| {
            let env_instance = std::env::var("INSTANCE_ID").unwrap_or_default();
            if !env_instance.is_empty() {
                env_instance
            } else {
                format!(
                    "{}-{}",
                    display_name.to_lowercase().replace(' ', "-"),
                    chrono::Utc::now().timestamp_millis()
                )
            }
        });

        // .connector_name() builder override takes priority over display_name-derived value.
        let connector_name = self
            .connector_name
            .unwrap_or_else(|| format!("app-{}", display_name.to_lowercase().replace(' ', "-")));

        let connector_config = ConnectorConfig {
            host: resolved_host,
            tenant_id,
            connector_type: connector_name,
            instance_id,
            version: manifest
                .version
                .clone()
                .unwrap_or_else(|| "1.0.0".to_string()),
            auth_token: env_config.auth_token,
            use_tls,
            transport_type,
            max_concurrent_requests: 100,
            reconnect_enabled: true,
            reconnect_delay_ms: 500,
            max_backoff_delay_ms: 60000,
            reconnect_jitter_ms: 500,
            display_name: Some(display_name.clone()),
            tags: Vec::new(),
            metadata: std::collections::HashMap::new(),
            metrics_enabled: env_config.metrics_enabled,
            metrics_interval_ms: env_config.metrics_interval_ms,
            heartbeat_interval: None,
            heartbeat_timeout: None,
        };

        Ok(App {
            manifest,
            static_config,
            handler: self.handler,
            connector_config,
            local_port: self.local_port,
        })
    }

    /// Build and run the app.
    ///
    /// This is a convenience method that combines `build()` and `run()`.
    pub async fn run(self) -> Result<()> {
        self.build()?.run().await
    }
}

// =============================================================================
// Built App Connector (Internal)
// =============================================================================

/// Internal connector implementation created by App.
struct BuiltAppConnector {
    manifest: AppManifest,
    static_config: Option<StaticFileConfig>,
    handler: Option<HandlerFn>,
    /// Unique connector type derived from app name (e.g. "app-my-dashboard").
    /// This determines the gateway_id on the server, so each distinctly-named
    /// app gets its own gateway and sidebar entry.
    connector_type: String,
}

impl crate::connector::BaseConnector for BuiltAppConnector {
    fn connector_type(&self) -> &str {
        &self.connector_type
    }

    fn version(&self) -> &str {
        self.manifest.version.as_deref().unwrap_or("1.0.0")
    }

    fn behavior(&self) -> ConnectorBehavior {
        ConnectorBehavior::App
    }

    fn supported_encodings(&self) -> Vec<PayloadEncoding> {
        vec![PayloadEncoding::Json]
    }

    fn metadata(&self) -> HashMap<String, String> {
        let manifest_json = serde_json::to_string(&self.manifest).unwrap_or_default();
        let mut meta = HashMap::new();
        meta.insert("app_manifest".to_string(), manifest_json);
        meta.insert("timeout_ms".to_string(), "10000".to_string());
        meta
    }

    fn execute(
        &self,
        request: serde_json::Value,
        _capability_id: Option<&str>,
    ) -> Pin<Box<dyn Future<Output = Result<serde_json::Value>> + Send + '_>> {
        Box::pin(async move {
            // Parse the request
            let page_request: AppPageRequest =
                serde_json::from_value(request).unwrap_or_else(|_| AppPageRequest {
                    path: "/".to_string(),
                    params: HashMap::new(),
                });

            // Try static file first
            if let Some(ref static_config) = self.static_config
                && let Some(response) = static_config.try_serve(&page_request.path)
            {
                return Ok(serde_json::to_value(response)?);
            }

            // Call handler if available
            if let Some(ref handler) = self.handler {
                let response = handler(page_request).await;
                return Ok(serde_json::to_value(response)?);
            }

            // Default 404
            Ok(serde_json::to_value(AppPageResponse::not_found())?)
        })
    }
}

#[async_trait]
impl AppConnector for BuiltAppConnector {
    fn app_manifest(&self) -> AppManifest {
        self.manifest.clone()
    }

    fn static_file_config(&self) -> Option<&StaticFileConfig> {
        self.static_config.as_ref()
    }

    async fn render_page(&self, request: AppPageRequest) -> AppPageResponse {
        if let Some(ref handler) = self.handler {
            handler(request).await
        } else {
            AppPageResponse::not_found()
        }
    }
}

// =============================================================================
// Macros
// =============================================================================

/// Macro for quickly serving a static site to Strike48.
///
/// # Example
///
/// ```rust,ignore
/// use strike48_connector::serve_app;
///
/// #[tokio::main]
/// async fn main() {
///     serve_app!("./dist", name = "My React App").await.unwrap();
/// }
/// ```
#[macro_export]
macro_rules! serve_app {
    ($dir:expr) => {
        $crate::behaviors::serve::App::static_files($dir)
            .display_name(env!("CARGO_PKG_NAME"))
            .run()
    };
    ($dir:expr, name = $name:expr) => {
        $crate::behaviors::serve::App::static_files($dir)
            .display_name($name)
            .run()
    };
    ($dir:expr, name = $name:expr, icon = $icon:expr) => {
        $crate::behaviors::serve::App::static_files($dir)
            .display_name($name)
            .icon($icon)
            .run()
    };
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_builder_basic() {
        let app = App::builder()
            .display_name("Test App")
            .entry_path("/")
            .build();

        assert!(app.is_ok());
        let app = app.unwrap();
        assert_eq!(app.manifest.name, "Test App");
        assert_eq!(app.manifest.entry_path, "/");
        // Default connector_type derived from display_name
        assert_eq!(app.connector_config.connector_type, "app-test-app");
    }

    #[test]
    fn test_builder_requires_name() {
        let result = App::builder().build();
        assert!(result.is_err());
    }

    #[test]
    fn test_builder_with_static_files() {
        let app = App::static_files("./test")
            .display_name("Static App")
            .build();

        assert!(app.is_ok());
        let app = app.unwrap();
        assert!(app.static_config.is_some());
    }

    #[test]
    fn test_builder_with_connector_name() {
        let app = App::builder()
            .display_name("My App")
            .connector_name("app-my-app-prod-host1")
            .build();

        assert!(app.is_ok());
        let app = app.unwrap();
        assert_eq!(app.manifest.name, "My App");
        assert_eq!(app.connector_config.connector_type, "app-my-app-prod-host1");
    }

    #[test]
    fn test_builder_name_alias() {
        // .name() should work as alias for .display_name()
        let app = App::builder().name("Legacy Name").build();
        assert!(app.is_ok());
        let app = app.unwrap();
        assert_eq!(app.manifest.name, "Legacy Name");
    }

    #[test]
    fn test_builder_with_options() {
        let app = App::builder()
            .display_name("Full App")
            .description("A fully configured app")
            .icon("hero-star")
            .version("2.0.0")
            .navigation(NavigationConfig::top_level())
            .host("example.com:443")
            .tenant_id("my-tenant")
            .use_tls(true)
            .build();

        assert!(app.is_ok());
        let app = app.unwrap();
        assert_eq!(app.manifest.name, "Full App");
        assert_eq!(
            app.manifest.description,
            Some("A fully configured app".to_string())
        );
        assert_eq!(app.manifest.icon, Some("hero-star".to_string()));
        assert_eq!(app.manifest.version, Some("2.0.0".to_string()));
        assert!(app.manifest.navigation.is_some());
    }
}