diff --git a/app_python/app.py b/app_python/app.py new file mode 100644 index 0000000000..65f956bee7 --- /dev/null +++ b/app_python/app.py @@ -0,0 +1,85 @@ +import os +import socket +import platform +import logging +from datetime import datetime, timezone +from flask import Flask, jsonify, request + +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') +logger = logging.getLogger(__name__) + +app = Flask(__name__) + +HOST = os.getenv('HOST', '0.0.0.0') +PORT = int(os.getenv('PORT', 5000)) +START_TIME = datetime.now(timezone.utc) + +def get_system_info(): + return { + 'hostname': socket.gethostname(), + 'platform': platform.system(), + 'platform_version': platform.release(), + 'architecture': platform.machine(), + 'cpu_count': os.cpu_count(), + 'python_version': platform.python_version() + } + +def get_uptime(): + delta = datetime.now(timezone.utc) - START_TIME + seconds = int(delta.total_seconds()) + hours, remainder = divmod(seconds, 3600) + minutes, _ = divmod(remainder, 60) + return { + 'seconds': seconds, + 'human': f"{hours} hour{'s' if hours != 1 else ''}, {minutes} minute{'s' if minutes != 1 else ''}" + } + +@app.route('/', methods=['GET']) +def index(): + logger.info(f"Request: {request.method} {request.path} from {request.remote_addr}") + uptime = get_uptime() + return jsonify({ + "service": { + "name": "devops-info-service", + "version": "1.0.0", + "description": "DevOps course info service", + "framework": "Flask" + }, + "system": get_system_info(), + "runtime": { + "uptime_seconds": uptime['seconds'], + "uptime_human": uptime['human'], + "current_time": datetime.now(timezone.utc).isoformat(), + "timezone": "UTC" + }, + "request": { + "client_ip": request.remote_addr or 'unknown', + "user_agent": request.headers.get('User-Agent', 'unknown'), + "method": request.method, + "path": request.path + }, + "endpoints": [ + {"path": "/", "method": "GET", "description": "Service information"}, + {"path": "/health", "method": "GET", "description": "Health check"} + ] + }) + +@app.route('/health', methods=['GET']) +def health(): + return jsonify({ + 'status': 'healthy', + 'timestamp': datetime.now(timezone.utc).isoformat(), + 'uptime_seconds': get_uptime()['seconds'] + }) + +@app.errorhandler(404) +def not_found(error): + return jsonify({'error': 'Not Found', 'message': 'Endpoint does not exist'}), 404 + +@app.errorhandler(500) +def internal_error(error): + return jsonify({'error': 'Internal Server Error', 'message': 'An unexpected error occurred'}), 500 + +if __name__ == '__main__': + logger.info('Application starting...') + app.run(host=HOST, port=PORT, debug=False) diff --git a/app_python/docs/LAB01.md b/app_python/docs/LAB01.md new file mode 100644 index 0000000000..05e96ad286 --- /dev/null +++ b/app_python/docs/LAB01.md @@ -0,0 +1,33 @@ +# LAB01 - DevOps Info Service + +## Framework Selection +Flask 3.0.3 - lightweight framework suitable for simple APIs and microservices. + +## Best Practices Applied +1. Structured logging with timestamps +2. Error handling for 404/500 responses +3. Environment variables for configuration +4. PEP8 compliant code organization + +## API Documentation +GET / - Service and system information +GET /health - Health check endpoint + +text + +## Testing Evidence +![Main endpoint](screenshots/01-main-endpoint.png) +![Health check](screenshots/02-health-check.png) +![Terminal output](screenshots/03-formatted-output.png) + +## GitHub Community Engagement +- Starred: inno-devops-labs/DevOps-Core-Course +- Starred: simple-container-com/api +- Following: Cre-eD, marat-biriushev, pierrepicaud +- Following 3 classmates + +Stars increase project visibility. Following helps track best practices. + +## Challenges & Solutions +- Windows venv activation via direct python.exe path +- Client IP shows 127.0.0.1 for localhost correctly \ No newline at end of file diff --git a/app_python/docs/screenshots/01-main-endpoint.png b/app_python/docs/screenshots/01-main-endpoint.png new file mode 100644 index 0000000000..9d7e876906 Binary files /dev/null and b/app_python/docs/screenshots/01-main-endpoint.png differ diff --git a/app_python/docs/screenshots/02-health-check.png b/app_python/docs/screenshots/02-health-check.png new file mode 100644 index 0000000000..550fdf19fa Binary files /dev/null and b/app_python/docs/screenshots/02-health-check.png differ diff --git a/app_python/docs/screenshots/03-formatted-output.png b/app_python/docs/screenshots/03-formatted-output.png new file mode 100644 index 0000000000..5d441626c1 Binary files /dev/null and b/app_python/docs/screenshots/03-formatted-output.png differ diff --git a/app_python/requirements.txt b/app_python/requirements.txt new file mode 100644 index 0000000000..95fef4eb66 --- /dev/null +++ b/app_python/requirements.txt @@ -0,0 +1 @@ +Flask==3.0.3 diff --git a/app_python/tests/__init__.py b/app_python/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2