Skip to content

Commit 8b90e44

Browse files
committed
fix: bugs in various spots
Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
1 parent 883ae6d commit 8b90e44

7 files changed

Lines changed: 43 additions & 33 deletions

File tree

Dockerfile

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
FROM python:3.9-slim-bullseye
1+
FROM python:3.9-slim-bookworm as base
22
WORKDIR /usr/src/app
33

4-
ENV POETRY_VERSION=1.4.2
5-
64
RUN apt-get update && apt-get install -y curl && apt-get clean
7-
RUN pip install "poetry==$POETRY_VERSION"
8-
COPY poetry.lock pyproject.toml README.md ./
9-
RUN mkdir -p socketdock && touch socketdock/__init__.py
10-
RUN poetry config virtualenvs.create false \
11-
&& poetry install --without=dev --no-interaction --no-ansi
5+
ENV POETRY_VERSION=1.5.1
6+
ENV POETRY_HOME=/opt/poetry
7+
RUN curl -sSL https://install.python-poetry.org | python -
8+
9+
ENV PATH="/opt/poetry/bin:$PATH"
10+
RUN poetry config virtualenvs.in-project true
11+
12+
# Setup project
13+
COPY pyproject.toml poetry.lock ./
14+
RUN poetry install --without dev
15+
16+
FROM python:3.9-slim-bookworm
17+
WORKDIR /usr/src/app
18+
COPY --from=base /usr/src/app/.venv /usr/src/app/.venv
19+
ENV PATH="/usr/src/app/.venv/bin:$PATH"
1220

1321
COPY socketdock socketdock
1422

demo/docker-compose.yaml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
version: '3'
22

33
services:
4-
websocket-gateway:
4+
socketdock:
55
build: ..
66
ports:
77
- "8765:8765"
88
volumes:
99
- ../socketdock:/usr/src/app/socketdock:z
1010
command: >
1111
--bindip 0.0.0.0
12-
--backend http
13-
--message-uri ${LAMBDA_ENDPOINT}
14-
--disconnect-uri ${LAMBDA_DISCONNECT_ENDPOINT}
15-
--endpoint ${EXTERNAL_ENDPOINT}
16-
17-
# Socket Dock Parameters:
18-
# parser.add_argument('--bindip', default='127.0.0.1')
19-
# parser.add_argument('--bindport', default=8765)
20-
# parser.add_argument('--externalhostandport', default="127.0.0.1:8765")
21-
# parser.add_argument('--backend', default="loopback", choices=["loopback", "http"])
22-
# parser.add_argument('--message_uri')
23-
# parser.add_argument('--disconnect_uri')
12+
--backend loopback
13+
--message-uri https://example.com
14+
--disconnect-uri https://example.com
15+
--endpoint http://socketdock:8765
16+
--log-level INFO

demo/socket_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ async def hello():
1414
print(f"< {response}", flush=True)
1515

1616

17-
asyncio.run(hello())
17+
if __name__ == "__main__":
18+
asyncio.run(hello())

socketdock/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def main():
3434
if args.backend == "loopback":
3535
from .testbackend import TestBackend
3636

37-
backend = TestBackend()
37+
backend = TestBackend(args.endpoint)
3838
elif args.backend == "http":
3939
from .httpbackend import HTTPBackend
4040

socketdock/api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async def status_handler(request: Request):
4444
async def socket_send(request: Request, connectionid: str):
4545
"""Send a message to a connected socket."""
4646
LOGGER.info("Inbound message for %s", connectionid)
47-
LOGGER.info("Existing connections: %s", active_connections.keys())
47+
LOGGER.debug("Existing connections: %s", active_connections.keys())
4848

4949
if connectionid not in active_connections:
5050
return text("FAIL", status=500)
@@ -61,7 +61,7 @@ async def socket_send(request: Request, connectionid: str):
6161
async def socket_disconnect(request: Request, connectionid: str):
6262
"""Disconnect a socket."""
6363
LOGGER.info("Disconnect %s", connectionid)
64-
LOGGER.info("Existing connections: %s", active_connections.keys())
64+
LOGGER.debug("Existing connections: %s", active_connections.keys())
6565

6666
if connectionid not in active_connections:
6767
return text("FAIL", status=500)
@@ -80,12 +80,12 @@ async def socket_handler(request: Request, websocket: Websocket):
8080
try:
8181
# register user
8282
LOGGER.info("new client connected")
83-
socket_id = websocket.connection.id.hex
83+
socket_id = websocket.ws_proto.id.hex
8484
active_connections[socket_id] = websocket
8585
lifetime_connections += 1
86-
LOGGER.info("Existing connections: %s", active_connections.keys())
87-
LOGGER.info("Added connection: %s", socket_id)
88-
LOGGER.info("Request headers: %s", dict(request.headers.items()))
86+
LOGGER.debug("Existing connections: %s", active_connections.keys())
87+
LOGGER.debug("Added connection: %s", socket_id)
88+
LOGGER.debug("Request headers: %s", dict(request.headers.items()))
8989

9090
await backend.socket_connected(
9191
connection_id=socket_id,

socketdock/httpbackend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def __init__(
2929

3030
def send_callback(self, connection_id: str) -> str:
3131
"""Return the callback URI for sending a message to a connected socket."""
32-
return f"{self.socket_base_uri}/{connection_id}/send"
32+
return f"{self.socket_base_uri}/socket/{connection_id}/send"
3333

3434
def disconnect_callback(self, connection_id: str) -> str:
3535
"""Return the callback URI for disconnecting a connected socket."""
36-
return f"{self.socket_base_uri}/{connection_id}/disconnect"
36+
return f"{self.socket_base_uri}/socket/{connection_id}/disconnect"
3737

3838
def callback_uris(self, connection_id: str) -> Dict[str, str]:
3939
"""Return labelled callback URIs."""

socketdock/testbackend.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Test backend for SocketDock."""
22

3+
import logging
34
from typing import Dict, Union
45
import aiohttp
56

67
from .backend import Backend
78

9+
LOGGER = logging.getLogger(__name__)
10+
811

912
class TestBackend(Backend):
1013
"""Test backend for SocketDock."""
@@ -22,21 +25,26 @@ async def socket_connected(
2225
2326
This test backend doesn't care, but can be useful to clean up state.
2427
"""
28+
LOGGER.debug("Connected to test backend: %s", connection_id)
2529

2630
async def inbound_socket_message(
2731
self,
2832
connection_id: str,
2933
message: Union[str, bytes],
3034
):
3135
"""Receive socket message."""
32-
send_uri = f"{self.base_uri}/{connection_id}/send"
36+
LOGGER.debug("Recieved message [%s]: %s", connection_id, message)
37+
send_uri = f"{self.base_uri}/socket/{connection_id}/send"
3338
async with aiohttp.ClientSession() as session:
3439
async with session.post(send_uri, data="Hello yourself") as resp:
40+
if not resp.ok:
41+
raise Exception(f"Failed to post to: {send_uri}")
3542
response = await resp.text()
36-
print(response)
43+
LOGGER.debug("Response: %s", response)
3744

3845
async def socket_disconnected(self, connection_id: str):
3946
"""Socket disconnected.
4047
4148
This test backend doesn't care, but can be useful to clean up state.
4249
"""
50+
LOGGER.debug("Disconnected from test backend: %s", connection_id)

0 commit comments

Comments
 (0)