Browse Source

feat: add configurable IP address binding support

- Add HOST configuration to AppConfig interface and loadConfig function
- Update server to listen on configured host instead of hardcoded port
- Add HOST environment variable documentation to .env.example
- Update install script to prompt for host/IP address selection:
  - Default: 0.0.0.0 (all interfaces)
  - Option: 127.0.0.1 (localhost only)
  - Option: Custom IP address
- Include HOST in environment file generation during installation
- Enhance logging to show configured host and accessible web UI URL

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh 8 months ago
parent
commit
d17044b49e
5 changed files with 21 additions and 3 deletions
  1. 3 0
      .env.example
  2. BIN
      data/shops.db
  3. 11 0
      scripts/install.sh
  4. 4 1
      src/config/index.ts
  5. 3 2
      src/index.ts

+ 3 - 0
.env.example

@@ -1,6 +1,9 @@
 # API Key for Bearer authentication
 # API Key for Bearer authentication
 API_KEY=your-secret-key-here
 API_KEY=your-secret-key-here
 
 
+# HTTP server host/IP address (0.0.0.0 = all interfaces, 127.0.0.1 = localhost only)
+HOST=0.0.0.0
+
 # HTTP server port
 # HTTP server port
 PORT=3000
 PORT=3000
 
 

BIN
data/shops.db


+ 11 - 0
scripts/install.sh

@@ -35,6 +35,15 @@ if [ -z "$API_KEY" ]; then
     exit 1
     exit 1
 fi
 fi
 
 
+# Prompt for host/IP address
+echo "Enter the host/IP address to listen on:"
+echo "  - 0.0.0.0 for all interfaces (accessible from anywhere)"
+echo "  - 127.0.0.1 for localhost only (accessible only from this machine)"
+echo "  - or enter a specific IP address"
+echo "Default: 0.0.0.0"
+read HOST
+HOST=${HOST:-0.0.0.0}
+
 # Prompt for port (optional)
 # Prompt for port (optional)
 echo "Enter the HTTP port (default: 3000):"
 echo "Enter the HTTP port (default: 3000):"
 read PORT
 read PORT
@@ -47,6 +56,7 @@ MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-3}
 
 
 echo ""
 echo ""
 echo "Configuration:"
 echo "Configuration:"
+echo "  Host: $HOST"
 echo "  Port: $PORT"
 echo "  Port: $PORT"
 echo "  Max Concurrent Jobs: $MAX_CONCURRENT_JOBS"
 echo "  Max Concurrent Jobs: $MAX_CONCURRENT_JOBS"
 echo ""
 echo ""
@@ -105,6 +115,7 @@ npm install --production
 echo "Creating environment file..."
 echo "Creating environment file..."
 cat > "$INSTALL_DIR/.env" <<EOF
 cat > "$INSTALL_DIR/.env" <<EOF
 API_KEY=$API_KEY
 API_KEY=$API_KEY
+HOST=$HOST
 PORT=$PORT
 PORT=$PORT
 MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
 MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
 EOF
 EOF

+ 4 - 1
src/config/index.ts

@@ -1,6 +1,7 @@
 import { logger } from '../utils/logger';
 import { logger } from '../utils/logger';
 
 
 export interface AppConfig {
 export interface AppConfig {
+  host: string;
   port: number;
   port: number;
   apiKey: string;
   apiKey: string;
   maxConcurrentJobs: number;
   maxConcurrentJobs: number;
@@ -14,12 +15,14 @@ export function loadConfig(): AppConfig {
     throw new Error('API_KEY environment variable is required');
     throw new Error('API_KEY environment variable is required');
   }
   }
 
 
+  const host = process.env.HOST || '0.0.0.0';
   const port = parseInt(process.env.PORT || '3000', 10);
   const port = parseInt(process.env.PORT || '3000', 10);
   const maxConcurrentJobs = parseInt(process.env.MAX_CONCURRENT_JOBS || '3', 10);
   const maxConcurrentJobs = parseInt(process.env.MAX_CONCURRENT_JOBS || '3', 10);
 
 
-  logger.info('Configuration loaded', { port, maxConcurrentJobs });
+  logger.info('Configuration loaded', { host, port, maxConcurrentJobs });
 
 
   return {
   return {
+    host,
     port,
     port,
     apiKey,
     apiKey,
     maxConcurrentJobs
     maxConcurrentJobs

+ 3 - 2
src/index.ts

@@ -132,8 +132,8 @@ async function main() {
     // Create and start HTTP server
     // Create and start HTTP server
     const app = createServer(config.apiKey, jobQueue, db);
     const app = createServer(config.apiKey, jobQueue, db);
 
 
-    const server = app.listen(config.port, () => {
-      logger.info(`Server listening on port ${config.port}`);
+    const server = app.listen(config.port, config.host, () => {
+      logger.info(`Server listening on ${config.host}:${config.port}`);
       logger.info('API endpoints:');
       logger.info('API endpoints:');
       logger.info(`  POST   /api/jobs        - Create new scraping job`);
       logger.info(`  POST   /api/jobs        - Create new scraping job`);
       logger.info(`  GET    /api/jobs/:id    - Get job status and result`);
       logger.info(`  GET    /api/jobs/:id    - Get job status and result`);
@@ -142,6 +142,7 @@ async function main() {
       logger.info(`  GET    /api/shops/:id   - Get shop details and analytics`);
       logger.info(`  GET    /api/shops/:id   - Get shop details and analytics`);
       logger.info(`  GET    /health          - Health check`);
       logger.info(`  GET    /health          - Health check`);
       logger.info('');
       logger.info('');
+      logger.info(`Web UI available at: http://${config.host === '0.0.0.0' ? 'localhost' : config.host}:${config.port}`);
       logger.info('Authentication: Bearer token required for /api/* endpoints');
       logger.info('Authentication: Bearer token required for /api/* endpoints');
     });
     });