redirector-cli
Version:
Global CLI tool for managing Redirector backend services with Docker Compose
458 lines (318 loc) โข 8.54 kB
Markdown
# Redirector CLI
[](https://badge.fury.io/js/redirector-cli)
[](https://opensource.org/licenses/MIT)
A global CLI tool for managing Redirector backend services with Docker Compose. Deploy and manage your Redirector API anywhere with zero configuration.
## ๐ Quick Start
```bash
# Install globally
npm install -g redirector-cli
# Initialize project
mkdir my-redirector && cd my-redirector
redirector setup
# Start services
redirector start
# Check status
redirector status
```
Your Redirector API will be available at `http://localhost:3000`!
## ๐ Requirements
- **Node.js** 16.0.0 or higher
- **Docker** and **Docker Compose**
- **npm** or **yarn**
## ๐ Installation
### Global Installation (Recommended)
```bash
npm install -g redirector-cli
```
### Local Installation
```bash
npx redirector-cli setup
```
## ๐ Commands
### `redirector setup`
Initialize a new Redirector project in the current directory.
```bash
redirector setup [options]
Options:
-p, --port <number> Backend API port (default: 3000)
--postgres-port <number> PostgreSQL port (default: 5432)
-u, --docker-username <username> Docker Hub username (default: shivarajbakale)
-f, --force Overwrite existing configuration
--no-interactive Skip interactive prompts
```
**Examples:**
```bash
# Interactive setup
redirector setup
# Non-interactive with custom port
redirector setup --port 8080 --no-interactive
# Force overwrite existing project
redirector setup --force
```
### `redirector start`
Start all Redirector services.
```bash
redirector start [options]
Options:
--no-detach Run in foreground (not detached)
--pull Pull latest images before starting
--build Build images before starting
```
**Examples:**
```bash
# Start services in background
redirector start
# Start with latest images
redirector start --pull
# Start in foreground
redirector start --no-detach
```
### `redirector stop`
Stop all Redirector services.
```bash
redirector stop [options]
Options:
-v, --remove-volumes Also remove volumes and data
```
**Examples:**
```bash
# Stop services
redirector stop
# Stop and remove all data
redirector stop --remove-volumes
```
### `redirector restart`
Restart all Redirector services.
```bash
redirector restart [options]
Options:
--pull Pull latest images before restarting
```
### `redirector reset`
Stop services and remove all data and volumes.
```bash
redirector reset [options]
Options:
-f, --force Skip confirmation prompt
-y, --confirm Automatically confirm reset
```
**โ ๏ธ Warning:** This will permanently delete all data!
### `redirector status`
Show status of all services.
```bash
redirector status [options]
Options:
-j, --json Output status as JSON
-v, --verbose Show detailed information
```
**Examples:**
```bash
# Human-readable status
redirector status
# JSON output for scripts
redirector status --json
# Detailed information
redirector status --verbose
```
### `redirector logs`
Show logs for services.
```bash
redirector logs [service] [options]
Arguments:
service Service name (backend, postgres, or all)
Options:
-f, --follow Follow log output
-t, --tail <number> Number of lines to show from end of logs
```
**Examples:**
```bash
# Show all logs
redirector logs
# Show backend logs only
redirector logs backend
# Follow all logs
redirector logs --follow
# Follow backend logs
redirector logs backend --follow
```
## ๐ง Configuration
The CLI uses the following configuration sources (in order of precedence):
1. **Command-line options**
2. **Environment variables**
3. **`.redirector.json` file**
4. **Default values**
### Environment Variables
```bash
BACKEND_PORT=3000 # Backend API port
POSTGRES_PORT=5432 # PostgreSQL port
DOCKER_USERNAME=shivarajbakale # Docker Hub username
PROJECT_NAME=redirector # Project name
NODE_ENV=production # Environment (production/development)
```
### Configuration File
The CLI creates a `.redirector.json` file in your project:
```json
{
"backendPort": 3000,
"postgresPort": 5432,
"dockerUsername": "shivarajbakale",
"projectName": "my-project",
"environment": "production"
}
```
## ๐ Project Structure
After running `redirector setup`, your project will contain:
```
my-redirector/
โโโ docker-compose.yml # Docker Compose configuration
โโโ .env # Environment variables
โโโ .redirector.json # CLI configuration
โโโ .gitignore # Git ignore rules
โโโ README.md # Project documentation
โโโ data/ # Volume mount directory
```
## ๐ API Endpoints
Once started, your Redirector API provides:
- **Health Check**: `GET /health`
- **Groups**: `GET /groups/list`, `POST /groups/create`
- **Requests**: `GET /requests`, `POST /requests/create`
Full API documentation: [Redirector API Docs](https://github.com/shivarajbakale/redirector-app)
## ๐ณ Docker Images
The CLI uses pre-built Docker images:
- **Backend**: `shivarajbakale/redirector-backend:latest`
- **Database**: `postgres:15`
Images are automatically pulled when starting services.
## ๐ Troubleshooting
### Docker Issues
```bash
# Check Docker installation
docker --version
docker compose version
# Check Docker daemon
docker info
# Check running containers
docker ps
```
### Port Conflicts
```bash
# Check if port is in use
lsof -i :3000
# Use custom port
redirector setup --port 8080
```
### Service Health
```bash
# Check service status
redirector status
# View service logs
redirector logs
# Test API health
curl http://localhost:3000/health
```
### Reset Everything
```bash
# Complete reset
redirector reset --force
# Restart setup
redirector setup --force
```
## ๐งช Development
### Building from Source
```bash
# Clone repository
git clone https://github.com/shivarajbakale/redirector-app.git
cd redirector-app/packages/backend/cli
# Install dependencies
npm install
# Build
npm run build
# Test locally
npm link
redirector --help
```
### Running Tests
```bash
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
```
### Linting
```bash
# Check code style
npm run lint
# Fix code style
npm run lint:fix
```
## ๐ Examples
### Basic Usage
```bash
# Create new project
mkdir my-api && cd my-api
redirector setup
# Start services
redirector start
# Test API
curl http://localhost:3000/health
# Stop services
redirector stop
```
### Custom Configuration
```bash
# Setup with custom ports
redirector setup --port 8080 --postgres-port 5433
# Start with latest images
redirector start --pull
# Monitor logs
redirector logs --follow
```
### Production Deployment
```bash
# Setup production environment
redirector setup --no-interactive
export NODE_ENV=production
# Start services
redirector start
# Monitor status
redirector status --json
```
### CI/CD Integration
```bash
#!/bin/bash
# deploy.sh
# Install CLI
npm install -g redirector-cli
# Setup project
redirector setup --no-interactive --force
# Start services
redirector start --pull
# Wait for health check
until curl -f http://localhost:3000/health; do
echo "Waiting for API..."
sleep 5
done
echo "Deployment successful!"
```
## ๐ค Contributing
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Links
- **Main Repository**: [redirector-app](https://github.com/shivarajbakale/redirector-app)
- **Docker Hub**: [shivarajbakale/redirector-backend](https://hub.docker.com/r/shivarajbakale/redirector-backend)
- **npm Package**: [redirector-cli](https://www.npmjs.com/package/redirector-cli)
- **Issues**: [GitHub Issues](https://github.com/shivarajbakale/redirector-app/issues)
## ๐ Support
If you find this project helpful, please consider:
- โญ Starring the repository
- ๐ Reporting bugs
- ๐ก Suggesting features
- ๐ Improving documentation
Made with โค๏ธ by [Shivaraj Bakale](https://github.com/shivarajbakale)