85 lines
2.1 KiB
Python
85 lines
2.1 KiB
Python
import json
|
|
import os
|
|
|
|
DEFAULT_CONFIG = {
|
|
'server': {
|
|
'host': '0.0.0.0',
|
|
'port': 8080,
|
|
'workers': 0,
|
|
'keepalive_timeout': 30,
|
|
'max_body_size': 524288000,
|
|
},
|
|
'database': {
|
|
'path': 'db/liteadmin.db',
|
|
'busy_timeout': 5000,
|
|
},
|
|
'session': {
|
|
'store': 'db',
|
|
'max_age': 86400,
|
|
},
|
|
'cache': {
|
|
'home_ttl': 30,
|
|
'list_ttl': 10,
|
|
'detail_ttl': 5,
|
|
},
|
|
'admin': {
|
|
'username': 'admin',
|
|
'password': 'admin123',
|
|
},
|
|
'site': {
|
|
'name': '社区平台',
|
|
'footer': '社区平台 © 2026 Powered by LiteAdmin',
|
|
},
|
|
}
|
|
|
|
_config = None
|
|
|
|
|
|
def _migrate_old_config(cfg):
|
|
"""将旧格式配置迁移到新格式"""
|
|
if 'server' not in cfg:
|
|
# 旧格式:顶级字段直接是 database/admin/site
|
|
cfg = {
|
|
'server': {
|
|
'host': '0.0.0.0',
|
|
'port': 8080,
|
|
'workers': 0,
|
|
'keepalive_timeout': 30,
|
|
'max_body_size': 524288000,
|
|
},
|
|
'database': cfg.get('database', {'path': 'db/liteadmin.db'}),
|
|
'session': {
|
|
'store': 'db',
|
|
'max_age': 86400,
|
|
},
|
|
'cache': {
|
|
'home_ttl': 30,
|
|
'list_ttl': 10,
|
|
'detail_ttl': 5,
|
|
},
|
|
'admin': cfg.get('admin', {'username': 'admin', 'password': 'admin123'}),
|
|
'site': cfg.get('site', {'name': '社区平台', 'footer': '社区平台 © 2026 Powered by LiteAdmin'}),
|
|
}
|
|
return cfg
|
|
|
|
|
|
def load_config(config_path='config.json'):
|
|
global _config
|
|
if os.path.exists(config_path):
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
cfg = json.load(f)
|
|
cfg = _migrate_old_config(cfg)
|
|
_config = cfg
|
|
else:
|
|
_config = dict(DEFAULT_CONFIG)
|
|
with open(config_path, 'w', encoding='utf-8') as f:
|
|
json.dump(DEFAULT_CONFIG, f, ensure_ascii=False, indent=2)
|
|
return _config
|
|
|
|
|
|
def get_config():
|
|
global _config
|
|
if _config is None:
|
|
return load_config()
|
|
return _config
|