Exemplo n.º 1
0
def make():
    """
    Make the Tornado `RuleRouter`.
    """

    # API v1 endpoints
    v1_app = Application([
        (r'^/?(?P<environs>.*?)/v1/manifest/?', ManifestHandler),
        (r'^.*?/v1/environs/(?P<environ_id>.+)', EnvironHandler),
        (r'^.*?/v1/proxy/(?P<binder_id>[^@]+)\@(?P<token>[^\/]+)/(?P<path>.+)',
         ProxyHandler)
    ])

    # API v0 endpoints
    v0_app = Application([
        (r'^/?(?P<environs>.*?)/v0/manifest/?', ManifestHandler),
        (r'^.*?/v0/environ/(?P<environ_id>.+)', EnvironHandler),
        (r'^.*?/v0/proxy/(?P<binder_id>[^@]+)\@(?P<token>[^\/]+)/(?P<path>.+)',
         ProxyHandler)
    ])

    index_app = Application([(r'^/', IndexHandler)])

    return RuleRouter([
        Rule(PathMatches(r'^.*?/v1/.*'), v1_app),
        Rule(PathMatches(r'^.*?/v0/.*'), v0_app),
        Rule(PathMatches(r'^/$'), index_app)
    ])
Exemplo n.º 2
0
    def get_app(self):
        app = Application()

        def request_callable(request):
            request.connection.write_headers(
                ResponseStartLine("HTTP/1.1", 200, "OK"),
                HTTPHeaders({"Content-Length": "2"}))
            request.connection.write(b"OK")
            request.connection.finish()

        router = CustomRouter()
        router.add_routes({
            "/nested_handler": (app, _get_named_handler("nested_handler"))
        })

        app.add_handlers(".*", [
            (HostMatches("www.example.com"), [
                (PathMatches("/first_handler"),
                 "tornado.test.routing_test.SecondHandler", {}, "second_handler")
            ]),
            Rule(PathMatches("/.*handler"), router),
            Rule(PathMatches("/first_handler"), FirstHandler, name="first_handler"),
            Rule(PathMatches("/request_callable"), request_callable),
            ("/connection_delegate", ConnectionDelegate())
        ])

        return app
Exemplo n.º 3
0
def make_router():
    from source.payment.cmb import cmb_app
    from source.tasks import tasks_app

    cmb_router = RuleRouter([
        Rule(PathMatches('/cmb/.*'), cmb_app),
        Rule(PathMatches('/tasks/.*'), tasks_app),
    ])
    return cmb_router
Exemplo n.º 4
0
    def get_app(self):
        wsgi_app = WSGIContainer(self.wsgi_app)

        class Handler(RequestHandler):
            def get(self, *args, **kwargs):
                self.finish(self.reverse_url("tornado"))

        return RuleRouter([
            (PathMatches("/tornado.*"), Application([(r"/tornado/test", Handler, {}, "tornado")])),
            (PathMatches("/wsgi"), wsgi_app),
        ])
Exemplo n.º 5
0
    def make_web_app(self):
        # Start with web application routes
        app = Application([
            (r"/assets/(.*)", StaticFileHandler,
             dict(path=tornado_settings['static_path'])),
            (r"/dashboard/(.*)", MainUIRequestHandler,
             dict(
                 data_queues=self.data_queues,
                 foreman=self.foreman,
             )),
            (r"/dashws", DashboardWebSocket,
             dict(
                 data_queues=self.data_queues,
                 foreman=self.foreman,
             )),
            (r"/history/(.*)", HistoryUIRequestHandler,
             dict(data_queues=self.data_queues, )),
            (r"/plugins/(.*)", PluginsUIRequestHandler,
             dict(data_queues=self.data_queues, )),
            (r"/settings/(.*)", SettingsUIRequestHandler,
             dict(data_queues=self.data_queues, )),
            (r"/filebrowser/(.*)", ElementFileBrowserUIRequestHandler,
             dict(data_queues=self.data_queues, )),
            (r"/(.*)", RedirectHandler, dict(url="/dashboard/")),
        ], **tornado_settings)

        # Add API routes
        app.add_handlers(r'.*', [
            (PathMatches(r"/api/.*"), APIRequestRouter(app)),
        ])

        return app
Exemplo n.º 6
0
 def add_handler(self, pattern: str, target: Any,
                 target_params: Optional[Dict[str, Any]]) -> None:
     if pattern in self.pattern_to_rule:
         self.remove_handler(pattern)
     new_rule = Rule(PathMatches(pattern), target, target_params)
     self.pattern_to_rule[pattern] = new_rule
     self.rules.append(new_rule)
Exemplo n.º 7
0
    def __init__(self,
                 pattern: str,
                 handler: Type[RestHandler],
                 parameters: Optional[Dict] = None):
        """Parameters:

        * ``pattern``: Regular expression to be matched. Any capturing
          groups in the regex will be passed in to the handler's
          get/post/etc methods as arguments (by keyword if named, by
          position if unnamed. Named and unnamed capturing groups
          may not be mixed in the same rule).

        * ``handler``: `~.web.RequestHandler` subclass to be invoked.

        * ``parameters`` (optional): A dictionary of additional arguments
          to be passed to the handler's constructor.

        """
        name = None
        if issubclass(handler, RestHandler):
            name = handler.get_rev_name()

        super().__init__(PathMatches(pattern), handler, parameters, name)

        self.regex = self.matcher.regex
        self.handler_class = self.target
        self.kwargs = parameters
Exemplo n.º 8
0
def make_app(root_dir):
    rules = [
        Rule(PathMatches(r'/(.*)'),
             SaveHandler,
             target_kwargs={'root_dir': root_dir})
    ]
    return Application(rules)
def make_app(custom=False):
    handlers = [
        (PathMatches(r'/simple'), SimpleHandler),
        (r'/crash', CrashHandler),
        (r'/call-simple', CallSimpleHandler),
        (r'/super-simple', SuperSimpleHandler),
        (r'/coro', CoroHandler),
        (r'/coro-throw', CoroThrowHandler),
        (r'/fake-coro', FakeCoroHandler),
        (r'/init', InitializeHandler),
        (r'/html-insertion', HTMLInsertionHandler),
        (r'/bad-get-status', BadGetStatusHandler),
        (r'/force-cat-response/(\S+)/(\S+)/(\S+)', ProcessCatHeadersHandler),
        (r'/304-cat-response/(\S+)/(\S+)', ProcessCatHeadersHandler, {
            'response_code': 304
        }),
        (r'/echo-headers', EchoHeaderHandler),
        (r'/native-simple', NativeSimpleHandler),
        (r'/multi-trace', MultiTraceHandler),
        (r'/web-socket', WebSocketHandler),
        (r'/ensure-future', EnsureFutureHandler),
        (r'/call-web-socket', WebNestedHandler),
        (r'/block/(\d+)', BlockingHandler),
        (r'/block-with-yield/(\d+)', BlockingHandler, {
            'yield_before_finish': True
        }),
    ]
    if custom:
        return CustomApplication()
    else:
        return tornado.web.Application(handlers, log_function=dummy)
Exemplo n.º 10
0
    def _build_routing(self, routing):
        from tornado.routing import Rule, AnyMatches, PathMatches

        routes = [
            Rule(PathMatches(route), _RestApplicationRouter(self, subRoutes))
            for route, subRoutes in routing.items()
        ]
        routes.append(Rule(AnyMatches(), self.wildcard_router))
        self.default_router = _RestApplicationRouter(self, routes)
        self._build_reverse_routing()
Exemplo n.º 11
0
def main():
    IOLoop.configure('tornado.platform.asyncio.AsyncIOLoop')
    router = RuleRouter([Rule(PathMatches(r"/.*"), MyApplication())])
    server = HTTPServer(router,
                        ssl_options={
                            "certfile": str(cert),
                            "keyfile": str(key),
                        })
    server.listen(443)
    IOLoop.instance().start()
Exemplo n.º 12
0
    def get_app(self):
        app = Application()

        def request_callable(request):
            request.write(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
            request.finish()

        app.add_handlers(
            ".*", [(HostMatches("www.example.com"), [
                (PathMatches("/first_handler"),
                 "tornado.test.routing_test.SecondHandler", {},
                 "second_handler")
            ]),
                   Rule(PathMatches("/first_handler"),
                        FirstHandler,
                        name="first_handler"),
                   Rule(PathMatches("/request_callable"), request_callable),
                   ("/connection_delegate", ConnectionDelegate())])

        return app
Exemplo n.º 13
0
def main():
    rules = []
    for i, appName in enumerate(supportedApps):
        app = handlers[i].get_app()
        rules.append(Rule(PathMatches("/%s.*" % appName), app))

    # Default app
    settings = dict(template_path="html", static_path="static", debug=True)
    app = tornado.web.Application([
        (r"/(.*)", DefaultHandler),
        (r'/favicon.ico', tornado.web.StaticFileHandler),
        (r'/static/', tornado.web.StaticFileHandler),
    ], **settings)
    rules.append(Rule(PathMatches(r"/.*"), app))
    router = RuleRouter(rules)

    http_server = tornado.httpserver.HTTPServer(router)
    port = int(os.environ.get("PORT", 5000))
    http_server.listen(port)
    tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 14
0
    def _build_router_rules(self):
        """
        Builds the router rules. Each service is registered to the path configured in the Service object. Rules are
        created for both http and websocket routers and set to the rules objects of the router.
        """
        self.logger.debug(messages.ROUTER_BUILD_ROUTER_RULES)

        http_rules = []
        websocket_rules = []

        for name in sorted(self._services):
            service = self._services[name]

            path = '/{}/.*'.format(service.path)

            self.logger.debug(
                messages.ROUTER_BUILD_ROUTER_RULES_ADD_SERVICE.format(
                    service.name, path))
            http_rules.append(Rule(PathMatches(path), service.http_router))
            websocket_rules.append(
                Rule(PathMatches(path), service.websocket_router))

        self._http_router_rules = http_rules
        self._websocket_router_rules = websocket_rules
Exemplo n.º 15
0
    def _operations_from_urlspec(urlspec):
        """Generator of operations described in the handler's routes list

        :param urlspec:
        :type urlspec: URLSpec descendant
        """
        handler_class = urlspec.handler_class
        for r in handler_class.routes:
            matcher = PathMatches(r.get('path_pattern'))
            if matcher.regex == urlspec.regex:
                for http_method in r.get('supported_methods', []):
                    method = getattr(handler_class, r.get('call_method'))
                    operation_data = yaml_utils.load_yaml_from_docstring(
                        method.__doc__)
                    if operation_data:
                        operation = {http_method.lower(): operation_data}
                        yield operation
Exemplo n.º 16
0
def make_router(routing_table):
    """Resolves a uri to the Story and line number to execute."""

    method_rules = []
    for method, routes in routing_table.items():
        rules = [
            Rule(
                PathMatches(route.endpoint),
                CustomRouter(route.filename, route.linenum)
            ) for route in routes
        ]
        # create a new rule by method mapping to many rule by path
        method_rules.append(Rule(MethodMatches(method), RuleRouter(rules)))

    router = RuleRouter(method_rules)

    return router
Exemplo n.º 17
0
def main(cutoff_date, port, quiet):

    CUTOFF = parse_iso(cutoff_date)

    INDEX = requests.get(MAIN_PYPI).content

    class MainIndexHandler(RequestHandler):

        async def get(self):
            return self.write(INDEX)

    class PackageIndexHandler(RequestHandler):

        async def get(self, package):

            package_index = requests.get(JSON_URL.format(package=package)).json()
            release_links = ""
            for release in package_index['releases'].values():
                for file in release:
                    release_date = parse_iso(file['upload_time'])
                    if release_date < CUTOFF:
                        if file['requires_python'] is None:
                            release_links += '    <a href="{url}#sha256={sha256}">{filename}</a><br/>\n'.format(url=file['url'], sha256=file['digests']['sha256'], filename=file['filename'])
                        else:
                            rp = file['requires_python'].replace('>', '&gt;')
                            release_links += '    <a href="{url}#sha256={sha256}" data-requires-python="{rp}">{filename}</a><br/>\n'.format(url=file['url'], sha256=file['digests']['sha256'], rp=rp, filename=file['filename'])

            self.write(PACKAGE_HTML.format(package=package, links=release_links))

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 0))
    if port is None:
        port = sock.getsockname()[1]
    sock.close()

    app = Application([(r"/", MainIndexHandler),
                       (PathMatches(r"/(?P<package>\S+)\//?"), PackageIndexHandler)])

    app.listen(port=port)

    if not quiet:
        print(f'Starting pypi-timemachine server at http://localhost:{port}')

    IOLoop.instance().start()
Exemplo n.º 18
0
    def _load_application(self):
        """
        """
        if settings.translation:
            try:
                from tornado import locale
                locale.load_translations(settings.translations_dir)
            except:
                warnings.warn(
                    'locale dir load failure,maybe your config file is not set correctly.'
                )

        apps = settings.INSTALLED_APPS
        if not apps:
            raise ConfigError('settings.INSTALLED_APPS is empty')
        handlers = []
        for app_name in apps:
            handlers += get_handlers(app_name)
        app = self._install_application(handlers)
        self.router = RuleRouter([Rule(PathMatches('/.*'), app)])
Exemplo n.º 19
0
 def create_application():
     """Return tornado web app."""
     application = tornado.web.Application(
         [
             (r"/(?P<camera>[A-Za-z0-9_]+)/mjpeg-stream", DynamicStreamHandler),
             (
                 (
                     r"/(?P<camera>[A-Za-z0-9_]+)/static-mjpeg-streams/"
                     r"(?P<mjpeg_stream>[A-Za-z0-9_\-]+)"
                 ),
                 StaticStreamHandler,
             ),
             (r"/ws-stream", RegularSocketHandler),
             (r"/websocket", WebSocketHandler),
             (r"/(?P<camera>[A-Za-z0-9_]+)/stream", DeprecatedStreamHandler),
             (r"/ui/", IndexHandler),
             (r"/ui/about", AboutHandler),
             (r"/ui/cameras", CamerasHandler),
             (r"/ui/index", IndexHandler),
             (r"/ui/recordings", RecordingsHandler),
             (r"/ui/settings", SettingsHandler),
             (
                 r"/recordings/(.*)",
                 tornado.web.StaticFileHandler,
                 {"path": RECORDER_PATH},
             ),
             (r"/", IndexRedirect),
         ],
         default_handler_class=NotFoundHandler,
         template_path=PATH_TEMPLATES,
         static_path=PATH_STATIC,
         static_url_prefix=PREFIX_STATIC,
         debug=True,
     )
     application.add_handlers(
         r".*",
         [
             (PathMatches(r"/api/.*"), APIRouter(application)),
         ],
     )
     return application
Exemplo n.º 20
0
def add_prefix_to_handlers(prefix, auth_dict, handlers):
    login_url = "/login"
    if prefix == '/':
        return login_url, handlers

    prefix = prefix.rstrip('/')
    login_url = '{}/{}'.format(prefix, login_url.lstrip('/'))

    for i in range(len(handlers)):
        hdl = handlers[i]
        if isinstance(hdl, Rule):
            # log.debug(hdl.matcher.regex.pattern)
            ptn = '{}/{}'.format(prefix, hdl.matcher.regex.pattern.lstrip('/'))
            tgt = hdl.target
            kwargs = hdl.target_kwargs
            name = hdl.name
            hdl = Rule(PathMatches(ptn), tgt, kwargs, name)
            # log.debug("  routing %s to %s", hdl.matcher.regex.pattern, tgt)
        else:
            if isinstance(hdl[0], re.Pattern):
                # log.debug(hdl[0].pattern)
                hdl[0] = re.compile('{}/{}'.format(prefix,
                                                   hdl[0].pattern.lstrip('/')))
                # log.debug(hdl[0].pattern)
                # log.debug("  routing %s to %s", hdl[0].pattern, hdl[1])
            else:
                # log.debug(hdl[0])
                if isinstance(hdl, list):
                    hdl[0] = '{}/{}'.format(prefix, hdl[0].lstrip('/'))
                if isinstance(hdl, tuple):
                    hdl = tuple(['{}/{}'.format(prefix, hdl[0].lstrip('/'))] +
                                list(hdl[1:]))
                else:
                    raise RuntimeError()
                # log.debug("  routing %s to %s", hdl[0], hdl[1])
        handlers[i] = hdl

    handlers.insert(len(handlers),
                    ('/', HomeRedirectHandler, dict(prefix=prefix)))

    return login_url, handlers
Exemplo n.º 21
0
    def add_component(self, component: type):
        """
        Adds a components handlers to the http or websocket routers respectively.

        :param component: Services component
        :raises ValueError: Error indicating the component provided is not a subclass of Component.
        :raises TypeError: Error indicating the component provided is not a class.
        """
        try:
            if not issubclass(component, Component):
                msg = messages.SERVICE_ADD_COMPONENT_INVALID_COMPONENT_CLASS.format(
                    component.__name__)

                self.logger.error(msg)
                raise ValueError(msg)
        except TypeError:
            self.logger.error(
                messages.SERVICE_ADD_COMPONENT_INVALID_COMPONENT_TYPE)
            raise TypeError(
                messages.SERVICE_ADD_COMPONENT_INVALID_COMPONENT_TYPE)

        self.logger.debug(
            messages.SERVICE_ADD_COMPONENT.format(self.name,
                                                  component.component_name,
                                                  component.component_path))

        if component.component_http_handler is None and component.component_websocket_handler is None:
            self.logger.warning(
                messages.SERVICE_ADD_COMPONENT_WITH_NO_HANDLER.format(
                    component.component_name, self.name))

        http_handlers = []
        websocket_handlers = []

        if component.component_http_handler:
            self.logger.debug(
                messages.SERVICE_ADD_COMPONENT_REGISTER_HANDLER.format(
                    self.name, component.component_name, 'http_handler'))
            http_handlers = [(r'/.*', component.component_http_handler,
                              dict(component_class=component))]

        if component.component_websocket_handler:
            self.logger.debug(
                messages.SERVICE_ADD_COMPONENT_REGISTER_HANDLER.format(
                    self.name, component.component_name, 'websocket_handler'))
            websocket_handlers = [(r'/.*',
                                   component.component_websocket_handler,
                                   dict(component_class=component))]

        http_application = Application(http_handlers)
        websocket_application = Application(websocket_handlers)

        self.http_rules.append(
            Rule(PathMatches('/{}.*'.format(component.component_path)),
                 http_application))
        self.websocket_rules.append(
            Rule(PathMatches('/{}.*'.format(component.component_path)),
                 websocket_application))

        self.http_router = RuleRouter(self.http_rules)
        self.websocket_router = RuleRouter(self.websocket_rules)
Exemplo n.º 22
0
def make_app(cookie_secret=None,
             workspace='fixtures/workspace',
             title='Pipelines',
             auth=None,
             history_limit=0,
             prefix='/'):
    if cookie_secret is None:
        raise PipelineError('Cookie secret can not be empty')

    if not os.path.isdir(workspace):
        raise PipelinesError('Workspace is not a valid directory: %s' %
                             workspace)

    auth_dict = _get_auth_dict(auth)

    slug_regexp = '[0-9a-zA-Z_\\-]+'
    endpoints = [
        Rule(PathMatches(r"/api/pipelines/"),
             GetPipelinesHandler,
             name='api_base'),
        Rule(PathMatches(r"/api/pipelines/({slug})/".format(slug=slug_regexp)),
             GetPipelineHandler),
        Rule(
            PathMatches(
                r"/api/pipelines/({slug})/run".format(slug=slug_regexp)),
            RunPipelineHandler),
        Rule(
            PathMatches(r"/api/pipelines/({slug})/({slug})/status".format(
                slug=slug_regexp)), GetStatusHandler),
        Rule(
            PathMatches(r"/api/pipelines/({slug})/({slug})/log".format(
                slug=slug_regexp)), GetLogsHandler),
        Rule(
            PathMatches(
                r"/api/pipelines/({slug})/triggers".format(slug=slug_regexp)),
            GetTriggersHandler),
        Rule(PathMatches(r"/api/webhook/({slug})".format(slug=slug_regexp)),
             WebhookHandler),
        Rule(PathMatches(r"/api/slackbot/({slug})".format(slug=slug_regexp)),
             SlackbotHandler),
        Rule(PathMatches(r"/login"), LoginHandler, name='login'),
        Rule(PathMatches(r'/(.*)'), AuthStaticFileHandler, {
            'path': _get_static_path('app'),
            "default_filename": "index.html"
        }),
    ]

    if auth_dict and auth_dict.get('type') == 'gh':
        hdl = Rule(PathMatches('/ghauth'),
                   GithubOAuth2LoginHandler,
                   name='ghauth')
        endpoints.insert(len(endpoints) - 1, hdl)

    # prefix support
    login_url, endpoints = add_prefix_to_handlers(prefix, auth_dict, endpoints)

    return Application(
        endpoints,
        title=title,
        workspace_path=workspace,
        auth=auth_dict,
        login_url=login_url,
        debug="True",
        cookie_secret=cookie_secret,
        history_limit=history_limit,
        prefix=prefix.rstrip('/'),
    )
Exemplo n.º 23
0
def get_data_server(verbose=True):
    """
    This starts up a tornado server and returns a handle to a DataServer
    object which can be used to register files to serve.
    """

    global _data_server

    if _data_server is not None:
        return _data_server

    from tornado.ioloop import IOLoop
    from tornado.web import RequestHandler, Application, StaticFileHandler
    from tornado.routing import PathMatches

    class WebServer(Application):
        host = None
        port = None

        def run(self, host=None, port=8886):
            self.host = host
            self.port = port
            try:
                self.listen(port)
                IOLoop.instance().start()
            finally:
                self.host = None
                self.port = None

    class DataServer(object):
        def start(self, app):
            self._files = {}
            self._thread = Thread(target=self.start_app)
            self._thread.daemon = True
            self._thread.start()
            self._app = app
            while self._app.host is None and self._app.port is None:
                time.sleep(0.1)

        @property
        def port(self):
            return self._app.port

        @property
        def host(self):
            return self._app.host

        def start_app(self):

            asyncio.set_event_loop(asyncio.new_event_loop())

            host = socket.gethostbyname('localhost')

            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.bind(('localhost', 0))
            port = sock.getsockname()[1]
            sock.close()

            access_log = logging.getLogger("tornado.access")
            access_log.setLevel('ERROR')

            self._app.run(host=host, port=port)

        def serve_file(self, filename, extension=''):
            with open(filename, 'rb') as f:
                content = f.read()

            hash = md5(content).hexdigest() + extension
            self._files[hash] = os.path.abspath(filename)
            return 'http://' + self.host + ':' + str(
                self.port) + '/data/' + hash

        def static_url(self, rest):
            return 'http://' + self.host + ':' + str(
                self.port) + '/static/' + rest

        def get_file_contents(self, hash):
            with open(self._files[hash], 'rb') as f:
                return f.read()

    mimetypes.add_type('image/fits', '.fits')
    mimetypes.add_type('image/fits', '.fts')
    mimetypes.add_type('image/fits', '.fit')

    ds = DataServer()

    class DataHandler(RequestHandler):
        async def get(self, hash):
            # Do our best to set an appropriate Content-Type.
            filename = ds._files[hash]
            content_type = mimetypes.guess_type(filename)[0]
            if content_type is None:
                content_type = 'application/binary'
            self.set_header('Content-Type', content_type)

            # Add wide-open CORS headers to allow external WWT apps to access
            # data. This isn't needed in the default case, but comes in handy
            # when testing updates to the research app with an alternative
            # localhost port. Note that a hostile actor can just ignore these
            # settings, and our default stance is that data are globally
            # accessible, so this really shouldn't affect the level of security
            # we provide.
            self.set_header('Access-Control-Allow-Origin', '*')
            self.set_header('Access-Control-Allow-Methods', 'GET,HEAD')
            self.set_header(
                'Access-Control-Allow-Headers',
                'Content-Disposition,Content-Encoding,Content-Length,Content-Type'
            )

            self.write(ds.get_file_contents(hash))

    app = WebServer([
        (PathMatches(r'/data/(?P<hash>\S+)'), DataHandler),
        (r'/static/(.*)', StaticFileHandler, {
            'path': os.path.join(os.path.dirname(__file__), 'web_static'),
            'default_filename': 'index.html',
        }),
    ])

    ds.start(app)

    _data_server = ds

    return ds
Exemplo n.º 24
0
    def make_web_app(self):
        # Start with web application routes
        from unmanic.webserver.websocket import UnmanicWebsocketHandler
        app = Application([
            (r"/unmanic/websocket", UnmanicWebsocketHandler),
            (r"/unmanic/downloads/(.*)", DownloadsHandler),
            (r"/(.*)", RedirectHandler, dict(
                url="/unmanic/ui/dashboard/"
            )),
        ], **tornado_settings)

        # Add API routes
        from unmanic.webserver.api_request_router import APIRequestRouter
        app.add_handlers(r'.*', [
            (
                PathMatches(r"/unmanic/api/.*"),
                APIRequestRouter(app)
            ),
        ])

        # Add frontend routes
        from unmanic.webserver.main import MainUIRequestHandler
        app.add_handlers(r'.*', [
            (r"/unmanic/css/(.*)", StaticFileHandler, dict(
                path=tornado_settings['static_css']
            )),
            (r"/unmanic/fonts/(.*)", StaticFileHandler, dict(
                path=tornado_settings['static_fonts']
            )),
            (r"/unmanic/icons/(.*)", StaticFileHandler, dict(
                path=tornado_settings['static_icons']
            )),
            (r"/unmanic/img/(.*)", StaticFileHandler, dict(
                path=tornado_settings['static_img']
            )),
            (r"/unmanic/js/(.*)", StaticFileHandler, dict(
                path=tornado_settings['static_js']
            )),
            (
                PathMatches(r"/unmanic/ui/(.*)"),
                MainUIRequestHandler,
            ),
        ])

        # Add widgets routes
        from unmanic.webserver.plugins import DataPanelRequestHandler
        from unmanic.webserver.plugins import PluginStaticFileHandler
        from unmanic.webserver.plugins import PluginAPIRequestHandler
        app.add_handlers(r'.*', [
            (
                PathMatches(r"/unmanic/panel/[^/]+(/(?!static/|assets$).*)?$"),
                DataPanelRequestHandler
            ),
            (
                PathMatches(r"/unmanic/plugin_api/[^/]+(/(?!static/|assets$).*)?$"),
                PluginAPIRequestHandler
            ),
            (r"/unmanic/panel/.*/static/(.*)", PluginStaticFileHandler, dict(
                path=tornado_settings['static_img']
            )),
        ])

        if self.developer:
            self._log("API Docs - Updating...", level="debug")
            try:
                from unmanic.webserver.api_v2.schema.swagger import generate_swagger_file
                errors = generate_swagger_file()
                for error in errors:
                    self._log(error, level="warn")
                else:
                    self._log("API Docs - Updated successfully", level="debug")
            except Exception as e:
                self._log("Failed to reload API schema", message2=str(e), level="error")

        # Start the Swagger UI. Automatically generated swagger.json can also
        # be served using a separate Swagger-service.
        from swagger_ui import tornado_api_doc
        tornado_api_doc(
            app,
            config_path=os.path.join(os.path.dirname(__file__), "..", "webserver", "docs", "api_schema_v2.json"),
            url_prefix="/unmanic/swagger",
            title="Unmanic application API"
        )

        return app
Exemplo n.º 25
0
def get_data_server(verbose=True):
    """
    This starts up a tornado server and returns a handle to a DataServer
    object which can be used to register files to serve.
    """

    global _data_server

    if _data_server is not None:
        return _data_server

    from tornado.ioloop import IOLoop
    from tornado.web import RequestHandler, Application
    from tornado.routing import PathMatches

    class WebServer(Application):

        host = None
        port = None

        def run(self, host=None, port=8886):
            self.host = host
            self.port = port
            try:
                self.listen(port)
                IOLoop.instance().start()
            finally:
                self.host = None
                self.port = None

    class DataServer(object):

        def start(self, app):
            self._files = {}
            self._thread = Thread(target=self.start_app)
            self._thread.daemon = True
            self._thread.start()
            self._app = app
            while self._app.host is None and self._app.port is None:
                time.sleep(0.1)

        @property
        def port(self):
            return self._app.port

        @property
        def host(self):
            return self._app.host

        def start_app(self):

            asyncio.set_event_loop(asyncio.new_event_loop())

            host = socket.gethostbyname('localhost')

            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.bind(('localhost', 0))
            port = sock.getsockname()[1]
            sock.close()

            access_log = logging.getLogger("tornado.access")
            access_log.setLevel('ERROR')

            self._app.run(host=host, port=port)

        def serve_file(self, filename, real_name=True, extension=''):
            with open(filename, 'rb') as f:
                content = f.read()
            if real_name:
                hash = os.path.basename(filename)
            else:
                hash = md5(content).hexdigest() + extension
            self._files[hash] = os.path.abspath(filename)
            return 'http://' + self.host + ':' + str(self.port) + '/data/' + hash

        def get_file_contents(self, hash):
            with open(self._files[hash], 'rb') as f:
                return f.read()

    ds = DataServer()

    class DataHandler(RequestHandler):
        async def get(self, hash):
            self.write(ds.get_file_contents(hash))

    app = WebServer([(PathMatches(r"/data/(?P<hash>\S+)"), DataHandler)])

    ds.start(app)

    _data_server = ds

    return ds
Exemplo n.º 26
0
 def add_handler(self, pattern, target, target_params):
     if pattern in self.pattern_to_rule:
         self.remove_handler(pattern)
     new_rule = Rule(PathMatches(pattern), target, target_params)
     self.pattern_to_rule[pattern] = new_rule
     self.rules.append(new_rule)
Exemplo n.º 27
0
class HandlerInApp1(tornado.web.RequestHandler):
    def get(self):
        self.write("Handler in app1")


app1 = Application([(r"/app1/handler", HandlerInApp1)])


class HandlerInApp2(tornado.web.RequestHandler):
    def get(self):
        self.write("Handler in app2")


app2 = Application([(r"/app2/handler", HandlerInApp2)])

if __name__ == '__main__':
    # 创建服务器
    server = tornado.web.HTTPServer(
        RuleRouter([
            Rule(PathMatches(r"/app1.*"), app1),
            Rule(PathMatches(r"/app2.*"), app2)
        ]))
    # 侦听端口 8888
    server.listen(8888)
    # 启动
    try:
        tornado.ioloop.IOLoop.current().start()
    except KeyboardInterrupt:
        print("Server stopped by user")
Exemplo n.º 28
0
from tornado.routing import RuleRouter, Rule, PathMatches
from tornado.web import url, Application
from core.views import MainView

from apps.account.urls import URL_ACCOUNT

URL_PATTERNS = RuleRouter([
    Rule(PathMatches("/account.*"), URL_ACCOUNT),
])
Exemplo n.º 29
0
    io_loop = tornado.ioloop.IOLoop.current()
    asyncio.set_event_loop(io_loop.asyncio_loop)

    db_engine = io_loop.asyncio_loop.run_until_complete(
        create_engine(settings.DB_URI, loop=io_loop.asyncio_loop))

    amqp_connection = io_loop.asyncio_loop.run_until_complete(
        make_amqp_connection(db_engine))

    if clean:
        io_loop.asyncio_loop.run_until_complete(db_prepair(db_engine))

    application = tornado.web.Application(
        [
            Rule(
                PathMatches("/api/v1/.*"),
                tornado.web.Application([
                    (r"/api/v1/device/(?P<id>\d+)/reports", ReportHandler),
                    (r"/api/v1/device", DeviceHandler),
                ],
                                        amqp_connection=amqp_connection,
                                        engine=db_engine), {}, 'api_v1')
        ],
        default_handler_class=NotFoundHandler)
    application.listen(8888)

    try:
        io_loop.start()
    except KeyboardInterrupt:
        print('STOP')
        db_engine.close()
Exemplo n.º 30
0
def main(requirements_file, port, quiet):

    INDEX = requests.get(MAIN_PYPI).content
    INDEX = INDEX.replace(b'href="/simple', b'href="')

    SPECIFIERS = {}
    with open(requirements_file, 'r') as fd:
        for req in requirements.parse(fd):
            if req.specs:
                SPECIFIERS[req.name] = SpecifierSet(','.join(
                    [''.join(spec) for spec in req.specs]))

    class MainIndexHandler(RequestHandler):
        async def get(self):
            return self.write(INDEX)

    class PackageIndexHandler(RequestHandler):
        async def get(self, package):

            if package not in SPECIFIERS:
                return self.redirect(MAIN_PYPI + package)

            package_index = requests.get(
                JSON_URL.format(package=package)).json()

            release_links = ""
            for version, release in package_index['releases'].items():
                if Version(version) in SPECIFIERS[package]:
                    for file in release:
                        if file['requires_python'] is None:
                            release_links += '    <a href="{url}#sha256={sha256}">{filename}</a><br/>\n'.format(
                                url=file['url'],
                                sha256=file['digests']['sha256'],
                                filename=file['filename'])
                        else:
                            rp = file['requires_python'].replace('>', '&gt;')
                            release_links += '    <a href="{url}#sha256={sha256}" data-requires-python="{rp}">{filename}</a><br/>\n'.format(
                                url=file['url'],
                                sha256=file['digests']['sha256'],
                                rp=rp,
                                filename=file['filename'])

            self.write(
                PACKAGE_HTML.format(package=package, links=release_links))

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 0))
    if port is None:
        port = sock.getsockname()[1]
    sock.close()

    app = Application([(r"/", MainIndexHandler),
                       (PathMatches(r"/(?P<package>\S+)\//?"),
                        PackageIndexHandler)])

    app.listen(port=port)

    if not quiet:
        print(f'Starting PyPIcky server at http://localhost:{port}')

    IOLoop.instance().start()