diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/cremation-monorepo.iml b/.idea/cremation-monorepo.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/cremation-monorepo.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4220aac --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9570d44 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..50795a5 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 5b2e882..94ff7c3 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,12 @@ ## ภาพรวม โปรเจกต์นี้เป็น Monorepo แบบ Full-Stack ที่มีโครงสร้างพร้อมใช้งานสำหรับการพัฒนาและทดสอบ: -1. **Backend (Django/DRF):** โครงสร้าง Modular Monolith พร้อมระบบ Authentication ด้วย Djoser/JWT +1. **Backend (Django/DRF):** โครงสร้าง Modular Monolith พร้อมระบบ Authentication ด้วย Djoser/JWT รวมถึง Stateless โดยสมบูรณ์ เนื่องจาก Session/Cache ถูกย้ายไปเก็บที่ Redis 2. **ฐานข้อมูล (HA):** CockroachDB Cluster 3 Node (เข้ากันได้กับ PostgreSQL) 3. **Frontend (Web):** React + Vite + Tailwind CSS / DaisyUI และ Mobile ผ่าน Expo 4. **Automation:** Docker Compose พร้อมตั้งค่า Database อัตโนมัติเมื่อเริ่มต้น +5. **Caching/Broker:** Redis สำหรับการเก็บ Session/Cache และใช้เป็น Message Broker ให้ Celery +6. **Async Processing:** Celery Worker สำหรับจัดการ Asynchronous Tasks (งานเบื้องหลัง เช่น การส่งอีเมล, การสร้างรายงาน) ## การเริ่มต้นใช้งาน (Run Local) diff --git a/backend/cremation_backend/settings.py b/backend/cremation_backend/settings.py index d040070..216ecb1 100644 --- a/backend/cremation_backend/settings.py +++ b/backend/cremation_backend/settings.py @@ -28,7 +28,6 @@ DEBUG = True ALLOWED_HOSTS = [] - # Application definition DJANGO_APPS = [ @@ -190,4 +189,33 @@ DJOSER = { 'PERMISSIONS': { 'user_create': ['rest_framework.permissions.AllowAny'], } -} \ No newline at end of file +} + +# 1. ตั้งค่า Redis Cache +CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + # 'redis' คือ Hostname ของ Service ใน Docker Compose + "LOCATION": "redis://redis:6379/1", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + "IGNORE_EXCEPTIONS": True # ป้องกันการ Crash ถ้า Redis ล่ม + } + } +} + +# 2. บังคับให้ Django ใช้ Cache (Redis) ในการเก็บ Session +SESSION_ENGINE = "django.contrib.sessions.backends.cache" +SESSION_CACHE_ALIAS = "default" + +# 3. ใช้ Redis สำหรับเก็บ Token Cache (สำหรับ DRF/JWT ถ้าจำเป็น) +# CACHE_MIDDLEWARE_SECONDS = 60 * 5 # 5 นาที +# CACHE_MIDDLEWARE_KEY_PREFIX = 'auth_cache' + +# CELERY CONFIGURATION +CELERY_BROKER_URL = 'redis://redis:6379/0' # ใช้ Redis เป็น Broker +CELERY_RESULT_BACKEND = 'redis://redis:6379/0' # ใช้ Redis ในการเก็บผลลัพธ์ของ Task +CELERY_ACCEPT_CONTENT = ['json'] +CELERY_TASK_SERIALIZER = 'json' +CELERY_RESULT_SERIALIZER = 'json' +CELERY_TIMEZONE = 'Asia/Bangkok' \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index a34790a..888ba5f 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -7,3 +7,6 @@ psycopg[binary] typing_extensions djoser # สำหรับ endpoints: /users/ (Register), /token/login (Login), /password/reset djangorestframework-simplejwt # สำหรับสร้าง JWT (JSON Web Tokens) +django-redis # สำหรับเชื่อมต่อ Django กับ Redis +redis # ไคลเอนต์ Python สำหรับ Redis +celery # ตัว Worker \ No newline at end of file diff --git a/infra/docker-compose.yml b/infra/docker-compose.yml index 85f79fb..b10a2fd 100644 --- a/infra/docker-compose.yml +++ b/infra/docker-compose.yml @@ -44,6 +44,27 @@ services: # ตั้งค่าให้ Container ตายหลังจากรัน init เสร็จ (ไม่รันซ้ำ) restart: "no" + # Redis Service + redis: + image: redis:7-alpine + container_name: redis + ports: + - "6379:6379" + restart: always + + # Celery Worker Service + celery_worker: + build: + context: ../backend + dockerfile: Dockerfile + container_name: celery_worker + volumes: + - ../backend:/app + command: celery -A cremation_backend worker -l info # รัน worker process + depends_on: + - redis # Worker ต้องรอให้ Redis พร้อม + - cockroach-1 # Worker อาจจะต้องเข้าถึง DB ด้วย + # Backend/API (DRF) backend: build: @@ -58,6 +79,7 @@ services: - cockroach-1 - cockroach-2 - cockroach-3 + - redis environment: DB_HOST: cockroach-1 DB_PORT: 26257