diff --git a/README.md b/README.md index 8881d65..8f0a111 100644 --- a/README.md +++ b/README.md @@ -1,241 +1,516 @@ # Quote App - DevOps CI/CD Project -A random quote generator web application built to demonstrate DevOps practices including CI/CD pipelines, containerization, and orchestration. + +![CI/CD Pipeline](https://github.com/AdityaJareda/quote-app-devops/actions/workflows/ci-cd.yml/badge.svg) +![Node.js](https://img.shields.io/badge/Node.js-18.x-green?logo=node.js) +![Docker](https://img.shields.io/docker/pulls/adityajareda/quote-app?logo=docker) +![Docker Image Size](https://img.shields.io/docker/image-size/adityajareda/quote-app/latest?logo=docker) +![License](https://img.shields.io/badge/license-MIT-green) +![Tests](https://img.shields.io/badge/tests-14%20passing-brightgreen) +![Coverage](https://img.shields.io/badge/coverage-85%25-yellowgreen) -## Features +> A full-stack random quote generator demonstrating modern DevOps practices including automated CI/CD pipelines, Docker containerization, security scanning, and comprehensive testing. -* Random quote generation -* Category-based filtering -* RESTful API backend -* Responsive frontend design -* Statistics tracking -* Share functionality +--- -## Tech Stack +## Features -**Backend:** +### User Features +- **Random Quote Generation** - Get inspired with motivational quotes +- **Category Filtering** - Browse quotes by category (motivation, life, wisdom, etc.) +- **Statistics Tracking** - View total quotes and personal viewing history +- **Share Functionality** - Copy quotes to clipboard or share via native API +- **Keyboard Shortcuts** - Space for new quote, 'S' to share +- **Responsive Design** - Works seamlessly on desktop, tablet, and mobile + +### Technical Features +- **RESTful API** - Clean, documented endpoints +- **Fully Dockerized** - Multi-stage optimized build +- **CI/CD Pipeline** - Automated testing, building, and deployment +- **Security Scanning** - Automated vulnerability detection with Trivy +- **Comprehensive Testing** - 14 unit tests with 85%+ coverage +- **Multi-platform Support** - Built for amd64 and arm64 architectures +- **Health Checks** - Built-in monitoring and health endpoints -* Node.js -* Express.js -* RESTful API +--- -**Frontend:** +## Tech Stack -* HTML5 -* CSS3 (with CSS Variables) -* Vanilla JavaScript (ES6+) +### Backend +- **Node.js 18.x** - Runtime environment +- **Express.js 4.18** - Web framework +- **RESTful API** - Standard HTTP methods -**DevOps Tools:** +### Frontend +- **HTML5** - Semantic markup +- **CSS3** - Modern styling with Flexbox, Grid, and CSS Variables +- **Vanilla JavaScript (ES6+)** - No framework dependencies -* Docker -* Docker Compose -* GitHub Actions -* Kubernetes (Minikube) -* Terraform (planned) +### DevOps & Infrastructure +- **Docker & Docker Compose** - Containerization +- **GitHub Actions** - CI/CD automation +- **Jest & Supertest** - Testing framework +- **Trivy** - Security vulnerability scanning +- **Docker Hub** - Image registry +- **Kubernetes** (upcoming) - Container orchestration -## Prerequisites +--- -* Node.js 18.x or higher -* npm 9.x or higher -* Git +## Quick Start -## Installation +### Prerequisites -1. **Clone the repository** +- Node.js 18.x or higher +- npm 9.x or higher +- Git +- Docker (optional, for containerized deployment) +### Option 1: Run with Docker (Recommended) ```bash -git clone https://github.com/AdityaJareda/quote-app-devops.git quote-app -cd quote-app -``` +# Pull and run from Docker Hub +docker run -p 3000:3000 adityajareda/quote-app:latest -2. **Install dependencies** +# Access at http://localhost:3000 +``` +### Option 2: Run with Docker Compose ```bash -npm install -``` +# Clone repository +git clone https://github.com/AdityaJareda/quote-app-devops.git +cd quote-app-devops + +# Start with Docker Compose +docker compose up -d -3. **Create environment file** +# Access at http://localhost:3000 +``` +### Option 3: Local Development ```bash +# Clone repository +git clone https://github.com/AdityaJareda/quote-app-devops.git +cd quote-app-devops + +# Install dependencies +npm install + +# Create environment file cp .env.example .env + +# Start development server (with auto-reload) +npm run dev + +# Access at http://localhost:3000 ``` -4. **Start the server** +--- -```bash -# Development mode (with auto-reload) -npm run dev +## API Documentation -# Production mode -npm start +### Base URL ``` +http://localhost:3000 +``` + +### Endpoints + +| Method | Endpoint | Description | Example | +|--------|----------|-------------|---------| +| GET | `/health` | Health check | Returns server status | +| GET | `/api/quotes` | Get all quotes | Supports pagination (`?page=1&limit=10`) | +| GET | `/api/quotes/random` | Get random quote | Returns one random quote | +| GET | `/api/quotes/:id` | Get quote by ID | `/api/quotes/1` | +| GET | `/api/quotes/category/:category` | Filter by category | `/api/quotes/category/motivation` | +| GET | `/api/categories` | Get all categories | Returns list of available categories | +| POST | `/api/quotes` | Add new quote | Requires `text`, `author`, `category` | -5. **Access the application** +### Example Request +```bash +curl http://localhost:3000/api/quotes/random +``` -* Frontend: [http://localhost:3000](http://localhost:3000) -* API Health: [http://localhost:3000/health](http://localhost:3000/health) +### Example Response +```json +{ + "id": 1, + "text": "The only way to do great work is to love what you do.", + "author": "Steve Jobs", + "category": "motivation" +} +``` -## API Endpoints +### Error Responses +```json +{ + "error": "Quote not found", + "id": 999 +} +``` -| Method | Endpoint | Description | -| ------ | -------------------------------- | -------------------------- | -| GET | `/health` | Health check | -| GET | `/api/quotes` | Get all quotes (paginated) | -| GET | `/api/quotes/random` | Get random quote | -| GET | `/api/quotes/:id` | Get quote by ID | -| GET | `/api/quotes/category/:category` | Get quotes by category | -| GET | `/api/categories` | Get all categories | -| POST | `/api/quotes` | Add new quote | +--- ## Testing +### Run Tests ```bash -# Run tests +# Run all tests with coverage npm test -# Run tests in watch mode +# Run tests in watch mode (for development) npm run test:watch -# Check code quality -npm run lint +# Generate coverage report +npm test -- --coverage ``` +### Test Coverage + +- **Statements:** 87.5% +- **Branches:** 75% +- **Functions:** 83.3% +- **Lines:** 87.5% + +**14 passing tests** covering: +- Health check endpoint +- Quote retrieval (all, random, by ID, by category) +- Categories endpoint +- POST endpoint validation +- Error handling +- 404 handler + +--- + ## Docker +### Multi-Stage Dockerfile + +Our Dockerfile uses multi-stage builds for optimization: + +**Stage 1: Builder** +- Installs all dependencies +- Runs tests (build fails if tests fail) +- Prepares application files + +**Stage 2: Production** +- Minimal production image +- Only production dependencies +- Non-root user for security +- Health checks included + +**Final Image Size:** ~150MB + +### Build Locally ```bash # Build image -docker build -t quote-app . +docker build -t quote-app:local . # Run container -docker run -p 3000:3000 quote-app +docker run -d -p 3000:3000 --name my-quote-app quote-app:local -# Using Docker Compose -docker-compose up -``` +# View logs +docker logs -f my-quote-app -## Docker Hub +# Stop container +docker stop my-quote-app +``` -Pull the pre-built image: +### Docker Compose +**Production mode:** ```bash -docker pull adityajareda/quote-app:latest +docker compose up -d ``` -**Image Details:** -- Repository: [adityajareda/quote-app](https://hub.docker.com/r/adityajareda/quote-app) -- Size: ~150MB -- Base: Node.js 18 Alpine -- Tags: `latest`, `v1.0.0` - -### Running from Docker Hub - +**Development mode** (with live reload): ```bash -# Quick run -docker run -p 3000:3000 adityajareda/quote-app:latest +docker compose -f docker-compose.dev.yml up +``` -# With environment variables +### Environment Variables +```bash +# Run with custom environment docker run -p 3000:3000 \ -e NODE_ENV=production \ -e PORT=3000 \ + -e APP_NAME="My Quote App" \ adityajareda/quote-app:latest +``` -# With Docker Compose -docker compose up -d +--- + +## CI/CD Pipeline + +### GitHub Actions Workflow + +Our automated pipeline runs on every push and pull request: +``` +Code Push to GitHub + | + v ++---------------------------------------------------+ +| Stage 1: Test | +| - Setup Node.js 18 | +| - Install dependencies (npm ci) | +| - Run Jest tests | +| - Generate coverage report | ++---------------------------------------------------+ + | (only if tests pass) + v ++---------------------------------------------------+ +| Stage 2: Build & Push | +| - Build Docker image (multi-stage) | +| - Tag with commit SHA and 'latest' | +| - Multi-platform build (amd64, arm64) | +| - Push to Docker Hub | ++---------------------------------------------------+ + | + v ++---------------------------------------------------+ +| Stage 3: Security Scan | +| - Run Trivy vulnerability scanner | +| - Generate SARIF report | +| - Upload to GitHub Security tab | ++---------------------------------------------------+ + | + v ++---------------------------------------------------+ +| Stage 4: Deployment Notification | +| - Create deployment summary | ++---------------------------------------------------+ ``` -## Project Structure +### Pipeline Features + +- **Automated Testing** - Runs on every commit +- **Docker Build & Push** - Automatic image publishing +- **Security Scanning** - Trivy vulnerability detection +- **Multi-platform** - Builds for amd64 and arm64 +- **Branch Protection** - Requires passing tests before merge +- **Status Badges** - Real-time build status +- **Deployment Summaries** - Detailed run reports + +### View Pipeline Status + +Check the [Actions tab](https://github.com/AdityaJareda/quote-app-devops/actions) to see: +- Build status +- Test results +- Security scan reports +- Deployment history +--- + +## Project Structure ``` -quote-app/ +quote-app-devops/ +├── .github/ +│ └── workflows/ +│ ├── ci-cd.yml # Main CI/CD pipeline +│ └── health-check.yml # Scheduled health checks ├── src/ -│ ├── app.js # Main Express application -│ ├── routes/ # API routes -│ ├── controllers/ # Business logic +│ ├── app.js # Express server +│ ├── routes/ # API route handlers +│ ├── controllers/ # Business logic │ └── data/ -│ └── quotes.json # Quote data +│ └── quotes.json # Quote database ├── public/ -│ ├── index.html # Frontend UI -│ ├── style.css # Styles -│ └── script.js # Frontend logic -├── tests/ # Test files -├── .github/ -│ └── workflows/ # CI/CD pipelines -├── k8s/ # Kubernetes manifests -├── Dockerfile -├── docker-compose.yml -└── package.json +│ ├── index.html # Frontend UI +│ ├── style.css # Styling +│ └── script.js # Frontend logic +├── tests/ +│ └── app.test.js # Unit tests (14 tests) +├── coverage/ # Test coverage reports +├── docs/ # Documentation +│ └── DOCKER.md # Docker guide +├── .dockerignore # Docker build exclusions +├── .env.example # Environment template +├── .gitignore # Git exclusions +├── docker-compose.yml # Production compose +├── docker-compose.dev.yml # Development compose +├── Dockerfile # Multi-stage build +├── jest.config.js # Test configuration +├── LICENSE # MIT License +├── package.json # Node.js dependencies +└── README.md # This file ``` +--- + ## Environment Variables -| Variable | Description | Default | -| -------- | ----------- | ----------- | -| PORT | Server port | 3000 | -| NODE_ENV | Environment | development | +| Variable | Description | Default | Required | +|----------|-------------|---------|----------| +| `PORT` | Server port | `3000` | No | +| `NODE_ENV` | Environment (`development`, `production`) | `development` | No | +| `APP_NAME` | Application name | `Quote App` | No | +| `APP_VERSION` | Application version | `1.0.0` | No | + +### Example `.env` file +```env +PORT=3000 +NODE_ENV=production +APP_NAME=Quote App +APP_VERSION=1.0.0 +``` + +--- -## Development +## Deployment +### Deploy with Docker ```bash -# Install dependencies -npm install +# Pull latest image +docker pull adityajareda/quote-app:latest -# Start development server -npm run dev +# Run on server +docker run -d \ + --name quote-app \ + --restart unless-stopped \ + -p 3000:3000 \ + -e NODE_ENV=production \ + adityajareda/quote-app:latest +``` -# Run tests -npm test +### Deploy with Kubernetes +```bash +# Apply manifests (coming in Day 4) +kubectl apply -f k8s/ -# Build for production -npm start +# Check deployment +kubectl get pods +kubectl get services + +# Access application +kubectl port-forward service/quote-app-service 3000:80 ``` -## Deployment +--- -### Docker +## Monitoring & Observability -```bash -docker build -t adityajareda/quote-app . -docker push adityajareda/quote-app +### Health Checks + +**Endpoint:** `GET /health` + +**Response:** +```json +{ + "status": "healthy", + "timestamp": "2025-01-15T10:30:00.000Z", + "uptime": 3600.5, + "environment": "production", + "quotesLoaded": 10 +} ``` -### Kubernetes +### Docker Health Check +Built-in Docker health check runs every 30 seconds: ```bash -kubectl apply -f k8s/ +# Check container health +docker inspect --format='{{.State.Health.Status}}' quote-app-container ``` -## TODO +### GitHub Security -* [ ] Add unit tests -* [ ] Add integration tests -* [ ] Implement Docker containerization -* [ ] Set up GitHub Actions CI/CD -* [ ] Deploy to Kubernetes -* [ ] Add Terraform configuration -* [ ] Implement monitoring with Netdata -* [ ] Add database persistence +View security scan results: +- Go to repository **Security** tab +- Click **Code scanning** +- View Trivy vulnerability reports + +--- + +## Roadmap + +### Completed +- [x] Full-stack application (Frontend + Backend) +- [x] RESTful API with 7 endpoints +- [x] Comprehensive unit tests (14 tests, 85%+ coverage) +- [x] Dockerization with multi-stage build +- [x] Docker Compose setup +- [x] Published to Docker Hub +- [x] GitHub Actions CI/CD pipeline +- [x] Automated security scanning +- [x] Branch protection rules +- [x] Multi-platform Docker builds + +### In Progress +- [ ] Kubernetes deployment (Day 4) +- [ ] Horizontal Pod Autoscaler +- [ ] Ingress configuration + +### Planned +- [ ] Terraform infrastructure as code +- [ ] Monitoring with Prometheus + Grafana +- [ ] Centralized logging with ELK stack +- [ ] Database integration (MongoDB) +- [ ] User authentication +- [ ] Rate limiting +- [ ] API documentation with Swagger +- [ ] Load testing with k6 +- [ ] Blue-green deployment strategy + +--- ## Contributing -This is a learning project for DevOps practices. Feel free to fork and experiment! +This is a learning project, but contributions are welcome! + +### How to Contribute + +1. Fork the repository +2. Create a feature branch (`git checkout -b feature/amazing-feature`) +3. Make your changes +4. Write/update tests +5. Ensure all tests pass (`npm test`) +6. Commit your changes (`git commit -m 'feat: Add amazing feature'`) +7. Push to the branch (`git push origin feature/amazing-feature`) +8. Open a Pull Request + +### Commit Convention + +We follow [Conventional Commits](https://www.conventionalcommits.org/): + +- `feat:` - New feature +- `fix:` - Bug fix +- `docs:` - Documentation changes +- `test:` - Test updates +- `ci:` - CI/CD changes +- `refactor:` - Code refactoring +- `chore:` - Maintenance tasks + +--- ## License -MIT License - feel free to use this project for learning purposes. +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +--- ## Author -**Your Name** +**Aditya Singh** -* GitHub: [@AdityaJareda](https://github.com/AdityaJareda) -* LinkedIn: [Aditya Singh](https://www.linkedin.com/in/adityajareda) +- GitHub: [@AdityaJareda](https://github.com/AdityaJareda) +- LinkedIn: [Aditya Singh](https://www.linkedin.com/in/adityajareda) + +--- ## Acknowledgments -* Built as part of DevOps learning journey -* Inspired by various DevOps best practices -* Quote data sourced from public domain +- Built as part of my DevOps learning journey +- Inspired by modern DevOps best practices and industry standards +- Quote data sourced from public domain + +--- + +## Additional Resources + +- [Docker Documentation](https://docs.docker.com/) +- [GitHub Actions Documentation](https://docs.github.com/en/actions) +- [Express.js Guide](https://expressjs.com/) +- [Jest Testing Framework](https://jestjs.io/) +- [Docker Hub Repository](https://hub.docker.com/r/adityajareda/quote-app) --- -**A small step toward mastering DevOps.** +A small step toward mastering DevOps.