diff --git a/.env.example b/.env.example index 71a9074a6..0649839a4 100644 --- a/.env.example +++ b/.env.example @@ -31,3 +31,5 @@ USE_MINIO=1 # Nginx Configuration NGINX_PORT=80 + +MONGO_DB_URL="mongodb://plane-mongodb:27017/" \ No newline at end of file diff --git a/apiserver/plane/db/mongodb.py b/apiserver/plane/db/mongodb.py new file mode 100644 index 000000000..355e16f78 --- /dev/null +++ b/apiserver/plane/db/mongodb.py @@ -0,0 +1,23 @@ +from pymongo import MongoClient + +def singleton(cls): + instances = {} + + def wrapper(*args, **kwargs): + if cls not in instances: + instances[cls] = cls(*args, **kwargs) + return instances[cls] + + return wrapper + +@singleton +class Database: + db = None + client = None + + def __init__(self, mongo_uri, database_name): + self.client = MongoClient(mongo_uri) + self.db = self.client[database_name] + + def get_db(self): + return self.db diff --git a/apiserver/plane/middleware/api_log_middleware.py b/apiserver/plane/middleware/api_log_middleware.py index a49d43b55..d6040fe88 100644 --- a/apiserver/plane/middleware/api_log_middleware.py +++ b/apiserver/plane/middleware/api_log_middleware.py @@ -1,9 +1,14 @@ -from plane.db.models import APIToken, APIActivityLog +# Python imports +import os + +# Django imports +from plane.db.mongodb import Database class APITokenLogMiddleware: def __init__(self, get_response): self.get_response = get_response + self.database = Database(os.environ.get("MONGO_DB_URL"), "plane") def __call__(self, request): request_body = request.body @@ -17,23 +22,31 @@ class APITokenLogMiddleware: # If the API key is present, log the request if api_key: try: - APIActivityLog.objects.create( - token_identifier=api_key, - path=request.path, - method=request.method, - query_params=request.META.get("QUERY_STRING", ""), - headers=str(request.headers), - body=( - request_body.decode("utf-8") if request_body else None - ), - response_body=( - response.content.decode("utf-8") - if response.content - else None - ), - response_code=response.status_code, - ip_address=request.META.get("REMOTE_ADDR", None), - user_agent=request.META.get("HTTP_USER_AGENT", None), + db = self.database.get_db() + collection = db["api_activity_logs"] + _ = collection.insert_one( + { + "token_identifier": api_key, + "path": request.path, + "method": request.method, + "query_params": request.META.get("QUERY_STRING", ""), + "headers": str(request.headers), + "body": ( + request_body.decode("utf-8") + if request_body + else None + ), + "response_body": ( + response.content.decode("utf-8") + if response.content + else None + ), + "response_code": response.status_code, + "ip_address": request.META.get("REMOTE_ADDR", None), + "user_agent": request.META.get( + "HTTP_USER_AGENT", None + ), + } ) except Exception as e: diff --git a/apiserver/requirements/base.txt b/apiserver/requirements/base.txt index 194bf8d90..e9147e62d 100644 --- a/apiserver/requirements/base.txt +++ b/apiserver/requirements/base.txt @@ -33,4 +33,4 @@ posthog==3.0.2 cryptography==42.0.0 lxml==4.9.3 boto3==1.28.40 - +pymongo==4.6.1 \ No newline at end of file diff --git a/deploy/selfhost/docker-compose.yml b/deploy/selfhost/docker-compose.yml index 60861878c..fd6e6d562 100644 --- a/deploy/selfhost/docker-compose.yml +++ b/deploy/selfhost/docker-compose.yml @@ -55,6 +55,7 @@ x-app-env : &app-env - MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-"secret-key"} - BUCKET_NAME=${BUCKET_NAME:-uploads} - FILE_SIZE_LIMIT=${FILE_SIZE_LIMIT:-5242880} + - MONGO_DB_URL=${MONGO_DB_URL:-"mongodb://plane-mongodb:27017/"} @@ -138,6 +139,14 @@ services: command: postgres -c 'max_connections=1000' volumes: - pgdata:/var/lib/postgresql/data + + plane-mongodb: + <<: *app-env + image: mongo:7.0.5 + pull_policy: if_not_present + restart: unless-stopped + volumes: + - mongodbdata:/data/db plane-redis: <<: *app-env @@ -172,3 +181,4 @@ volumes: pgdata: redisdata: uploads: + mongodbdata: diff --git a/docker-compose-local.yml b/docker-compose-local.yml index 5f49e4897..59a5041d4 100644 --- a/docker-compose-local.yml +++ b/docker-compose-local.yml @@ -8,6 +8,7 @@ volumes: redisdata: uploads: pgdata: + mongodbdata: services: @@ -45,6 +46,16 @@ services: - .env environment: PGDATA: /var/lib/postgresql/data + + plane-mongodb: + image: mongo:7.0.5 + restart: unless-stopped + networks: + - dev_env + volumes: + - mongodbdata:/data/db + env_file: + - .env web: build: