Esempio n. 1
0
    def add_static_handler(self,
                           location: str,
                           path: str,
                           default_filename: Optional[str] = None,
                           start: bool = False) -> None:
        """
        Configure a static handler to serve data from the specified path.
        """
        if location[0] != "/":
            location = "/" + location

        if location[-1] != "/":
            location = location + "/"

        options = {"path": path}
        if default_filename is None:
            options["default_filename"] = "index.html"

        self._handlers.append(
            routing.Rule(routing.PathMatches(r"%s(.*)" % location),
                         web.StaticFileHandler, options))
        self._handlers.append(
            routing.Rule(routing.PathMatches(r"%s" % location[:-1]),
                         web.RedirectHandler, {"url": location[1:]}))

        if start:
            self._handlers.append((r"/", web.RedirectHandler, {
                "url": location[1:]
            }))
Esempio n. 2
0
 def add_static_content(self, path: str, content: str, content_type: str = "application/javascript") -> None:
     self._handlers.append(
         routing.Rule(
             routing.PathMatches(r"%s(.*)" % path),
             server.StaticContentHandler,
             {"transport": self, "content": content, "content_type": content_type},
         )
     )
Esempio n. 3
0
def initServer():
    picHandle = tornado.web.Application([
        (r"/", MainHandler),
    ])

    router = routing.RuleRouter([
        tornado.web.Rule(routing.PathMatches("/"), picHandle),
    ])

    server = tornado.web.HTTPServer(router)
    return server
Esempio n. 4
0
    async def start(self,
                    targets: Sequence[inmanta.protocol.endpoints.CallTarget],
                    additional_rules: List[routing.Rule] = []) -> None:
        """
        Start the server on the current ioloop
        """
        global_url_map: Dict[str, Dict[
            str, common.UrlMethod]] = self.get_global_url_map(targets)

        rules: List[routing.Rule] = []
        rules.extend(additional_rules)

        for url, handler_config in global_url_map.items():
            rules.append(
                routing.Rule(routing.PathMatches(url), RESTHandler, {
                    "transport": self,
                    "config": handler_config
                }))
            LOGGER.debug("Registering handler(s) for url %s and methods %s",
                         url, ", ".join(handler_config.keys()))

        application = web.Application(rules, compress_response=True)

        crt = inmanta_config.Config.get("server", "ssl_cert_file", None)
        key = inmanta_config.Config.get("server", "ssl_key_file", None)

        if crt is not None and key is not None:
            ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            ssl_ctx.load_cert_chain(crt, key)

            self._http_server = httpserver.HTTPServer(application,
                                                      decompress_request=True,
                                                      ssl_options=ssl_ctx)
            LOGGER.debug("Created REST transport with SSL")
        else:
            self._http_server = httpserver.HTTPServer(application,
                                                      decompress_request=True)

        bind_port = server_config.get_bind_port()
        bind_addresses = server_config.server_bind_address.get()

        for bind_addr in bind_addresses:
            self._http_server.listen(bind_port, bind_addr)
            LOGGER.info(f"Server listening on {bind_addr}:{bind_port}")
        self.running = True

        LOGGER.debug("Start REST transport")