Backend Draft
This commit is contained in:
3
backend/core/__init__.py
Normal file
3
backend/core/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .celery import app as celery_app
|
||||
|
||||
__all__ = ('celery_app',)
|
||||
BIN
backend/core/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
backend/core/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/celery.cpython-314.pyc
Normal file
BIN
backend/core/__pycache__/celery.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/settings.cpython-314.pyc
Normal file
BIN
backend/core/__pycache__/settings.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/urls.cpython-314.pyc
Normal file
BIN
backend/core/__pycache__/urls.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/core/__pycache__/wsgi.cpython-314.pyc
Normal file
BIN
backend/core/__pycache__/wsgi.cpython-314.pyc
Normal file
Binary file not shown.
7
backend/core/asgi.py
Normal file
7
backend/core/asgi.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.dev')
|
||||
|
||||
application = get_asgi_application()
|
||||
20
backend/core/celery.py
Normal file
20
backend/core/celery.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
from celery import Celery
|
||||
|
||||
# Set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.dev')
|
||||
|
||||
app = Celery('mtcbd')
|
||||
|
||||
# Using a string here means the worker doesn't have to serialize
|
||||
# the configuration object to child processes.
|
||||
# - namespace='CELERY' means all celery-related configuration keys
|
||||
# should have a `CELERY_` prefix.
|
||||
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||
|
||||
# Load task modules from all registered Django apps.
|
||||
app.autodiscover_tasks()
|
||||
|
||||
@app.task(bind=True, ignore_result=True)
|
||||
def debug_task(self):
|
||||
print(f'Request: {self.request!r}')
|
||||
0
backend/core/settings/__init__.py
Normal file
0
backend/core/settings/__init__.py
Normal file
BIN
backend/core/settings/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
backend/core/settings/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/core/settings/__pycache__/base.cpython-314.pyc
Normal file
BIN
backend/core/settings/__pycache__/base.cpython-314.pyc
Normal file
Binary file not shown.
BIN
backend/core/settings/__pycache__/dev.cpython-314.pyc
Normal file
BIN
backend/core/settings/__pycache__/dev.cpython-314.pyc
Normal file
Binary file not shown.
158
backend/core/settings/base.py
Normal file
158
backend/core/settings/base.py
Normal file
@@ -0,0 +1,158 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from datetime import timedelta
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load .env file from the project root (not backend dir)
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
||||
PROJECT_DIR = BASE_DIR.parent
|
||||
load_dotenv(PROJECT_DIR / ".env")
|
||||
|
||||
SECRET_KEY = os.getenv("SECRET_KEY", "django-insecure-default-key-generate-yours")
|
||||
|
||||
# Installed Apps
|
||||
INSTALLED_APPS = [
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
|
||||
# Third party
|
||||
"rest_framework",
|
||||
"corsheaders",
|
||||
"rest_framework_simplejwt",
|
||||
"django_extensions",
|
||||
"drf_spectacular",
|
||||
"django_filters",
|
||||
|
||||
# Local Apps
|
||||
"tenants",
|
||||
"accounts",
|
||||
"dashboard",
|
||||
"projects",
|
||||
"analytics",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"corsheaders.middleware.CorsMiddleware",
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
# Custom Middleware
|
||||
"tenants.middleware.TenantMiddleware",
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "core.urls"
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"DIRS": [],
|
||||
"APP_DIRS": True,
|
||||
"OPTIONS": {
|
||||
"context_processors": [
|
||||
"django.template.context_processors.debug",
|
||||
"django.template.context_processors.request",
|
||||
"django.contrib.auth.context_processors.auth",
|
||||
"django.contrib.messages.context_processors.messages",
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = "core.wsgi.application"
|
||||
ASGI_APPLICATION = "core.asgi.application"
|
||||
|
||||
# Database defaults
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": BASE_DIR / "db.sqlite3",
|
||||
}
|
||||
}
|
||||
|
||||
# Use Accounts app custom User model
|
||||
AUTH_USER_MODEL = "accounts.User"
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
||||
},
|
||||
{
|
||||
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
||||
},
|
||||
]
|
||||
|
||||
LANGUAGE_CODE = "en-us"
|
||||
TIME_ZONE = "UTC"
|
||||
USE_I18N = True
|
||||
USE_TZ = True
|
||||
|
||||
STATIC_URL = "static/"
|
||||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||||
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
|
||||
# REST Framework settings
|
||||
REST_FRAMEWORK = {
|
||||
"DEFAULT_AUTHENTICATION_CLASSES": (
|
||||
"rest_framework_simplejwt.authentication.JWTAuthentication",
|
||||
),
|
||||
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
|
||||
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
|
||||
"PAGE_SIZE": 10,
|
||||
"DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
|
||||
"DEFAULT_THROTTLE_CLASSES": [
|
||||
"rest_framework.throttling.AnonRateThrottle",
|
||||
"rest_framework.throttling.UserRateThrottle"
|
||||
],
|
||||
"DEFAULT_THROTTLE_RATES": {
|
||||
"anon": "100/day",
|
||||
"user": "1000/day"
|
||||
}
|
||||
}
|
||||
|
||||
# Simple JWT Settings
|
||||
SIMPLE_JWT = {
|
||||
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
|
||||
"REFRESH_TOKEN_LIFETIME": timedelta(days=7),
|
||||
"ROTATE_REFRESH_TOKENS": True,
|
||||
"AUTH_HEADER_TYPES": ("Bearer",),
|
||||
}
|
||||
|
||||
# DRF Spectacular
|
||||
SPECTACULAR_SETTINGS = {
|
||||
"TITLE": "MTCBD API",
|
||||
"DESCRIPTION": "Multi-Tenant Cloud Based Dashboard APIs",
|
||||
"VERSION": "1.0.0",
|
||||
"SERVE_INCLUDE_SCHEMA": False,
|
||||
}
|
||||
|
||||
# Redis Caching
|
||||
CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django_redis.cache.RedisCache",
|
||||
"LOCATION": os.getenv("REDIS_URL", "redis://127.0.0.1:6379/1"),
|
||||
"OPTIONS": {
|
||||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Celery Configuration
|
||||
CELERY_BROKER_URL = os.getenv("REDIS_URL", "redis://127.0.0.1:6379/2")
|
||||
CELERY_RESULT_BACKEND = os.getenv("REDIS_URL", "redis://127.0.0.1:6379/2")
|
||||
CELERY_ACCEPT_CONTENT = ['json']
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
56
backend/core/settings/dev.py
Normal file
56
backend/core/settings/dev.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from .base import *
|
||||
import os
|
||||
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = ["*"]
|
||||
|
||||
CORS_ALLOW_ALL_ORIGINS = True
|
||||
CORS_ALLOW_HEADERS = [
|
||||
"accept",
|
||||
"accept-encoding",
|
||||
"authorization",
|
||||
"content-type",
|
||||
"dnt",
|
||||
"origin",
|
||||
"user-agent",
|
||||
"x-csrftoken",
|
||||
"x-requested-with",
|
||||
"x-tenant-id", # Allow custom tenant header
|
||||
]
|
||||
CORS_EXPOSE_HEADERS = ["content-type", "x-tenant-id"]
|
||||
|
||||
# Ensure SQLite is used for early local dev if env variables are missing for mysql
|
||||
_DB_NAME = os.getenv("DB_NAME", "")
|
||||
if _DB_NAME:
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.mysql",
|
||||
"NAME": os.getenv("DB_NAME", "mtcbd_db"),
|
||||
"USER": os.getenv("DB_USER", "root"),
|
||||
"PASSWORD": os.getenv("DB_PASSWORD", ""),
|
||||
"HOST": os.getenv("DB_HOST", "127.0.0.1"),
|
||||
"PORT": os.getenv("DB_PORT", "3306"),
|
||||
"OPTIONS": {
|
||||
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
|
||||
"charset": "utf8mb4",
|
||||
},
|
||||
}
|
||||
}
|
||||
else:
|
||||
print("WARNING: Using SQLite3 DB. For MySQL, configure DB_NAME in .env.")
|
||||
|
||||
# Basic console logging for localdev
|
||||
LOGGING = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"handlers": {
|
||||
"console": {
|
||||
"class": "logging.StreamHandler",
|
||||
},
|
||||
},
|
||||
"root": {
|
||||
"handlers": ["console"],
|
||||
"level": "INFO",
|
||||
},
|
||||
}
|
||||
79
backend/core/settings/prod.py
Normal file
79
backend/core/settings/prod.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from .base import *
|
||||
import dj_database_url
|
||||
import os
|
||||
|
||||
DEBUG = False
|
||||
|
||||
# Comma-separated list of allowed hosts
|
||||
_allowed = os.getenv("ALLOWED_HOSTS", "")
|
||||
ALLOWED_HOSTS = [h.strip() for h in _allowed.split(",") if h.strip()]
|
||||
|
||||
# Strict CORS
|
||||
_cors = os.getenv("CORS_ALLOWED_ORIGINS", "")
|
||||
CORS_ALLOWED_ORIGINS = [h.strip() for h in _cors.split(",") if h.strip()]
|
||||
CORS_ALLOW_ALL_ORIGINS = False
|
||||
|
||||
# Production Database with Connection Pooling Settings
|
||||
DATABASES = {
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.mysql",
|
||||
"NAME": os.getenv("DB_NAME", "mtcbd_db"),
|
||||
"USER": os.getenv("DB_USER", "root"),
|
||||
"PASSWORD": os.getenv("DB_PASSWORD", ""),
|
||||
"HOST": os.getenv("DB_HOST", "127.0.0.1"),
|
||||
"PORT": os.getenv("DB_PORT", "3306"),
|
||||
"OPTIONS": {
|
||||
"init_command": "SET sql_mode='STRICT_TRANS_TABLES'",
|
||||
"charset": "utf8mb4",
|
||||
},
|
||||
"CONN_MAX_AGE": int(os.getenv("DB_CONN_MAX_AGE", "60")),
|
||||
}
|
||||
}
|
||||
|
||||
# Production security settings
|
||||
SECURE_SSL_REDIRECT = os.getenv("SECURE_SSL_REDIRECT", "True") == "True"
|
||||
SESSION_COOKIE_SECURE = True
|
||||
CSRF_COOKIE_SECURE = True
|
||||
SECURE_BROWSER_XSS_FILTER = True
|
||||
SECURE_CONTENT_TYPE_NOSNIFF = True
|
||||
|
||||
# Redis caching
|
||||
CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django_redis.cache.RedisCache",
|
||||
"LOCATION": os.getenv("REDIS_URL", "redis://127.0.0.1:6379/1"),
|
||||
"OPTIONS": {
|
||||
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Production Logging
|
||||
LOGGING = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"verbose": {
|
||||
"format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}",
|
||||
"style": "{",
|
||||
},
|
||||
},
|
||||
"handlers": {
|
||||
"console": {
|
||||
"level": "INFO",
|
||||
"class": "logging.StreamHandler",
|
||||
"formatter": "verbose",
|
||||
},
|
||||
},
|
||||
"root": {
|
||||
"handlers": ["console"],
|
||||
"level": "INFO",
|
||||
},
|
||||
"loggers": {
|
||||
"django": {
|
||||
"handlers": ["console"],
|
||||
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
|
||||
"propagate": False,
|
||||
},
|
||||
},
|
||||
}
|
||||
13
backend/core/urls.py
Normal file
13
backend/core/urls.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/auth/', include('accounts.urls')),
|
||||
path('api/', include('projects.urls')),
|
||||
path('api/dashboard/', include('dashboard.urls')),
|
||||
path('api/analytics/', include('analytics.urls')),
|
||||
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||
path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
||||
]
|
||||
16
backend/core/wsgi.py
Normal file
16
backend/core/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for core project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings.dev")
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in New Issue
Block a user