mirror of
https://github.com/xfgryujk/blivechat.git
synced 2024-12-25 12:21:00 +08:00
命令行参数合并到配置里
This commit is contained in:
parent
f622144809
commit
6ad2e145b2
33
api/chat.py
33
api/chat.py
@ -223,7 +223,8 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
|
||||
# 跨域测试用
|
||||
def check_origin(self, origin):
|
||||
if self.application.settings['debug']:
|
||||
cfg = config.get_config()
|
||||
if cfg.debug:
|
||||
return True
|
||||
return super().check_origin(origin)
|
||||
|
||||
@ -241,24 +242,24 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
self.close()
|
||||
|
||||
async def _on_joined_room(self):
|
||||
if self.settings['debug']:
|
||||
cfg = config.get_config()
|
||||
if cfg.debug:
|
||||
await self._send_test_message()
|
||||
|
||||
# 不允许自动翻译的提示
|
||||
if self.auto_translate:
|
||||
cfg = config.get_config()
|
||||
if (
|
||||
cfg.allow_translate_rooms
|
||||
# 身份码就不管了吧,反正配置正确的情况下不会看到这个提示
|
||||
and self.room_key.type == services.chat.RoomKeyType.ROOM_ID
|
||||
and self.room_key.value not in cfg.allow_translate_rooms
|
||||
):
|
||||
self.send_cmd_data(Command.ADD_TEXT, make_text_message_data(
|
||||
author_name='blivechat',
|
||||
author_type=2,
|
||||
content='Translation is not allowed in this room. Please download to use translation',
|
||||
author_level=60,
|
||||
))
|
||||
if (
|
||||
self.auto_translate
|
||||
and cfg.allow_translate_rooms
|
||||
# 身份码就不管了吧,反正配置正确的情况下不会看到这个提示
|
||||
and self.room_key.type == services.chat.RoomKeyType.ROOM_ID
|
||||
and self.room_key.value not in cfg.allow_translate_rooms
|
||||
):
|
||||
self.send_cmd_data(Command.ADD_TEXT, make_text_message_data(
|
||||
author_name='blivechat',
|
||||
author_type=2,
|
||||
content='Translation is not allowed in this room. Please download to use translation',
|
||||
author_level=60,
|
||||
))
|
||||
|
||||
# 测试用
|
||||
async def _send_test_message(self):
|
||||
|
22
config.py
22
config.py
@ -18,15 +18,19 @@ CONFIG_PATH_LIST = [
|
||||
_config: Optional['AppConfig'] = None
|
||||
|
||||
|
||||
def init():
|
||||
if reload():
|
||||
def init(cmd_args):
|
||||
if reload(cmd_args):
|
||||
return
|
||||
logger.warning('Using default config')
|
||||
|
||||
config = AppConfig()
|
||||
config.load_cmd_args(cmd_args)
|
||||
|
||||
global _config
|
||||
_config = AppConfig()
|
||||
_config = config
|
||||
|
||||
|
||||
def reload():
|
||||
def reload(cmd_args):
|
||||
config_path = ''
|
||||
for path in CONFIG_PATH_LIST:
|
||||
if os.path.exists(path):
|
||||
@ -38,6 +42,8 @@ def reload():
|
||||
config = AppConfig()
|
||||
if not config.load(config_path):
|
||||
return False
|
||||
config.load_cmd_args(cmd_args)
|
||||
|
||||
global _config
|
||||
_config = config
|
||||
return True
|
||||
@ -49,6 +55,7 @@ def get_config():
|
||||
|
||||
class AppConfig:
|
||||
def __init__(self):
|
||||
self.debug = False
|
||||
self.host = '127.0.0.1'
|
||||
self.port = 12450
|
||||
self.database_url = 'sqlite:///data/database.db'
|
||||
@ -78,6 +85,13 @@ class AppConfig:
|
||||
self.open_live_access_key_id != '' and self.open_live_access_key_secret != '' and self.open_live_app_id != 0
|
||||
)
|
||||
|
||||
def load_cmd_args(self, args):
|
||||
if args.host is not None:
|
||||
self.host = args.host
|
||||
if args.port is not None:
|
||||
self.port = args.port
|
||||
self.debug = args.debug
|
||||
|
||||
def load(self, path):
|
||||
try:
|
||||
config = configparser.ConfigParser()
|
||||
|
25
main.py
25
main.py
@ -59,17 +59,17 @@ def init():
|
||||
|
||||
init_logging(args.debug)
|
||||
logger.info('App started, initializing')
|
||||
config.init()
|
||||
config.init(args)
|
||||
|
||||
utils.request.init()
|
||||
models.database.init(args.debug)
|
||||
models.database.init()
|
||||
|
||||
services.avatar.init()
|
||||
services.translate.init()
|
||||
services.open_live.init()
|
||||
services.chat.init()
|
||||
|
||||
init_server(args.host, args.port, args.debug)
|
||||
init_server()
|
||||
if server is None:
|
||||
return False
|
||||
|
||||
@ -123,36 +123,31 @@ def init_logging(debug):
|
||||
logging.getLogger('tornado.access').setLevel(logging.WARNING)
|
||||
|
||||
|
||||
def init_server(host, port, debug):
|
||||
def init_server():
|
||||
cfg = config.get_config()
|
||||
if host is None:
|
||||
host = cfg.host
|
||||
if port is None:
|
||||
port = cfg.port
|
||||
|
||||
app = tornado.web.Application(
|
||||
ROUTES,
|
||||
websocket_ping_interval=10,
|
||||
debug=debug,
|
||||
debug=cfg.debug,
|
||||
autoreload=False
|
||||
)
|
||||
try:
|
||||
global server
|
||||
server = app.listen(
|
||||
port,
|
||||
host,
|
||||
cfg.port,
|
||||
cfg.host,
|
||||
xheaders=cfg.tornado_xheaders,
|
||||
max_body_size=1024 * 1024,
|
||||
max_buffer_size=1024 * 1024
|
||||
)
|
||||
except OSError:
|
||||
logger.warning('Address is used %s:%d', host, port)
|
||||
logger.warning('Address is used %s:%d', cfg.host, cfg.port)
|
||||
return
|
||||
finally:
|
||||
if cfg.open_browser_at_startup:
|
||||
url = 'http://localhost/' if port == 80 else f'http://localhost:{port}/'
|
||||
url = 'http://localhost/' if cfg.port == 80 else f'http://localhost:{cfg.port}/'
|
||||
webbrowser.open(url)
|
||||
logger.info('Server started: %s:%d', host, port)
|
||||
logger.info('Server started: %s:%d', cfg.host, cfg.port)
|
||||
|
||||
|
||||
async def run():
|
||||
|
@ -12,7 +12,7 @@ class OrmBase(sqlalchemy.orm.DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
def init(_debug):
|
||||
def init():
|
||||
cfg = config.get_config()
|
||||
global _engine
|
||||
_engine = sqlalchemy.create_engine(
|
||||
@ -22,7 +22,7 @@ def init(_debug):
|
||||
pool_timeout=3, # 连接数达到最大时获取新连接的超时时间
|
||||
# pool_pre_ping=True, # 获取连接时先检测是否可用
|
||||
pool_recycle=60 * 60, # 回收超过1小时的连接,防止数据库服务器主动断开不活跃的连接
|
||||
# echo=debug, # 输出SQL语句
|
||||
# echo=cfg.debug, # 输出SQL语句
|
||||
)
|
||||
|
||||
OrmBase.metadata.create_all(_engine)
|
||||
|
@ -205,9 +205,10 @@ class Plugin:
|
||||
token = ''.join(random.choice(string.hexdigits) for _ in range(32))
|
||||
self._set_token(token)
|
||||
|
||||
cfg = config.get_config()
|
||||
env = {
|
||||
**os.environ,
|
||||
'BLC_PORT': str(12450), # TODO 读配置
|
||||
'BLC_PORT': str(cfg.port),
|
||||
'BLC_TOKEN': self._token,
|
||||
}
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user