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