#!/bin/bash # Health Check Script for DevSecOps Todo Application # This script verifies that both frontend and backend services are running correctly set -e # Exit on any error echo "๐Ÿฅ Starting health checks for DevSecOps Todo Application..." # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration BACKEND_URL="${BACKEND_URL:-http://localhost:3001}" FRONTEND_URL="${FRONTEND_URL:-http://localhost:3000}" MAX_RETRIES=5 RETRY_DELAY=2 # Function to check service health check_service() { local url=$1 local service_name=$2 local retries=0 echo "๐Ÿ” Checking $service_name at $url..." while [ $retries -lt $MAX_RETRIES ]; do if curl -f -s "$url/health" > /dev/null 2>&1; then echo -e "${GREEN}โœ… $service_name is healthy${NC}" return 0 else retries=$((retries + 1)) echo -e "${YELLOW}โณ $service_name not ready, attempt $retries/$MAX_RETRIES${NC}" if [ $retries -lt $MAX_RETRIES ]; then sleep $RETRY_DELAY fi fi done echo -e "${RED}โŒ $service_name health check failed after $MAX_RETRIES attempts${NC}" return 1 } # Function to check API endpoints check_api_endpoints() { echo "๐Ÿ” Testing API endpoints..." # Test todos endpoint if curl -f -s "$BACKEND_URL/api/todos" > /dev/null 2>&1; then echo -e "${GREEN}โœ… Todos API endpoint is working${NC}" else echo -e "${RED}โŒ Todos API endpoint failed${NC}" return 1 fi # Test stats endpoint if curl -f -s "$BACKEND_URL/api/stats" > /dev/null 2>&1; then echo -e "${GREEN}โœ… Stats API endpoint is working${NC}" else echo -e "${RED}โŒ Stats API endpoint failed${NC}" return 1 fi return 0 } # Function to test basic functionality test_basic_functionality() { echo "๐Ÿงช Testing basic functionality..." # Test creating a todo local response=$(curl -s -X POST \ -H "Content-Type: application/json" \ -d '{"title":"Health Check Todo","priority":"low"}' \ "$BACKEND_URL/api/todos") if echo "$response" | grep -q "Health Check Todo"; then echo -e "${GREEN}โœ… Todo creation test passed${NC}" else echo -e "${RED}โŒ Todo creation test failed${NC}" return 1 fi return 0 } # Main health check execution main() { echo "==================================================" echo "๐Ÿš€ DevSecOps Todo Application Health Check" echo "==================================================" local failed=0 # Check backend health if ! check_service "$BACKEND_URL" "Backend"; then failed=1 fi # Check frontend health (if running on different port) if [ "$FRONTEND_URL" != "$BACKEND_URL" ]; then if ! check_service "$FRONTEND_URL" "Frontend"; then failed=1 fi fi # Test API endpoints if ! check_api_endpoints; then failed=1 fi # Test basic functionality if ! test_basic_functionality; then failed=1 fi echo "==================================================" if [ $failed -eq 0 ]; then echo -e "${GREEN}๐ŸŽ‰ All health checks passed!${NC}" echo -e "${GREEN}๐Ÿ“Š Application is ready for use${NC}" exit 0 else echo -e "${RED}๐Ÿ’ฅ Some health checks failed!${NC}" echo -e "${RED}๐Ÿ”ง Please check the application logs${NC}" exit 1 fi } # Show usage if help requested if [[ "$1" == "--help" || "$1" == "-h" ]]; then echo "DevSecOps Todo Application Health Check Script" echo "" echo "Usage: $0 [OPTIONS]" echo "" echo "Environment Variables:" echo " BACKEND_URL Backend service URL (default: http://localhost:3001)" echo " FRONTEND_URL Frontend service URL (default: http://localhost:3000)" echo "" echo "Options:" echo " -h, --help Show this help message" echo "" echo "Examples:" echo " $0 # Check local services" echo " BACKEND_URL=http://api.example.com $0 # Check remote backend" exit 0 fi # Run main function main "$@"