simple_server/
simple_server.rs

1//! A simple HTTP server example using cli-command for argument parsing.
2//!
3//! This example demonstrates how to use cli-command to parse command line arguments
4//! for a web server with various configuration options.
5
6use cli_command::{parse_command_line, Command};
7use std::net::{IpAddr, Ipv4Addr, SocketAddr};
8
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let cmd = parse_command_line()?;
11
12    match cmd.name.as_str() {
13        "serve" => {
14            println!("Starting HTTP server...");
15            start_server(&cmd)?;
16        }
17        "help" | "--help" | "-h" => {
18            print_help();
19        }
20        _ => {
21            println!("Unknown command: {}", cmd.name);
22            print_help();
23        }
24    }
25
26    Ok(())
27}
28
29fn start_server(cmd: &Command) -> Result<(), Box<dyn std::error::Error>> {
30    let host: IpAddr = cmd.get_argument_or_default("host", Ipv4Addr::new(127, 0, 0, 1).into())?;
31    let port: u16 = cmd.get_argument_or_default("port", 8080)?;
32    let workers: usize = cmd.get_argument_or_default("workers", 4)?;
33    let max_connections: usize = cmd.get_argument_or_default("max-connections", 1000)?;
34
35    let enable_ssl = cmd.contains_argument("ssl");
36    let enable_compression = cmd.contains_argument("compression");
37    let verbose = cmd.contains_argument("verbose") || cmd.contains_argument("v");
38
39    let config_file = cmd.get_argument("config");
40
41    let addr = SocketAddr::new(host, port);
42
43    println!("Server configuration:");
44    println!("  Address: {}", addr);
45    println!("  Workers: {}", workers);
46    println!("  Max connections: {}", max_connections);
47    println!("  SSL enabled: {}", enable_ssl);
48    println!("  Compression enabled: {}", enable_compression);
49    println!("  Verbose logging: {}", verbose);
50
51    if let Some(config) = config_file {
52        println!("  Config file: {}", config);
53    }
54
55    println!("\nServer would start on {} with {} workers", addr, workers);
56
57    Ok(())
58}
59
60fn print_help() {
61    println!("Simple HTTP Server");
62    println!();
63    println!("Usage:");
64    println!("  server serve [OPTIONS]");
65    println!("  server help");
66    println!();
67    println!("Commands:");
68    println!("  serve    Start the HTTP server");
69    println!("  help     Show this help message");
70    println!();
71    println!("Options:");
72    println!("  --host <IP>              Server host (default: 127.0.0.1)");
73    println!("  --port <PORT>            Server port (default: 8080)");
74    println!("  --workers <COUNT>        Number of worker threads (default: 4)");
75    println!("  --max-connections <NUM>  Maximum concurrent connections (default: 1000)");
76    println!("  --ssl                    Enable SSL/TLS");
77    println!("  --compression            Enable response compression");
78    println!("  --config <FILE>          Configuration file path");
79    println!("  --verbose, -v            Enable verbose logging");
80    println!();
81    println!("Examples:");
82    println!("  server serve --port 3000 --workers 8");
83    println!("  server serve --host 0.0.0.0 --port 80 --ssl");
84    println!("  server serve --config /etc/server.conf --verbose");
85}