This commit is contained in:
2025-12-09 14:46:02 +08:00
parent c7a22a288a
commit abe314fdc8
76 changed files with 7601 additions and 1667 deletions

View File

@@ -8,27 +8,33 @@ https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
from django.conf import settings
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hertz_server_django.settings')
# Import Django first to ensure proper initialization
from django.core.asgi import get_asgi_application
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
# Import other modules AFTER Django setup
from django.conf import settings
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
# Import websocket routing AFTER Django setup to avoid AppRegistryNotReady
from hertz_demo import routing as demo_routing
from hertz_studio_django_yolo import routing as yolo_routing
# Merge websocket routes
websocket_urlpatterns = (
demo_routing.websocket_urlpatterns +
yolo_routing.websocket_urlpatterns
)
if 'hertz_studio_django_yolo' in settings.INSTALLED_APPS:
from hertz_studio_django_yolo import routing as yolo_routing
websocket_urlpatterns = (
demo_routing.websocket_urlpatterns +
yolo_routing.websocket_urlpatterns
)
else:
websocket_urlpatterns = demo_routing.websocket_urlpatterns
# 在开发环境下放宽Origin校验便于第三方客户端如 Apifox、wscat调试
websocket_app = AuthMiddlewareStack(
@@ -47,3 +53,4 @@ else:
"http": django_asgi_app,
"websocket": AllowedHostsOriginValidator(websocket_app),
})

View File

@@ -62,8 +62,12 @@ DEBUG = config('DEBUG', default=True, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='localhost,127.0.0.1', cast=lambda v: [s.strip() for s in v.split(',')])
# Database switch configuration
# Database engine configuration (sqlite/mysql) with backward compatibility
# Prefer `DB_ENGINE` env var; fallback to legacy `USE_REDIS_AS_DB`
DB_ENGINE = config('DB_ENGINE', default=None)
USE_REDIS_AS_DB = config('USE_REDIS_AS_DB', default=True, cast=bool)
if DB_ENGINE is None:
DB_ENGINE = 'sqlite' if USE_REDIS_AS_DB else 'mysql'
# Application definition
@@ -74,21 +78,27 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Third party apps
'rest_framework',
'corsheaders',
'channels',
'drf_spectacular',
# 必备注册的app不要删
'hertz_demo', # 初始化演示模块
'hertz_demo', # 初始化演示模块
'hertz_studio_django_captcha', # 验证码模块
'hertz_studio_django_auth', # 权限模块
'hertz_studio_django_system_monitor', # 系统监测模块
'hertz_studio_django_log', # 日志管理模块
'hertz_studio_django_notice', # 通知模块
# ======在下面导入你需要的app======
'hertz_studio_django_ai', #ai聊天模块
'hertz_studio_django_kb', # 知识库 ai和kb库是相互绑定的
'hertz_studio_django_wiki', # 文章模块
'hertz_studio_django_yolo', # YOLO目标检测模块
'hertz_studio_django_yolo_train', # Yolo训练模块
]
@@ -128,21 +138,17 @@ WSGI_APPLICATION = 'hertz_server_django.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
if USE_REDIS_AS_DB:
# Redis as primary database (for caching and session storage)
if DB_ENGINE == 'sqlite':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'data/db.sqlite3',
}
}
# Use Redis for sessions
# Use Redis-backed sessions when on SQLite (optional, keeps prior behavior)
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = 'default'
else:
# MySQL database configuration
elif DB_ENGINE == 'mysql':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
@@ -156,6 +162,14 @@ else:
},
}
}
else:
# Fallback to SQLite for unexpected values
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'data/db.sqlite3',
}
}
# Redis
CACHES = {

View File

@@ -44,9 +44,26 @@ urlpatterns = [
# Hertz Log routes
path('api/log/', include('hertz_studio_django_log.urls')),
# Hertz Notice routes
path('api/notice/', include('hertz_studio_django_notice.urls')),
# ===========在下面添加你需要的路由===========
# Hertz AI routes
path('api/ai/', include('hertz_studio_django_ai.urls')),
# Hertz Knowledge Base routes
path('api/kb/', include('hertz_studio_django_kb.urls')),
# Hertz Wiki routes
path('api/wiki/', include('hertz_studio_django_wiki.urls')),
# Hertz YOLO routes
path('api/yolo/', include('hertz_studio_django_yolo.urls')),
# YOLO 训练管理
path('api/yolo/train/', include('hertz_studio_django_yolo_train.urls')),
]