-
Notifications
You must be signed in to change notification settings - Fork 6
/
extensions.py
28 lines (24 loc) · 880 Bytes
/
extensions.py
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
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize extensions
db = SQLAlchemy()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
def get_rate_limit_string():
"""Get rate limit string from environment variables"""
per_minute = os.getenv('RATE_LIMIT_PER_MINUTE', '20')
per_hour = os.getenv('RATE_LIMIT_PER_HOUR', '200')
per_day = os.getenv('RATE_LIMIT_PER_DAY', '1000')
return f"{per_minute} per minute;{per_hour} per hour;{per_day} per day"
# Initialize limiter
limiter = Limiter(
key_func=get_remote_address,
default_limits=[get_rate_limit_string()],
storage_uri=f"{os.getenv('RATE_LIMIT_STORAGE', 'memory')}://"
)