feat: implement server configuration with address and port options
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
12
src/args.rs
12
src/args.rs
@@ -64,8 +64,16 @@ pub struct ModeArgs {
|
|||||||
pub status: bool,
|
pub status: bool,
|
||||||
|
|
||||||
#[arg(group("mode"), help_heading("Mode Options"), long, conflicts_with_all(["save", "get", "diff", "list", "update", "delete", "info", "status"]))]
|
#[arg(group("mode"), help_heading("Mode Options"), long, conflicts_with_all(["save", "get", "diff", "list", "update", "delete", "info", "status"]))]
|
||||||
#[arg(help("Start REST HTTP server on specified address:port or socket path"))]
|
#[arg(help("Start REST HTTP server"))]
|
||||||
pub server: Option<String>,
|
pub server: bool,
|
||||||
|
|
||||||
|
#[arg(help_heading("Server Options"), long, env("KEEP_SERVER_ADDRESS"))]
|
||||||
|
#[arg(help("Server address to bind to"))]
|
||||||
|
pub server_address: Option<String>,
|
||||||
|
|
||||||
|
#[arg(help_heading("Server Options"), long, env("KEEP_SERVER_PORT"))]
|
||||||
|
#[arg(help("Server port to bind to"))]
|
||||||
|
pub server_port: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -26,11 +26,27 @@ pub fn mode_server(
|
|||||||
conn: &mut rusqlite::Connection,
|
conn: &mut rusqlite::Connection,
|
||||||
data_path: PathBuf,
|
data_path: PathBuf,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let server_address = settings.get_server_address(&crate::args::Args::parse(), config)
|
// Get server address from args or config with default
|
||||||
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
|
let server_address = if let Some(addr) = &settings.server_address {
|
||||||
|
addr.clone()
|
||||||
|
} else if let Some(server_config) = &config.server {
|
||||||
|
server_config.address.clone().unwrap_or_else(|| "127.0.0.1".to_string())
|
||||||
|
} else {
|
||||||
|
"127.0.0.1".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get server port from args or config with default
|
||||||
|
let server_port = if let Some(port) = settings.server_port {
|
||||||
|
port
|
||||||
|
} else if let Some(server_config) = &config.server {
|
||||||
|
server_config.port.unwrap_or(21080)
|
||||||
|
} else {
|
||||||
|
21080
|
||||||
|
};
|
||||||
|
|
||||||
let server_config = common::ServerConfig {
|
let server_config = common::ServerConfig {
|
||||||
address: server_address,
|
address: server_address,
|
||||||
|
port: Some(server_port),
|
||||||
password: settings.server_password.clone(),
|
password: settings.server_password.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -46,7 +62,14 @@ async fn run_server(
|
|||||||
conn: rusqlite::Connection,
|
conn: rusqlite::Connection,
|
||||||
data_dir: PathBuf,
|
data_dir: PathBuf,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
debug!("SERVER: Starting REST HTTP server on {}", config.address);
|
// Construct address with port
|
||||||
|
let bind_address = if let Some(port) = config.port {
|
||||||
|
format!("{}:{}", config.address, port)
|
||||||
|
} else {
|
||||||
|
format!("{}:21080", config.address)
|
||||||
|
};
|
||||||
|
|
||||||
|
debug!("SERVER: Starting REST HTTP server on {}", bind_address);
|
||||||
|
|
||||||
// Use the existing database connection
|
// Use the existing database connection
|
||||||
let db_conn = Arc::new(Mutex::new(conn));
|
let db_conn = Arc::new(Mutex::new(conn));
|
||||||
@@ -73,12 +96,12 @@ async fn run_server(
|
|||||||
.layer(CorsLayer::permissive())
|
.layer(CorsLayer::permissive())
|
||||||
);
|
);
|
||||||
|
|
||||||
let addr: SocketAddr = if config.address.starts_with('/') || config.address.starts_with("./") {
|
let addr: SocketAddr = if bind_address.starts_with('/') || bind_address.starts_with("./") {
|
||||||
// Unix socket - not supported by axum directly, fall back to TCP
|
// Unix socket - not supported by axum directly, fall back to TCP
|
||||||
warn!("Unix sockets not yet implemented, falling back to TCP on 127.0.0.1:8080");
|
warn!("Unix sockets not yet implemented, falling back to TCP on 127.0.0.1:21080");
|
||||||
"127.0.0.1:8080".parse()?
|
"127.0.0.1:21080".parse()?
|
||||||
} else {
|
} else {
|
||||||
config.address.parse()?
|
bind_address.parse()?
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("SERVER: HTTP server listening on {}", addr);
|
info!("SERVER: HTTP server listening on {}", addr);
|
||||||
|
|||||||
@@ -20,20 +20,10 @@ use utoipa::ToSchema;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
pub address: String,
|
pub address: String,
|
||||||
|
pub port: Option<u16>,
|
||||||
pub password: Option<String>,
|
pub password: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for ServerConfig {
|
|
||||||
type Err = anyhow::Error;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
Ok(ServerConfig {
|
|
||||||
address: s.to_string(),
|
|
||||||
password: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub db: Arc<Mutex<rusqlite::Connection>>,
|
pub db: Arc<Mutex<rusqlite::Connection>>,
|
||||||
|
|||||||
Reference in New Issue
Block a user