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>"
|
|
|
|
|
|
|
|
|
|
import os
|
2021-02-07 10:33:12 +08:00
|
|
|
import logging
|
2021-06-17 17:28:20 +08:00
|
|
|
import platform
|
|
|
|
|
|
|
|
|
|
import pytz
|
2021-04-15 20:15:59 +08:00
|
|
|
|
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2021-02-06 08:58:58 +08:00
|
|
|
from tornado.log import enable_pretty_logging
|
2021-06-17 10:39:47 +08:00
|
|
|
from tornado import web, httpserver, ioloop, options
|
|
|
|
|
|
2021-07-03 09:49:23 +08:00
|
|
|
from Mongo import OtherMongoResource
|
2021-06-17 10:39:47 +08:00
|
|
|
from handler import IndexHandler, UserHandler, ResourceHandler, TopHandler, UserLikeHandler, NameHandler, \
|
|
|
|
|
CommentHandler, AnnouncementHandler, CaptchaHandler, MetricsHandler, GrafanaIndexHandler, GrafanaSearchHandler, \
|
2021-07-11 11:37:45 +08:00
|
|
|
GrafanaQueryHandler, BlacklistHandler, NotFoundHandler, DBDumpHandler, CommentChildHandler, DoubanHandler, \
|
|
|
|
|
CommentNewestHandler
|
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-06-11 17:08:30 +08:00
|
|
|
|
2021-02-06 08:58:58 +08:00
|
|
|
class RunServer:
|
|
|
|
|
root_path = os.path.dirname(__file__)
|
|
|
|
|
static_path = os.path.join(root_path, '')
|
|
|
|
|
handlers = [
|
|
|
|
|
(r'/api/resource', ResourceHandler),
|
2021-02-06 11:35:28 +08:00
|
|
|
(r'/api/top', TopHandler),
|
2021-06-12 11:22:57 +08:00
|
|
|
(r'/api/like', UserLikeHandler),
|
2021-04-04 14:08:32 +08:00
|
|
|
(r'/api/user', UserHandler),
|
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-07-03 10:40:13 +08:00
|
|
|
(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),
|
2021-06-11 17:08:30 +08:00
|
|
|
(r'/api/db_dump', DBDumpHandler),
|
2021-06-15 17:28:17 +08:00
|
|
|
(r'/api/announcement', AnnouncementHandler),
|
2021-02-06 08:58:58 +08:00
|
|
|
(r'/', IndexHandler),
|
2021-07-10 23:28:44 +08:00
|
|
|
(r'/api/douban', DoubanHandler),
|
2021-06-13 10:04:46 +08:00
|
|
|
(r'/(.*\.html|.*\.js|.*\.css|.*\.png|.*\.jpg|.*\.ico|.*\.gif|.*\.woff2|.*\.gz|.*\.zip|.*\.svg|.*\.json)',
|
2021-06-12 20:38:32 +08:00
|
|
|
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"),
|
2021-06-15 14:34:52 +08:00
|
|
|
"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)
|
2021-06-17 17:28:20 +08:00
|
|
|
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__":
|
2021-06-17 17:28:20 +08:00
|
|
|
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-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
|
2021-06-17 12:37:37 +08:00
|
|
|
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)
|