blivechat/main.py

57 lines
1.7 KiB
Python
Raw Normal View History

2019-05-21 19:15:12 +08:00
# -*- coding: utf-8 -*-
2019-05-26 17:14:59 +08:00
import argparse
2019-06-20 20:03:07 +08:00
import webbrowser
2019-05-22 14:10:27 +08:00
import logging
2019-05-21 19:15:12 +08:00
import os
import tornado.ioloop
import tornado.web
2019-06-12 13:55:49 +08:00
import views.chat
import views.config
import views.main
2019-05-22 01:11:23 +08:00
2019-05-22 14:10:27 +08:00
logger = logging.getLogger(__name__)
2019-05-21 19:15:12 +08:00
WEB_ROOT = os.path.join(os.path.dirname(__file__), 'frontend', 'dist')
def main():
2019-05-26 17:14:59 +08:00
parser = argparse.ArgumentParser(description='用于OBS的仿YouTube风格的bilibili直播聊天层')
parser.add_argument('--host', help='服务器host默认为127.0.0.1', default='127.0.0.1')
2019-07-10 15:30:35 +08:00
parser.add_argument('--port', help='服务器端口默认为12450', type=int, default=12450)
2019-05-26 17:14:59 +08:00
parser.add_argument('--debug', help='调试模式', action='store_true')
args = parser.parse_args()
2019-05-22 14:10:27 +08:00
logging.basicConfig(
format='{asctime} {levelname} [{name}]: {message}',
datefmt='%Y-%m-%d %H:%M:%S',
style='{',
2019-05-26 17:14:59 +08:00
level=logging.INFO if not args.debug else logging.DEBUG
2019-05-22 14:10:27 +08:00
)
2019-05-26 17:14:59 +08:00
app = tornado.web.Application(
[
2019-06-12 13:55:49 +08:00
(r'/chat', views.chat.ChatHandler),
(r'/config', views.config.ConfigsHandler),
(r'/config/(.+)', views.config.ConfigHandler),
2019-06-22 17:33:01 +08:00
(r'/((css|fonts|img|js|static)/.*)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
2019-05-26 17:14:59 +08:00
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
2019-06-12 13:55:49 +08:00
(r'/.*', views.main.MainHandler, {'path': WEB_ROOT})
2019-05-26 17:14:59 +08:00
],
websocket_ping_interval=30,
debug=args.debug,
autoreload=False
)
app.listen(args.port, args.host)
2019-07-18 09:08:03 +08:00
logger.info('Server started: %s:%d', args.host, args.port)
2019-06-20 20:03:07 +08:00
url = 'http://localhost' if args.port == 80 else f'http://localhost:{args.port}'
webbrowser.open(url)
2019-05-21 19:15:12 +08:00
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
main()