74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
import os
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
|
import redis.asyncio as redis
|
|
import asyncio
|
|
from config import *
|
|
|
|
# Create a new event loop for asyncio
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
|
|
# ========================= RETRIEVE CONFIG =========================
|
|
if not all([MONGO_PASSWORD, MONGODB_HOST, REDIS_HOST]):
|
|
raise EnvironmentError("Missing required environment variables for MongoDB/Redis")
|
|
|
|
# ========================= DATABASE URI =========================
|
|
mongo_uri = f"mongodb://admin:{MONGO_PASSWORD}@{MONGODB_HOST}:{MONGO_PORT}/"
|
|
|
|
# ========================= MONGO CLIENT =========================
|
|
mongo_client = AsyncIOMotorClient(mongo_uri)
|
|
|
|
# Function to get database collections lazily
|
|
def get_collection(db_name, collection_name):
|
|
return mongo_client[db_name][collection_name]
|
|
|
|
# Access collections when needed
|
|
competitions_col = get_collection("competitions", "competitions")
|
|
competition_request_col = get_collection("competitions", "request")
|
|
users_collection = get_collection("auth", "users")
|
|
users_token_collection = get_collection("auth", "token")
|
|
notify_collection = get_collection("notify", "notify")
|
|
posts_collection = get_collection("posts", "posts")
|
|
news_collection = get_collection("news", "news")
|
|
social_collection = get_collection("social", "social")
|
|
vk_photos_collection = get_collection("vk", "photos")
|
|
invitations_collection = get_collection("invitations", "invitations")
|
|
delivery_collection = get_collection("delivery", "delivery")
|
|
exhibition_collection = get_collection("exhibition", "exhibition")
|
|
|
|
# ========================= GRIDFS =========================
|
|
bucket_competitions = AsyncIOMotorGridFSBucket(mongo_client["competitions"])
|
|
gridfs_map = {
|
|
"competition": bucket_competitions,
|
|
"competitions": bucket_competitions,
|
|
"users": AsyncIOMotorGridFSBucket(mongo_client["auth"]),
|
|
"posts": AsyncIOMotorGridFSBucket(mongo_client["posts"]),
|
|
"news": AsyncIOMotorGridFSBucket(mongo_client["news"]),
|
|
"social": AsyncIOMotorGridFSBucket(mongo_client["social"]),
|
|
"exhibition": AsyncIOMotorGridFSBucket(mongo_client["exhibition"]),
|
|
}
|
|
|
|
|
|
# ========================= REDIS CLIENT =========================
|
|
redis_client = redis.Redis(
|
|
host=REDIS_HOST,
|
|
port=REDIS_PORT,
|
|
db=0,
|
|
decode_responses=True
|
|
)
|
|
|
|
redis_uri = f'redis://{REDIS_HOST}:{REDIS_PORT}/'
|
|
rabbit_mq_uri = f'amqps://{RABBITMQ_USER}:{RABBITMQ_PASS}@{RABBITMQ_HOST}:{RABBITMQ_PORT}/'
|
|
|
|
# Exporting collections and clients
|
|
__all__ = [
|
|
"competitions_col", "competition_request_col",
|
|
"users_collection", "users_token_collection",
|
|
"notify_collection", "social_collection",
|
|
"posts_collection", "news_collection",
|
|
"gridfs_map", "redis_client", "redis_uri"
|
|
"mongo_client", "invitations_collection",
|
|
"delivery_collection", "exhibition_collection",
|
|
"rabbit_mq_uri"
|
|
]
|