40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
import asyncio
|
|
import os
|
|
from argparse import Namespace
|
|
import tornado
|
|
from src.logger import logger as l
|
|
import src.stats as stats
|
|
|
|
|
|
def start_server(settings: list, args: Namespace) -> None:
|
|
logger = l.getChild(__name__)
|
|
|
|
def _get_all_feeds():
|
|
return [{ 'rss': sets['name'], 'file': f"/feeds/{sets['rss']}" } for sets in settings if os.path.isfile(f"{args.directory}/{sets['rss']}")]
|
|
|
|
|
|
class MainHandler(tornado.web.RequestHandler):
|
|
def set_default_headers(self):
|
|
self.set_header("Access-Control-Allow-Origin", "*")
|
|
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
|
|
self.set_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
|
|
|
def get(self):
|
|
self.render("index.html", feeds=_get_all_feeds(), last_modified_at=stats.get_last_modified_at())
|
|
|
|
|
|
async def start_web_server():
|
|
logger.info(f"Starting web server on port {args.port}")
|
|
app = tornado.web.Application(
|
|
[
|
|
(r"/", MainHandler),
|
|
(r'/feeds/(.*)', tornado.web.StaticFileHandler, {'path': args.directory}),
|
|
],
|
|
template_path=os.path.join(os.path.dirname(__file__), "templates"),
|
|
static_path=args.directory,
|
|
debug=args.debug,
|
|
)
|
|
app.listen(args.port)
|
|
await asyncio.Event().wait()
|
|
|
|
asyncio.run(start_web_server()) |