playlistcast/server.py

71 lines
2.2 KiB
Python
Raw Normal View History

2020-02-20 22:46:33 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Runs playlistcast media server"""
2020-02-20 22:46:33 +01:00
import os
import sys
import asyncio
import logging
2020-02-20 22:46:33 +01:00
import graphene
import tornado.web
import tornado.ioloop
from playlistcast.protocol import chromecast, ssdp
2020-02-20 22:46:33 +01:00
from playlistcast.api import Subscription, Query, Mutation
from playlistcast.api import browse
2020-02-20 22:46:33 +01:00
# https://stackoverflow.com/a/18812776
# Add vendor directory to module search path
PARENT_DIR = os.path.abspath(os.path.dirname(__file__))
VENDOR_DIR = os.path.join(PARENT_DIR, 'vendor/tornadoql')
2020-02-20 22:46:33 +01:00
sys.path.append(VENDOR_DIR)
from tornadoql.tornadoql import GraphQLSubscriptionHandler, GraphQLHandler, GraphiQLHandler # pylint: disable=E0401,C0413
2020-02-20 22:46:33 +01:00
STATIC_PATH = os.path.abspath('frontend/build')
LOG = logging.getLogger('playlistcast')
2020-02-20 22:46:33 +01:00
class IndexHandler(tornado.web.RequestHandler):
"""Serve index.html"""
# pylint: disable=W0223
2020-02-20 22:46:33 +01:00
def get(self):
"""Get request"""
2020-02-20 22:46:33 +01:00
path = os.path.join(STATIC_PATH, 'index.html')
with open(path, 'rb') as file:
self.finish(file.read())
2020-02-20 22:46:33 +01:00
def startup():
"""Run some scheduled tasks on start"""
loop = asyncio.get_event_loop()
loop.create_task(ssdp.find_upnp_services())
loop.create_task(chromecast.list_devices())
LOG.debug('startup tasks complete')
2020-02-20 22:46:33 +01:00
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
2020-02-20 22:46:33 +01:00
# configuration
DEBUG = False
PORT = 9666
HOST = '0.0.0.0'
2020-02-20 22:46:33 +01:00
# server
SCHEMA = graphene.Schema(query=Query, mutation=Mutation, subscription=Subscription)
ENDPOINTS = [
2020-02-20 22:46:33 +01:00
(r'/subscriptions', GraphQLSubscriptionHandler, dict(opts=dict(sockets=[],
subscriptions={}),
schema=SCHEMA)),
(r'/graphql', GraphQLHandler, dict(schema=SCHEMA)),
2020-02-20 22:46:33 +01:00
(r'/graphiql', GraphiQLHandler),
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': STATIC_PATH}),
(r'/', IndexHandler),
(r'/resource/(.*)', browse.BrowseResourceHandler),
2020-02-20 22:46:33 +01:00
]
APP = tornado.web.Application(ENDPOINTS)
APP.listen(PORT, HOST)
2020-02-20 22:46:33 +01:00
startup()
import playlistcast.scheduler # pylint: disable=W0611
2020-02-20 22:46:33 +01:00
tornado.ioloop.IOLoop.current().start()