How huonggiangtms/e_commerce_rails works
Xác định loại dự án và mục đích chính
• Đây là một ứng dụng web full-stack xây dựng trên Ruby on Rails, có:
– Giao diện khách hàng (public site + PWA/service worker)
– Giao diện quản trị (Admin dashboard)
– Module crawl sản phẩm từ bên ngoài (Product Crawler)
– Chatbot tương tác với người dùng (StimulusJS + AI service)
– Giỏ hàng, thanh toán (Stripe), email thông báo (ActionMailer)
– Xử lý bất đồng bộ (Sidekiq + Redis)
Phân tích cấu trúc thư mục
– app/controllers, app/models, app/views: pattern MVC điển hình của Rails
– app/frontend: StimulusJS controllers, entrypoints quản lý JS/CSS qua Vite
– app/services: business logic riêng biệt (AI Studio client, crawler, chat service…)
– app/jobs: background jobs cho crawl và lịch chạy định kỳ
– config: routes, database, sidekiq, credentials, deploy, puma
– bin/, .kamal/: script & hook để deploy/boot với công cụ kamal/docker
– db/: migration, schema, seeds
– public/: trang lỗi, robots.txt, manifest PWA, service-worker
– Gemfile, package.json: thư viện Ruby và JS
Tạo hệ thống thiết kế sơ đồ
a. Xác định các thành phần chính (Component)
1) Client Browser / PWA
2) Web Server (Puma)
3) Rails Application
• Controllers (MVC)
• Views (SLIM templates + StimulusJS)
• Models (ActiveRecord PostgreSQL)
• Services Layer (crawling, chat, payment)
• Mailers (email)
• Jobs (Sidekiq + Redis)
4) Database (PostgreSQL)
5) Redis (cache + queue)
6) Storage (Active Storage trên local/S3)
7) Third-party Services
• Stripe API (thanh toán)
• AI Studio / Chatbot API
• External ecommerce sites (crawler target)
b. Mối quan hệ và luồng dữ liệu
– Trình duyệt gửi HTTP(S) tới Web Server (Puma)
– Puma chuyển request vào Rails Router → Controller
– Controller tương tác Model (ActiveRecord → PostgreSQL) và/hoặc gọi Service/Job
– Views kết hợp dữ liệu từ Controller, StimulusJS bắt các sự kiện ở client
– StimulusJS gọi AJAX tới controller endpoint (chatbot, order form, cart…)
– Background Job (Sidekiq + Redis) thực hiện crawl, gửi email, chạy lịch định kỳ
– Storage lưu trữ file (ảnh sản phẩm, Active Storage)
– Rails App gọi Stripe API cho thanh toán
– Rails App gọi AI Studio / Chat Service cho chatbot
c. Các pattern & nguyên tắc kiến trúc
– MVC (Model-View-Controller)
– Service Objects tách biệt logic phức tạp ra khỏi controller/model
– Asynchronous Processing với Sidekiq/Redis
– StimulusJS cho progressive enhancement front-end
– PWA (service worker, manifest)
– Hook deploy tự động (kamal + Docker)
d. Công nghệ/Thư viện nổi bật
– Ruby on Rails, StimulusJS, Vite
– PostgreSQL, ActiveRecord, ActiveStorage
– Sidekiq, Redis
– Devise (Authentication), Rolify (Authorization)
– Stripe (Payment), ActionMailer
– Docker, Kamal (deployment), Puma
• Sử dụng hình hộp (rectangle) để biểu diễn mỗi thành phần (component)
• Gắn nhãn rõ ràng: tên component + công nghệ (vd: “Web Server (Puma)”)
• Dùng mũi tên có hướng để chỉ luồng dữ liệu / phụ thuộc giữa các thành phần
• Phân biệt bằng màu sắc hoặc hình dáng:
– Client-side (màu xanh)
– Backend Services (màu cam)
– Data Storage (màu vàng)
– Third-party (màu xám)
• Vẽ riêng phần Backend: chia nhỏ thành Web Process, Job Process, Service Layer, Data Layer
• Thêm chú thích ngắn gọn nếu cần: cron job/crawler schedule, hook deploy, env vars
Lưu ý khi trình bày
– Đảm bảo không bỏ sót thành phần quan trọng: Redis, Sidekiq workers, Service Objects
– Tách rõ ràng hai luồng chính:
1) Real-time request/response (HTTP + AJAX)
2) Background processing (jobs, scheduled tasks)
– Thể hiện quan hệ với external APIs: Stripe, AI Studio, các site cần crawl
– Giữ sơ đồ đủ chi tiết để dev team hiểu kiến trúc, nhưng không rối mắt
Khi hoàn thành, sơ đồ sẽ giúp mọi thành viên nhanh chóng hình dung được toàn cảnh hệ thống, luồng dữ liệu, và cách các thành phần phối hợp với nhau. Chúc bạn thành công!