|
17 | 17 | from localstack.aws.spec import load_service |
18 | 18 | from localstack.config import external_service_url |
19 | 19 | from localstack.constants import AWS_REGION_US_EAST_1, DOCKER_IMAGE_NAME_PRO |
| 20 | +from localstack.http.hypercorn import GatewayServer |
20 | 21 | from localstack.utils.bootstrap import setup_logging |
21 | 22 | from localstack.utils.collections import select_attributes |
22 | 23 | from localstack.utils.container_utils.container_client import PortMappings |
|
27 | 28 | from localstack.utils.serving import Server |
28 | 29 | from localstack.utils.strings import short_uid, to_bytes, to_str, truncate |
29 | 30 | from localstack_ext.bootstrap.licensingv2 import ENV_LOCALSTACK_API_KEY, ENV_LOCALSTACK_AUTH_TOKEN |
30 | | -from werkzeug import Request, Response |
31 | | -from werkzeug import serving as werkzeug_serving |
32 | | -from werkzeug.datastructures import Headers |
| 31 | +from rolo import Request, Response |
| 32 | +from rolo.gateway import Gateway |
33 | 33 |
|
34 | 34 | from aws_replicator import config as repl_config |
35 | 35 | from aws_replicator.client.utils import truncate_content |
@@ -65,22 +65,26 @@ def do_run(self): |
65 | 65 | self.register_in_instance() |
66 | 66 | bind_host = self.config.get("bind_host") or DEFAULT_BIND_HOST |
67 | 67 |
|
68 | | - # werkzeug uses under the hood a stdlib ``http.server.HTTPServer``, which should be enough to serve |
69 | | - # a simple AWS proxy. if HTTP2 and websockets are ever needed, then we should move to a more |
70 | | - # sophisticated runtime (like hypercorn or twisted) |
71 | | - self._server = werkzeug_serving.make_server(bind_host, self.port, self._wsgi_app) |
72 | | - self._server.serve_forever() |
| 68 | + # we use a hypercorn server to serve the proxy. |
| 69 | + # TODO: there are likely ways to simplify this, and we are maybe getting rid of hypercorn soon, |
| 70 | + # but it seems currently the proxy doesn't work with Twisted, likely due to some IO assumptions in |
| 71 | + # the ``proxy_request`` code. |
| 72 | + self._server = GatewayServer( |
| 73 | + Gateway(request_handlers=[self._handler_chain_handler]), |
| 74 | + listen=[localstack_config.HostAndPort(bind_host, self.port)], |
| 75 | + use_ssl=False, |
| 76 | + ) |
| 77 | + self._server.start() |
| 78 | + self._server.join() |
73 | 79 |
|
74 | 80 | def do_shutdown(self): |
75 | 81 | if self._server: |
76 | 82 | self._server.shutdown() |
77 | 83 |
|
78 | | - @Request.application |
79 | | - def _wsgi_app(self, request: Request) -> Response: |
80 | | - """A wsgi-compatible interface for serving the proxy server.""" |
81 | | - # make headers mutable (needed for serving through werkzeug) |
82 | | - request.headers = Headers(request.headers) |
83 | | - return self.proxy_request(request) |
| 84 | + def _handler_chain_handler(self, chain, context, response): |
| 85 | + """Exposes the ``proxy_request`` routine as a handler chain handler to be served through a gateway.""" |
| 86 | + response.update_from(self.proxy_request(context.request)) |
| 87 | + chain.stop() |
84 | 88 |
|
85 | 89 | def proxy_request(self, request: Request) -> Response: |
86 | 90 | parsed = self._extract_region_and_service(request.headers) |
|
0 commit comments