Configuration Guide
This guide covers all configuration options available in XyPriss.
Configuration Methods
XyPriss supports multiple configuration methods with the following priority order:
- Runtime Configuration (highest priority)
- Environment Variables
- Configuration File (xypriss.config.json)
- Default Values (lowest priority)
Runtime Configuration
Pass configuration directly to
createServer()
:
import { createServer } from "xypriss";
const server = createServer({
env: "production",
server: {
port: 3000,
host: "0.0.0.0"
},
cache: {
strategy: "redis"
}
});
Configuration File
Create xypriss.config.json
in your
project root:
{
"env": "development",
"server": {
"port": 3000,
"host": "localhost",
"autoPortSwitch": {
"enabled": true,
"portRange": [3000, 3010],
"strategy": "increment"
}
},
"cache": {
"strategy": "memory",
"maxSize": 104857600,
"ttl": 3600
},
"cluster": {
"enabled": false,
"workers": "auto"
}
}
Environment Variables
XyPriss recognizes these environment variables:
# Server Configuration
NODE_ENV=production
PORT=3000
HOST=0.0.0.0
# Cache Configuration
CACHE_STRATEGY=redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your-password
# Cluster Configuration
CLUSTER_ENABLED=true
CLUSTER_WORKERS=4
# Security Configuration
RATE_LIMIT_WINDOW=900000
RATE_LIMIT_MAX=100
Server Configuration
ServerConfig Interface
interface ServerConfig {
port?: number; // Default: 3000
host?: string; // Default: "localhost"
enableMiddleware?: boolean; // Default: true
autoPortSwitch?: {
enabled?: boolean; // Default: false
maxAttempts?: number; // Default: 10
portRange?: [number, number]; // Default: [3000, 3100]
strategy?: "increment" | "random" | "predefined";
};
}
Example
const server = createServer({
server: {
port: 8080,
autoPortSwitch: {
enabled: true,
maxAttempts: 5,
portRange: [8080, 8090],
strategy: "increment"
}
}
});
Cache Configuration
Memory Cache
const server = createServer({
cache: {
strategy: "memory",
maxSize: 100 * 1024 * 1024, // 100MB
ttl: 3600 // 1 hour
}
});
Redis Cache
const server = createServer({
cache: {
strategy: "redis",
ttl: 7200,
redis: {
host: "redis.example.com",
port: 6379,
password: "your-password",
db: 1
}
}
});
Redis Cluster
const server = createServer({
cache: {
strategy: "redis",
redis: {
cluster: true,
nodes: [
{ host: "redis-1.example.com", port: 6379 },
{ host: "redis-2.example.com", port: 6379 },
{ host: "redis-3.example.com", port: 6379 }
]
}
}
});
Cluster Configuration
const server = createServer({
cluster: {
enabled: true,
workers: 4,
autoScale: {
enabled: true,
minWorkers: 2,
maxWorkers: 8,
cpuThreshold: 75,
memoryThreshold: 80,
scaleInterval: 30000
}
}
});
Environment-Specific Configuration
Development
const developmentConfig = {
env: "development",
server: {
port: 3000,
host: "localhost"
},
cache: {
strategy: "memory",
maxSize: 50 * 1024 * 1024
},
cluster: {
enabled: false
}
};
Production
const productionConfig = {
env: "production",
server: {
port: process.env.PORT || 8080,
host: "0.0.0.0"
},
cache: {
strategy: "redis",
redis: {
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT || "6379")
}
},
cluster: {
enabled: true,
workers: "auto"
}
};
Best Practices
- Use environment variables for sensitive data
- Use configuration files for complex settings
- Validate configuration in your application startup
- Document custom configuration for your team
- Use different configurations for different environments
- Monitor configuration changes in production