forked from noahgift/python-devops-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
137 lines (116 loc) · 3.67 KB
/
Copy pathapplication.py
File metadata and controls
137 lines (116 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
"""
Flask Web Application
A web interface for the Python DevOps project
"""
from flask import Flask, render_template, request, jsonify
import json
import sys
import os
# Add src directory to path for imports
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from cli.lambda_function import lambda_handler
from cli.helloclick import tokenize
from cli.gcli import search
from aws.cloudwatch_monitor import CloudWatchMonitor
app = Flask(__name__)
@app.route('/')
def index():
"""Home page"""
return render_template('index.html')
@app.route('/api/lambda', methods=['POST'])
def api_lambda():
"""Lambda function API endpoint"""
try:
data = request.get_json()
name = data.get('name', 'World')
# Call the Lambda function
result = lambda_handler({'name': name}, {})
return jsonify({
'success': True,
'result': result,
'message': 'Lambda function executed successfully'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'message': 'Error executing Lambda function'
}), 500
@app.route('/api/tokenize', methods=['POST'])
def api_tokenize():
"""Tokenize text API endpoint"""
try:
data = request.get_json()
phrase = data.get('phrase', '')
if not phrase:
return jsonify({
'success': False,
'error': 'No phrase provided',
'message': 'Please provide a phrase to tokenize'
}), 400
# Tokenize the phrase
words = phrase.split()
return jsonify({
'success': True,
'original': phrase,
'tokenized': words,
'count': len(words),
'message': 'Text tokenized successfully'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'message': 'Error tokenizing text'
}), 500
@app.route('/api/cloudwatch', methods=['GET'])
def api_cloudwatch():
"""CloudWatch metrics API endpoint"""
try:
monitor = CloudWatchMonitor()
# Get function info
function_info = monitor.get_lambda_function_info('python-devops-lambda')
# Get metrics
metrics = monitor.get_lambda_metrics('python-devops-lambda', 24)
# Get recent logs
logs = monitor.get_lambda_logs('python-devops-lambda', 1)
return jsonify({
'success': True,
'function_info': function_info,
'metrics': metrics,
'recent_logs': logs[:5], # Last 5 logs
'message': 'CloudWatch data retrieved successfully'
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'message': 'Error retrieving CloudWatch data'
}), 500
@app.route('/api/health')
def api_health():
"""Health check endpoint"""
return jsonify({
'status': 'healthy',
'service': 'Python DevOps Web API',
'version': '1.0.0'
})
@app.errorhandler(404)
def not_found(error):
"""404 error handler"""
return jsonify({
'success': False,
'error': 'Not Found',
'message': 'The requested resource was not found'
}), 404
@app.errorhandler(500)
def internal_error(error):
"""500 error handler"""
return jsonify({
'success': False,
'error': 'Internal Server Error',
'message': 'An internal server error occurred'
}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)