Compare commits

...

1 Commits

Author SHA1 Message Date
Abed aec71dc6ff feat#64 2025-02-20 15:49:14 +08:00
2 changed files with 133 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy requirements.txt first to leverage Docker caching
COPY requirements.txt ./
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code to the container
COPY . .
# Expose the port that Flask runs on
EXPOSE 5000
# Set environment variables for Flask
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
# Command to run the Flask application
CMD ["flask", "run"]
+110
View File
@@ -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)