blivechat/main.py

44 lines
1.1 KiB
Python
Raw Normal View History

2019-05-21 19:15:12 +08:00
# -*- coding: utf-8 -*-
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-05-22 01:11:23 +08:00
import chat
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')
# noinspection PyAbstractClass
class MainHandler(tornado.web.StaticFileHandler):
"""为了使用Vue Router的history模式把所有请求转发到index.html"""
async def get(self, *args, **kwargs):
await super().get('index.html', *args, **kwargs)
def main():
2019-05-22 14:10:27 +08:00
logging.basicConfig(
format='{asctime} {levelname} [{name}]: {message}',
datefmt='%Y-%m-%d %H:%M:%S',
style='{',
level=logging.INFO
)
2019-05-21 19:15:12 +08:00
app = tornado.web.Application([
2019-05-22 01:11:23 +08:00
(r'/chat', chat.ChatHandler),
2019-05-21 19:15:12 +08:00
(r'/((css|img|js)/.*)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': WEB_ROOT}),
(r'/.*', MainHandler, {'path': WEB_ROOT})
2019-05-22 01:11:23 +08:00
], websocket_ping_interval=30)
2019-05-21 19:15:12 +08:00
app.listen(80, '127.0.0.1')
2019-05-22 14:10:27 +08:00
logger.info('服务器启动127.0.0.1:80')
2019-05-21 19:15:12 +08:00
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
main()