Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a02c1204ce | ||
|
|
258779e234 | ||
|
|
342defca6b | ||
|
|
580aec7d57 | ||
|
|
54b91af76c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,3 +7,4 @@ Trash/
|
|||||||
src/version.h
|
src/version.h
|
||||||
dev-config/
|
dev-config/
|
||||||
db/
|
db/
|
||||||
|
copy_executable_local.sh
|
||||||
|
|||||||
Binary file not shown.
12
src/config.c
12
src/config.c
@@ -577,12 +577,20 @@ const char* get_config_value(const char* key) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Priority 1: Database configuration (updated from file)
|
// Priority 1: Command line overrides via environment variables
|
||||||
|
if (strcmp(key, "relay_port") == 0) {
|
||||||
|
const char* port_override = getenv("C_RELAY_PORT_OVERRIDE");
|
||||||
|
if (port_override) {
|
||||||
|
return port_override;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Priority 2: Database configuration (updated from file)
|
||||||
if (get_database_config(key, buffer, sizeof(buffer)) == 0) {
|
if (get_database_config(key, buffer, sizeof(buffer)) == 0) {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Priority 2: Environment variables (fallback)
|
// Priority 3: Environment variables (fallback)
|
||||||
const char* env_value = getenv(key);
|
const char* env_value = getenv(key);
|
||||||
if (env_value) {
|
if (env_value) {
|
||||||
return env_value;
|
return env_value;
|
||||||
|
|||||||
138
src/main.c
138
src/main.c
@@ -1941,8 +1941,15 @@ int validate_event_expiration(cJSON* event, char* error_message, size_t error_si
|
|||||||
|
|
||||||
// Initialize database connection and schema
|
// Initialize database connection and schema
|
||||||
int init_database() {
|
int init_database() {
|
||||||
// Use configurable database path, falling back to default
|
// Priority 1: Command line database path override
|
||||||
const char* db_path = get_config_value("database_path");
|
const char* db_path = getenv("C_RELAY_DATABASE_PATH_OVERRIDE");
|
||||||
|
|
||||||
|
// Priority 2: Configuration system (if available)
|
||||||
|
if (!db_path) {
|
||||||
|
db_path = get_config_value("database_path");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Priority 3: Default path
|
||||||
if (!db_path) {
|
if (!db_path) {
|
||||||
db_path = DEFAULT_DATABASE_PATH;
|
db_path = DEFAULT_DATABASE_PATH;
|
||||||
}
|
}
|
||||||
@@ -3012,15 +3019,18 @@ void print_usage(const char* program_name) {
|
|||||||
printf("C Nostr Relay Server\n");
|
printf("C Nostr Relay Server\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf(" -p, --port PORT Listen port (default: %d)\n", DEFAULT_PORT);
|
printf(" -p, --port PORT Listen port (default: %d)\n", DEFAULT_PORT);
|
||||||
printf(" -c, --config FILE Configuration file path\n");
|
printf(" -c, --config FILE Configuration file path\n");
|
||||||
printf(" -d, --config-dir DIR Configuration directory path\n");
|
printf(" -d, --config-dir DIR Configuration directory path\n");
|
||||||
printf(" -h, --help Show this help message\n");
|
printf(" -D, --database-path PATH Database file path (default: %s)\n", DEFAULT_DATABASE_PATH);
|
||||||
|
printf(" -h, --help Show this help message\n");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("Examples:\n");
|
printf("Examples:\n");
|
||||||
printf(" %s --config /path/to/config.json\n", program_name);
|
printf(" %s --config /path/to/config.json\n", program_name);
|
||||||
printf(" %s --config-dir ~/.config/c-relay-dev\n", program_name);
|
printf(" %s --config-dir ~/.config/c-relay-dev\n", program_name);
|
||||||
printf(" %s --port 9999 --config-dir /etc/c-relay\n", program_name);
|
printf(" %s --port 9999 --config-dir /etc/c-relay\n", program_name);
|
||||||
|
printf(" %s --database-path /var/lib/c-relay/relay.db\n", program_name);
|
||||||
|
printf(" %s --database-path ./test.db --port 7777\n", program_name);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3028,6 +3038,7 @@ int main(int argc, char* argv[]) {
|
|||||||
int port = DEFAULT_PORT;
|
int port = DEFAULT_PORT;
|
||||||
char* config_dir_override = NULL;
|
char* config_dir_override = NULL;
|
||||||
char* config_file_override = NULL;
|
char* config_file_override = NULL;
|
||||||
|
char* database_path_override = NULL;
|
||||||
|
|
||||||
// Parse command line arguments
|
// Parse command line arguments
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
@@ -3041,12 +3052,7 @@ int main(int argc, char* argv[]) {
|
|||||||
log_error("Invalid port number");
|
log_error("Invalid port number");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// Store port in configuration system
|
// Port will be stored in configuration system after it's initialized
|
||||||
char port_str[16];
|
|
||||||
snprintf(port_str, sizeof(port_str), "%d", port);
|
|
||||||
set_database_config("relay_port", port_str, "command_line");
|
|
||||||
// Re-apply configuration to make sure global variables are updated
|
|
||||||
apply_configuration_to_globals();
|
|
||||||
} else {
|
} else {
|
||||||
log_error("Port argument requires a value");
|
log_error("Port argument requires a value");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -3065,6 +3071,13 @@ int main(int argc, char* argv[]) {
|
|||||||
log_error("Config directory argument requires a value");
|
log_error("Config directory argument requires a value");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
} else if (strcmp(argv[i], "-D") == 0 || strcmp(argv[i], "--database-path") == 0) {
|
||||||
|
if (i + 1 < argc) {
|
||||||
|
database_path_override = argv[++i];
|
||||||
|
} else {
|
||||||
|
log_error("Database path argument requires a value");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log_error("Unknown argument");
|
log_error("Unknown argument");
|
||||||
print_usage(argv[0]);
|
print_usage(argv[0]);
|
||||||
@@ -3086,12 +3099,23 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
printf(BLUE BOLD "=== C Nostr Relay Server ===" RESET "\n");
|
printf(BLUE BOLD "=== C Nostr Relay Server ===" RESET "\n");
|
||||||
|
|
||||||
|
// Apply database path override BEFORE any database operations
|
||||||
|
if (database_path_override) {
|
||||||
|
log_info("Database path override specified from command line");
|
||||||
|
printf(" Override path: %s\n", database_path_override);
|
||||||
|
// Set environment variable so init_database can use the correct path
|
||||||
|
setenv("C_RELAY_DATABASE_PATH_OVERRIDE", database_path_override, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize database FIRST (required for configuration system)
|
// Initialize database FIRST (required for configuration system)
|
||||||
if (init_database() != 0) {
|
if (init_database() != 0) {
|
||||||
log_error("Failed to initialize database");
|
log_error("Failed to initialize database");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Database path override is applied via environment variable - no need to store in config
|
||||||
|
// (storing database path in database creates circular dependency)
|
||||||
|
|
||||||
// Initialize nostr library BEFORE configuration system
|
// Initialize nostr library BEFORE configuration system
|
||||||
// (required for Nostr event generation in config files)
|
// (required for Nostr event generation in config files)
|
||||||
if (nostr_init() != 0) {
|
if (nostr_init() != 0) {
|
||||||
@@ -3108,6 +3132,96 @@ int main(int argc, char* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update database_path field to reflect actual database path used
|
||||||
|
if (database_path_override) {
|
||||||
|
// Convert to absolute path and normalize
|
||||||
|
char actual_db_path[1024];
|
||||||
|
if (database_path_override[0] == '/') {
|
||||||
|
// Already absolute
|
||||||
|
strncpy(actual_db_path, database_path_override, sizeof(actual_db_path) - 1);
|
||||||
|
} else {
|
||||||
|
// Make absolute by prepending current working directory
|
||||||
|
char cwd[1024];
|
||||||
|
if (getcwd(cwd, sizeof(cwd))) {
|
||||||
|
// Handle the case where path starts with ./
|
||||||
|
const char* clean_path = database_path_override;
|
||||||
|
if (strncmp(database_path_override, "./", 2) == 0) {
|
||||||
|
clean_path = database_path_override + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we don't exceed buffer size
|
||||||
|
int written = snprintf(actual_db_path, sizeof(actual_db_path), "%s/%s", cwd, clean_path);
|
||||||
|
if (written >= (int)sizeof(actual_db_path)) {
|
||||||
|
log_warning("Database path too long, using original path");
|
||||||
|
strncpy(actual_db_path, database_path_override, sizeof(actual_db_path) - 1);
|
||||||
|
actual_db_path[sizeof(actual_db_path) - 1] = '\0';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
strncpy(actual_db_path, database_path_override, sizeof(actual_db_path) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
actual_db_path[sizeof(actual_db_path) - 1] = '\0';
|
||||||
|
|
||||||
|
// Update the database_path configuration to reflect actual path used
|
||||||
|
if (set_database_config("database_path", actual_db_path, "system") == 0) {
|
||||||
|
log_info("Updated database_path configuration with actual path used");
|
||||||
|
} else {
|
||||||
|
log_warning("Failed to update database_path configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store metadata about configuration file path used
|
||||||
|
if (strlen(g_config_manager.config_file_path) > 0) {
|
||||||
|
// Convert to absolute path and normalize (fix double slash issue)
|
||||||
|
char actual_config_path[1024];
|
||||||
|
if (g_config_manager.config_file_path[0] == '/') {
|
||||||
|
// Already absolute - use as-is
|
||||||
|
strncpy(actual_config_path, g_config_manager.config_file_path, sizeof(actual_config_path) - 1);
|
||||||
|
} else {
|
||||||
|
// Make absolute by prepending current working directory
|
||||||
|
char cwd[1024];
|
||||||
|
if (getcwd(cwd, sizeof(cwd))) {
|
||||||
|
// Handle the case where path starts with ./
|
||||||
|
const char* clean_path = g_config_manager.config_file_path;
|
||||||
|
if (strncmp(g_config_manager.config_file_path, "./", 2) == 0) {
|
||||||
|
clean_path = g_config_manager.config_file_path + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any trailing slash from cwd to avoid double slash
|
||||||
|
size_t cwd_len = strlen(cwd);
|
||||||
|
if (cwd_len > 0 && cwd[cwd_len - 1] == '/') {
|
||||||
|
cwd[cwd_len - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove any leading slash from clean_path to avoid double slash
|
||||||
|
if (clean_path[0] == '/') {
|
||||||
|
clean_path++;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(actual_config_path, sizeof(actual_config_path), "%s/%s", cwd, clean_path);
|
||||||
|
} else {
|
||||||
|
strncpy(actual_config_path, g_config_manager.config_file_path, sizeof(actual_config_path) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
actual_config_path[sizeof(actual_config_path) - 1] = '\0';
|
||||||
|
|
||||||
|
if (set_database_config("config_location", actual_config_path, "system") == 0) {
|
||||||
|
log_info("Stored configuration location metadata");
|
||||||
|
} else {
|
||||||
|
log_warning("Failed to store configuration location metadata");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply command line overrides AFTER configuration system is initialized
|
||||||
|
if (port != DEFAULT_PORT) {
|
||||||
|
log_info("Applying port override from command line");
|
||||||
|
printf(" Port: %d\n", port);
|
||||||
|
// Set environment variable for port override (runtime only, not persisted)
|
||||||
|
char port_str[16];
|
||||||
|
snprintf(port_str, sizeof(port_str), "%d", port);
|
||||||
|
setenv("C_RELAY_PORT_OVERRIDE", port_str, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize NIP-11 relay information
|
// Initialize NIP-11 relay information
|
||||||
init_relay_info();
|
init_relay_info();
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#define SQL_SCHEMA_H
|
#define SQL_SCHEMA_H
|
||||||
|
|
||||||
/* Schema version constant */
|
/* Schema version constant */
|
||||||
#define EMBEDDED_SCHEMA_VERSION "3"
|
#define EMBEDDED_SCHEMA_VERSION "4"
|
||||||
|
|
||||||
/* Embedded SQL schema as C string literal */
|
/* Embedded SQL schema as C string literal */
|
||||||
static const char* const EMBEDDED_SCHEMA_SQL =
|
static const char* const EMBEDDED_SCHEMA_SQL =
|
||||||
@@ -14,7 +14,7 @@ static const char* const EMBEDDED_SCHEMA_SQL =
|
|||||||
-- SQLite schema for storing Nostr events with JSON tags support\n\
|
-- SQLite schema for storing Nostr events with JSON tags support\n\
|
||||||
\n\
|
\n\
|
||||||
-- Schema version tracking\n\
|
-- Schema version tracking\n\
|
||||||
PRAGMA user_version = 3;\n\
|
PRAGMA user_version = 4;\n\
|
||||||
\n\
|
\n\
|
||||||
-- Enable foreign key support\n\
|
-- Enable foreign key support\n\
|
||||||
PRAGMA foreign_keys = ON;\n\
|
PRAGMA foreign_keys = ON;\n\
|
||||||
@@ -267,20 +267,6 @@ BEGIN\n\
|
|||||||
VALUES (NEW.key, OLD.value, NEW.value, 'system', 'configuration update');\n\
|
VALUES (NEW.key, OLD.value, NEW.value, 'system', 'configuration update');\n\
|
||||||
END;\n\
|
END;\n\
|
||||||
\n\
|
\n\
|
||||||
-- Active Configuration View\n\
|
|
||||||
CREATE VIEW active_config AS\n\
|
|
||||||
SELECT\n\
|
|
||||||
key,\n\
|
|
||||||
value,\n\
|
|
||||||
description,\n\
|
|
||||||
config_type,\n\
|
|
||||||
data_type,\n\
|
|
||||||
requires_restart,\n\
|
|
||||||
updated_at\n\
|
|
||||||
FROM config\n\
|
|
||||||
WHERE config_type IN ('system', 'user')\n\
|
|
||||||
ORDER BY config_type, key;\n\
|
|
||||||
\n\
|
|
||||||
-- Runtime Statistics View\n\
|
-- Runtime Statistics View\n\
|
||||||
CREATE VIEW runtime_stats AS\n\
|
CREATE VIEW runtime_stats AS\n\
|
||||||
SELECT\n\
|
SELECT\n\
|
||||||
|
|||||||
BIN
test_check.db
Normal file
BIN
test_check.db
Normal file
Binary file not shown.
BIN
test_clean_paths.db
Normal file
BIN
test_clean_paths.db
Normal file
Binary file not shown.
BIN
test_combined.db
Normal file
BIN
test_combined.db
Normal file
Binary file not shown.
Binary file not shown.
BIN
test_db.db-wal
Normal file
BIN
test_db.db-wal
Normal file
Binary file not shown.
BIN
test_metadata.db
Normal file
BIN
test_metadata.db
Normal file
Binary file not shown.
BIN
test_override.db-shm
Normal file
BIN
test_override.db-shm
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user