Exemplo n.º 1
0
class Root(Resource):

    """ Twisted web root resource """

    def __init__(self, plugin):

        """ Constructor """

        Resource.__init__(self)

        self.wsgi = WSGIResource(plugin.service.reactor,
                                 plugin.service.reactor.getThreadPool(),
                                 plugin.app)


    def getChild(self, child, request):

        """ Get web child resource helper """

        request.prepath.pop()
        request.postpath.insert(0, child)
        return self.wsgi


    def render(self, request):

        """ WSGI renderer helper """

        return self.wsgi.render(request)
Exemplo n.º 2
0
        class FlaskAppWrapper(Resource):
            """
            wraps the flask app as a WSGI resource while allow the react index.html (and its associated static content)
            to be served as the default page.
            """
            def __init__(self):
                super().__init__()
                self.wsgi = WSGIResource(reactor, reactor.getThreadPool(), app)
                import sys
                if getattr(sys, 'frozen', False):
                    # pyinstaller lets you copy files to arbitrary locations under the _MEIPASS root dir
                    uiRoot = os.path.join(sys._MEIPASS, 'ui')
                elif cfg.webappPath is not None:
                    uiRoot = cfg.webappPath
                else:
                    # release script moves the ui under the analyser package because setuptools doesn't seem to include
                    # files from outside the package
                    uiRoot = os.path.join(os.path.dirname(__file__), 'ui')
                logger.info('Serving ui from ' + str(uiRoot))
                self.react = ReactApp(uiRoot)
                self.static = static.File(os.path.join(uiRoot, 'static'))
                self.icons = static.File(cfg.iconPath)
                ws_server.factory.startFactory()
                self.ws_resource = WebSocketResource(ws_server.factory)

            def getChild(self, path, request):
                """
                Overrides getChild to allow the request to be routed to the wsgi app (i.e. flask for the rest api
                calls), the static dir (i.e. for the packaged css/js etc), the various concrete files (i.e. the public
                dir from react-app), the command icons or to index.html (i.e. the react app) for everything else.
                :param path:
                :param request:
                :return:
                """
                # allow CORS (CROSS-ORIGIN RESOURCE SHARING) for debug purposes
                request.setHeader('Access-Control-Allow-Origin', '*')
                request.setHeader('Access-Control-Allow-Methods', 'GET, PUT')
                request.setHeader('Access-Control-Allow-Headers',
                                  'x-prototype-version,x-requested-with')
                request.setHeader('Access-Control-Max-Age', '2520')  # 42 hours
                logger.debug(f"Handling {path}")
                if path == b'ws':
                    return self.ws_resource
                elif path == b'api':
                    request.prepath.pop()
                    request.postpath.insert(0, path)
                    return self.wsgi
                elif path == b'static':
                    return self.static
                elif path == b'icons':
                    return self.icons
                else:
                    return self.react.get_file(path)

            def render(self, request):
                return self.wsgi.render(request)
Exemplo n.º 3
0
class Root( resource.Resource ):
    """Root resource that combines the two sites/entry points"""

    def __init__(self, app):
    	resource.Resource.__init__(self)
    	self.wsgi = WSGIResource(reactor, reactor.getThreadPool(), app)

    def getChild( self, child, request ):
        # request.isLeaf = True
        request.prepath.pop()
        request.postpath.insert(0,child)
        return self.wsgi

    def render( self, request ):
        """Delegate to the WSGI resource"""
        return self.wsgi.render( request )
Exemplo n.º 4
0
        class FlaskAppWrapper(Resource):
            """
            wraps the flask app as a WSGI resource while allow the react index.html (and its associated static content)
            to be served as the default page.
            """
            def __init__(self):
                super().__init__()
                self.wsgi = WSGIResource(reactor, reactor.getThreadPool(), app)
                import sys
                if getattr(sys, 'frozen', False):
                    # pyinstaller lets you copy files to arbitrary locations under the _MEIPASS root dir
                    uiRoot = sys._MEIPASS
                else:
                    # release script moves the ui under the analyser package because setuptools doesn't seem to include
                    # files from outside the package
                    uiRoot = os.path.dirname(__file__)
                logger.info('Serving ui from ' + str(uiRoot))
                self.react = ReactApp(os.path.join(uiRoot, 'ui'))
                self.static = static.File(os.path.join(uiRoot, 'ui', 'static'))

            def getChild(self, path, request):
                """
                Overrides getChild to allow the request to be routed to the wsgi app (i.e. flask for the rest api 
                calls),
                the static dir (i.e. for the packaged css/js etc), the various concrete files (i.e. the public 
                dir from react-app) or to index.html (i.e. the react app) for everything else.
                :param path: 
                :param request: 
                :return: 
                """
                if path == b'api':
                    request.prepath.pop()
                    request.postpath.insert(0, path)
                    return self.wsgi
                elif path == b'static':
                    return self.static
                else:
                    return self.react.getFile(path)

            def render(self, request):
                return self.wsgi.render(request)
Exemplo n.º 5
0
class Root(Resource):
    """ Twisted web root resource """
    def __init__(self, plugin):
        """ Constructor """

        Resource.__init__(self)

        self.wsgi = WSGIResource(plugin.service.reactor,
                                 plugin.service.reactor.getThreadPool(),
                                 plugin.app)

    def getChild(self, child, request):
        """ Get web child resource helper """

        request.prepath.pop()
        request.postpath.insert(0, child)
        return self.wsgi

    def render(self, request):
        """ WSGI renderer helper """

        return self.wsgi.render(request)
Exemplo n.º 6
0
 def render(self, request):
     # socket host has no 'port' attribute
     # but WSGI requires it - let it be `None`
     request.getHost().port = None
     return _WSGIResource.render(self, request)