init
This commit is contained in:
0
backend/videocall_app/__init__.py
Normal file
0
backend/videocall_app/__init__.py
Normal file
20
backend/videocall_app/asgi.py
Normal file
20
backend/videocall_app/asgi.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# videocall_app/asgi.py - ASGI configuration for WebSocket support
|
||||
import os
|
||||
from channels.auth import AuthMiddlewareStack
|
||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||
from channels.security.websocket import AllowedHostsOriginValidator
|
||||
from django.core.asgi import get_asgi_application
|
||||
from apps.rooms.routing import websocket_urlpatterns
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'videocall_app.settings')
|
||||
|
||||
django_asgi_app = get_asgi_application()
|
||||
|
||||
application = ProtocolTypeRouter({
|
||||
"http": django_asgi_app,
|
||||
"websocket": AllowedHostsOriginValidator(
|
||||
AuthMiddlewareStack(
|
||||
URLRouter(websocket_urlpatterns)
|
||||
)
|
||||
),
|
||||
})
|
||||
303
backend/videocall_app/settings.py
Normal file
303
backend/videocall_app/settings.py
Normal file
@@ -0,0 +1,303 @@
|
||||
# videocall_app/settings.py - Django main settings configuration
|
||||
import os
|
||||
from decouple import config
|
||||
from pathlib import Path
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
# Security settings
|
||||
SECRET_KEY = config('SECRET_KEY', default='your-secret-key-here-change-in-production')
|
||||
DEBUG = config('DEBUG', default=False, cast=bool)
|
||||
|
||||
# ALLOWED_HOSTS - только ваши настоящие домены
|
||||
allowed_hosts_default = 'yourdomain.com,www.yourdomain.com,localhost,127.0.0.1'
|
||||
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=allowed_hosts_default).split(',')
|
||||
|
||||
# Убираем пустые строки и пробелы
|
||||
ALLOWED_HOSTS = [host.strip() for host in ALLOWED_HOSTS if host.strip()]
|
||||
|
||||
# В DEBUG режиме разрешаем localhost для разработки
|
||||
if DEBUG:
|
||||
ALLOWED_HOSTS.extend(['localhost', '127.0.0.1', '0.0.0.0'])
|
||||
|
||||
# Application definition
|
||||
DJANGO_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
]
|
||||
|
||||
THIRD_PARTY_APPS = [
|
||||
'rest_framework',
|
||||
'corsheaders',
|
||||
'channels',
|
||||
'django_ratelimit',
|
||||
]
|
||||
|
||||
LOCAL_APPS = [
|
||||
'apps.core',
|
||||
'apps.rooms',
|
||||
'apps.authentication',
|
||||
]
|
||||
|
||||
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
|
||||
|
||||
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',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'videocall_app.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [os.path.join(BASE_DIR, 'templates')],
|
||||
'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 = 'videocall_app.wsgi.application'
|
||||
ASGI_APPLICATION = 'videocall_app.asgi.application'
|
||||
|
||||
# Database configuration
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql',
|
||||
'NAME': config('DB_NAME', default='videocall_db'),
|
||||
'USER': config('DB_USER', default='postgres'),
|
||||
'PASSWORD': config('DB_PASSWORD', default=''),
|
||||
'HOST': config('DB_HOST', default='localhost'),
|
||||
'PORT': config('DB_PORT', default='5432'),
|
||||
}
|
||||
}
|
||||
|
||||
# Redis configuration
|
||||
REDIS_URL = config('REDIS_URL', default='redis://localhost:6379/0')
|
||||
|
||||
# Channels configuration for WebSockets
|
||||
CHANNEL_LAYERS = {
|
||||
'default': {
|
||||
'BACKEND': 'channels_redis.core.RedisChannelLayer',
|
||||
'CONFIG': {
|
||||
'hosts': [REDIS_URL],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Cache configuration
|
||||
CACHES = {
|
||||
'default': {
|
||||
'BACKEND': 'django_redis.cache.RedisCache',
|
||||
'LOCATION': REDIS_URL,
|
||||
'OPTIONS': {
|
||||
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Session configuration
|
||||
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
|
||||
SESSION_CACHE_ALIAS = 'default'
|
||||
SESSION_COOKIE_AGE = 86400 # 24 hours
|
||||
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
|
||||
SESSION_SAVE_EVERY_REQUEST = True
|
||||
SESSION_COOKIE_HTTPONLY = True
|
||||
SESSION_COOKIE_SECURE = not DEBUG # Use secure cookies in production
|
||||
SESSION_COOKIE_SAMESITE = 'Lax'
|
||||
|
||||
# CSRF Configuration
|
||||
CSRF_COOKIE_HTTPONLY = False # Allow JavaScript to read CSRF token
|
||||
CSRF_COOKIE_SECURE = not DEBUG # Use secure cookies in production
|
||||
CSRF_COOKIE_SAMESITE = 'Lax'
|
||||
CSRF_TRUSTED_ORIGINS = [
|
||||
'http://localhost:3000',
|
||||
'http://127.0.0.1:3000',
|
||||
]
|
||||
|
||||
if not DEBUG:
|
||||
# Add your production domains
|
||||
CSRF_TRUSTED_ORIGINS.extend([
|
||||
'https://yourdomain.com',
|
||||
'https://www.yourdomain.com',
|
||||
])
|
||||
|
||||
# REST Framework configuration
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||
'rest_framework.authentication.SessionAuthentication',
|
||||
],
|
||||
'DEFAULT_PERMISSION_CLASSES': [
|
||||
'rest_framework.permissions.AllowAny', # Changed to allow custom auth
|
||||
],
|
||||
'DEFAULT_THROTTLE_CLASSES': [
|
||||
'rest_framework.throttling.AnonRateThrottle',
|
||||
'rest_framework.throttling.UserRateThrottle'
|
||||
],
|
||||
'DEFAULT_THROTTLE_RATES': {
|
||||
'anon': '100/hour',
|
||||
'user': '1000/hour'
|
||||
}
|
||||
}
|
||||
|
||||
# CORS settings
|
||||
CORS_ALLOWED_ORIGINS = config(
|
||||
'CORS_ALLOWED_ORIGINS',
|
||||
default='http://localhost:3000,http://127.0.0.1:3000'
|
||||
).split(',')
|
||||
|
||||
CORS_ALLOW_CREDENTIALS = True
|
||||
CORS_ALLOW_ALL_ORIGINS = DEBUG # Only in development
|
||||
|
||||
# WebSocket origins for Channels
|
||||
ALLOWED_HOSTS_INCLUDE_WEBSOCKET = True
|
||||
|
||||
# Additional CORS headers for development
|
||||
if DEBUG:
|
||||
CORS_ALLOW_HEADERS = [
|
||||
'accept',
|
||||
'accept-encoding',
|
||||
'authorization',
|
||||
'content-type',
|
||||
'dnt',
|
||||
'origin',
|
||||
'user-agent',
|
||||
'x-csrftoken',
|
||||
'x-requested-with',
|
||||
]
|
||||
|
||||
# Django Ratelimit settings
|
||||
RATELIMIT_USE_CACHE = 'default'
|
||||
RATELIMIT_ENABLE = True
|
||||
|
||||
# В файле backend/videocall_app/settings.py замените секцию LOGGING на:
|
||||
|
||||
# Logging configuration
|
||||
import os
|
||||
|
||||
# Создаем директорию для логов если её нет
|
||||
LOGS_DIR = BASE_DIR / 'logs'
|
||||
LOGS_DIR.mkdir(exist_ok=True)
|
||||
|
||||
LOGGING = {
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'formatters': {
|
||||
'verbose': {
|
||||
'format': '[{levelname}] {asctime} {name}: {message}',
|
||||
'style': '{',
|
||||
},
|
||||
'simple': {
|
||||
'format': '{levelname} {message}',
|
||||
'style': '{',
|
||||
},
|
||||
},
|
||||
'handlers': {
|
||||
'console': {
|
||||
'class': 'logging.StreamHandler',
|
||||
'formatter': 'verbose',
|
||||
},
|
||||
'file': {
|
||||
'class': 'logging.FileHandler',
|
||||
'filename': LOGS_DIR / 'django.log',
|
||||
'formatter': 'verbose',
|
||||
} if os.access(LOGS_DIR, os.W_OK) else {
|
||||
# Fallback to console if can't write to file
|
||||
'class': 'logging.StreamHandler',
|
||||
'formatter': 'verbose',
|
||||
},
|
||||
},
|
||||
'root': {
|
||||
'handlers': ['console'],
|
||||
'level': 'INFO',
|
||||
},
|
||||
'loggers': {
|
||||
'django': {
|
||||
'handlers': ['console'],
|
||||
'level': 'INFO',
|
||||
'propagate': False,
|
||||
},
|
||||
'apps.authentication': {
|
||||
'handlers': ['console'],
|
||||
'level': 'INFO',
|
||||
'propagate': False,
|
||||
},
|
||||
'apps.rooms': {
|
||||
'handlers': ['console'],
|
||||
'level': 'INFO',
|
||||
'propagate': False,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# Internationalization
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
TIME_ZONE = 'UTC'
|
||||
USE_I18N = True
|
||||
USE_TZ = True
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
STATIC_URL = '/static/'
|
||||
STATIC_ROOT = BASE_DIR / 'staticfiles'
|
||||
|
||||
# Additional locations of static files
|
||||
STATICFILES_DIRS = []
|
||||
if (BASE_DIR / 'static').exists():
|
||||
STATICFILES_DIRS.append(BASE_DIR / 'static')
|
||||
|
||||
# Static files finders
|
||||
STATICFILES_FINDERS = [
|
||||
'django.contrib.staticfiles.finders.FileSystemFinder',
|
||||
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
|
||||
]
|
||||
|
||||
# Media files
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = BASE_DIR / 'media'
|
||||
|
||||
# Application-specific settings
|
||||
ROOM_EXPIRY_HOURS = 24
|
||||
MAX_PARTICIPANTS_PER_ROOM = 2
|
||||
SHORT_CODE_LENGTH = 6
|
||||
|
||||
# Default primary key field type
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
|
||||
# Security settings for production
|
||||
if not DEBUG:
|
||||
# Trust proxy headers from nginx
|
||||
USE_X_FORWARDED_HOST = True
|
||||
USE_X_FORWARDED_PORT = True
|
||||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||
|
||||
# ВАЖНО: НЕ ВКЛЮЧАЕМ принудительное перенаправление на HTTPS
|
||||
# так как это делает nginx
|
||||
SECURE_SSL_REDIRECT = False
|
||||
|
||||
# Остальные настройки безопасности
|
||||
SECURE_BROWSER_XSS_FILTER = True
|
||||
SECURE_CONTENT_TYPE_NOSNIFF = True
|
||||
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
|
||||
SECURE_HSTS_SECONDS = 31536000
|
||||
SESSION_COOKIE_SECURE = True
|
||||
CSRF_COOKIE_SECURE = True
|
||||
|
||||
# Дополнительные заголовки безопасности
|
||||
SECURE_REFERRER_POLICY = 'same-origin'
|
||||
17
backend/videocall_app/urls.py
Normal file
17
backend/videocall_app/urls.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# videocall_app/urls.py - Main URL configuration
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/auth/', include('apps.authentication.urls')),
|
||||
path('api/rooms/', include('apps.rooms.urls')),
|
||||
path('api/', include('apps.core.urls')),
|
||||
]
|
||||
|
||||
# Serve media files in development
|
||||
if settings.DEBUG:
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
||||
16
backend/videocall_app/wsgi.py
Normal file
16
backend/videocall_app/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for videocall_app 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/5.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'videocall_app.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in New Issue
Block a user