Skip to content

Commit 034ae54

Browse files
author
Alan Christie
committed
feat: Support for secure ingress
1 parent fba6307 commit 034ae54

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

app/app.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
app_public = FastAPI()
3030
app_internal = FastAPI()
3131

32-
_INGRESS_LOCATION: str = os.getenv("INGRESS_LOCATION")
33-
assert _INGRESS_LOCATION, "INGRESS_LOCATION environment variable must be set"
32+
# Configuration...
33+
_INGRESS_LOCATION: str = os.getenv("WS_INGRESS_LOCATION", "localhost:8080")
34+
assert _INGRESS_LOCATION, "WS_INGRESS_LOCATION environment variable must be set"
35+
_INGRESS_SECURE: bool = os.getenv("WS_INGRESS_SECURE", "no").lower() == "yes"
36+
_LOGGER.info("INGRESS_LOCATION: %s", _INGRESS_LOCATION)
37+
_LOGGER.info("INGRESS_SECURE: %s", _INGRESS_SECURE)
3438

3539
_AMPQ_EXCHANGE: str = "event-streams"
36-
_AMPQ_URL: str = os.getenv("AMPQ_URL")
37-
assert _AMPQ_URL, "AMPQ_URL environment variable must be set"
40+
_AMPQ_URL: str = os.getenv("WS_AMPQ_URL", "")
41+
assert _AMPQ_URL, "WS_AMPQ_URL environment variable must be set"
3842
_LOGGER.info("AMPQ_URL: %s", _AMPQ_URL)
3943

4044
# Create our local database.
@@ -43,15 +47,13 @@
4347
# value when NONE is passed in as it's value.
4448
_DATABASE_PATH = "/data/event-streams.db"
4549
_LOGGER.info("Creating SQLite database (%s)...", _DATABASE_PATH)
46-
4750
_DB_CONNECTION = sqlite3.connect(_DATABASE_PATH)
4851
_CUR = _DB_CONNECTION.cursor()
4952
_CUR.execute(
5053
"CREATE TABLE IF NOT EXISTS es (id INTEGER PRIMARY KEY, uuid TEXT, routing_key TEXT)"
5154
)
5255
_DB_CONNECTION.commit()
5356
_DB_CONNECTION.close()
54-
5557
_LOGGER.info("Created.")
5658

5759

@@ -139,8 +141,11 @@ def post_es(request_body: EventStreamPostRequestBody):
139141
and an ID the event stream is known by (that can be used to delete the stream).
140142
In our case, it's a WebSocket URL like 'ws://localhost:8000/event-stream/0000'.
141143
"""
144+
# Generate am new (difficult to guess) UUID for the event stream...
142145
uuid_str: str = shortuuid.uuid()
143-
location: str = f"ws://{_INGRESS_LOCATION}/event-stream/{uuid_str}"
146+
# And construct the location we'll be listening on...
147+
location: str = "wss://" if _INGRESS_SECURE else "ws://"
148+
location += f"{_INGRESS_LOCATION}/event-stream/{uuid_str}"
144149

145150
# Create a new ES record...
146151
# An ID is assigned automatically -

docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ services:
2323
image: informaticsmatters/squonk2-fastapi-ws-event-stream:latest
2424
container_name: es
2525
environment:
26-
AMPQ_URL: 'amqp://es:cheddar1963@rabbitmq:5672'
27-
INGRESS_LOCATION: 'localhost:8080'
26+
WS_AMPQ_URL: 'amqp://es:cheddar1963@rabbitmq:5672'
27+
WS_INGRESS_LOCATION: 'localhost:8080'
28+
WS_INGRESS_SECURE: 'no'
2829
ports:
2930
# Public (Websocket)
3031
- '8080:8080'

docker-entrypoint.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Run the container using both the customer-facing WebSocket service
66
# and the internal REST endpoint.
7-
# Done my simply launching two uvicorn instances in parallel.
7+
# Done by launching two uvicorn instances in parallel.
88
echo "+> Launching uvicorn..."
9-
uvicorn app.app:app_public --reload --host 0.0.0.0 --port 8080 & \
10-
uvicorn app.app:app_internal --reload --host 0.0.0.0 --port 8081
9+
uvicorn app.app:app_public --host 0.0.0.0 --port 8080 & \
10+
uvicorn app.app:app_internal --host 0.0.0.0 --port 8081

0 commit comments

Comments
 (0)