skillet 0.6.3

Skillet: micro expression language (arithmetic, logical, functions, arrays, conditionals, excel formulas) made in Rust bin cli and server
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
use skillet::{evaluate_with_custom, evaluate_with_assignments, Value, JSPluginLoader};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::{TcpListener, TcpStream};
use std::io::{BufRead, BufReader, Write};
use std::sync::{Arc, atomic::{AtomicU64, AtomicBool, Ordering}};
use std::time::Instant;

/// High-performance Skillet evaluation server
/// Eliminates process spawn overhead by keeping interpreter in memory
/// Supports concurrent request processing with connection pooling

#[derive(Debug, Deserialize)]
struct EvalRequest {
    expression: String,
    variables: Option<HashMap<String, serde_json::Value>>,
    output_json: Option<bool>,
    token: Option<String>,
}

#[derive(Debug, Serialize)]
struct EvalResponse {
    success: bool,
    result: Option<serde_json::Value>,
    error: Option<String>,
    execution_time_ms: f64,
    request_id: u64,
}

struct ServerStats {
    requests_processed: AtomicU64,
    total_execution_time: AtomicU64, // in microseconds
}

impl ServerStats {
    fn new() -> Self {
        Self {
            requests_processed: AtomicU64::new(0),
            total_execution_time: AtomicU64::new(0),
        }
    }
    
    fn record_request(&self, execution_time_us: u64) {
        self.requests_processed.fetch_add(1, Ordering::Relaxed);
        self.total_execution_time.fetch_add(execution_time_us, Ordering::Relaxed);
    }
    
    fn get_stats(&self) -> (u64, f64) {
        let count = self.requests_processed.load(Ordering::Relaxed);
        let total_time = self.total_execution_time.load(Ordering::Relaxed);
        let avg_time_ms = if count > 0 { 
            total_time as f64 / count as f64 / 1000.0 
        } else { 0.0 };
        (count, avg_time_ms)
    }
}

fn handle_client(
    mut stream: TcpStream,
    stats: Arc<ServerStats>,
    request_counter: Arc<AtomicU64>,
    server_token: Arc<Option<String>>,
) {
    let reader = BufReader::new(stream.try_clone().unwrap());
    
    for line in reader.lines() {
        let line = match line {
            Ok(line) => line,
            Err(_) => break,
        };
        
        if line.trim().is_empty() {
            continue;
        }
        
        let request_id = request_counter.fetch_add(1, Ordering::Relaxed);
        let start_time = Instant::now();
        
        let response = match serde_json::from_str::<EvalRequest>(&line) {
            Ok(req) => {
                if let Some(cfg_token) = server_token.as_ref() {
                    let supplied = req.token.as_deref().unwrap_or("");
                    if supplied != cfg_token {
                        EvalResponse {
                            success: false,
                            result: None,
                            error: Some("Unauthorized: invalid token".to_string()),
                            execution_time_ms: 0.0,
                            request_id,
                        }
                    } else {
                        process_request(req, request_id)
                    }
                } else {
                    process_request(req, request_id)
                }
            },
            Err(e) => EvalResponse {
                success: false,
                result: None,
                error: Some(format!("Invalid JSON request: {}", e)),
                execution_time_ms: 0.0,
                request_id,
            },
        };
        
        let execution_time = start_time.elapsed();
        stats.record_request(execution_time.as_micros() as u64);
        
        let response_json = serde_json::to_string(&response).unwrap_or_else(|_| {
            format!(r#"{{"success":false,"error":"Failed to serialize response","request_id":{}}}"#, request_id)
        });
        
        if let Err(_) = writeln!(stream, "{}", response_json) {
            break;
        }
        
        // Log request for monitoring
        if request_id % 1000 == 0 {
            let (total_requests, avg_time) = stats.get_stats();
            eprintln!("Processed {} requests, avg execution time: {:.2}ms", 
                total_requests, avg_time);
        }
    }
}

fn process_request(req: EvalRequest, request_id: u64) -> EvalResponse {
    let start_time = Instant::now();
    
    // Convert JSON variables to Skillet values
    let vars = match req.variables {
        Some(json_vars) => {
            let mut result = HashMap::new();
            for (key, value) in json_vars {
                match skillet::json_to_value(value) {
                    Ok(v) => { result.insert(key, v); }
                    Err(e) => {
                        return EvalResponse {
                            success: false,
                            result: None,
                            error: Some(format!("Error converting variable '{}': {}", key, e)),
                            execution_time_ms: start_time.elapsed().as_secs_f64() * 1000.0,
                            request_id,
                        };
                    }
                }
            }
            result
        }
        None => HashMap::new(),
    };
    
    // Evaluate expression
    let result = if req.expression.contains(";") || req.expression.contains(":=") {
        evaluate_with_assignments(&req.expression, &vars)
    } else {
        evaluate_with_custom(&req.expression, &vars)
    };
    
    let execution_time_ms = start_time.elapsed().as_secs_f64() * 1000.0;
    
    match result {
        Ok(val) => {
            let result_json = if req.output_json.unwrap_or(false) {
                // Format as structured JSON output
                let (result_value, type_name) = match val {
                    Value::Number(n) => (serde_json::json!(n), "Number"),
                    Value::String(s) => (serde_json::json!(s), "String"), 
                    Value::Boolean(b) => (serde_json::json!(b), "Boolean"),
                    Value::Currency(c) => (serde_json::json!(c), "Currency"),
                    Value::DateTime(dt) => (serde_json::json!(dt), "DateTime"),
                    Value::Array(arr) => {
                        let json_arr: Vec<serde_json::Value> = arr.iter().map(|v| match v {
                            Value::Number(n) => serde_json::json!(n),
                            Value::String(s) => serde_json::json!(s),
                            Value::Boolean(b) => serde_json::json!(b),
                            Value::Currency(c) => serde_json::json!(c),
                            Value::DateTime(dt) => serde_json::json!(dt),
                            Value::Null => serde_json::json!(null),
                            Value::Array(_) => serde_json::json!(format!("{:?}", v)),
                            Value::Json(s) => serde_json::from_str(&s).unwrap_or_else(|_| serde_json::json!(s)),
                        }).collect();
                        (serde_json::json!(json_arr), "Array")
                    },
                    Value::Null => (serde_json::json!(null), "Null"),
                    Value::Json(s) => {
                        match serde_json::from_str(&s) {
                            Ok(parsed) => (parsed, "Json"),
                            Err(_) => (serde_json::json!(s), "Json")
                        }
                    }
                };
                
                serde_json::json!({
                    "result": result_value,
                    "type": type_name,
                    "execution_time": format!("{:.2} ms", execution_time_ms)
                })
            } else {
                // Simple value output
                match val {
                    Value::Number(n) => serde_json::json!(n),
                    Value::String(s) => serde_json::json!(s),
                    Value::Boolean(b) => serde_json::json!(b),
                    Value::Currency(c) => serde_json::json!(c),
                    Value::DateTime(dt) => serde_json::json!(dt.to_string()),
                    Value::Array(arr) => {
                        let json_arr: Vec<serde_json::Value> = arr.iter().map(|v| match v {
                            Value::Number(n) => serde_json::json!(n),
                            Value::String(s) => serde_json::json!(s),
                            Value::Boolean(b) => serde_json::json!(b),
                            Value::Currency(c) => serde_json::json!(c),
                            Value::DateTime(dt) => serde_json::json!(dt.to_string()),
                            Value::Null => serde_json::json!(null),
                            Value::Array(_) => serde_json::json!(format!("{:?}", v)),
                            Value::Json(s) => serde_json::from_str(&s).unwrap_or_else(|_| serde_json::json!(s)),
                        }).collect();
                        serde_json::json!(json_arr)
                    },
                    Value::Null => serde_json::json!(null),
                    Value::Json(s) => serde_json::from_str(&s).unwrap_or_else(|_| serde_json::json!(s)),
                }
            };
            
            EvalResponse {
                success: true,
                result: Some(result_json),
                error: None,
                execution_time_ms,
                request_id,
            }
        }
        Err(e) => EvalResponse {
            success: false,
            result: None,
            error: Some(e.to_string()),
            execution_time_ms,
            request_id,
        },
    }
}

fn daemonize() -> Result<(), Box<dyn std::error::Error>> {
    use std::fs::OpenOptions;
    use std::os::unix::io::AsRawFd;
    
    // Fork the process
    match unsafe { libc::fork() } {
        -1 => return Err("Failed to fork process".into()),
        0 => {
            // Child process continues
        }
        _ => {
            // Parent process exits
            std::process::exit(0);
        }
    }
    
    // Create a new session
    if unsafe { libc::setsid() } == -1 {
        return Err("Failed to create new session".into());
    }
    
    // Fork again to ensure we're not a session leader
    match unsafe { libc::fork() } {
        -1 => return Err("Failed to fork second time".into()),
        0 => {
            // Grandchild continues
        }
        _ => {
            // Child exits
            std::process::exit(0);
        }
    }
    
    // DON'T change working directory to root - stay in current directory
    // This allows relative paths to work (like hooks directory)
    // std::env::set_current_dir("/").ok();
    
    // Close standard file descriptors
    unsafe {
        libc::close(libc::STDIN_FILENO);
        libc::close(libc::STDOUT_FILENO);
        libc::close(libc::STDERR_FILENO);
    }
    
    // Redirect stdin, stdout, stderr to /dev/null
    let dev_null = OpenOptions::new()
        .read(true)
        .write(true)
        .open("/dev/null")?;
    
    let null_fd = dev_null.as_raw_fd();
    unsafe {
        libc::dup2(null_fd, libc::STDIN_FILENO);
        libc::dup2(null_fd, libc::STDOUT_FILENO);
        libc::dup2(null_fd, libc::STDERR_FILENO);
    }
    
    Ok(())
}

fn write_pid_file(pid_file: &str) -> Result<(), Box<dyn std::error::Error>> {
    use std::fs::File;
    use std::io::Write;
    
    let pid = std::process::id();
    let mut file = File::create(pid_file)?;
    writeln!(file, "{}", pid)?;
    Ok(())
}

fn setup_signal_handlers() -> Arc<AtomicBool> {
    // Handle SIGTERM and SIGINT gracefully
    let running = Arc::new(AtomicBool::new(true));
    let r = running.clone();
    ctrlc::set_handler(move || {
        eprintln!("Received shutdown signal, gracefully stopping...");
        r.store(false, Ordering::SeqCst);
    }).expect("Error setting signal handler");
    running
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    
    if args.len() < 2 {
        eprintln!("Usage: sk_server <port> [num_threads] [options]");
        eprintln!("");
        eprintln!("Options:");
        eprintln!("  -d, --daemon         Run as daemon (background process)");
        eprintln!("  -H, --host <addr>    Bind host/interface (default: 127.0.0.1)");
        eprintln!("  --pid-file <file>    Write PID to file (default: skillet-server.pid)");
        eprintln!("  --log-file <file>    Write logs to file (daemon mode only)");
        eprintln!("  --token <value>      Require token for requests (or set SKILLET_AUTH_TOKEN)");
        eprintln!("");
        eprintln!("Examples:");
        eprintln!("  sk_server 8080              # Start server on port 8080");
        eprintln!("  sk_server 8080 16           # Start with 16 worker threads");
        eprintln!("  sk_server 8080 8 -d         # Run as daemon with 8 threads");
        eprintln!("  sk_server 8080 --host 0.0.0.0   # Expose on all interfaces");
        eprintln!("  sk_server 8080 --host 0.0.0.0 --token <secret>");
        eprintln!("  sk_server 8080 -d --pid-file /var/run/skillet.pid");
        eprintln!("");
        eprintln!("Protocol: Send JSON requests as newline-delimited messages");
        eprintln!("Request format: {{\"expression\": \"=2+3\", \"variables\": {{\"x\": 10}}, \"output_json\": true}}");
        std::process::exit(1);
    }
    
    let port: u16 = args[1].parse().unwrap_or_else(|_| {
        eprintln!("Error: Invalid port number");
        std::process::exit(1);
    });
    
    // Parse arguments
    let mut num_threads: usize = num_cpus::get();
    let mut bind_host: String = std::env::var("SKILLET_BIND_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
    let mut auth_token: Option<String> = std::env::var("SKILLET_AUTH_TOKEN").ok();
    let mut daemon_mode = false;
    let mut pid_file = "skillet-server.pid".to_string();
    let mut _log_file: Option<String> = None;
    let mut i = 2;
    
    while i < args.len() {
        match args[i].as_str() {
            "-d" | "--daemon" => {
                daemon_mode = true;
            }
            "-H" | "--host" => {
                if i + 1 < args.len() {
                    bind_host = args[i + 1].clone();
                    i += 1;
                } else {
                    eprintln!("Error: --host requires an address (e.g. 0.0.0.0)");
                    std::process::exit(1);
                }
            }
            "--pid-file" => {
                if i + 1 < args.len() {
                    pid_file = args[i + 1].clone();
                    i += 1;
                } else {
                    eprintln!("Error: --pid-file requires a filename");
                    std::process::exit(1);
                }
            }
            "--log-file" => {
                if i + 1 < args.len() {
                    _log_file = Some(args[i + 1].clone());
                    i += 1;
                } else {
                    eprintln!("Error: --log-file requires a filename");
                    std::process::exit(1);
                }
            }
            "--token" => {
                if i + 1 < args.len() {
                    auth_token = Some(args[i + 1].clone());
                    i += 1;
                } else {
                    eprintln!("Error: --token requires a value");
                    std::process::exit(1);
                }
            }
            arg => {
                // Try to parse as thread count if it's a number
                if let Ok(threads) = arg.parse::<usize>() {
                    num_threads = threads;
                } else {
                    eprintln!("Error: Unknown argument: {}", arg);
                    std::process::exit(1);
                }
            }
        }
        i += 1;
    }
    
    // Handle daemon mode before any output
    if daemon_mode {
        #[cfg(unix)]
        {
            // Print startup message before daemonizing
            eprintln!("Starting Skillet server as daemon...");
            eprintln!("Port: {}, Threads: {}, PID file: {}", port, num_threads, pid_file);
            
            if let Err(e) = daemonize() {
                eprintln!("Failed to daemonize: {}", e);
                std::process::exit(1);
            }
            
            // Write PID file after successful daemonization
            if let Err(_e) = write_pid_file(&pid_file) {
                // Log to syslog or a file since we can't use stderr
                std::process::exit(1);
            }
        }
        #[cfg(not(unix))]
        {
            eprintln!("Error: Daemon mode not supported on this platform");
            std::process::exit(1);
        }
    }
    
    // Setup signal handlers and running flag
    let running = setup_signal_handlers();
    
    // Auto-load JavaScript functions
    let hooks_dir = std::env::var("SKILLET_HOOKS_DIR").unwrap_or_else(|_| "hooks".to_string());
    let js_loader = JSPluginLoader::new(hooks_dir);
    
    match js_loader.auto_register() {
        Ok(count) => {
            if count > 0 && !daemon_mode {
                eprintln!("Loaded {} custom JavaScript function(s)", count);
            }
        }
        Err(e) => {
            if !daemon_mode {
                eprintln!("Warning: Failed to load JavaScript functions: {}", e);
            }
        }
    }
    
    let listener = TcpListener::bind(format!("{}:{}", bind_host, port))
        .unwrap_or_else(|e| {
            eprintln!("Error: Failed to bind to {}:{}: {}", bind_host, port, e);
            std::process::exit(1);
        });

    // Make listener non-blocking so we can check shutdown flag
    listener
        .set_nonblocking(true)
        .unwrap_or_else(|e| {
            eprintln!("Error: Failed to set non-blocking mode: {}", e);
            std::process::exit(1);
        });
    
    let stats = Arc::new(ServerStats::new());
    let request_counter = Arc::new(AtomicU64::new(0));
    
    if !daemon_mode {
        eprintln!("🚀 Skillet Server started on {}:{}", bind_host, port);
        eprintln!("📊 Worker threads: {}", num_threads);
        if auth_token.is_some() { eprintln!("🔒 Token auth: enabled"); }
        eprintln!("🔧 Ready for high-throughput expression evaluation");
        eprintln!("");
    }
    
    // Use a thread pool for handling connections
    let pool = threadpool::ThreadPool::new(num_threads);

    // Accept loop that can be interrupted by Ctrl+C
    let server_token = Arc::new(auth_token);
    while running.load(Ordering::Relaxed) {
        match listener.accept() {
            Ok((stream, _addr)) => {
                let stats = Arc::clone(&stats);
                let request_counter = Arc::clone(&request_counter);
                let server_token = Arc::clone(&server_token);
                pool.execute(move || {
                    handle_client(stream, stats, request_counter, server_token);
                });
            }
            Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                // No pending connections; sleep briefly and check again
                // Keep this very small to avoid per-connection latency
                std::thread::sleep(std::time::Duration::from_millis(1));
            }
            Err(e) => {
                eprintln!("Error accepting connection: {}", e);
                // Back off briefly on accept errors, but keep it tight
                std::thread::sleep(std::time::Duration::from_millis(10));
            }
        }
    }

    // Wait for outstanding tasks to complete
    pool.join();
    eprintln!("Server shutdown complete.");
}