features-cli 0.13.0

A CLI tool for discovering the features in a folder
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
//! HTTP server module for serving features data and embedded static files.
//!
//! This module provides functionality to start an HTTP server that serves:
//! - Features data as JSON at `/features.json`
//! - Embedded static files from the compiled binary
//! - A default index page at the root path
//!
//! The server uses the `warp` web framework and supports CORS for cross-origin requests.
//! Static files are embedded at compile time using the `include_dir` crate.

use anyhow::Result;
use include_dir::{Dir, include_dir};
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::time::sleep;
use warp::{Filter, Reply};

use crate::file_scanner::list_files_recursive_with_changes;
use crate::git_helper::get_repository_url;
use crate::models::Feature;

// Embed the public directory at compile time
static STATIC_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/public");

/// Configuration for the HTTP server
#[derive(Debug, Clone)]
pub struct ServerConfig {
    /// Port to run the server on
    pub port: u16,
    /// Host address to bind to
    pub host: [u8; 4],
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            port: 3000,
            host: [127, 0, 0, 1],
        }
    }
}

impl ServerConfig {
    /// Create a new server configuration with custom port
    pub fn new(port: u16) -> Self {
        Self {
            port,
            ..Default::default()
        }
    }

    /// Set the host address to bind to
    #[allow(dead_code)]
    pub fn with_host(mut self, host: [u8; 4]) -> Self {
        self.host = host;
        self
    }
}

/// Starts an HTTP server with file watching for a specific directory.
///
/// # Arguments
///
/// * `features` - Initial Feature objects to serve as JSON
/// * `port` - Port number to run the server on
/// * `watch_path` - Path to watch for file changes
/// * `on_ready` - Optional callback to be called when server is ready
/// * `skip_changes` - Whether changes were skipped during feature computation
/// * `ignore_paths` - Folders to ignore when features are recomputed
/// * `include_skipped_dirs` - Whether to scan folders skipped by default when features are recomputed
///
/// # Returns
///
/// * `Result<()>` - Ok if server starts successfully, Err otherwise
pub async fn serve_features_with_watching(
    features: &[Feature],
    port: u16,
    watch_path: PathBuf,
    on_ready: Option<Box<dyn FnOnce() + Send>>,
    skip_changes: bool,
    ignore_paths: Vec<PathBuf>,
    include_skipped_dirs: bool,
) -> Result<()> {
    let config = ServerConfig::new(port);
    serve_features_with_config_and_watching(
        features,
        config,
        Some(watch_path.clone()),
        on_ready,
        skip_changes,
        ignore_paths,
        include_skipped_dirs,
    )
    .await
}

/// Starts an HTTP server with custom configuration and optional file watching.
///
/// # Arguments
///
/// * `features` - Slice of Feature objects to serve as JSON
/// * `config` - Server configuration
/// * `watch_path` - Optional path to watch for file changes
/// * `on_ready` - Optional callback to be called when server is ready
/// * `skip_changes` - Whether changes were skipped during feature computation
/// * `ignore_paths` - Folders to ignore when features are recomputed
/// * `include_skipped_dirs` - Whether to scan folders skipped by default when features are recomputed
///
/// # Returns
///
/// * `Result<()>` - Ok if server starts successfully, Err otherwise
pub async fn serve_features_with_config_and_watching(
    features: &[Feature],
    config: ServerConfig,
    watch_path: Option<PathBuf>,
    on_ready: Option<Box<dyn FnOnce() + Send>>,
    skip_changes: bool,
    ignore_paths: Vec<PathBuf>,
    include_skipped_dirs: bool,
) -> Result<()> {
    // Create shared state for features
    let features_data = Arc::new(RwLock::new(features.to_vec()));

    // Get repository URL from git config
    let repository_url = watch_path
        .as_ref()
        .and_then(|path| get_repository_url(path));

    // Set up file watching if watch_path is provided
    if let Some(ref path) = watch_path {
        let features_data_clone = Arc::clone(&features_data);
        let watch_path_clone = path.clone();

        tokio::spawn(async move {
            if let Err(e) = setup_file_watcher(
                features_data_clone,
                watch_path_clone,
                ignore_paths,
                include_skipped_dirs,
            )
            .await
            {
                eprintln!("File watcher error: {}", e);
            }
        });
    }

    // Route for features.json with shared state
    let features_data_clone = Arc::clone(&features_data);
    let features_route = warp::path("features.json")
        .and(warp::get())
        .and_then(move || {
            let features_data = Arc::clone(&features_data_clone);
            async move {
                let features = features_data.read().await;
                let features_json = match serde_json::to_string_pretty(&*features) {
                    Ok(json) => json,
                    Err(e) => {
                        eprintln!("Failed to serialize features: {}", e);
                        return Err(warp::reject::custom(SerializationError));
                    }
                };

                Ok::<_, warp::Rejection>(warp::reply::with_header(
                    features_json,
                    "content-type",
                    "application/json",
                ))
            }
        });

    // Route for metadata.json with version info and repository URL
    let metadata_route = warp::path("metadata.json")
        .and(warp::get())
        .and_then(move || {
            let repo_url = repository_url.clone();
            async move {
                let mut metadata = serde_json::json!({
                    "version": env!("CARGO_PKG_VERSION"),
                    "skipChanges": skip_changes
                });

                // Add repository URL if available
                if let Some(url) = repo_url {
                    metadata["repository"] = serde_json::json!(url);
                }

                let metadata_json = match serde_json::to_string_pretty(&metadata) {
                    Ok(json) => json,
                    Err(e) => {
                        eprintln!("Failed to serialize metadata: {}", e);
                        return Err(warp::reject::custom(SerializationError));
                    }
                };

                Ok::<_, warp::Rejection>(warp::reply::with_header(
                    metadata_json,
                    "content-type",
                    "application/json",
                ))
            }
        });

    // Route for root path to serve index.html
    let index_route = warp::path::end().and(warp::get()).and_then(serve_index);

    // Route for static files from embedded directory
    let static_route = warp::path::tail()
        .and(warp::get())
        .and_then(serve_static_file);

    let routes = features_route
        .or(metadata_route)
        .or(index_route)
        .or(static_route)
        .with(warp::cors().allow_any_origin())
        .recover(handle_rejection);

    // Call the ready callback if provided
    if let Some(callback) = on_ready {
        callback();
    }

    println!(
        "Server running at http://{}:{}",
        config
            .host
            .iter()
            .map(|&b| b.to_string())
            .collect::<Vec<_>>()
            .join("."),
        config.port,
    );
    warp::serve(routes).run((config.host, config.port)).await;

    Ok(())
}

/// Sets up file system watching for the specified path.
async fn setup_file_watcher(
    features_data: Arc<RwLock<Vec<Feature>>>,
    watch_path: PathBuf,
    ignore_paths: Vec<PathBuf>,
    include_skipped_dirs: bool,
) -> Result<()> {
    let (tx, mut rx) = tokio::sync::mpsc::channel(100);

    // Set up the file watcher in a blocking task
    let watch_path_clone = watch_path.clone();
    let _watcher = tokio::task::spawn_blocking(move || -> Result<RecommendedWatcher> {
        let mut watcher = RecommendedWatcher::new(
            move |res: notify::Result<Event>| {
                match res {
                    Ok(event) => {
                        // Send event through channel
                        if let Err(e) = tx.blocking_send(event) {
                            eprintln!("Failed to send file system event: {}", e);
                        }
                    }
                    Err(e) => eprintln!("File watcher error: {:?}", e),
                }
            },
            Config::default(),
        )?;

        watcher.watch(&watch_path_clone, RecursiveMode::Recursive)?;
        Ok(watcher)
    })
    .await??;

    // Process file system events
    while let Some(event) = rx.recv().await {
        // Check if this is a file we care about (README.md files or directory changes)
        let should_recompute = event.paths.iter().any(|path| {
            path.file_name()
                .map(|name| name == "README.md")
                .unwrap_or(false)
                || event.kind.is_create()
                || event.kind.is_remove()
        });

        if should_recompute {
            // Add a small delay to avoid excessive recomputation during rapid changes
            sleep(Duration::from_millis(500)).await;

            match list_files_recursive_with_changes(
                &watch_path,
                &ignore_paths,
                include_skipped_dirs,
            ) {
                Ok(new_features) => {
                    let mut features = features_data.write().await;
                    *features = new_features;
                    println!("✅ Features updated successfully");
                }
                Err(e) => {
                    eprintln!("❌ Failed to recompute features: {}", e);
                }
            }
        }
    }

    Ok(())
}

/// Custom error type for serialization failures
#[derive(Debug)]
struct SerializationError;
impl warp::reject::Reject for SerializationError {}

/// Serves the index page for the root path.
///
/// If embedded `index.html` exists, it will be served. Otherwise, a default
/// HTML page with navigation links will be returned.
///
/// # Returns
///
/// * `Result<impl warp::Reply, warp::Rejection>` - HTML response or rejection
async fn serve_index() -> Result<impl warp::Reply, warp::Rejection> {
    if let Some(file) = STATIC_DIR.get_file("index.html") {
        let content = file.contents_utf8().unwrap_or("");
        Ok(
            warp::reply::with_header(content, "content-type", "text/html; charset=utf-8")
                .into_response(),
        )
    } else {
        let html = create_default_index_html();
        Ok(
            warp::reply::with_header(html, "content-type", "text/html; charset=utf-8")
                .into_response(),
        )
    }
}

/// Serves static files from the embedded directory.
///
/// # Arguments
///
/// * `path` - The requested file path
///
/// # Returns
///
/// * `Result<impl warp::Reply, warp::Rejection>` - File content or rejection
async fn serve_static_file(path: warp::path::Tail) -> Result<impl warp::Reply, warp::Rejection> {
    let path_str = path.as_str();

    // Try to get the file from the embedded directory
    if let Some(file) = STATIC_DIR.get_file(path_str) {
        let content_type = get_content_type(path_str);

        if let Some(contents) = file.contents_utf8() {
            // Text file
            Ok(warp::reply::with_header(contents, "content-type", content_type).into_response())
        } else {
            // Binary file
            Ok(
                warp::reply::with_header(file.contents(), "content-type", content_type)
                    .into_response(),
            )
        }
    } else {
        Err(warp::reject::not_found())
    }
}

/// Determines the content type based on file extension.
///
/// # Arguments
///
/// * `path` - The file path
///
/// # Returns
///
/// * `&'static str` - The appropriate MIME type
fn get_content_type(path: &str) -> &'static str {
    let extension = Path::new(path)
        .extension()
        .and_then(|ext| ext.to_str())
        .unwrap_or("");

    match extension.to_lowercase().as_str() {
        "html" => "text/html; charset=utf-8",
        "css" => "text/css; charset=utf-8",
        "js" => "application/javascript; charset=utf-8",
        "json" => "application/json; charset=utf-8",
        "svg" => "image/svg+xml",
        "png" => "image/png",
        "jpg" | "jpeg" => "image/jpeg",
        "gif" => "image/gif",
        "ico" => "image/x-icon",
        "txt" => "text/plain; charset=utf-8",
        "pdf" => "application/pdf",
        "xml" => "application/xml; charset=utf-8",
        "woff" => "font/woff",
        "woff2" => "font/woff2",
        "ttf" => "font/ttf",
        "eot" => "application/vnd.ms-fontobject",
        _ => "application/octet-stream",
    }
}

/// Creates the default HTML page when no index.html is found.
fn create_default_index_html() -> String {
    r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Features Dashboard</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 40px;
            line-height: 1.6;
            background: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 { color: #333; }
        .links { list-style: none; padding: 0; }
        .links li { margin: 10px 0; }
        .links a {
            color: #007acc;
            text-decoration: none;
            padding: 8px 15px;
            border: 1px solid #007acc;
            border-radius: 4px;
            display: inline-block;
        }
        .links a:hover { background: #007acc; color: white; }
    </style>
</head>
<body>
    <div class="container">
        <h1>đŸ—ī¸ Features Dashboard</h1>
        <p>Welcome to the feature-based architecture server!</p>
        <ul class="links">
            <li><a href="/features.json">📊 View Features JSON</a></li>
            <li><a href="/metadata.json">â„šī¸ View Metadata JSON</a></li>
        </ul>
        <p><small>This server provides features data and serves embedded static files from the binary.</small></p>
    </div>
</body>
</html>"#.to_string()
}

/// Handles HTTP request rejections and converts them to appropriate responses.
async fn handle_rejection(
    err: warp::Rejection,
) -> Result<impl warp::Reply, std::convert::Infallible> {
    let (code, message) = if err.is_not_found() {
        (warp::http::StatusCode::NOT_FOUND, "NOT_FOUND")
    } else if err
        .find::<warp::filters::body::BodyDeserializeError>()
        .is_some()
    {
        (warp::http::StatusCode::BAD_REQUEST, "BAD_REQUEST")
    } else if err.find::<warp::reject::MethodNotAllowed>().is_some() {
        (
            warp::http::StatusCode::METHOD_NOT_ALLOWED,
            "METHOD_NOT_ALLOWED",
        )
    } else {
        eprintln!("Unhandled rejection: {:?}", err);
        (
            warp::http::StatusCode::INTERNAL_SERVER_ERROR,
            "INTERNAL_SERVER_ERROR",
        )
    };

    Ok(warp::reply::with_status(message, code))
}