diff --git a/api/chat.py b/api/chat.py index f539b72..4f8e10d 100644 --- a/api/chat.py +++ b/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): diff --git a/config.py b/config.py index 64e0eaf..1c148bd 100644 --- a/config.py +++ b/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() diff --git a/main.py b/main.py index c1f0e44..799e90d 100755 --- a/main.py +++ b/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(): diff --git a/models/database.py b/models/database.py index 458d81b..51a1b28 100644 --- a/models/database.py +++ b/models/database.py @@ -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) diff --git a/services/plugin.py b/services/plugin.py index 696db1f..b84b662 100644 --- a/services/plugin.py +++ b/services/plugin.py @@ -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: