Files
YYeTsBot/yyetsweb/server.py

118 lines
4.4 KiB
Python
Raw Normal View History

2021-02-06 08:58:58 +08:00
#!/usr/local/bin/python3
# coding: utf-8
# YYeTsBot - server.py
# 2/5/21 21:02
#
__author__ = "Benny <benny.think@gmail.com>"
2021-02-07 10:33:12 +08:00
import logging
2021-07-11 15:50:52 +08:00
import os
import platform
import pytz
2021-04-15 20:15:59 +08:00
from apscheduler.schedulers.background import BackgroundScheduler
2021-07-11 15:50:52 +08:00
from tornado import httpserver, ioloop, options, web
2021-02-06 08:58:58 +08:00
from tornado.log import enable_pretty_logging
2021-06-17 10:39:47 +08:00
2021-07-11 15:50:52 +08:00
from handler import (AnnouncementHandler, BlacklistHandler, CaptchaHandler,
2021-08-15 12:28:19 +08:00
CategoryHandler, CommentChildHandler, CommentHandler,
CommentNewestHandler, CommentReactionHandler,
2021-07-24 13:45:57 +08:00
DBDumpHandler, DoubanHandler, DoubanReportHandler,
GrafanaIndexHandler, GrafanaQueryHandler,
2021-08-15 12:28:19 +08:00
GrafanaSearchHandler, IndexHandler, LikeHandler,
MetricsHandler, NameHandler, NotFoundHandler,
NotificationHandler, ResourceHandler,
ResourceLatestHandler, TopHandler, UserEmailHandler,
UserHandler)
2021-07-11 15:50:52 +08:00
from migration.douban_sync import sync_douban
2021-08-15 12:28:19 +08:00
from Mongo import OtherMongoResource, ResourceLatestMongoResource
2021-02-06 08:58:58 +08:00
enable_pretty_logging()
2021-02-06 19:58:40 +08:00
2021-02-10 10:01:35 +08:00
if os.getenv("debug"):
logging.basicConfig(level=logging.DEBUG)
2021-02-06 19:58:40 +08:00
2021-02-06 08:58:58 +08:00
class RunServer:
root_path = os.path.dirname(__file__)
2021-08-15 12:28:19 +08:00
static_path = os.path.join(root_path, 'templates')
2021-02-06 08:58:58 +08:00
handlers = [
2021-08-15 12:28:19 +08:00
(r'/', IndexHandler),
2021-02-06 08:58:58 +08:00
(r'/api/resource', ResourceHandler),
2021-08-15 12:28:19 +08:00
(r'/api/resource/latest', ResourceLatestHandler),
2021-02-06 11:35:28 +08:00
(r'/api/top', TopHandler),
2021-08-15 12:28:19 +08:00
(r'/api/like', LikeHandler),
2021-04-04 14:08:32 +08:00
(r'/api/user', UserHandler),
2021-08-15 12:28:19 +08:00
(r'/api/user/email', UserEmailHandler),
2021-02-10 11:06:59 +08:00
(r'/api/name', NameHandler),
2021-05-30 10:34:48 +08:00
(r'/api/comment', CommentHandler),
2021-08-15 12:28:19 +08:00
(r'/api/comment/reaction', CommentReactionHandler),
(r'/api/comment/child', CommentChildHandler),
2021-07-11 11:37:45 +08:00
(r'/api/comment/newest', CommentNewestHandler),
2021-05-30 10:34:48 +08:00
(r'/api/captcha', CaptchaHandler),
2021-02-06 17:21:33 +08:00
(r'/api/metrics', MetricsHandler),
2021-03-14 18:19:59 +08:00
(r'/api/grafana/', GrafanaIndexHandler),
(r'/api/grafana/search', GrafanaSearchHandler),
(r'/api/grafana/query', GrafanaQueryHandler),
2021-02-11 08:41:20 +08:00
(r'/api/blacklist', BlacklistHandler),
(r'/api/db_dump', DBDumpHandler),
(r'/api/announcement', AnnouncementHandler),
2021-07-10 23:28:44 +08:00
(r'/api/douban', DoubanHandler),
2021-07-18 11:51:39 +08:00
(r'/api/douban/report', DoubanReportHandler),
2021-08-15 12:28:19 +08:00
(r'/api/notification', NotificationHandler),
(r'/api/category', CategoryHandler),
2021-07-12 18:11:41 +08:00
(r'/(.*\.html|.*\.js|.*\.css|.*\.png|.*\.jpg|.*\.ico|.*\.gif|.*\.woff2|.*\.gz|.*\.zip|'
r'.*\.svg|.*\.json|.*\.txt)',
web.StaticFileHandler,
2021-02-06 08:58:58 +08:00
{'path': static_path}),
]
2021-04-04 14:08:32 +08:00
settings = {
2021-04-14 21:21:31 +08:00
"cookie_secret": os.getenv("cookie_secret", "eo2kcgpKwXj8Q3PKYj6nIL1J4j3b58DX"),
"default_handler_class": NotFoundHandler,
"login_url": "/login",
2021-04-04 14:08:32 +08:00
}
2021-04-14 21:21:31 +08:00
application = web.Application(handlers, **settings)
2021-02-06 08:58:58 +08:00
@staticmethod
2021-04-14 21:21:31 +08:00
def run_server(port, host):
tornado_server = httpserver.HTTPServer(RunServer.application, xheaders=True)
2021-02-06 08:58:58 +08:00
tornado_server.bind(port, host)
if platform.uname().system == "Windows":
tornado_server.start(1)
else:
tornado_server.start(0)
2021-02-06 08:58:58 +08:00
try:
2021-02-06 19:58:40 +08:00
print('Server is running on http://{}:{}'.format(host, port))
2021-02-06 08:58:58 +08:00
ioloop.IOLoop.instance().current().start()
except KeyboardInterrupt:
ioloop.IOLoop.instance().stop()
print('"Ctrl+C" received, exiting.\n')
if __name__ == "__main__":
timez = pytz.timezone('Asia/Shanghai')
scheduler = BackgroundScheduler(timezone=timez)
2021-07-03 09:49:23 +08:00
scheduler.add_job(OtherMongoResource().reset_top, 'cron', hour=0, minute=0, day=1)
2021-07-11 15:50:52 +08:00
scheduler.add_job(sync_douban, 'cron', hour=0, minute=0, day=1)
2021-08-15 12:28:19 +08:00
scheduler.add_job(ResourceLatestMongoResource().refresh_latest_resource, 'cron', minute=0)
2021-02-07 09:32:23 +08:00
scheduler.start()
2021-02-06 08:58:58 +08:00
options.define("p", default=8888, help="running port", type=int)
options.define("h", default='127.0.0.1', help="listen address", type=str)
options.parse_command_line()
p = options.options.p
h = options.options.h
banner = """
Lazarus came back from the dead. By @Bennythink
"""
print(banner)
2021-02-06 08:58:58 +08:00
RunServer.run_server(port=p, host=h)