8000 use GatewayServer instead of werkzeug · localstack/localstack-extensions@5d9e5c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d9e5c3

Browse files
committed
use GatewayServer instead of werkzeug
1 parent 6202f74 commit 5d9e5c3

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

aws-replicator/aws_replicator/client/auth_proxy.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from localstack.aws.spec import load_service
1818
from localstack.config import external_service_url
1919
from localstack.constants import AWS_REGION_US_EAST_1, DOCKER_IMAGE_NAME_PRO
20+
from localstack.http.hypercorn import GatewayServer
2021
from localstack.utils.bootstrap import setup_logging
2122
from localstack.utils.collections import select_attributes
2223
from localstack.utils.container_utils.container_client import PortMappings
@@ -27,9 +28,8 @@
2728
from localstack.utils.serving import Server
2829
from localstack.utils.strings import short_uid, to_bytes, to_str, truncate
2930
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
3333

3434
from aws_replicator import config as repl_config
3535
from aws_replicator.client.utils import truncate_content
@@ -65,22 +65,26 @@ def do_run(self):
6565
self.register_in_instance()
6666
bind_host = self.config.get("bind_host") or DEFAULT_BIND_HOST
6767

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()
7379

7480
def do_shutdown(self):
7581
if self._server:
7682
self._server.shutdown()
7783

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()
8488

8589
def proxy_request(self, request: Request) -> Response:
8690
parsed = self._extract_region_and_service(request.headers)

aws-replicator/aws_replicator/server/aws_request_forwarder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ def _extract_region_from_domain(self, context: RequestContext):
177177
extract the region and inject an Authorization header with this region into the request.
178178
"""
179179
try:
180-
from localstack.constants import TEST_AWS_ACCESS_KEY_ID
181-
except ImportError:
182-
# compatibility with localstack 3.5
183180
from localstack.testing.config import TEST_AWS_ACCESS_KEY_ID
181+
except ImportError:
182+
# backwards compatibility before 3.5
183+
from localstack.constants import TEST_AWS_ACCESS_KEY_ID
184184

185185
headers = CaseInsensitiveDict(dict(context.request.headers))
186186
host_header = headers.get("Host") or ""

aws-replicator/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ install_requires =
2222
localstack-client
2323
localstack-ext
2424
xmltodict
25+
rolo
2526
# TODO: runtime dependencies below should be removed over time (required for some LS imports)
2627
boto
2728
cbor2
@@ -39,7 +40,6 @@ test =
3940
pyproject-flake8
4041
pytest
4142
pytest-httpserver
42-
rolo
4343

4444
[options.package_data]
4545
aws_replicator =

0 commit comments

Comments
 (0)
0