mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
feat: storing logs in mongodb
This commit is contained in:
parent
7628419a26
commit
4ef82bad22
@ -31,3 +31,5 @@ USE_MINIO=1
|
|||||||
|
|
||||||
# Nginx Configuration
|
# Nginx Configuration
|
||||||
NGINX_PORT=80
|
NGINX_PORT=80
|
||||||
|
|
||||||
|
MONGO_DB_URL="mongodb://plane-mongodb:27017/"
|
23
apiserver/plane/db/mongodb.py
Normal file
23
apiserver/plane/db/mongodb.py
Normal file
@ -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
|
@ -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:
|
class APITokenLogMiddleware:
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
self.database = Database(os.environ.get("MONGO_DB_URL"), "plane")
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
request_body = request.body
|
request_body = request.body
|
||||||
@ -17,23 +22,31 @@ class APITokenLogMiddleware:
|
|||||||
# If the API key is present, log the request
|
# If the API key is present, log the request
|
||||||
if api_key:
|
if api_key:
|
||||||
try:
|
try:
|
||||||
APIActivityLog.objects.create(
|
db = self.database.get_db()
|
||||||
token_identifier=api_key,
|
collection = db["api_activity_logs"]
|
||||||
path=request.path,
|
_ = collection.insert_one(
|
||||||
method=request.method,
|
{
|
||||||
query_params=request.META.get("QUERY_STRING", ""),
|
"token_identifier": api_key,
|
||||||
headers=str(request.headers),
|
"path": request.path,
|
||||||
body=(
|
"method": request.method,
|
||||||
request_body.decode("utf-8") if request_body else None
|
"query_params": request.META.get("QUERY_STRING", ""),
|
||||||
),
|
"headers": str(request.headers),
|
||||||
response_body=(
|
"body": (
|
||||||
response.content.decode("utf-8")
|
request_body.decode("utf-8")
|
||||||
if response.content
|
if request_body
|
||||||
else None
|
else None
|
||||||
),
|
),
|
||||||
response_code=response.status_code,
|
"response_body": (
|
||||||
ip_address=request.META.get("REMOTE_ADDR", None),
|
response.content.decode("utf-8")
|
||||||
user_agent=request.META.get("HTTP_USER_AGENT", None),
|
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:
|
except Exception as e:
|
||||||
|
@ -33,4 +33,4 @@ posthog==3.0.2
|
|||||||
cryptography==42.0.0
|
cryptography==42.0.0
|
||||||
lxml==4.9.3
|
lxml==4.9.3
|
||||||
boto3==1.28.40
|
boto3==1.28.40
|
||||||
|
pymongo==4.6.1
|
@ -55,6 +55,7 @@ x-app-env : &app-env
|
|||||||
- MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-"secret-key"}
|
- MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-"secret-key"}
|
||||||
- BUCKET_NAME=${BUCKET_NAME:-uploads}
|
- BUCKET_NAME=${BUCKET_NAME:-uploads}
|
||||||
- FILE_SIZE_LIMIT=${FILE_SIZE_LIMIT:-5242880}
|
- 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'
|
command: postgres -c 'max_connections=1000'
|
||||||
volumes:
|
volumes:
|
||||||
- pgdata:/var/lib/postgresql/data
|
- 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:
|
plane-redis:
|
||||||
<<: *app-env
|
<<: *app-env
|
||||||
@ -172,3 +181,4 @@ volumes:
|
|||||||
pgdata:
|
pgdata:
|
||||||
redisdata:
|
redisdata:
|
||||||
uploads:
|
uploads:
|
||||||
|
mongodbdata:
|
||||||
|
@ -8,6 +8,7 @@ volumes:
|
|||||||
redisdata:
|
redisdata:
|
||||||
uploads:
|
uploads:
|
||||||
pgdata:
|
pgdata:
|
||||||
|
mongodbdata:
|
||||||
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
@ -45,6 +46,16 @@ services:
|
|||||||
- .env
|
- .env
|
||||||
environment:
|
environment:
|
||||||
PGDATA: /var/lib/postgresql/data
|
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:
|
web:
|
||||||
build:
|
build:
|
||||||
|
Loading…
Reference in New Issue
Block a user