|
|
|
@@ -0,0 +1,110 @@
|
|
|
|
|
from flask import Flask, request, jsonify
|
|
|
|
|
import os
|
|
|
|
|
import consul
|
|
|
|
|
import configparser
|
|
|
|
|
from werkzeug.utils import secure_filename,send_from_directory
|
|
|
|
|
from opentelemetry import trace
|
|
|
|
|
from opentelemetry.instrumentation.flask import FlaskInstrumentor
|
|
|
|
|
from opentelemetry.instrumentation.requests import RequestsInstrumentor
|
|
|
|
|
from opentelemetry.sdk.trace import TracerProvider
|
|
|
|
|
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
|
|
|
|
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
|
|
|
|
|
|
|
|
|
|
# Initialize Flask app
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
# Configuration for file uploads
|
|
|
|
|
UPLOAD_FOLDER = './amqp/example/uploaded_files'
|
|
|
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
|
|
|
|
|
|
# Initialize Consul client
|
|
|
|
|
consul_client = consul.Consul(host='192.168.229.128', port=8500)
|
|
|
|
|
|
|
|
|
|
# OpenTelemetry configuration
|
|
|
|
|
trace.set_tracer_provider(TracerProvider())
|
|
|
|
|
otlp_exporter = OTLPSpanExporter(endpoint="http://192.168.229.128:4317", insecure=True)
|
|
|
|
|
span_processor = BatchSpanProcessor(otlp_exporter)
|
|
|
|
|
trace.get_tracer_provider().add_span_processor(span_processor)
|
|
|
|
|
FlaskInstrumentor().instrument_app(app)
|
|
|
|
|
RequestsInstrumentor().instrument()
|
|
|
|
|
|
|
|
|
|
# In-memory store for CRUD operations
|
|
|
|
|
data_store = {}
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
# Read the application.properties file
|
|
|
|
|
config.read('application.properties')
|
|
|
|
|
#
|
|
|
|
|
# Add individual config areas
|
|
|
|
|
#
|
|
|
|
|
self.OTLP_endpoint = config.get('item.example.OTLP_endpoint')
|
|
|
|
|
|
|
|
|
|
# CRUD Endpoints
|
|
|
|
|
|
|
|
|
|
@app.route('/items', methods=['POST'])
|
|
|
|
|
def create_item():
|
|
|
|
|
item_id = request.json.get('item_id')
|
|
|
|
|
data = request.json.get('data')
|
|
|
|
|
|
|
|
|
|
if not item_id or not data:
|
|
|
|
|
return jsonify({"error": "Item ID and data are required"}), 400
|
|
|
|
|
|
|
|
|
|
data_store[item_id] = data
|
|
|
|
|
return jsonify({"message": "Item created", "item_id": item_id})
|
|
|
|
|
|
|
|
|
|
@app.route('/items', methods=['GET'])
|
|
|
|
|
def get_all_items():
|
|
|
|
|
return jsonify({"items": data_store})
|
|
|
|
|
|
|
|
|
|
@app.route('/items/<item_id>', methods=['GET'])
|
|
|
|
|
def read_item(item_id):
|
|
|
|
|
data = data_store.get(item_id)
|
|
|
|
|
if data:
|
|
|
|
|
return jsonify({"item_id": item_id, "data": data})
|
|
|
|
|
return jsonify({"error": "Item not found"}), 404
|
|
|
|
|
|
|
|
|
|
@app.route('/items/<item_id>', methods=['PUT'])
|
|
|
|
|
def update_item(item_id):
|
|
|
|
|
data = request.json.get('data')
|
|
|
|
|
if not data:
|
|
|
|
|
return jsonify({"error": "Data is required"}), 400
|
|
|
|
|
|
|
|
|
|
if item_id in data_store:
|
|
|
|
|
data_store[item_id] = data
|
|
|
|
|
return jsonify({"message": "Item updated", "item_id": item_id})
|
|
|
|
|
return jsonify({"error": "Item not found"}), 404
|
|
|
|
|
|
|
|
|
|
@app.route('/items/<item_id>', methods=['DELETE'])
|
|
|
|
|
def delete_item(item_id):
|
|
|
|
|
if item_id in data_store:
|
|
|
|
|
del data_store[item_id]
|
|
|
|
|
return jsonify({"message": "Item deleted", "item_id": item_id})
|
|
|
|
|
return jsonify({"error": "Item not found"}), 404
|
|
|
|
|
|
|
|
|
|
# File Upload and Download Endpoints
|
|
|
|
|
|
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
|
|
|
def upload_file():
|
|
|
|
|
if 'file' not in request.files:
|
|
|
|
|
return jsonify({"error": "No file part"}), 400
|
|
|
|
|
|
|
|
|
|
file = request.files['file']
|
|
|
|
|
if file.filename == '':
|
|
|
|
|
return jsonify({"error": "No selected file"}), 400
|
|
|
|
|
|
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
|
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
|
|
|
|
return jsonify({"message": "File uploaded", "filename": filename})
|
|
|
|
|
|
|
|
|
|
@app.route('/download/<filename>', methods=['GET'])
|
|
|
|
|
def download_file(filename):
|
|
|
|
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
|
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
|
|
|
|
|
return jsonify({"error": "File not found"}), 404
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run(debug=True)
|