cn_formal_settings.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import os
  2. from AnsjerPush.config import BASE_DIR
  3. from dotenv import load_dotenv
  4. load_dotenv(os.path.join(BASE_DIR, '.env'))
  5. def get_list(name, default=None, sep=","):
  6. value = os.getenv(name)
  7. if value is None:
  8. return default or []
  9. return [x.strip() for x in value.split(sep) if x.strip()]
  10. ACCESS_KEY_ID = os.getenv('ACCESS_KEY_ID', default='')
  11. SECRET_ACCESS_KEY = os.getenv('SECRET_ACCESS_KEY', default='')
  12. AWS_ACCESS_KEY_ID = get_list('AWS_ACCESS_KEY_ID', default=[])
  13. AWS_SECRET_ACCESS_KEY = get_list('AWS_SECRET_ACCESS_KEY', default=[])
  14. ALICLOUD_AK = os.getenv('ALICLOUD_AK', default='')
  15. ALICLOUD_SK = os.getenv('ALICLOUD_SK', default='')
  16. OSS_ROLE_ARN = os.getenv('OSS_ROLE_ARN', default='')
  17. HUAWEICLOUD_AK = os.getenv('HUAWEICLOUD_AK', default='')
  18. HUAWEICLOUD_SK = os.getenv('HUAWEICLOUD_SK', default='')
  19. SECRET_KEY = '$2hf5g$a%_^kk0-l25l$!o5)yc=dvtnfpc8(+$rh4fq4twa_xx'
  20. DEBUG = False
  21. ALLOWED_HOSTS = ["*"]
  22. INSTALLED_APPS = [
  23. 'django.contrib.admin',
  24. 'django.contrib.auth',
  25. 'django.contrib.contenttypes',
  26. 'django.contrib.sessions',
  27. 'django.contrib.messages',
  28. 'django.contrib.staticfiles',
  29. 'corsheaders',
  30. 'imagekit',
  31. 'Model'
  32. ]
  33. MIDDLEWARE = [
  34. 'django.middleware.security.SecurityMiddleware',
  35. 'django.contrib.sessions.middleware.SessionMiddleware',
  36. 'corsheaders.middleware.CorsMiddleware',
  37. 'django.middleware.common.CommonMiddleware',
  38. # 'MiddleWare.requestRecord.RequestRecordMiddleware', # 记录请求信息
  39. # 'django.middleware.csrf.CsrfViewMiddleware',
  40. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  41. 'django.contrib.messages.middleware.MessageMiddleware',
  42. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  43. ]
  44. ROOT_URLCONF = 'AnsjerPush.urls'
  45. TEMPLATES = [
  46. {
  47. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  48. 'DIRS': [os.path.join(BASE_DIR, 'templates')]
  49. ,
  50. 'APP_DIRS': True,
  51. 'OPTIONS': {
  52. 'context_processors': [
  53. 'django.template.context_processors.debug',
  54. 'django.template.context_processors.request',
  55. 'django.contrib.auth.context_processors.auth',
  56. 'django.contrib.messages.context_processors.messages',
  57. ],
  58. },
  59. },
  60. ]
  61. WSGI_APPLICATION = 'AnsjerPush.wsgi.application'
  62. # 业务数据库
  63. DATABASE_DATA = os.getenv('DATABASE_DATA')
  64. SERVER_HOST = os.getenv('SERVER_HOST')
  65. DATABASES_USER = os.getenv('DATABASES_USER')
  66. DATABASES_PASS = os.getenv('DATABASES_PASS')
  67. # 推送数据库
  68. DATABASE_DATA2 = os.getenv('DATABASE_DATA2')
  69. SERVER_HOST2 = os.getenv('SERVER_HOST2')
  70. DATABASES_USER2 = os.getenv('DATABASES_USER2')
  71. DATABASES_PASS2 = os.getenv('DATABASES_PASS2')
  72. DATABASES = {
  73. 'default': {
  74. 'ENGINE': 'django.db.backends.mysql',
  75. 'NAME': DATABASE_DATA,
  76. 'USER': DATABASES_USER,
  77. 'PASSWORD': DATABASES_PASS,
  78. 'HOST': SERVER_HOST,
  79. 'PORT': '3306',
  80. 'AUTOCOMMIT': True,
  81. 'OPTIONS': {'charset': 'utf8mb4',
  82. 'use_unicode': True,
  83. 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
  84. }
  85. },
  86. 'mysql02': {
  87. 'ENGINE': 'django.db.backends.mysql',
  88. 'NAME': DATABASE_DATA2,
  89. 'USER': DATABASES_USER2,
  90. 'PASSWORD': DATABASES_PASS2,
  91. 'HOST': SERVER_HOST2,
  92. 'PORT': '3306',
  93. 'AUTOCOMMIT': True,
  94. 'OPTIONS': {'charset': 'utf8mb4',
  95. 'use_unicode': True,
  96. 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
  97. }
  98. }
  99. }
  100. DATABASE_ROUTERS = ['AnsjerPush.database_router.DatabaseAppsRouter']
  101. DATABASE_APPS_MAPPING = {
  102. 'db1': 'default',
  103. 'db2': 'mysql02',
  104. }
  105. # Password validation
  106. # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
  107. AUTH_PASSWORD_VALIDATORS = [
  108. {
  109. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  110. },
  111. {
  112. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  113. },
  114. {
  115. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  116. },
  117. {
  118. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  119. },
  120. ]
  121. # Internationalization
  122. # https://docs.djangoproject.com/en/2.1/topics/i18n/
  123. LANGUAGE_CODE = 'en-us'
  124. TIME_ZONE = 'UTC'
  125. USE_I18N = True
  126. USE_L10N = True
  127. USE_TZ = True
  128. # Static files (CSS, JavaScript, Images)
  129. # https://docs.djangoproject.com/en/2.1/howto/static-files/
  130. STATIC_URL = '/static/'
  131. # 跨域增加忽略
  132. # 跨域增加忽略
  133. CORS_ALLOW_CREDENTIALS = True
  134. CORS_ORIGIN_ALLOW_ALL = True
  135. CORS_ORIGIN_WHITELIST = ()
  136. CORS_ALLOW_METHODS = (
  137. 'DELETE',
  138. 'GET',
  139. 'OPTIONS',
  140. 'PATCH',
  141. 'POST',
  142. 'PUT',
  143. 'VIEW',
  144. )
  145. CORS_ALLOW_HEADERS = (
  146. 'accept',
  147. 'accept-encoding',
  148. 'authorization',
  149. 'content-type',
  150. 'dnt',
  151. 'origin',
  152. 'user-agent',
  153. 'x-csrftoken',
  154. 'x-requested-with',
  155. )
  156. # 日志模块
  157. LOGGING = {
  158. 'version': 1,
  159. 'disable_existing_loggers': True,
  160. 'formatters': {
  161. 'error_format': {
  162. 'format': '%(asctime)s %(threadName)s %(thread)d %(lineno)d %(levelname)s %(message)s'
  163. },
  164. 'standard': {
  165. 'format': '[%(asctime)s] [%(filename)s:%(lineno)d] [%(module)s:%(funcName)s] '
  166. '[%(levelname)s]- %(message)s'},
  167. },
  168. 'filters': {
  169. },
  170. 'handlers': {
  171. 'mail_admins': {
  172. 'level': 'ERROR',
  173. 'class': 'django.utils.log.AdminEmailHandler',
  174. 'include_html': True,
  175. },
  176. 'default': {
  177. 'level': 'ERROR',
  178. 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
  179. 'filename': BASE_DIR + '/static/log/error/error.log',
  180. 'maxBytes': 1024 * 1024 * 5, # 5 MB
  181. 'backupCount': 3,
  182. 'formatter': 'error_format',
  183. },
  184. 'console': {
  185. 'level': 'ERROR',
  186. 'class': 'logging.StreamHandler',
  187. 'formatter': 'error_format'
  188. },
  189. # 总计info、time、v1_push日志给57GB空间
  190. 'info': {
  191. 'level': 'INFO',
  192. 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
  193. 'filename': BASE_DIR + '/static/log/info/info.log',
  194. 'backupCount': 3,
  195. 'maxBytes': 1024 * 1024 * 2 * 1024, # 一天以内2GB 保存5天共10GB
  196. 'formatter': 'standard',
  197. 'encoding': 'utf-8',
  198. },
  199. 'time': {
  200. 'level': 'INFO',
  201. 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
  202. 'filename': BASE_DIR + '/static/log/time/info.log',
  203. 'backupCount': 7,
  204. 'maxBytes': 1024 * 1024 * 5 * 1024, # 一天9GB 保存5天 共45GB
  205. 'formatter': 'standard',
  206. 'encoding': 'utf-8',
  207. },
  208. 'v1_push': {
  209. 'level': 'INFO',
  210. 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
  211. 'filename': BASE_DIR + '/static/log/v1log/info.log',
  212. 'backupCount': 1,
  213. 'maxBytes': 1024 * 1024 * 1 * 1024, # 一天0.05GB 保存40天 共2GB
  214. 'formatter': 'standard',
  215. 'encoding': 'utf-8',
  216. },
  217. 'customized_push': {
  218. 'level': 'INFO',
  219. 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
  220. 'filename': BASE_DIR + '/static/log/customized_push/info.log',
  221. 'backupCount': 3,
  222. 'maxBytes': 1024 * 1024 * 2 * 100, # 100M
  223. 'formatter': 'standard',
  224. 'encoding': 'utf-8',
  225. },
  226. 'error_info': {
  227. 'level': 'INFO',
  228. 'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
  229. 'filename': BASE_DIR + '/static/log/error_info/info.log',
  230. 'backupCount': 3,
  231. 'maxBytes': 1024 * 1024 * 2 * 100, # 100M
  232. 'formatter': 'standard',
  233. 'encoding': 'utf-8',
  234. },
  235. },
  236. 'loggers': {
  237. 'django': {
  238. 'handlers': ['default', 'console'],
  239. 'level': 'ERROR',
  240. 'propagate': False
  241. },
  242. # log 调用时需要当作参数传入
  243. 'info': {
  244. 'handlers': ['info'],
  245. 'level': 'INFO',
  246. 'propagate': False
  247. },
  248. 'time': {
  249. 'handlers': ['time'],
  250. 'level': 'INFO',
  251. 'propagate': False
  252. },
  253. 'v1_push': {
  254. 'handlers': ['v1_push'],
  255. 'level': 'INFO',
  256. 'propagate': False
  257. },
  258. 'customized_push': {
  259. 'handlers': ['customized_push'],
  260. 'level': 'INFO',
  261. 'propagate': False
  262. },
  263. 'error_info': {
  264. 'handlers': ['error_info'],
  265. 'level': 'INFO',
  266. 'propagate': False
  267. },
  268. }
  269. }