Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54b91af76c | ||
|
|
6d9b4efb7e |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,5 +2,8 @@ nostr_core_lib/
|
|||||||
nips/
|
nips/
|
||||||
build/
|
build/
|
||||||
relay.log
|
relay.log
|
||||||
|
relay.pid
|
||||||
Trash/
|
Trash/
|
||||||
src/version.h
|
src/version.h
|
||||||
|
dev-config/
|
||||||
|
db/
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -35,20 +35,29 @@ if [ "$HELP" = true ]; then
|
|||||||
echo " --preserve-config, -p Keep existing configuration file (don't regenerate)"
|
echo " --preserve-config, -p Keep existing configuration file (don't regenerate)"
|
||||||
echo " --help, -h Show this help message"
|
echo " --help, -h Show this help message"
|
||||||
echo ""
|
echo ""
|
||||||
|
echo "Development Setup:"
|
||||||
|
echo " Uses local config directory: ./dev-config/"
|
||||||
|
echo " This avoids conflicts with production instances using ~/.config/c-relay/"
|
||||||
|
echo ""
|
||||||
echo "Default behavior: Automatically regenerates configuration file on each build"
|
echo "Default behavior: Automatically regenerates configuration file on each build"
|
||||||
echo " for development purposes"
|
echo " for development purposes"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Handle configuration file and database regeneration
|
# Handle configuration file and database regeneration
|
||||||
CONFIG_FILE="$HOME/.config/c-relay/c_relay_config_event.json"
|
# Use local development config directory to avoid conflicts with production
|
||||||
|
DEV_CONFIG_DIR="./dev-config"
|
||||||
|
CONFIG_FILE="$DEV_CONFIG_DIR/c_relay_config_event.json"
|
||||||
DB_FILE="./db/c_nostr_relay.db"
|
DB_FILE="./db/c_nostr_relay.db"
|
||||||
|
|
||||||
|
# Create development config directory if it doesn't exist
|
||||||
|
mkdir -p "$DEV_CONFIG_DIR"
|
||||||
|
|
||||||
if [ "$PRESERVE_CONFIG" = false ]; then
|
if [ "$PRESERVE_CONFIG" = false ]; then
|
||||||
if [ -f "$CONFIG_FILE" ]; then
|
if [ -f "$CONFIG_FILE" ]; then
|
||||||
echo "Removing old configuration file to trigger regeneration..."
|
echo "Removing old development configuration file to trigger regeneration..."
|
||||||
rm -f "$CONFIG_FILE"
|
rm -f "$CONFIG_FILE"
|
||||||
echo "✓ Configuration file removed - will be regenerated with new keys"
|
echo "✓ Development configuration file removed - will be regenerated with new keys"
|
||||||
fi
|
fi
|
||||||
if [ -f "$DB_FILE" ]; then
|
if [ -f "$DB_FILE" ]; then
|
||||||
echo "Removing old database to trigger fresh key generation..."
|
echo "Removing old database to trigger fresh key generation..."
|
||||||
@@ -56,9 +65,9 @@ if [ "$PRESERVE_CONFIG" = false ]; then
|
|||||||
echo "✓ Database removed - will be recreated with embedded schema and new keys"
|
echo "✓ Database removed - will be recreated with embedded schema and new keys"
|
||||||
fi
|
fi
|
||||||
elif [ "$PRESERVE_CONFIG" = true ]; then
|
elif [ "$PRESERVE_CONFIG" = true ]; then
|
||||||
echo "Preserving existing configuration and database as requested"
|
echo "Preserving existing development configuration and database as requested"
|
||||||
else
|
else
|
||||||
echo "No existing configuration or database found - will generate fresh setup"
|
echo "No existing development configuration or database found - will generate fresh setup"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Build the project first
|
# Build the project first
|
||||||
@@ -124,8 +133,8 @@ echo "Database will be initialized automatically on startup if needed"
|
|||||||
echo "Starting relay server..."
|
echo "Starting relay server..."
|
||||||
echo "Debug: Current processes: $(ps aux | grep 'c_relay_' | grep -v grep || echo 'None')"
|
echo "Debug: Current processes: $(ps aux | grep 'c_relay_' | grep -v grep || echo 'None')"
|
||||||
|
|
||||||
# Start relay in background and capture its PID
|
# Start relay in background and capture its PID with development config directory
|
||||||
$BINARY_PATH > relay.log 2>&1 &
|
$BINARY_PATH --config-dir "$DEV_CONFIG_DIR" > relay.log 2>&1 &
|
||||||
RELAY_PID=$!
|
RELAY_PID=$!
|
||||||
|
|
||||||
echo "Started with PID: $RELAY_PID"
|
echo "Started with PID: $RELAY_PID"
|
||||||
@@ -158,10 +167,11 @@ if ps -p "$RELAY_PID" >/dev/null 2>&1; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "=== Relay server running in background ==="
|
echo "=== Relay server running in background ==="
|
||||||
|
echo "Development config: $DEV_CONFIG_DIR/"
|
||||||
echo "To kill relay: pkill -f 'c_relay_'"
|
echo "To kill relay: pkill -f 'c_relay_'"
|
||||||
echo "To check status: ps aux | grep c_relay_"
|
echo "To check status: ps aux | grep c_relay_"
|
||||||
echo "To view logs: tail -f relay.log"
|
echo "To view logs: tail -f relay.log"
|
||||||
echo "Binary: $BINARY_PATH"
|
echo "Binary: $BINARY_PATH --config-dir $DEV_CONFIG_DIR"
|
||||||
echo "Ready for Nostr client connections!"
|
echo "Ready for Nostr client connections!"
|
||||||
else
|
else
|
||||||
echo "ERROR: Relay failed to start"
|
echo "ERROR: Relay failed to start"
|
||||||
|
|||||||
51
src/config.c
51
src/config.c
@@ -34,16 +34,38 @@ int init_configuration_system(void) {
|
|||||||
memset(&g_config_manager, 0, sizeof(config_manager_t));
|
memset(&g_config_manager, 0, sizeof(config_manager_t));
|
||||||
g_config_manager.db = g_db;
|
g_config_manager.db = g_db;
|
||||||
|
|
||||||
// Get XDG configuration directory
|
// Check for command line config file override first
|
||||||
if (get_xdg_config_dir(g_config_manager.config_dir_path, sizeof(g_config_manager.config_dir_path)) != 0) {
|
const char* config_file_override = getenv(CONFIG_FILE_OVERRIDE_ENV);
|
||||||
log_error("Failed to determine XDG configuration directory");
|
if (config_file_override && strlen(config_file_override) > 0) {
|
||||||
return -1;
|
// Use specific config file override
|
||||||
|
strncpy(g_config_manager.config_file_path, config_file_override,
|
||||||
|
sizeof(g_config_manager.config_file_path) - 1);
|
||||||
|
g_config_manager.config_file_path[sizeof(g_config_manager.config_file_path) - 1] = '\0';
|
||||||
|
|
||||||
|
// Extract directory from file path for config_dir_path
|
||||||
|
char* last_slash = strrchr(g_config_manager.config_file_path, '/');
|
||||||
|
if (last_slash) {
|
||||||
|
size_t dir_len = last_slash - g_config_manager.config_file_path;
|
||||||
|
strncpy(g_config_manager.config_dir_path, g_config_manager.config_file_path, dir_len);
|
||||||
|
g_config_manager.config_dir_path[dir_len] = '\0';
|
||||||
|
} else {
|
||||||
|
// File in current directory
|
||||||
|
strcpy(g_config_manager.config_dir_path, ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
log_info("Using configuration file from command line override");
|
||||||
|
} else {
|
||||||
|
// Get XDG configuration directory (with --config-dir override support)
|
||||||
|
if (get_xdg_config_dir(g_config_manager.config_dir_path, sizeof(g_config_manager.config_dir_path)) != 0) {
|
||||||
|
log_error("Failed to determine configuration directory");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build configuration file path
|
||||||
|
snprintf(g_config_manager.config_file_path, sizeof(g_config_manager.config_file_path),
|
||||||
|
"%s/%s", g_config_manager.config_dir_path, CONFIG_FILE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build configuration file path
|
|
||||||
snprintf(g_config_manager.config_file_path, sizeof(g_config_manager.config_file_path),
|
|
||||||
"%s/%s", g_config_manager.config_dir_path, CONFIG_FILE_NAME);
|
|
||||||
|
|
||||||
log_info("Configuration directory: %s");
|
log_info("Configuration directory: %s");
|
||||||
printf(" %s\n", g_config_manager.config_dir_path);
|
printf(" %s\n", g_config_manager.config_dir_path);
|
||||||
log_info("Configuration file: %s");
|
log_info("Configuration file: %s");
|
||||||
@@ -273,13 +295,22 @@ int load_config_from_database(void) {
|
|||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
int get_xdg_config_dir(char* path, size_t path_size) {
|
int get_xdg_config_dir(char* path, size_t path_size) {
|
||||||
const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
|
// Priority 1: Command line --config-dir override
|
||||||
|
const char* config_dir_override = getenv(CONFIG_DIR_OVERRIDE_ENV);
|
||||||
|
if (config_dir_override && strlen(config_dir_override) > 0) {
|
||||||
|
strncpy(path, config_dir_override, path_size - 1);
|
||||||
|
path[path_size - 1] = '\0';
|
||||||
|
log_info("Using config directory from command line override");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Priority 2: XDG_CONFIG_HOME environment variable
|
||||||
|
const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
|
||||||
if (xdg_config_home && strlen(xdg_config_home) > 0) {
|
if (xdg_config_home && strlen(xdg_config_home) > 0) {
|
||||||
// Use XDG_CONFIG_HOME if set
|
// Use XDG_CONFIG_HOME if set
|
||||||
snprintf(path, path_size, "%s/%s", xdg_config_home, CONFIG_XDG_DIR_NAME);
|
snprintf(path, path_size, "%s/%s", xdg_config_home, CONFIG_XDG_DIR_NAME);
|
||||||
} else {
|
} else {
|
||||||
// Fall back to ~/.config
|
// Priority 3: Fall back to ~/.config
|
||||||
const char* home = getenv("HOME");
|
const char* home = getenv("HOME");
|
||||||
if (!home) {
|
if (!home) {
|
||||||
log_error("Neither XDG_CONFIG_HOME nor HOME environment variable is set");
|
log_error("Neither XDG_CONFIG_HOME nor HOME environment variable is set");
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
#define CONFIG_FILE_NAME "c_relay_config_event.json"
|
#define CONFIG_FILE_NAME "c_relay_config_event.json"
|
||||||
#define CONFIG_ADMIN_PRIVKEY_ENV "C_RELAY_ADMIN_PRIVKEY"
|
#define CONFIG_ADMIN_PRIVKEY_ENV "C_RELAY_ADMIN_PRIVKEY"
|
||||||
#define CONFIG_RELAY_PRIVKEY_ENV "C_RELAY_PRIVKEY"
|
#define CONFIG_RELAY_PRIVKEY_ENV "C_RELAY_PRIVKEY"
|
||||||
|
#define CONFIG_DIR_OVERRIDE_ENV "C_RELAY_CONFIG_DIR_OVERRIDE"
|
||||||
|
#define CONFIG_FILE_OVERRIDE_ENV "C_RELAY_CONFIG_FILE_OVERRIDE"
|
||||||
#define NOSTR_PUBKEY_HEX_LENGTH 64
|
#define NOSTR_PUBKEY_HEX_LENGTH 64
|
||||||
#define NOSTR_PRIVKEY_HEX_LENGTH 64
|
#define NOSTR_PRIVKEY_HEX_LENGTH 64
|
||||||
#define NOSTR_EVENT_ID_HEX_LENGTH 64
|
#define NOSTR_EVENT_ID_HEX_LENGTH 64
|
||||||
|
|||||||
72
src/main.c
72
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,13 +3019,26 @@ 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(" -h, --help Show this help message\n");
|
printf(" -c, --config FILE Configuration file path\n");
|
||||||
|
printf(" -d, --config-dir DIR Configuration directory path\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("Examples:\n");
|
||||||
|
printf(" %s --config /path/to/config.json\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 --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");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
int port = DEFAULT_PORT;
|
int port = DEFAULT_PORT;
|
||||||
|
char* config_dir_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++) {
|
||||||
@@ -3042,6 +3062,27 @@ int main(int argc, char* argv[]) {
|
|||||||
log_error("Port argument requires a value");
|
log_error("Port argument requires a value");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
} else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) {
|
||||||
|
if (i + 1 < argc) {
|
||||||
|
config_file_override = argv[++i];
|
||||||
|
} else {
|
||||||
|
log_error("Config file argument requires a value");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} else if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--config-dir") == 0) {
|
||||||
|
if (i + 1 < argc) {
|
||||||
|
config_dir_override = argv[++i];
|
||||||
|
} else {
|
||||||
|
log_error("Config directory argument requires a value");
|
||||||
|
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]);
|
||||||
@@ -3049,18 +3090,41 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store config overrides in global variables for configuration system access
|
||||||
|
if (config_dir_override) {
|
||||||
|
setenv("C_RELAY_CONFIG_DIR_OVERRIDE", config_dir_override, 1);
|
||||||
|
}
|
||||||
|
if (config_file_override) {
|
||||||
|
setenv("C_RELAY_CONFIG_FILE_OVERRIDE", config_file_override, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// Set up signal handlers
|
// Set up signal handlers
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
signal(SIGTERM, signal_handler);
|
signal(SIGTERM, signal_handler);
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store database path override in configuration if specified
|
||||||
|
if (database_path_override) {
|
||||||
|
if (set_database_config("database_path", database_path_override, "command_line") != 0) {
|
||||||
|
log_warning("Failed to store database path override in configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
|
|||||||
BIN
test_db.db-shm
Normal file
BIN
test_db.db-shm
Normal file
Binary file not shown.
BIN
test_db.db-wal
Normal file
BIN
test_db.db-wal
Normal file
Binary file not shown.
Reference in New Issue
Block a user