Skip to main content

palladium_cli/commands/start/
mod.rs

1use std::path::{Path, PathBuf};
2
3use clap::Args;
4use schemars::JsonSchema;
5use serde::Serialize;
6use serde_json::json;
7
8use crate::CliResult;
9
10mod config;
11mod daemon;
12mod runtime;
13
14pub use config::StartFileConfig;
15
16#[derive(Args, Debug, Serialize, JsonSchema)]
17#[serde(rename_all = "kebab-case")]
18pub struct StartArgs {
19    /// Run in the foreground, blocking until the process is killed.
20    #[arg(long)]
21    pub foreground: bool,
22
23    /// Use the single-core engine instead of the default multi-core engine.
24    #[arg(long)]
25    pub single: bool,
26
27    /// Path to write the engine PID file (defaults to <socket>.pid).
28    #[arg(long)]
29    pub pid_file: Option<PathBuf>,
30
31    /// Enable plugin management (WASM and native).
32    #[arg(long, default_value = "true")]
33    pub enable_plugins: bool,
34
35    /// Output in JSON format.
36    #[arg(long)]
37    pub json: bool,
38
39    /// TCP control plane listener address (enables TCP+mTLS control plane).
40    #[arg(long)]
41    pub control_plane_tcp: Option<String>,
42
43    /// QUIC control plane listener address (enables QUIC+mTLS control plane).
44    #[arg(long)]
45    pub control_plane_quic: Option<String>,
46
47    /// Server certificate for TCP/QUIC control plane (PEM).
48    #[arg(long)]
49    pub control_plane_tls_cert: Option<PathBuf>,
50
51    /// Server private key for TCP/QUIC control plane (PEM).
52    #[arg(long)]
53    pub control_plane_tls_key: Option<PathBuf>,
54
55    /// CA certificate for validating client certs (PEM).
56    #[arg(long)]
57    pub control_plane_tls_ca: Option<PathBuf>,
58
59    /// Path to a TOML config file. If omitted, uses PD_CONFIG when set.
60    #[arg(long)]
61    pub config: Option<PathBuf>,
62}
63
64pub fn run(args: &StartArgs, socket: &Path) -> CliResult {
65    let pid_file = args
66        .pid_file
67        .clone()
68        .unwrap_or_else(|| socket.with_extension("pid"));
69
70    if args.foreground {
71        if args.json {
72            println!(
73                "{}",
74                json!({
75                    "status": "running",
76                    "mode": mode_name(args),
77                    "socket": socket,
78                    "foreground": true
79                })
80            );
81        }
82        runtime::run_foreground(args, socket, &pid_file)
83    } else {
84        daemon::run_daemon(args, socket, &pid_file)
85    }
86}
87
88pub fn run_daemon(args: &StartArgs, socket: &Path, pid_file: &Path) -> CliResult {
89    daemon::run_daemon(args, socket, pid_file)
90}
91
92pub fn run_foreground(args: &StartArgs, socket: &Path, pid_file: &Path) -> CliResult {
93    runtime::run_foreground(args, socket, pid_file)
94}
95
96pub(super) fn mode_name(args: &StartArgs) -> &'static str {
97    if args.single {
98        "single-core"
99    } else {
100        "multi-core"
101    }
102}