Skip to content

Commit 07c8cd5

Browse files
author
whsmith
committed
update requiremnts, fix .gitignore, add rate limiting
1 parent 6e9f3a1 commit 07c8cd5

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

.gitignore

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,4 @@ build/
5555
include/
5656
lib/
5757
lib64/
58-
libs/
59-
60-
# Project Specific
61-
62-
data/testimage.jpg
63-
temp/*.*
64-
temp/*
65-
jobs/*.*
66-
jobs/*
67-
```
58+
libs/

main.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from flask import Flask, request, jsonify, send_file, render_template
2+
from flask_limiter import Limiter
3+
from flask_limiter.util import get_remote_address
24
from datetime import datetime
35
import cv2
46
import dlib
@@ -17,13 +19,34 @@
1719
# Create Flask app
1820
app = Flask(__name__)
1921

22+
# Create rate limiter
23+
limiter = Limiter(
24+
get_remote_address,
25+
app=app,
26+
default_limits=["1 per second", "10 per minute", "1000 per day"],
27+
storage_uri=os.getenv("mongo_url"),
28+
)
29+
30+
# Set the key function
31+
limiter.key_func = lambda: request.remote_addr
32+
2033
# Connect to MongoDB and create TTL index for jobs collection
2134
client = MongoClient(os.getenv("mongo_url"))
2235
db = client[os.getenv("mongo_db")]
2336

2437
# Create TTL index for jobs collection with expiration after one hour
2538
expire_after = int(os.getenv("mongo_job_expire_after"))
26-
if expire_after is not 0:
39+
40+
# Check if the TTL index exists and has the same expiration time
41+
existing_index_info = db.jobs.index_information()
42+
if (
43+
"created_at_1" not in existing_index_info
44+
or existing_index_info["created_at_1"]["expireAfterSeconds"] != expire_after
45+
):
46+
# If the index doesn't exist or has a different expiration time, delete and recreate it
47+
if "created_at_1" in existing_index_info:
48+
db.jobs.drop_index("created_at_1")
49+
2750
db.jobs.create_index("created_at", expireAfterSeconds=expire_after)
2851

2952
# Create GridFS object
@@ -122,12 +145,14 @@ def process_image(image):
122145

123146

124147
@app.route("/", methods=["GET"])
148+
@limiter.exempt
125149
def index():
126150
"""Render the index.html template"""
127151
return render_template("index.html")
128152

129153

130154
@app.route("/overlay", methods=["POST"])
155+
@limiter.limit("10 per minute", override_defaults=False)
131156
def overlay():
132157
"""Process an image and return the processed image and data"""
133158
try:
@@ -177,6 +202,7 @@ def overlay():
177202

178203

179204
@app.route("/jobs/<job_id>", methods=["GET"])
205+
@limiter.exempt
180206
def get_job(job_id):
181207
"""Get a job by its ID"""
182208
try:
@@ -189,6 +215,7 @@ def get_job(job_id):
189215

190216

191217
@app.route("/jobs/<job_id>/result_image.png", methods=["GET"])
218+
@limiter.limit("20 per minute")
192219
def get_result_image(job_id):
193220
"""Get the result image of a job by its ID"""
194221
try:

requirements.txt

576 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)