Skip to content

Commit 2837fd6

Browse files
Adding test flask cos event listener
1 parent a7cb647 commit 2837fd6

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

apps/cos-event-trigger/.mise.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[tools]
2+
python = "3.12"
3+
usage = "latest"
4+
5+
[env]
6+
_.python.venv = { path = ".venv", create = true }
7+
PROJECT_NAME = "{{ config_root | basename }}"
8+
9+
[tasks."uv:reqs"]
10+
description = "Install dependencies from requirements file"
11+
alias = "uvr"
12+
run = "uv pip install -r requirements.txt"
13+
14+
[tasks."uv:freeze"]
15+
description = "Create requirements.txt from currently installed modules"
16+
alias = "uvf"
17+
run = "uv pip freeze > requirements.txt"
18+
19+
[tasks."uv:install"]
20+
description = "Install pip packages"
21+
alias = "uvi"
22+
run = "uv pip install"
23+
24+
[tasks.info]
25+
description = "Print project information"
26+
run = '''
27+
echo "Project: $PROJECT_NAME"
28+
echo "Virtual Environment: $VIRTUAL_ENV"
29+
'''

apps/cos-event-trigger/Dockerfile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Stage 1: Build stage
2+
FROM python:3.12-slim AS builder
3+
4+
WORKDIR /app
5+
6+
# Install venv
7+
RUN python -m venv /opt/venv
8+
# Activate venv
9+
ENV PATH="/opt/venv/bin:$PATH"
10+
11+
COPY requirements.txt .
12+
RUN pip install --no-cache-dir --upgrade pip && \
13+
pip install --no-cache-dir -r requirements.txt
14+
15+
# Stage 2: Runtime stage
16+
FROM python:3.12-slim
17+
18+
WORKDIR /app
19+
20+
# Create non-root user
21+
RUN adduser --disabled-password --gecos "" appuser
22+
23+
# Copy virtual environment from builder stage
24+
COPY --from=builder /opt/venv /opt/venv
25+
26+
# Set environment variables
27+
ENV PATH="/opt/venv/bin:$PATH" \
28+
PYTHONDONTWRITEBYTECODE=1 \
29+
PYTHONUNBUFFERED=1
30+
31+
# Copy application code
32+
COPY app.py .
33+
34+
# Switch to non-root user
35+
USER appuser
36+
37+
EXPOSE 8080
38+
39+
CMD ["python", "app.py"]

apps/cos-event-trigger/app.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
from flask import Flask, request, jsonify
3+
import json
4+
from datetime import datetime
5+
6+
app = Flask(__name__)
7+
8+
# Equivalent to EventStats in Go
9+
class EventStats:
10+
def __init__(self):
11+
self.by_bucket = {}
12+
self.by_type = {}
13+
self.by_object = {}
14+
15+
def to_dict(self):
16+
return {
17+
"by_bucket": self.by_bucket,
18+
"by_type": self.by_type,
19+
"by_object": self.by_object
20+
}
21+
22+
# Global stats object
23+
stats = EventStats()
24+
25+
@app.route('/stats', methods=['GET'])
26+
def get_stats():
27+
return jsonify(stats.to_dict())
28+
29+
@app.route('/', methods=['POST'])
30+
def handle_event():
31+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
32+
body = request.data.decode('utf-8')
33+
34+
# Parse the event data
35+
event = json.loads(body)
36+
37+
# Update stats
38+
bucket = event.get('bucket', 'unknown')
39+
operation = event.get('operation', 'unknown')
40+
key = event.get('key', 'unknown')
41+
42+
stats.by_bucket[bucket] = stats.by_bucket.get(bucket, 0) + 1
43+
stats.by_type[operation] = stats.by_type.get(operation, 0) + 1
44+
stats.by_object[key] = stats.by_object.get(key, 0) + 1
45+
46+
print(f"{current_time} - Received:")
47+
print(f"\nBody: {body}")
48+
49+
return "OK"
50+
51+
if __name__ == '__main__':
52+
print("Listening on port 8080")
53+
app.run(host='0.0.0.0', port=8080)

0 commit comments

Comments
 (0)