import configparser import os import logging logger = logging.getLogger(__name__) _config = None _config_path = None def load_config(path=None): global _config, _config_path if path is None: path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'config.ini') _config_path = path _config = configparser.ConfigParser() read_files = _config.read(path, encoding='utf-8') if not read_files: logger.warning("配置文件不存在,使用默认值: %s", path) else: logger.info("配置文件加载完成: %s", path) return _config def get_config(): global _config if _config is None: load_config() return _config def get_db_type(): cfg = get_config() return cfg.get('database', 'type', fallback='mysql') def get_mysql_config(): cfg = get_config() return { 'host': cfg.get('mysql', 'host', fallback='127.0.0.1'), 'port': cfg.getint('mysql', 'port', fallback=33366), 'user': cfg.get('mysql', 'user', fallback='admin'), 'password': cfg.get('mysql', 'password', fallback='admin'), 'database': cfg.get('mysql', 'database', fallback='word_sign'), } def get_sqlite_config(): cfg = get_config() return { 'path': cfg.get('sqlite', 'path', fallback='./data/word_sign.db'), } def get_web_config(): cfg = get_config() return { 'host': cfg.get('web', 'host', fallback='0.0.0.0'), 'port': cfg.getint('web', 'port', fallback=5001), 'debug': cfg.getboolean('web', 'debug', fallback=False), } def get_log_config(): cfg = get_config() return { 'level': cfg.get('log', 'level', fallback='INFO'), 'file_path': cfg.get('log', 'file_path', fallback='./logs/'), } def get_limits_config(): cfg = get_config() return { 'docx_max_bytes': cfg.getint('limits', 'docx_max_bytes', fallback=1024 * 1024), 'image_max_bytes': cfg.getint('limits', 'image_max_bytes', fallback=256 * 1024), 'stamp_max_count': cfg.getint('limits', 'stamp_max_count', fallback=100), 'log_max_entries': cfg.getint('limits', 'log_max_entries', fallback=1000), } def get_oa_config(): cfg = get_config() return { 'action_timeout_seconds': cfg.getint('oa', 'action_timeout_seconds', fallback=30), }