You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
4.6 KiB
181 lines
4.6 KiB
import os
|
|
import sys
|
|
import argparse
|
|
from pydantic import BaseSettings
|
|
from functools import lru_cache
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
class AppSettings(BaseSettings):
|
|
"""
|
|
应用配置
|
|
"""
|
|
app_env: str = 'dev'
|
|
app_name: str = 'Dash-FasAPI-Admin'
|
|
app_root_path: str = '/dev-api'
|
|
app_host: str = '0.0.0.0'
|
|
app_port: int = 9099
|
|
app_version: str = '1.4.0'
|
|
app_reload: bool = True
|
|
app_ip_location_query: bool = True
|
|
app_same_time_login: bool = True
|
|
ds_server_url: str = 'localhost:12345'
|
|
ds_task_id: str = '15081964614112'
|
|
|
|
class JwtSettings(BaseSettings):
|
|
"""
|
|
Jwt配置
|
|
"""
|
|
jwt_secret_key: str = 'b01c66dc2c58dc6a0aabfe2144256be36226de378bf87f72c0c795dda67f4d55'
|
|
jwt_algorithm: str = 'HS256'
|
|
jwt_expire_minutes: int = 1440
|
|
jwt_redis_expire_minutes: int = 30
|
|
|
|
|
|
class DataBaseSettings(BaseSettings):
|
|
"""
|
|
数据库配置
|
|
"""
|
|
db_host: str = '47.113.147.166'
|
|
db_port: int = 3306
|
|
db_username: str = 'dbf'
|
|
db_password: str = '1q2w3e4r'
|
|
db_database: str = 'dash_test_w'
|
|
db_echo: bool = True
|
|
db_max_overflow: int = 10
|
|
db_pool_size: int = 50
|
|
db_pool_recycle: int = 3600
|
|
db_pool_timeout: int = 30
|
|
|
|
|
|
class RedisSettings(BaseSettings):
|
|
"""
|
|
Redis配置
|
|
"""
|
|
redis_host: str = '127.0.0.1'
|
|
redis_port: int = 6379
|
|
redis_username: str = ''
|
|
redis_password: str = ''
|
|
redis_database: int = 2
|
|
|
|
|
|
|
|
class MinioSettings(BaseSettings):
|
|
"""
|
|
Minio配置
|
|
"""
|
|
minio_address: str = '192.168.0.3:9000'
|
|
minio_admin: str = 'admin'
|
|
minio_password: str = 'admin123'
|
|
|
|
|
|
|
|
class CachePathConfig:
|
|
"""
|
|
缓存目录配置
|
|
"""
|
|
PATH = os.path.join(os.path.abspath(os.getcwd()), 'caches')
|
|
PATHSTR = 'caches'
|
|
|
|
|
|
class RedisInitKeyConfig:
|
|
"""
|
|
系统内置Redis键名
|
|
"""
|
|
ACCESS_TOKEN = {'key': 'access_token', 'remark': '登录令牌信息'}
|
|
SYS_DICT = {'key': 'sys_dict', 'remark': '数据字典'}
|
|
SYS_CONFIG = {'key': 'sys_config', 'remark': '配置信息'}
|
|
CAPTCHA_CODES = {'key': 'captcha_codes', 'remark': '图片验证码'}
|
|
ACCOUNT_LOCK = {'key': 'account_lock', 'remark': '用户锁定'}
|
|
PASSWORD_ERROR_COUNT = {'key': 'password_error_count', 'remark': '密码错误次数'}
|
|
SMS_CODE = {'key': 'sms_code', 'remark': '短信验证码'}
|
|
|
|
|
|
class GetConfig:
|
|
"""
|
|
获取配置
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.parse_cli_args()
|
|
|
|
@lru_cache()
|
|
def get_app_config(self):
|
|
"""
|
|
获取应用配置
|
|
"""
|
|
# 实例化应用配置模型
|
|
return AppSettings()
|
|
|
|
@lru_cache()
|
|
def get_jwt_config(self):
|
|
"""
|
|
获取Jwt配置
|
|
"""
|
|
# 实例化Jwt配置模型
|
|
return JwtSettings()
|
|
|
|
@lru_cache()
|
|
def get_database_config(self):
|
|
"""
|
|
获取数据库配置
|
|
"""
|
|
# 实例化数据库配置模型
|
|
return DataBaseSettings()
|
|
|
|
@lru_cache()
|
|
def get_redis_config(self):
|
|
"""
|
|
获取Redis配置
|
|
"""
|
|
# 实例化Redis配置模型
|
|
return RedisSettings()
|
|
|
|
|
|
@lru_cache()
|
|
def get_minio_config(self):
|
|
"""
|
|
获取Minio配置
|
|
"""
|
|
# 实例化Redis配置模型
|
|
return MinioSettings()
|
|
|
|
@staticmethod
|
|
def parse_cli_args():
|
|
"""
|
|
解析命令行参数
|
|
"""
|
|
if 'uvicorn' in sys.argv[0]:
|
|
# 使用uvicorn启动时,命令行参数需要按照uvicorn的文档进行配置,无法自定义参数
|
|
pass
|
|
else:
|
|
# 使用argparse定义命令行参数
|
|
parser = argparse.ArgumentParser(description='命令行参数')
|
|
parser.add_argument('--env', type=str, default='', help='运行环境')
|
|
# 解析命令行参数
|
|
args = parser.parse_args()
|
|
# 设置环境变量,如果未设置命令行参数,默认APP_ENV为dev
|
|
os.environ['APP_ENV'] = args.env if args.env else 'dev'
|
|
# 读取运行环境
|
|
run_env = os.environ.get('APP_ENV', '')
|
|
# 运行环境未指定时默认加载.env.dev
|
|
env_file = '.env.dev'
|
|
# 运行环境不为空时按命令行参数加载对应.env文件
|
|
if run_env != '':
|
|
env_file = f'.env.{run_env}'
|
|
# 加载配置
|
|
load_dotenv(env_file)
|
|
|
|
|
|
# 实例化获取配置类
|
|
get_config = GetConfig()
|
|
# 应用配置
|
|
AppConfig = get_config.get_app_config()
|
|
# Jwt配置
|
|
JwtConfig = get_config.get_jwt_config()
|
|
# 数据库配置
|
|
DataBaseConfig = get_config.get_database_config()
|
|
# Redis配置
|
|
RedisConfig = get_config.get_redis_config()
|
|
#Minio配置
|
|
MinioConfig = get_config.get_minio_config()
|
|
|