pub fn run(
hexz_path: String,
port: u16,
daemon: bool,
nbd: bool,
s3: bool,
) -> Result<()>Expand description
Executes the serve command to start a network server.
Opens a Hexz snapshot and starts a server that exposes it over HTTP, NBD, or S3 protocol. The server runs until interrupted (Ctrl+C) or, in daemon mode, until explicitly killed.
§Arguments
hexz_path- Path to the.hxzsnapshot file to serveport- TCP port to bind todaemon- If true, daemonize the process and run in backgroundnbd- If true, use NBD protocol; otherwise use HTTPs3- If true, use S3 gateway mode (not yet implemented)
§Server Startup Sequence
-
Daemonization (if requested):
- Detach from terminal
- Redirect stdout to
/tmp/hexz-serve.log - Redirect stderr to
/tmp/hexz-serve.err
-
Snapshot Loading:
- Open snapshot file via
FileBackend - Read and parse header
- Load compression dictionary if present
- Initialize decompressor (LZ4 or Zstd)
- Open snapshot file via
-
Server Start:
- Create Tokio multi-threaded runtime
- Start HTTP or NBD server on specified port
- Listen for incoming connections
-
Serving:
- Handle requests until process is interrupted
- Each request decompresses data on-demand
- No persistent state or caching
§Protocol Selection
- If
nbd=true: Start NBD server viahexz_server::serve_nbd() - If
s3=true: Reserved for future S3 gateway (currently prints error) - Otherwise: Start HTTP server via
hexz_server::serve_http()
§Errors
Returns an error if:
- Snapshot file cannot be opened or read
- Header or dictionary cannot be parsed (corrupted snapshot)
- Port is already in use (address binding fails)
- Daemonization fails (resource limits, permissions)
- Server fails to start (Tokio runtime error)
§Examples
use hexz_cli::cmd::sys::serve;
// Start HTTP server on port 8080
serve::run(
"snapshot.hxz".to_string(),
8080,
false, // not daemon
false, // HTTP mode
false, // not S3
)?;
// Start NBD server as daemon
serve::run(
"snapshot.hxz".to_string(),
10809,
true, // daemon mode
true, // NBD mode
false,
)?;