Skip to content

Commit 140cce8

Browse files
committed
working locally
1 parent 7873981 commit 140cce8

File tree

18 files changed

+245
-31
lines changed

18 files changed

+245
-31
lines changed
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
1-
# Use an official Python runtime as a parent image
2-
FROM python:3.10.4-slim-buster AS builder
1+
# Use a smaller base image
2+
FROM python:3.10.4-slim-buster
33

44
# Set environment variables
55
ENV PYTHONDONTWRITEBYTECODE 1
66
ENV PYTHONUNBUFFERED 1
77

88
# Set work directory
9-
WORKDIR /code
9+
WORKDIR /app
10+
11+
RUN apt-get update && apt-get install -y \
12+
gcc \
13+
python3-dev \
14+
libpq-dev
1015

1116
# Install dependencies
12-
COPY requirements.txt /code/
17+
COPY requirements.txt /app/
18+
RUN pip install --upgrade pip
1319
RUN pip install --no-cache-dir -r requirements.txt
1420

15-
# Copy project
16-
COPY . /code/
21+
# Copy project files
22+
COPY . /app/
1723

1824
# Collect static files
1925
RUN python manage.py collectstatic --noinput
2026

21-
22-
# Use a smaller base image for the final stage
23-
FROM python:3.10.4-slim-buster
24-
25-
# Set environment variables
26-
ENV PYTHONDONTWRITEBYTECODE 1
27-
ENV PYTHONUNBUFFERED 1
28-
29-
# Set work directory
30-
WORKDIR /code
31-
32-
# Copy the dependencies from the builder stage
33-
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
34-
COPY --from=builder /code /code
35-
3627
# Create a non-root user
3728
RUN adduser --disabled-password --gecos '' django
3829

3930
# Set the user to the non-root user
4031
USER django
4132

4233
# Start server
43-
CMD ["sh", "-c", "python manage.py migrate && gunicorn defang_sample.wsgi:application --bind 0.0.0.0:8000"]
34+
CMD ["sh", "-c", "python manage.py migrate && gunicorn django_defang.wsgi:application --bind 0.0.0.0:8000"]

samples/django-channels-redis/app/chat/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ChatConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "chat"
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import json
2+
3+
from asgiref.sync import async_to_sync
4+
from channels.generic.websocket import WebsocketConsumer
5+
from pydantic import BaseModel
6+
from .models import ChatMessage
7+
8+
class MessagePayload(BaseModel):
9+
user: str = "Anonymous"
10+
message: str
11+
timestamp: float = None
12+
13+
class GroupPayload(BaseModel):
14+
type: str
15+
user: str = "Anonymous"
16+
message: str
17+
timestamp: float = None
18+
19+
class ChatConsumer(WebsocketConsumer):
20+
def connect(self):
21+
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
22+
self.room_group_name = f"chat_{self.room_name}"
23+
# Join room group
24+
async_to_sync(self.channel_layer.group_add)(
25+
self.room_group_name, self.channel_name
26+
)
27+
self.accept()
28+
29+
def disconnect(self, close_code):
30+
# Leave room group
31+
async_to_sync(self.channel_layer.group_discard)(
32+
self.room_group_name, self.channel_name
33+
)
34+
35+
# Receive message from WebSocket
36+
def receive(self, text_data):
37+
text_data_json = json.loads(text_data)
38+
payload = MessagePayload(**text_data_json)
39+
message = payload.message
40+
41+
persisted = ChatMessage.objects.create(
42+
user=payload.user,
43+
message=message,
44+
room=self.room_name,
45+
)
46+
47+
group_payload = GroupPayload(
48+
type="chat_message",
49+
message=message,
50+
user=payload.user,
51+
timestamp=int(persisted.timestamp.timestamp() * 1000)
52+
)
53+
54+
# Send message to room group
55+
async_to_sync(self.channel_layer.group_send)(
56+
self.room_group_name, group_payload.model_dump()
57+
)
58+
59+
# Receive message from room group
60+
def chat_message(self, event):
61+
payload = GroupPayload(**event)
62+
message_payload = MessagePayload(
63+
user=payload.user,
64+
message=payload.message,
65+
timestamp=payload.timestamp,
66+
)
67+
68+
# Send message to WebSocket
69+
self.send(text_data=message_payload.model_dump_json())
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 5.1 on 2024-08-22 22:02
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = []
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name="ChatMessage",
15+
fields=[
16+
(
17+
"id",
18+
models.BigAutoField(
19+
auto_created=True,
20+
primary_key=True,
21+
serialize=False,
22+
verbose_name="ID",
23+
),
24+
),
25+
("user", models.CharField(max_length=50)),
26+
("message", models.TextField()),
27+
("room", models.CharField(max_length=50)),
28+
("timestamp", models.DateTimeField(auto_now_add=True)),
29+
],
30+
),
31+
]

samples/django-channels-redis/app/chat/migrations/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.db import models
2+
3+
class ChatMessage(models.Model):
4+
user = models.CharField(max_length=50)
5+
message = models.TextField()
6+
room = models.CharField(max_length=50)
7+
timestamp = models.DateTimeField(auto_now_add=True)
8+
9+
def __str__(self):
10+
return f"{self.user} - {self.timestamp.strftime('%Y-%m-%d %H:%M:%S')}"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import re_path
2+
3+
from . import consumers
4+
5+
websocket_urlpatterns = [
6+
re_path(r"ws/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()),
7+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

0 commit comments

Comments
 (0)