Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
575e4868be | ||
|
|
846682ac4a | ||
|
|
f7105a62cc | ||
|
|
0628344dcf | ||
|
|
d25db9367c | ||
|
|
1d29e50852 | ||
|
|
851e3fc2dc | ||
|
|
74770cd99d | ||
|
|
ce0735d775 |
@@ -1,4 +1,5 @@
|
|||||||
# کپی کنید: cp .env.example .env — و همه مقادیر change-me را عوض کنید.
|
# کپی کنید: cp .env.example .env — و همه مقادیر change-me را عوض کنید.
|
||||||
|
# راه سادهتر: bash scripts/gen-env.sh — همهٔ رمزها را تصادفی و قوی میسازد.
|
||||||
# docker compose بهطور خودکار این فایل را میخواند؛ بدون آن استک بالا نمیآید.
|
# docker compose بهطور خودکار این فایل را میخواند؛ بدون آن استک بالا نمیآید.
|
||||||
|
|
||||||
# --- رمزهای اصلی (اجباری) ---
|
# --- رمزهای اصلی (اجباری) ---
|
||||||
@@ -14,10 +15,57 @@ ADMIN_PASSWORD=change-me-admin-password
|
|||||||
# پورت ورود Traefik در توسعه (پیشفرض 5173)
|
# پورت ورود Traefik در توسعه (پیشفرض 5173)
|
||||||
TRAEFIK_HTTP_PORT=5173
|
TRAEFIK_HTTP_PORT=5173
|
||||||
# نشانی عمومی سایت برای sitemap و آدرس مطلق OG — در تولید مثل https://aramesh.example
|
# نشانی عمومی سایت برای sitemap و آدرس مطلق OG — در تولید مثل https://aramesh.example
|
||||||
|
# در تولید این مقدار هم پایهٔ پروب بیروننگر مانیتورینگ است.
|
||||||
SITE_URL=
|
SITE_URL=
|
||||||
# آدرسهای مجاز CORS (لیست JSON) — پیشفرض فقط لوکالهاست
|
# آدرسهای مجاز CORS (لیست JSON) — پیشفرض فقط لوکالهاست
|
||||||
# CORS_ORIGINS=["http://localhost:5173"]
|
# CORS_ORIGINS=["http://localhost:5173"]
|
||||||
|
|
||||||
|
# --- بیلد فرانتاند (در زمان npm run build روی هاست خوانده میشود — stack.sh پاس میدهد) ---
|
||||||
|
# اگر API روی مبدأ دیگری است (مثل توسعه ویندوز با پورت 19001) تنظیم کنید؛ هممبدأ یعنی خالی
|
||||||
|
VITE_API_URL=
|
||||||
|
# اسکریپت ردیاب Umami — خالی یعنی آنالیتیکس خاموش؛ در تولید مثل:
|
||||||
|
# VITE_UMAMI_SRC=https://analytics.aramesh.example/script.js
|
||||||
|
VITE_UMAMI_SRC=
|
||||||
|
# شناسهٔ وبسایت از داشبورد Umami (اولین بار: وبسایت را در analytics.localhost بسازید)
|
||||||
|
VITE_UMAMI_WEBSITE_ID=
|
||||||
|
|
||||||
|
# --- چتبات (دستیار هوشمند) ---
|
||||||
|
# LLM سازگار با OpenAI — اجباری برای پاسخگویی چتبات؛ خالی یعنی سرویس بالا میآید اما پاسخ نمیدهد
|
||||||
|
LLM_BASE_URL=
|
||||||
|
LLM_API_KEY=
|
||||||
|
LLM_MODEL=openai/gpt-oss-120b
|
||||||
|
# توکن محافظت از POST /chat/reindex — خالی یعنی فقط از شبکه داخلی
|
||||||
|
CHAT_REINDEX_TOKEN=
|
||||||
|
|
||||||
|
# --- مانیتورینگ (docker-compose.monitoring.yml) ---
|
||||||
|
GRAFANA_ADMIN_USER=admin
|
||||||
|
GRAFANA_ADMIN_PASSWORD=change-me-grafana-password
|
||||||
|
# هشدارها به بله — توکن بات (همان شکل تلگرام؛ از BotFather بله) و شناسه چت/گروه
|
||||||
|
# شناسه چت: به بات یک پیام بدهید سپس:
|
||||||
|
# curl "https://tapi.bale.ai/bot<توکن>/getUpdates"
|
||||||
|
ALERT_BOT_TOKEN=
|
||||||
|
ALERT_CHAT_ID=0
|
||||||
|
|
||||||
|
# --- عملیات ---
|
||||||
|
# نگهداری پشتیبانهای شبانه (روز) — scripts/backup.sh
|
||||||
|
RETENTION_DAYS=14
|
||||||
|
# انتشار پشتیبان خارج از سرور (rclone remote مثل storage:aramesh-backups) — خالی یعنی خاموش
|
||||||
|
OFFSITE_RCLONE_REMOTE=
|
||||||
|
|
||||||
# --- فقط برای docker-compose.prod.yml (حالت تولید با TLS) ---
|
# --- فقط برای docker-compose.prod.yml (حالت تولید با TLS) ---
|
||||||
# DOMAIN=aramesh.example
|
# DOMAIN=aramesh.example
|
||||||
# ACME_EMAIL=admin@aramesh.example
|
# ACME_EMAIL=admin@aramesh.example
|
||||||
|
# دامنه دوم (در صورت وجود) — مسیرها و گواهی TLS برای هر دو صادر میشود
|
||||||
|
# DOMAIN_ALIAS=aramesh.example.ir
|
||||||
|
|
||||||
|
# --- پیشنمایش خصوصی و انتشار (scripts/preview-mode.sh) ---
|
||||||
|
# «on» یعنی سایت آنلاین است اما پشت گیت ورود و بدون ایندکس؛ «off» یعنی انتشار عمومی.
|
||||||
|
# این مقادیر را دستی ویرایش نکنید — اسکریپت preview-mode.sh هماهنگشان میکند:
|
||||||
|
# bash scripts/preview-mode.sh on|off|status → bash scripts/stack.sh deploy-prod
|
||||||
|
# PREVIEW_MODE=on
|
||||||
|
# هش htpasswd کاربر مشترک پیشنمایش — اولین «on» خودش میسازد
|
||||||
|
# PREVIEW_AUTH_USERS=preview:$apr1$...
|
||||||
|
# فهرست میانافزارهای فعال؛ «on» پرش میکند و «off» خالیش میکند
|
||||||
|
# PREVIEW_MIDDLEWARES=preview-auth@docker,preview-noindex@docker
|
||||||
|
# در انتشار، دامنه دوم را به اصلی ریدایرکت میکند تا محتوای تکراری ساخته نشود
|
||||||
|
# ALIAS_REDIRECT_MIDDLEWARES=to-primary@docker
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# اسکریپتهای شل باید در لینوکس هم LF باشند — CRLF در bash میشکند
|
||||||
|
*.sh text eol=lf
|
||||||
|
*.yml text
|
||||||
|
*.yaml text
|
||||||
|
*.json text
|
||||||
|
Dockerfile text
|
||||||
|
*.py text
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
# گیت سرور self-hosted شما (Gitea Actions / Forgejo) همین سینتکس را اجرا میکند؛
|
||||||
|
# روی GitHub هم بیتغییر کار میکند.
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v5
|
||||||
|
with:
|
||||||
|
python-version: "3.13"
|
||||||
|
|
||||||
|
- name: Install backend deps
|
||||||
|
run: pip install -r backend/requirements.txt -r backend/requirements-dev.txt
|
||||||
|
|
||||||
|
- name: Run backend tests
|
||||||
|
working-directory: backend
|
||||||
|
run: pytest -q
|
||||||
|
|
||||||
|
- name: Set up Node
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: "20"
|
||||||
|
|
||||||
|
- name: Frontend typecheck/build
|
||||||
|
working-directory: frontend
|
||||||
|
run: |
|
||||||
|
npm ci
|
||||||
|
npm run build
|
||||||
|
|
||||||
|
images:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: tests
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Build images
|
||||||
|
run: docker compose -f docker-compose.yml build backend frontend chatbot
|
||||||
@@ -9,6 +9,12 @@ env/
|
|||||||
# Environment
|
# Environment
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# مانیتورینگ — کانفیگ رندرشدهٔ alertmanager (حاوی توکن بله است)
|
||||||
|
monitoring/alertmanager/alertmanager.rendered.yml
|
||||||
|
|
||||||
|
# تنظیمات محلی هر ماشین
|
||||||
|
docker-compose.override.yml
|
||||||
|
|
||||||
# پشتیبانگیری محلی
|
# پشتیبانگیری محلی
|
||||||
backups/
|
backups/
|
||||||
|
|
||||||
@@ -22,6 +28,7 @@ dist/
|
|||||||
.vscode/
|
.vscode/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
|
.zcode/
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
*.sqlite3
|
*.sqlite3
|
||||||
|
|||||||
@@ -3,15 +3,20 @@
|
|||||||
مونوریپوی سایت گروه ساختمانی آرامش شامل:
|
مونوریپوی سایت گروه ساختمانی آرامش شامل:
|
||||||
|
|
||||||
- **`frontend/`** — سایت React + Vite + Tailwind (تمام متنها و تصاویر از API خوانده میشود)
|
- **`frontend/`** — سایت React + Vite + Tailwind (تمام متنها و تصاویر از API خوانده میشود)
|
||||||
- **`backend/`** — بکاند FastAPI + SQLAlchemy 2 + PostgreSQL + JWT + MinIO
|
- **`backend/`** — بکاند FastAPI + SQLAlchemy 2 + PostgreSQL (pgvector) + JWT + MinIO
|
||||||
|
- **`chatbot/`** — دستیار هوشمند (FastAPI + LangGraph + RAG با pgvector، پاسخ استریمی)
|
||||||
|
- **مانیتورینگ و آنالیتیکس** — Prometheus/Grafana/Loki و Umami بهصورت لایههای compose
|
||||||
|
|
||||||
## معماری
|
## معماری
|
||||||
|
|
||||||
```
|
```
|
||||||
Traefik (پورت ۵۱۷۳ — تنها نقطه ورود)
|
Traefik (تنها نقطه ورود — 80/443 در تولید)
|
||||||
├── / → Caddy (فایلهای استاتیک SPA)
|
├── / → Caddy (فایلهای استاتیک SPA)
|
||||||
├── /api → FastAPI (بکاند)
|
├── /api → FastAPI (بکاند) + متریک /metrics فقط داخلی
|
||||||
└── /media → MinIO (باکت aramesh-media — تصاویر آپلودی)
|
├── /media → MinIO (باکت aramesh-media)
|
||||||
|
├── /chat → چتبات (SSE استریم پاسخ)
|
||||||
|
├── grafana.* → Grafana (مانیتورینگ — لایه monitoring)
|
||||||
|
└── analytics.* → Umami (آنالیتیکس محصول)
|
||||||
```
|
```
|
||||||
|
|
||||||
- **همه محتوای متنی سایت در PostgreSQL است** — هیچ متنی در کد فرانتاند هاردکد نشده؛
|
- **همه محتوای متنی سایت در PostgreSQL است** — هیچ متنی در کد فرانتاند هاردکد نشده؛
|
||||||
@@ -19,107 +24,213 @@ Traefik (پورت ۵۱۷۳ — تنها نقطه ورود)
|
|||||||
- **تصاویر آپلودی در MinIO** (Object Storage سازگار با S3) ذخیره میشوند و با مسیر نسبی
|
- **تصاویر آپلودی در MinIO** (Object Storage سازگار با S3) ذخیره میشوند و با مسیر نسبی
|
||||||
`/media/...` برگردانده میشوند — بکاند ذخیرهسازی بدون تغییر محتوای دیتابیس قابل تعویض است
|
`/media/...` برگردانده میشوند — بکاند ذخیرهسازی بدون تغییر محتوای دیتابیس قابل تعویض است
|
||||||
- مسیریابی همهچیز روی **Traefik** است (با Docker labels) — nginx در پروژه وجود ندارد
|
- مسیریابی همهچیز روی **Traefik** است (با Docker labels) — nginx در پروژه وجود ندارد
|
||||||
|
- شبکههای compose تفکیک شدهاند: `edge` (سرویسهای پشت Traefik) و `internal`
|
||||||
|
(دیتابیس/ذخیرهسازی) — فرانتاند هیچ راه مستقیمی به دیتابیس ندارد
|
||||||
|
- **لاگها**: json-file با سقف ۱۰MB×۳ برای هر سرویس؛ علاوه بر آن Promtail همه را به
|
||||||
|
Loki میفرستد تا در Grafana جستوجو شوند
|
||||||
|
|
||||||
## ساختار
|
## ساختار
|
||||||
|
|
||||||
```
|
```
|
||||||
├── frontend/ # سایت React
|
├── frontend/ # سایت React (بیلد روی هاست — ایمیج فقط سرو میکند)
|
||||||
│ ├── src/ # کد منبع (api.tsx = لایه اتصال به بکاند)
|
│ ├── src/ # کد منبع (api.tsx = لایه اتصال به بکاند)
|
||||||
│ ├── Dockerfile # بیلد Node + سرو استاتیک با Caddy
|
│ ├── Caddyfile # سرو استاتیک + کش immutable برای /assets/*
|
||||||
│ └── Caddyfile # فقط سرو فایلهای استاتیک (مسیریابی روی Traefik است)
|
│ └── Dockerfile # Caddy — بدون root
|
||||||
├── backend/ # بکاند FastAPI
|
├── backend/ # بکاند FastAPI
|
||||||
│ ├── app/ # کد منبع (آپلود تصویر: app/api/admin.py — سئوی سرور: app/seo_pages.py)
|
│ ├── app/ # کد منبع (آپلود: app/api/admin.py — سئو: app/seo_pages.py)
|
||||||
│ ├── tests/ # تستهای API (pytest)
|
│ ├── migrations/ # مهاجرتهای نسخهدار Alembic
|
||||||
│ ├── requirements.txt
|
│ ├── tests/ # تستهای API (pytest)
|
||||||
│ ├── Dockerfile
|
│ ├── alembic.ini
|
||||||
│ └── .env.example
|
│ └── Dockerfile # بدون root — خاموشی تمیز با exec
|
||||||
├── scripts/backup.sh # پشتیبانگیری دیتابیس و تصاویر
|
├── chatbot/ # دستیار هوشمند (RAG + pgvector)
|
||||||
├── docker-compose.yml # اجرای کامل با یک دستور
|
├── monitoring/ # پیکربندی Prometheus/Alertmanager/Loki/Promtail/Grafana
|
||||||
└── docker-compose.prod.yml # لایه تولید: TLS با Let's Encrypt
|
├── scripts/
|
||||||
|
│ ├── stack.sh # ابزار اصلی عملیات — deploy/logs/health/backup/restore/update
|
||||||
|
│ ├── backup.sh # پشتیبان کامل (DB + تصاویر) با چکسام و گزینه انتشار بیرونی
|
||||||
|
│ ├── restore.sh # بازگردانی پشتیبان دیتابیس
|
||||||
|
│ ├── gen-env.sh # ساخت .env با رمزهای قوی تصادفی
|
||||||
|
│ ├── audit.sh # ممیزی آسیبپذیری وابستگیها و ایمیجها
|
||||||
|
│ ├── migrate-media.py # انتقال یکباره تصاویر CDN خارجی به MinIO
|
||||||
|
│ └── ci.sh # تست + بیلد ایمیج (محلی یا هر CI)
|
||||||
|
├── docker-compose.yml # استک اصلی (شامل چتبات و Umami)
|
||||||
|
├── docker-compose.monitoring.yml # لایه مانیتورینگ
|
||||||
|
├── docker-compose.prod.yml # لایه تولید: TLS، دامنه، سختسازی پورتها
|
||||||
|
└── docker-compose.override.yml # تنظیمات محلی این ماشین (در گیت نیست)
|
||||||
```
|
```
|
||||||
|
|
||||||
## اجرا با Docker (سادهترین راه)
|
## اجرا با Docker
|
||||||
|
|
||||||
پیشنیاز: Docker Desktop. بیلد فرانتاند روی هاست انجام میشود (کانتینر فقط فایلهای
|
پیشنیاز: Docker Desktop. بیلد فرانتاند روی هاست انجام میشود (کانتینر فقط فایلهای
|
||||||
`dist/` را سرو میکند)، پس اولین بار باید یکبار `npm run build` بزنید:
|
`dist/` را سرو میکند) — `scripts/stack.sh` همین را به یک دستور تبدیل میکند:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cp .env.example .env # مقادیر change-me را عوض کنید — بدون .env استک بالا نمیآید
|
cp .env.example .env # یا: bash scripts/gen-env.sh (رمزهای قوی تصادفی)
|
||||||
cd frontend && npm install && npm run build && cd ..
|
cd frontend && npm install && cd ..
|
||||||
docker compose up -d --build
|
bash scripts/stack.sh deploy # بیلد فرانت + بیلد و بالا آوردن استک + انتظار برای سلامت
|
||||||
```
|
```
|
||||||
|
|
||||||
سایت: http://localhost:5173 — ورود مدیریت: `/admin/login` (کاربر/رمز از `ADMIN_*` در `.env`)
|
- سایت: http://localhost:18081 (پورت از `TRAEFIK_HTTP_PORT` در `.env`) — ورود مدیریت: `/admin/login`
|
||||||
|
- دستورهای عملیات: `bash scripts/stack.sh` — `logs`، `ps`، `health`، `backup`، `restore`، `update`، `seed-force`
|
||||||
- مستندات API: http://localhost:8001/docs (با `docker-compose.override.yml` محلی)
|
- مستندات API: http://localhost:19001/docs (با `docker-compose.override.yml` محلی)
|
||||||
- کنسول MinIO: http://localhost:9001 — کاربر/رمز از متغیرهای `MINIO_ROOT_*` در `.env`
|
- کنسول MinIO: http://localhost:9001 — کاربر/رمز از متغیرهای `MINIO_ROOT_*` در `.env`
|
||||||
- دیتابیس PostgreSQL و آبجکتاستور MinIO هر دو در کانتینر جدا با volume دائمی
|
- دیتابیس PostgreSQL (تصویر pgvector)، آبجکتاستور MinIO و کش مدلهای چتبات هر یک
|
||||||
- بکاند هنگام شروع، خودش جداول را میسازد و **فقط جدولهای خالی** را seed میکند —
|
در کانتینر جدا با volume دائمی
|
||||||
ارتقای نسخه دادههای فعلی را از بین نمیبرد
|
- بکاند هنگام شروع، جداول پایه را میسازد، **مهاجرتهای Alembic** را اعمال میکند و
|
||||||
- در اولین اجرا، سرویس `minio-setup` باکت `aramesh-media` را میسازد، خواندن عمومی را
|
**فقط جدولهای خالی** را seed میکند — ارتقای نسخه دادههای فعلی را از بین نمیبرد
|
||||||
فعال میکند و تصاویر داخل `seed-media/` را در باکت میریزد — پس یک کلون تازه هم
|
- `minio-setup` باکت `aramesh-media` را میسازد و فقط **وقتی باکت خالی است** تصاویر
|
||||||
همان تصاویر سایت را دارد
|
`seed-media/` را میریزد — `db-init` هم دیتابیسهای `umami` و `aramesh_chatbot` را
|
||||||
|
روی همان نمونهٔ Postgres میسازد
|
||||||
|
|
||||||
> نکته برای تازهواردها: هیچ فایل رمزدار در گیت نیست. فقط `.env.example` را کپی کنید؛
|
> نکته برای تازهواردها: هیچ فایل رمزدار در گیت نیست. فقط `.env.example` را کپی کنید؛
|
||||||
> فایلهای `.env`/`env` در `.gitignore` هستند و هرگز commit نشوند.
|
> فایلهای `.env`/`env` در `.gitignore` هستند و هرگز commit نشوند.
|
||||||
|
>
|
||||||
|
> پایهٔ API فرانتاند در زمان بیلد از `VITE_API_URL` در `.env` خوانده میشود —
|
||||||
|
> در توسعه ویندوزیِ این ماشین `VITE_API_URL=http://localhost:19001` بگذارید.
|
||||||
|
|
||||||
## انتشار واقعی (TLS)
|
## انتشار واقعی (TLS)
|
||||||
|
|
||||||
برای بالا آوردن با HTTPS روی دامنه واقعی، لایه `docker-compose.prod.yml` را هم سوار کنید:
|
پیشنیاز: رکوردهای DNS (`example.com`، `analytics.example.com` و اگر مانیتورینگ فعال
|
||||||
|
است `grafana.example.com`) به این سرور، سپس در `.env`: `DOMAIN`، `ACME_EMAIL` و
|
||||||
|
`SITE_URL=https://example.com`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# در .env: DOMAIN=example.com و ACME_EMAIL=you@example.com
|
bash scripts/stack.sh deploy-prod
|
||||||
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build
|
|
||||||
```
|
```
|
||||||
|
|
||||||
- Traefik گواهی Let's Encrypt میگیرد و تمدید میکند؛ همه درخواستهای HTTP به HTTPS هدایت میشوند
|
- Traefik گواهی Let's Encrypt میگیرد و تمدید میکند؛ همه درخواستهای HTTP به HTTPS هدایت میشوند
|
||||||
- در این حالت `SITE_URL=https://example.com` را هم در `.env` بگذارید تا sitemap و آدرسهای OG مطلق شوند
|
- **پورتهای مدیریتی MinIO (۹۰۰۱ کنسول و ۱۹۰۰۰ API) در تولید فقط روی `127.0.0.1`
|
||||||
|
بسته میشوند** — دسترسی از راه دور فقط با تونل SSH:
|
||||||
|
`ssh -L 9001:127.0.0.1:9001 server`
|
||||||
|
- همه روترهای websecure هدرهای امنیتی میگیرند (HSTS + nosniff)
|
||||||
|
|
||||||
## اجرای محلی (توسعه)
|
### پیشنمایش خصوصی (کار روی سایت، قبل از انتشار عمومی)
|
||||||
|
|
||||||
### بکاند
|
سایت روی اینترنت بالا میآید و تیم با همان پنل مدیریت محتوا را ویرایش میکند
|
||||||
|
(ویرایش درجای ایمیل/تلفن/متنها و نوشتن مقاله)، اما **نه عموم به آن دسترسی دارند و
|
||||||
|
نه گوگل چیزی ایندکس میکند**. دو لایه محافظت:
|
||||||
|
|
||||||
|
1. **گیت ورود Traefik (Basic Auth)** — همه مسیرها (سایت، API، تصاویر و چت) پشت یک
|
||||||
|
نام کاربری/رمز مشترکاند؛ بدون آن فقط ۴۰۱ برمیگردد
|
||||||
|
2. **noindex** — هدر `X-Robots-Tag` روی همه پاسخها + `robots.txt` مسدودکننده +
|
||||||
|
نقشه سایت خالی؛ حتی اگر گیت برداشته شود، گوگل چیزی ذخیره نمیکند
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd backend
|
bash scripts/preview-mode.sh on # فعالسازی (بار اول نام کاربری/رمز میسازد و چاپ میکند)
|
||||||
python -m venv .venv
|
bash scripts/stack.sh deploy-prod # اعمال
|
||||||
.venv\Scripts\activate # ویندوز — در لینوکس: source .venv/bin/activate
|
bash scripts/preview-mode.sh status # وضعیت فعلی
|
||||||
pip install -r requirements.txt
|
|
||||||
|
|
||||||
# فایل .env از روی .env.example بسازید و DATABASE_URL را تنظیم کنید
|
|
||||||
python -m app.seed # ساخت جداول + محتوای اولیه
|
|
||||||
uvicorn app.main:app --reload
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### فرانتاند
|
- دامنه اصلی (`DOMAIN`) و دامنه دوم (`DOMAIN_ALIAS`) هر دو سرو میشوند و هر دو گیت دارند
|
||||||
|
- برای دادن دسترسی به کارفرما، همان نام کاربری/رمز مشترک را بدهید
|
||||||
|
- رمز مشترک در `.env` (`PREVIEW_AUTH_USERS`) میماند — فقط یکبار چاپ میشود
|
||||||
|
|
||||||
|
### انتشار عمومی (برداشتن پیشنمایش)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd frontend
|
bash scripts/preview-mode.sh off # حذف گیت ورود + اجازه ایندکس
|
||||||
npm install
|
bash scripts/stack.sh deploy-prod
|
||||||
npm run dev
|
|
||||||
```
|
```
|
||||||
|
|
||||||
در حالت توسعه، Vite درخواستهای `/api` و `/media` را به Traefik (پورت ۵۱۷۳) پروکسی میکند؛
|
بعد از این مرحله:
|
||||||
پس استک Docker باید در حال اجرا باشد. آدرس بکاند در `frontend/src/config.ts` از
|
|
||||||
`VITE_API_URL` خوانده میشود — پیشفرض خالی یعنی درخواستهای هممبدأ.
|
1. دامنه دوم بهصورت ۳۰۱ دائمی به دامنه اصلی ریدایرکت میشود (جلوگیری از محتوای تکراری)
|
||||||
|
2. در Search Console دامنه را تأیید و `https://DOMAIN/sitemap.xml` را ثبت کنید
|
||||||
|
3. اگر پیشتر چیزی ایندکس شده بود، حذفش را از همان کنسول درخواست کنید
|
||||||
|
|
||||||
|
### چکلیست رفتن به تولید
|
||||||
|
|
||||||
|
1. `bash scripts/gen-env.sh` — رمزهای قوی جدید (رمزهای `.env` توسعه را کپی نکنید)
|
||||||
|
2. DNS: رکوردهای دامنه و زیردامنهها + باز بودن پورتهای ۸۰/۴۴۳ در فایروال
|
||||||
|
3. `DOMAIN`، `DOMAIN_ALIAS` (دامنه دوم)، `ACME_EMAIL`، `SITE_URL` را در `.env` تنظیم کنید
|
||||||
|
4. `bash scripts/preview-mode.sh on` — اولین انتشار بهصورت پیشنمایش خصوصی میرود؛
|
||||||
|
محتوا را روی سایت آنلاین نهایی کنید و بعد `preview-mode.sh off` + deploy مجدد
|
||||||
|
5. `python scripts/migrate-media.py` — انتقال یکباره تصاویر CDN خارجی به MinIO
|
||||||
|
6. پشتیبانگیری خودکار: `docker compose --profile ops up -d db-backup` (پشتیبان ساعتی)
|
||||||
|
+ `scripts/backup.sh` شبانه با cron/Task Scheduler
|
||||||
|
7. یک تمرین بازگردانی: `bash scripts/restore.sh backups/db-<زمان>.sql.gz` روی محیط آزمایشی
|
||||||
|
8. مانیتورینگ (بخش پایین) و سپس چتبات/آنالیتیکس را با متغیرهای LLM و Umami فعال کنید
|
||||||
|
|
||||||
|
## مانیتورینگ (Prometheus + Grafana + Loki)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bash scripts/stack.sh monitoring
|
||||||
|
# در تولید (سه فایل با هم):
|
||||||
|
docker compose -f docker-compose.yml -f docker-compose.monitoring.yml -f docker-compose.prod.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
- داشبورد: `grafana.localhost` (توسعه) یا `grafana.example.com` (تولید — برچسبهای
|
||||||
|
روتر آن را در `docker-compose.override.yml` سرور بگذارید؛ نمونه در انتهای
|
||||||
|
`docker-compose.prod.yml`) — کاربر/رمز از `GRAFANA_ADMIN_*` در `.env`
|
||||||
|
- دو داشبورد آمادهٔ «زیرساخت» و «اپلیکیشن» بهصورت code-provisioned سوار میشوند
|
||||||
|
- **هشدارها به بله**: در `.env` مقدار `ALERT_BOT_TOKEN` (بات بله — همان شکل API تلگرام،
|
||||||
|
از BotFather بله) و `ALERT_CHAT_ID` را بگذارید (شناسه چت: به بات یک پیام بدهید سپس
|
||||||
|
`curl "https://tapi.bale.ai/bot<توکن>/getUpdates"`) — قواعد آماده در
|
||||||
|
`monitoring/prometheus/alerts.yml`:
|
||||||
|
پایینبودن هر سرویس، شکست بررسی عمق سلامت، پرشدن دیسک، نرخ خطای 5xx، انقضای گواهی TLS،
|
||||||
|
**کهنهشدن پشتیبان** (سنجهٔ `backup_success_time` از `scripts/backup.sh`) و...
|
||||||
|
- **لاگها در Grafana**: Promtail لاگ همهٔ کانتینرهای استک را به Loki میفرستد —
|
||||||
|
برای خطاهای بکاند از داشبورد «اپلیکیشن» پنل لاگ را ببینید
|
||||||
|
- **سلامت واقعی**: `/api/health` اتصال DB و MinIO را واقعاً میآزماید (503 در خرابی)؛
|
||||||
|
پروب blackbox هم همین را از بیرون و نشانی `SITE_URL` را عمومی چک میکند
|
||||||
|
- مصرف منابع مجموع لایه حدود ۱ تا ۱.۵ گیگابایت RAM است؛ حذف هر سرویس بقیه را نمیشکند
|
||||||
|
|
||||||
|
## آنالیتیکس محصول (Umami)
|
||||||
|
|
||||||
|
استک شامل سرویس Umami است؛ داشبورد: `analytics.localhost` (توسعه) یا
|
||||||
|
`analytics.example.com` (تولید). بدون کوکی — بدون نیاز به بنر رضایت.
|
||||||
|
|
||||||
|
راهاندازی (یکبار):
|
||||||
|
|
||||||
|
1. استک را بالا بیاورید و به `analytics.localhost` وارد شوید (پیشفرض `admin`/`umami`
|
||||||
|
— بلافاصله رمز را عوض کنید)
|
||||||
|
2. یک وبسایت با دامنهٔ سایت بسازید؛ «شناسه وبسایت» را در `.env` بگذارید:
|
||||||
|
`VITE_UMAMI_WEBSITE_ID=...` و آدرس اسکریپت: `VITE_UMAMI_SRC=https://analytics.example.com/script.js`
|
||||||
|
3. `bash scripts/stack.sh deploy` — اسکریپت ردیاب در `index.html` تزریق میشود
|
||||||
|
|
||||||
|
رخدادهای سفارشی که خودکار ارسال میشوند: ارسال فرم تماس (`contact-submit`)، عضویت
|
||||||
|
خبرنامه (`newsletter-signup`)، کلیک تلفن (`phone-click`)، باز کردن چتبات
|
||||||
|
(`chat-open`)، پرسش از چتبات (`chat-question`)، تغییر زبان (`lang-switch`) —
|
||||||
|
سرچشمه: `frontend/src/analytics.ts`.
|
||||||
|
|
||||||
|
## چتبات (دستیار هوشمند)
|
||||||
|
|
||||||
|
- RAG روی محتوای همان دیتابیس سایت: امبدینگ MiniLM چندزبانه + BM25 + رنکر — بردارها
|
||||||
|
در دیتابیس جدا `aramesh_chatbot` (pgvector) روی همان نمونهٔ Postgres
|
||||||
|
- مسیرها: `GET /chat/healthz`، `GET /chat/suggestions`، `GET /chat/stream` (SSE) —
|
||||||
|
رابط کاربری: ویجت شناور پایین صفحه (`ChatWidget.tsx`)
|
||||||
|
- راهاندازی: `LLM_BASE_URL`/`LLM_API_KEY`/`LLM_MODEL` در `.env` (هر LLM سازگار با
|
||||||
|
OpenAI) — بدون آن سرویس بالا میآید اما پاسخ نمیدهد
|
||||||
|
- اولین اجرا مدلها را دانلود میکند (~۵۰۰MB در حجم `chatbot_hf`) — کند است، طبیعی است
|
||||||
|
- ایندکس هر ۱۰ دقیقه خودش با تغییرهای محتوا همگام میشود؛ همگامسازی فوری با
|
||||||
|
`POST /chat/reindex` و هدر `x-reindex-token` (از `CHAT_REINDEX_TOKEN`)
|
||||||
|
|
||||||
## امکانات بکاند
|
## امکانات بکاند
|
||||||
|
|
||||||
- **API عمومی**: برند، منو، هیرو، خدمات، پروژهها، مقالات، نظرات، سوالات متداول، تیم،
|
- **API عمومی**: برند، منو، هیرو، خدمات، پروژهها، مقالات، نظرات، سوالات متداول، تیم،
|
||||||
تایملاین، آمار، دفاتر، مدلهای سرمایهگذاری و... — همه در دیتابیس
|
تایملاین، آمار، دفاتر، مدلهای سرمایهگذاری و... — همه در دیتابیس
|
||||||
- **فرم تماس** `POST /api/contact` و **خبرنامه** `POST /api/newsletter`
|
- **فرم تماس** `POST /api/contact` و **خبرنامه** `POST /api/newsletter` — هر دو با
|
||||||
- **پنل مدیریت** با JWT: ورود `POST /api/auth/login` (پیشفرض: `admin` / `aramesh123`)
|
محدودسازی نرخ (تماس: ۵ در ۱۵ دقیقه، خبرنامه: ۳ در ساعت برای هر IP) + دامنهٔ پنهان
|
||||||
و سپس CRUD کامل همه محتواها در `/api/admin/...`
|
(honeypot) ضدربات + میانافزار محدودسازی سیلاب روی Traefik
|
||||||
|
- **پنل مدیریت** با JWT: ورود `POST /api/auth/login` (مقادیر از `ADMIN_*` در `.env` —
|
||||||
|
هیچ رمز پیشفرضی در کد وجود ندارد) و سپس CRUD کامل همه محتواها در `/api/admin/...`
|
||||||
- **تغییر رمز** `POST /api/auth/change-password` — از نوار مدیریت پایین سایت (دکمه «رمز») هم در دسترس است
|
- **تغییر رمز** `POST /api/auth/change-password` — از نوار مدیریت پایین سایت (دکمه «رمز») هم در دسترس است
|
||||||
- **محدودسازی ورود**: بعد از ۵ ورود ناموفق در ۱۵ دقیقه از یک IP، درخواست با 429 رد میشود
|
- **محدودسازی ورود**: بعد از ۵ ورود ناموفق در ۱۵ دقیقه از یک IP، درخواست با 429 رد میشود
|
||||||
|
(IP واقعی کلاینت با `--proxy-headers` از پشت Traefik درست خوانده میشود)
|
||||||
- **آپلود تصویر** `POST /api/admin/upload` (فقط توکن ادمین، حداکثر ۱۰MB) — فایل در MinIO
|
- **آپلود تصویر** `POST /api/admin/upload` (فقط توکن ادمین، حداکثر ۱۰MB) — فایل در MinIO
|
||||||
ذخیره و URL نسبی `/media/...` برمیگردد
|
ذخیره و URL نسبی `/media/...` برمیگردد؛ در همان لحظه تا ۲۰۰۰ پیکسل کوچک و دوباره
|
||||||
|
فشرده میشود (Pillow) تا سرعت صفحه حفظ شود
|
||||||
|
- **خروجی CSV** از نوار مدیریت: پیامهای تماس و مشترکان خبرنامه (با BOM برای Excel)
|
||||||
- **ویرایش جزئی** `PATCH /api/admin/{resource}/{id}` — فقط فیلدهای ارسالشده عوض میشوند
|
- **ویرایش جزئی** `PATCH /api/admin/{resource}/{id}` — فقط فیلدهای ارسالشده عوض میشوند
|
||||||
- **ترتیببندی** `POST /api/admin/{resource}/reorder` — با `{"ids": [...]}`
|
- **ترتیببندی** `POST /api/admin/{resource}/reorder` — با `{"ids": [...]}`
|
||||||
- همه محتوا در یک درخواست: `GET /api/site` (مقالههای پیشنویس در آن نمیآیند)
|
- همه محتوا در یک درخواست: `GET /api/site` (مقالههای پیشنویس در آن نمیآیند)
|
||||||
- **تستها**: `cd backend && pip install -r requirements-dev.txt && pytest tests`
|
- **مهاجرتهای اسکیما**: Alembic (`backend/migrations/`) — هر تغییر اسکیمای جدید باید
|
||||||
|
revision شود: `cd backend && alembic revision --autogenerate -m "..."` و پس از بازبینی
|
||||||
|
commit؛ در استارت کانتینر خودکار `upgrade head` اجرا میشود
|
||||||
|
- **تستها**: `cd backend && .venv/Scripts/activate && pytest tests` (۲۴ تست — شامل
|
||||||
|
honeypot، محدودسازی نرخ و پاکسازی)
|
||||||
|
|
||||||
> در محیط واقعی حتماً `SECRET_KEY`، `ADMIN_PASSWORD` و `MINIO_ROOT_PASSWORD` را تغییر دهید
|
> در محیط واقعی حتماً `SECRET_KEY`، `ADMIN_PASSWORD` و `MINIO_ROOT_PASSWORD` را قوی
|
||||||
> (از `.env`) و بعد از اولین ورود، رمز را از نوار مدیریت عوض کنید.
|
> بگیرید (`bash scripts/gen-env.sh`) و بعد از اولین ورود، رمز را از نوار مدیریت عوض کنید.
|
||||||
|
|
||||||
## ویرایش زنده سایت (دوقلوی دیجیتال)
|
## ویرایش زنده سایت (دوقلوی دیجیتال)
|
||||||
|
|
||||||
@@ -157,7 +268,9 @@ npm run dev
|
|||||||
**فرمت ذخیره**: بدنه مقاله یک سند ساختاریافته TipTap (JSON) در PostgreSQL است — نه HTML خام.
|
**فرمت ذخیره**: بدنه مقاله یک سند ساختاریافته TipTap (JSON) در PostgreSQL است — نه HTML خام.
|
||||||
صفحه مقاله آن را با رندر استاتیک TipTap به HTML تبدیل میکند، پس فقط تگهای شناختهشده
|
صفحه مقاله آن را با رندر استاتیک TipTap به HTML تبدیل میکند، پس فقط تگهای شناختهشده
|
||||||
(پاراگراف، تیتر، لیست، نقلقول، تصویر، لینک) تولید میشوند. مقالات قدیمی (لیست پاراگراف)
|
(پاراگراف، تیتر، لیست، نقلقول، تصویر، لینک) تولید میشوند. مقالات قدیمی (لیست پاراگراف)
|
||||||
در اولین اجرا بهصورت خودکار به این فرمت تبدیل میشوند.
|
در اولین اجرا بهصورت خودکار به این فرمت تبدیل میشوند. در زمان ذخیره، آدرسهای
|
||||||
|
`href/src` سند در سمت سرور پاکسازی میشوند (`app/sanitize.py`) تا اسکیمهای خطرناک
|
||||||
|
مثل `javascript:` هرگز ذخیره نشوند.
|
||||||
|
|
||||||
## سئو
|
## سئو
|
||||||
|
|
||||||
@@ -179,24 +292,39 @@ npm run dev
|
|||||||
دیتابیس داخل آن تزریق میکند (`app/seo_pages.py`) — بازدیدکنندههای عادی همان SPA
|
دیتابیس داخل آن تزریق میکند (`app/seo_pages.py`) — بازدیدکنندههای عادی همان SPA
|
||||||
همیشگی را میبینند
|
همیشگی را میبینند
|
||||||
|
|
||||||
## پشتیبانگیری
|
## پشتیبانگیری و بازگردانی
|
||||||
|
|
||||||
همه محتوای سایت در PostgreSQL و تصاویر در MinIO است — هر دو را با یک دستور پشتیبان بگیرید:
|
همه محتوای سایت در PostgreSQL و تصاویر در MinIO است:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bash scripts/backup.sh # در backups/ — نگهداری پیشفرض ۱۴ روز
|
bash scripts/stack.sh backup # پشتیبان کامل: DB + تصاویر MinIO
|
||||||
|
docker compose --profile ops up -d db-backup # پشتیبان ساعتی خودکار DB (بدون cron میزبان)
|
||||||
```
|
```
|
||||||
|
|
||||||
- خروجی: `backups/db-<زمان>.sql.gz` (فشرده) و `backups/images-<زمان>/` (آینه باکت)
|
- خروجی: `backups/db-<زمان>.sql.gz` (فشرده + فایل `.sha256`) و `backups/images-<زمان>/`
|
||||||
- زمانبندی با cron یا Task Scheduler: نمونه دستور داخل خود اسکریپت هست
|
- اسکریپت پشتیبان، خروجی خالی/شکسته را خطا میگیرد، سنجهٔ تازگی برای هشدار Grafana
|
||||||
- بازیابی دیتابیس: `gunzip -c backups/db-<زمان>.sql.gz | docker compose exec -T db psql -U aramesh arameshplaza`
|
مینویسد و اگر `OFFSITE_RCLONE_REMOTE` در `.env` تنظیم باشد، پشتیبان را به حافظهٔ
|
||||||
|
دیگری هم کپی میکند (rclone)
|
||||||
|
- **بازگردانی**: `bash scripts/stack.sh restore backups/db-<زمان>.sql.gz`
|
||||||
|
(احتیاط: محتوای فعلی جایگزین میشود — قبل از انتشار واقعی یکبار تمرین کنید!)
|
||||||
|
- زمانبندی پشتیبان کامل با cron یا Task Scheduler: نمونه دستور داخل خود اسکریپت هست
|
||||||
|
|
||||||
|
## تست و CI
|
||||||
|
|
||||||
|
- `bash scripts/ci.sh` — تستهای pytest + بیلد ایمیجها + ممیزی وابستگیها
|
||||||
|
- `bash scripts/audit.sh` — pip-audit / npm audit / trivy (هر کدام نصب بود)
|
||||||
|
- `.github/workflows/ci.yml` — همین مراحل؛ سینتکس GitHub Actions روی Gitea Actions/
|
||||||
|
Forgejo هم اجرا میشود
|
||||||
|
|
||||||
## نکات امنیتی
|
## نکات امنیتی
|
||||||
|
|
||||||
- مقادیر محرمانه در `.env` ریشه هستند (در `.gitignore`) و در `docker-compose.yml`
|
- مقادیر محرمانه در `.env` ریشه هستند (در `.gitignore`) و در compose با سینتکس
|
||||||
با سینتکس `${VAR:?}` اجباری شدهاند — بدون `.env` استک بالا نمیآید
|
`${VAR:?}` اجباری شدهاند — بدون `.env` استک بالا نمیآید؛ هیچ رمز پیشفرضی در کد نیست
|
||||||
- رمز عبور ادمین با bcrypt هش میشود و از نوار مدیریت پایین سایت (دکمه «رمز») قابل تعویض است
|
- رمز عبور ادمین با bcrypt هش میشود و از نوار مدیریت پایین سایت (دکمه «رمز») قابل تعویض است
|
||||||
- ورود با محدودیت تلاش محافظت میشود: ۵ ورود ناموفق در ۱۵ دقیقه از هر IP → خطای 429
|
- توکنهای JWT ۸ ساعت اعتبار دارند (stateless — تغییر `SECRET_KEY` همه را خارج میکند)
|
||||||
- توکنهای JWT امضا دارند و مدت اعتبارشان قابل تنظیم است
|
- همهٔ کانتینرها **بدون root** اجرا میشوند؛ کانتکست بیلد هر ایمیج با `.dockerignore`
|
||||||
|
باریک است و لاگها سقف دارند
|
||||||
|
- فرمهای عمومی ضدرباتاند (honeypot + محدودسازی نرخ در بکاند + سقف سیلاب در Traefik)
|
||||||
|
- کنسول MinIO و API مستقیمش در تولید فقط از `127.0.0.1` در دسترساند (تونل SSH)
|
||||||
- تصاویر باکت MinIO عمومی (فقط خواندن) هستند؛ آپلود فقط با توکن ادمین ممکن است
|
- تصاویر باکت MinIO عمومی (فقط خواندن) هستند؛ آپلود فقط با توکن ادمین ممکن است
|
||||||
- مقالههای پیشنویس در هیچ انتهای عمومی نمیآیند و در HTML تزریقشده سئو هم افشا نمیشوند
|
- مقالههای پیشنویس در هیچ انتهای عمومی نمیآیند و در HTML تزریقشده سئو هم افشا نمیشوند
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# کانتکست بیلد باریک — .venv و کشهای تست محلی جایی در تصویر ندارند
|
||||||
|
.venv/
|
||||||
|
venv/
|
||||||
|
tests/
|
||||||
|
.pytest_cache/
|
||||||
|
__pycache__/
|
||||||
|
**/__pycache__/
|
||||||
|
*.pyc
|
||||||
|
README.md
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
Dockerfile
|
||||||
|
.dockerignore
|
||||||
+12
-3
@@ -1,3 +1,4 @@
|
|||||||
|
# بکاند FastAPI — تصویر باریک و بدون root
|
||||||
FROM python:3.13-slim
|
FROM python:3.13-slim
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
@@ -9,8 +10,16 @@ COPY requirements.txt .
|
|||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
COPY app ./app
|
COPY app ./app
|
||||||
|
COPY alembic.ini ./
|
||||||
|
COPY migrations ./migrations
|
||||||
|
|
||||||
|
# کاربر بدون دسترسی root — برنامه چیزی روی دیسک محلی نمینویسد
|
||||||
|
RUN useradd --system --create-home appuser \
|
||||||
|
&& chown -R appuser:appuser /app
|
||||||
|
USER appuser
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
# ساخت جداول پایه، مهاجرتهای Alembic و seed (بدون حذف دادههای موجود) و سپس
|
||||||
# ساخت جداول و seed (بدون حذف دادههای موجود) و سپس اجرای سرور
|
# اجرای سرور با exec — تا SIGTERM (docker stop) مستقیم به uvicorn برسد و خاموشی
|
||||||
CMD ["sh", "-c", "python -m app.seed && uvicorn app.main:app --host 0.0.0.0 --port 8000"]
|
# تمیز انجام شود؛ --proxy-headers برای دیدن IP واقعی کلاینت پشت Traefik
|
||||||
|
CMD ["sh", "-c", "python -m app.db_init && alembic upgrade head && python -m app.seed && exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips='*'"]
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
[alembic]
|
||||||
|
script_location = migrations
|
||||||
|
prepend_sys_path = .
|
||||||
|
|
||||||
|
# آدرس دیتابیس از DATABASE_URL (محیط) یا تنظیمات برنامه خوانده میشود — env.py
|
||||||
|
sqlalchemy.url =
|
||||||
|
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
||||||
+198
-16
@@ -1,10 +1,19 @@
|
|||||||
"""انتهای پنل مدیریت — همه مسیرها نیازمند توکن JWT معتبر هستند."""
|
"""انتهای پنل مدیریت — همه مسیرها نیازمند توکن JWT معتبر هستند."""
|
||||||
|
|
||||||
|
import csv
|
||||||
|
import ipaddress
|
||||||
|
import io
|
||||||
|
import re
|
||||||
|
import socket
|
||||||
|
import urllib.request
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import boto3
|
import boto3
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile, status
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, Request, Response, UploadFile, status
|
||||||
|
from PIL import Image, ImageOps
|
||||||
|
from pydantic import BaseModel
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -263,10 +272,69 @@ ALLOWED_IMAGE_TYPES = {
|
|||||||
"image/avif": "avif",
|
"image/avif": "avif",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# بهینهسازی در زمان آپلود — ابعاد بزرگ و فشردهسازی ضعیف، اصلیترین عامل کندی سایت است
|
||||||
|
MAX_DIMENSION = 2000
|
||||||
|
REENCODE_TYPES = {"image/jpeg", "image/png", "image/webp"}
|
||||||
|
|
||||||
|
|
||||||
|
def _optimize_image(data: bytes, content_type: str) -> tuple[bytes, str, int, int]:
|
||||||
|
"""تصویر را به حداکثر ۲۰۰۰ پیکسل محدود و دوباره فشرده میکند؛
|
||||||
|
اگر خروجی بزرگتر از اصلی شد، همان اصلی برگردد. (gif/avif دستنخورده)"""
|
||||||
|
img = Image.open(io.BytesIO(data))
|
||||||
|
img = ImageOps.exif_transpose(img)
|
||||||
|
orig_w, orig_h = img.size
|
||||||
|
if max(img.size) > MAX_DIMENSION:
|
||||||
|
img.thumbnail((MAX_DIMENSION, MAX_DIMENSION), Image.LANCZOS)
|
||||||
|
buf = io.BytesIO()
|
||||||
|
if content_type == "image/jpeg":
|
||||||
|
if img.mode not in ("RGB", "L"):
|
||||||
|
img = img.convert("RGB")
|
||||||
|
img.save(buf, "JPEG", quality=82, optimize=True, progressive=True)
|
||||||
|
elif content_type == "image/png":
|
||||||
|
img.save(buf, "PNG", optimize=True)
|
||||||
|
else:
|
||||||
|
img.save(buf, "WEBP", quality=82, method=4)
|
||||||
|
optimized = buf.getvalue()
|
||||||
|
if len(optimized) < len(data):
|
||||||
|
return optimized, content_type, *img.size
|
||||||
|
return data, content_type, orig_w, orig_h
|
||||||
|
|
||||||
|
|
||||||
|
def _readable_key(name: str, ext: str) -> str:
|
||||||
|
"""کلید ذخیرهسازی خوانا از نام دلخواه/نام فایل — «۱۴۰۳/elahieh-tower-a1b2c3.jpg».
|
||||||
|
|
||||||
|
نامگذاریها ایمن میشوند: نویسههای ناامن و جداکننده مسیر حذف، فاصلهها خط تیره،
|
||||||
|
طول محدود؛ خالی بودن نام → شناسه تصادفی مثل قبل. پسوند ۶ نویسهای تضمین یکتایی است.
|
||||||
|
"""
|
||||||
|
slug = re.sub(r"[^\w-ۿ\- ]+", "", name, flags=re.UNICODE).strip()
|
||||||
|
slug = re.sub(r"[\s_]+", "-", slug).strip("-")[:80]
|
||||||
|
unique = uuid.uuid4().hex[:6]
|
||||||
|
base = f"{slug}-{unique}" if slug else uuid.uuid4().hex
|
||||||
|
return f"{datetime.now(timezone.utc):%Y/%m}/{base}.{ext}"
|
||||||
|
|
||||||
|
|
||||||
|
def _store_image(data: bytes, content_type: str, name: str) -> str:
|
||||||
|
"""ذخیره تصویر در Object Storage با کلید خوانا — URL نسبی برمیگردد."""
|
||||||
|
ext = ALLOWED_IMAGE_TYPES[content_type]
|
||||||
|
key = _readable_key(name, ext)
|
||||||
|
s3 = boto3.client(
|
||||||
|
"s3",
|
||||||
|
endpoint_url=settings.s3_endpoint,
|
||||||
|
aws_access_key_id=settings.s3_access_key,
|
||||||
|
aws_secret_access_key=settings.s3_secret_key,
|
||||||
|
region_name="us-east-1",
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
s3.put_object(Bucket=settings.s3_bucket, Key=key, Body=data, ContentType=content_type)
|
||||||
|
except Exception:
|
||||||
|
raise HTTPException(status_code=502, detail="ذخیره فایل در Object Storage ناموفق بود")
|
||||||
|
return f"{settings.media_public_base}/{key}"
|
||||||
|
|
||||||
|
|
||||||
@router.post("/upload")
|
@router.post("/upload")
|
||||||
async def upload_media(
|
async def upload_media(
|
||||||
file: UploadFile = File(...),
|
file: UploadFile = File(...),
|
||||||
|
name: str = Form(""),
|
||||||
_admin: models.AdminUser = Depends(get_current_admin),
|
_admin: models.AdminUser = Depends(get_current_admin),
|
||||||
):
|
):
|
||||||
content_type = (file.content_type or "").lower()
|
content_type = (file.content_type or "").lower()
|
||||||
@@ -287,29 +355,143 @@ async def upload_media(
|
|||||||
detail=f"حجم فایل بیش از {settings.media_max_upload_mb} مگابایت است",
|
detail=f"حجم فایل بیش از {settings.media_max_upload_mb} مگابایت است",
|
||||||
)
|
)
|
||||||
|
|
||||||
now = datetime.now(timezone.utc)
|
width = height = None
|
||||||
key = f"{now:%Y/%m}/{uuid.uuid4().hex}.{ext}"
|
if content_type in REENCODE_TYPES:
|
||||||
|
try:
|
||||||
|
data, content_type, width, height = _optimize_image(data, content_type)
|
||||||
|
except Exception:
|
||||||
|
# تصویر خراب/ناشناخته — همان داده اصلی ذخیره میشود
|
||||||
|
pass
|
||||||
|
|
||||||
s3 = boto3.client(
|
# نام خوانا: نام ارسالی فرم، وگرنه نام اصلی فایل بدون پسوند
|
||||||
"s3",
|
display_name = (name or "").strip() or (file.filename or "").rsplit(".", 1)[0]
|
||||||
endpoint_url=settings.s3_endpoint,
|
url = _store_image(data, content_type, display_name)
|
||||||
aws_access_key_id=settings.s3_access_key,
|
|
||||||
aws_secret_access_key=settings.s3_secret_key,
|
|
||||||
region_name="us-east-1",
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
s3.put_object(Bucket=settings.s3_bucket, Key=key, Body=data, ContentType=content_type)
|
|
||||||
except Exception:
|
|
||||||
raise HTTPException(status_code=502, detail="ذخیره فایل در Object Storage ناموفق بود")
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"url": f"{settings.media_public_base}/{key}",
|
"url": url,
|
||||||
"key": key,
|
"key": url.removeprefix(f"{settings.media_public_base}/"),
|
||||||
"size": len(data),
|
"size": len(data),
|
||||||
"content_type": content_type,
|
"content_type": content_type,
|
||||||
|
"width": width,
|
||||||
|
"height": height,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- درونریزی تصویر از نشانی خارجی (چسباندن از Outline و مانند آن) ----------
|
||||||
|
|
||||||
|
|
||||||
|
class ImportImageIn(BaseModel):
|
||||||
|
url: str
|
||||||
|
|
||||||
|
|
||||||
|
def _assert_public_http(url: str) -> None:
|
||||||
|
"""فقط http/https به مقاصد عمومی — جلوگیری از SSRF به شبکه داخلی."""
|
||||||
|
parsed = urlparse(url)
|
||||||
|
if parsed.scheme not in ("http", "https") or not parsed.hostname:
|
||||||
|
raise HTTPException(status_code=400, detail="نشانی تصویر معتبر نیست")
|
||||||
|
try:
|
||||||
|
infos = socket.getaddrinfo(parsed.hostname, None)
|
||||||
|
except socket.gaierror:
|
||||||
|
raise HTTPException(status_code=400, detail="میزبان تصویر پیدا نشد")
|
||||||
|
for info in infos:
|
||||||
|
ip = ipaddress.ip_address(info[4][0])
|
||||||
|
if not ip.is_global:
|
||||||
|
raise HTTPException(status_code=400, detail="نشانی تصویر به میزبان داخلی اشاره میکند")
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/import-image")
|
||||||
|
async def import_image(
|
||||||
|
payload: ImportImageIn,
|
||||||
|
_admin: models.AdminUser = Depends(get_current_admin),
|
||||||
|
):
|
||||||
|
"""دانلود تصویر از نشانی خارجی، بهینهسازی و ذخیره در Object Storage خودِ سایت —
|
||||||
|
تصاویر چسباندهشده از Outline/مستندات خارجی به میزبانی سایت منتقل میشوند."""
|
||||||
|
_assert_public_http(payload.url)
|
||||||
|
|
||||||
|
max_bytes = settings.media_max_upload_mb * 1024 * 1024
|
||||||
|
request = urllib.request.Request(
|
||||||
|
payload.url, headers={"User-Agent": "ArameshSite/1.0 (+image import)"}
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(request, timeout=15) as response:
|
||||||
|
content_type = (response.headers.get("Content-Type") or "").split(";")[0].strip().lower()
|
||||||
|
if content_type not in ALLOWED_IMAGE_TYPES:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=415,
|
||||||
|
detail="فرمت تصویر پشتیبانی نمیشود (مجاز: jpg، png، webp، gif، avif)",
|
||||||
|
)
|
||||||
|
data = response.read(max_bytes + 1)
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
raise HTTPException(status_code=502, detail="دانلود تصویر از نشانی ناموفق بود")
|
||||||
|
if len(data) == 0:
|
||||||
|
raise HTTPException(status_code=400, detail="تصویر خالی است")
|
||||||
|
if len(data) > max_bytes:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=413,
|
||||||
|
detail=f"حجم تصویر بیش از {settings.media_max_upload_mb} مگابایت است",
|
||||||
|
)
|
||||||
|
|
||||||
|
width = height = None
|
||||||
|
if content_type in REENCODE_TYPES:
|
||||||
|
try:
|
||||||
|
data, content_type, width, height = _optimize_image(data, content_type)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# نام خوانا از مسیر نشانی — بدون پارامترهای کوئری
|
||||||
|
url_name = urlparse(payload.url).path.rsplit("/", 1)[-1].rsplit(".", 1)[0]
|
||||||
|
media_url = _store_image(data, content_type, url_name)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"url": media_url,
|
||||||
|
"key": media_url.removeprefix(f"{settings.media_public_base}/"),
|
||||||
|
"size": len(data),
|
||||||
|
"content_type": content_type,
|
||||||
|
"width": width,
|
||||||
|
"height": height,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- خروجی CSV (برای تیم فروش) ----------
|
||||||
|
|
||||||
|
def _csv_response(filename: str, header: list[str], rows: list[list]) -> Response:
|
||||||
|
buf = io.StringIO()
|
||||||
|
writer = csv.writer(buf)
|
||||||
|
writer.writerow(header)
|
||||||
|
writer.writerows(rows)
|
||||||
|
stamp = datetime.now(timezone.utc).strftime("%Y%m%d")
|
||||||
|
return Response(
|
||||||
|
# BOM ابتدای فایل — تا Excel متن فارسی را درست باز کند
|
||||||
|
content="\ufeff" + buf.getvalue(),
|
||||||
|
media_type="text/csv; charset=utf-8",
|
||||||
|
headers={"Content-Disposition": f'attachment; filename="{filename}-{stamp}.csv"'},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/messages/export.csv")
|
||||||
|
def export_messages(db: Session = Depends(get_db)):
|
||||||
|
rows = [
|
||||||
|
[m.id, m.name, m.phone, m.note, "yes" if m.is_read else "no", m.created_at.isoformat()]
|
||||||
|
for m in db.query(models.ContactMessage).order_by(models.ContactMessage.created_at.desc()).all()
|
||||||
|
]
|
||||||
|
return _csv_response(
|
||||||
|
"contact-messages", ["id", "name", "phone", "note", "is_read", "created_at"], rows
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/subscribers/export.csv")
|
||||||
|
def export_subscribers(db: Session = Depends(get_db)):
|
||||||
|
rows = [
|
||||||
|
[s.id, s.email, "yes" if s.is_active else "no", s.created_at.isoformat()]
|
||||||
|
for s in db.query(models.Subscriber).order_by(models.Subscriber.created_at.desc()).all()
|
||||||
|
]
|
||||||
|
return _csv_response(
|
||||||
|
"newsletter-subscribers", ["id", "email", "is_active", "created_at"], rows
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# پیامهای تماس
|
# پیامهای تماس
|
||||||
@router.get("/messages", response_model=list[ContactMessageOut])
|
@router.get("/messages", response_model=list[ContactMessageOut])
|
||||||
def list_messages(unread_only: bool = False, db: Session = Depends(get_db)):
|
def list_messages(unread_only: bool = False, db: Session = Depends(get_db)):
|
||||||
|
|||||||
@@ -1,17 +1,48 @@
|
|||||||
"""انتهای فرمها — ثبت پیام تماس و عضویت در خبرنامه."""
|
"""انتهای فرمها — ثبت پیام تماس و عضویت در خبرنامه.
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
هر فرم یک دامنهٔ پنهان (honeypot) دارد: رباتها همهٔ فیلدها را پُر میکنند؛
|
||||||
|
در آن حالت پاسخ موفقِ قلابی برمیگردد و چیزی ذخیره نمیشود.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, Request
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ..database import get_db
|
|
||||||
from .. import models
|
from .. import models
|
||||||
|
from ..database import get_db
|
||||||
|
from ..rate_limit import contact_limiter, newsletter_limiter
|
||||||
from ..schemas import ContactIn, ContactMessageOut, NewsletterIn, SubscriberOut
|
from ..schemas import ContactIn, ContactMessageOut, NewsletterIn, SubscriberOut
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_contact(payload: ContactIn) -> ContactMessageOut:
|
||||||
|
"""پاسخ موفق بدون ذخیره — تا ربات متوجه تله نشود."""
|
||||||
|
return ContactMessageOut(
|
||||||
|
id=0,
|
||||||
|
name=payload.name.strip(),
|
||||||
|
phone=payload.phone.strip(),
|
||||||
|
note="",
|
||||||
|
is_read=True,
|
||||||
|
created_at=datetime.now(timezone.utc),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_subscriber(payload: NewsletterIn) -> SubscriberOut:
|
||||||
|
return SubscriberOut(
|
||||||
|
id=0,
|
||||||
|
email=str(payload.email).lower(),
|
||||||
|
is_active=True,
|
||||||
|
created_at=datetime.now(timezone.utc),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/contact", response_model=ContactMessageOut, status_code=201)
|
@router.post("/contact", response_model=ContactMessageOut, status_code=201)
|
||||||
def submit_contact(payload: ContactIn, db: Session = Depends(get_db)):
|
def submit_contact(payload: ContactIn, request: Request, db: Session = Depends(get_db)):
|
||||||
|
contact_limiter.hit(request)
|
||||||
|
if payload.website:
|
||||||
|
return _fake_contact(payload)
|
||||||
message = models.ContactMessage(
|
message = models.ContactMessage(
|
||||||
name=payload.name.strip(),
|
name=payload.name.strip(),
|
||||||
phone=payload.phone.strip(),
|
phone=payload.phone.strip(),
|
||||||
@@ -24,7 +55,11 @@ def submit_contact(payload: ContactIn, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/newsletter", response_model=SubscriberOut, status_code=201)
|
@router.post("/newsletter", response_model=SubscriberOut, status_code=201)
|
||||||
def subscribe(payload: NewsletterIn, db: Session = Depends(get_db)):
|
def subscribe(payload: NewsletterIn, request: Request, db: Session = Depends(get_db)):
|
||||||
|
newsletter_limiter.hit(request)
|
||||||
|
if payload.website:
|
||||||
|
return _fake_subscriber(payload)
|
||||||
|
|
||||||
email = str(payload.email).lower()
|
email = str(payload.email).lower()
|
||||||
subscriber = db.query(models.Subscriber).filter(models.Subscriber.email == email).first()
|
subscriber = db.query(models.Subscriber).filter(models.Subscriber.email == email).first()
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
"""انتهای عمومی — خواندن محتوای سایت بدون نیاز به احراز هویت."""
|
"""انتهای عمومی — خواندن محتوای سایت بدون نیاز به احراز هویت."""
|
||||||
|
|
||||||
|
import boto3
|
||||||
|
from botocore.config import Config as BotoConfig
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from sqlalchemy import text
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ..config import settings
|
||||||
from ..database import get_db
|
from ..database import get_db
|
||||||
from .. import models
|
from .. import models
|
||||||
from ..schemas import (
|
from ..schemas import (
|
||||||
@@ -22,9 +27,44 @@ from ..schemas import (
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/live")
|
||||||
|
def live():
|
||||||
|
"""زندهبودن پروسه — بدون وابستگی؛ برای restart gate استفاده نمیشود."""
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/health")
|
@router.get("/health")
|
||||||
def health():
|
def health(db: Session = Depends(get_db)):
|
||||||
return {"status": "ok", "app": "Aramesh Building Group API"}
|
"""بررسی عمق سلامت — اتصال DB و MinIO واقعاً آزموده میشود؛
|
||||||
|
در صورت خرابی هر وابستگی، 503 با جزئیات برمیگردد."""
|
||||||
|
checks: dict[str, str] = {}
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.execute(text("SELECT 1"))
|
||||||
|
checks["database"] = "ok"
|
||||||
|
except Exception:
|
||||||
|
checks["database"] = "error"
|
||||||
|
|
||||||
|
try:
|
||||||
|
s3 = boto3.client(
|
||||||
|
"s3",
|
||||||
|
endpoint_url=settings.s3_endpoint,
|
||||||
|
aws_access_key_id=settings.s3_access_key,
|
||||||
|
aws_secret_access_key=settings.s3_secret_key,
|
||||||
|
region_name="us-east-1",
|
||||||
|
config=BotoConfig(connect_timeout=2, read_timeout=2, retries={"max_attempts": 1}),
|
||||||
|
)
|
||||||
|
s3.head_bucket(Bucket=settings.s3_bucket)
|
||||||
|
checks["storage"] = "ok"
|
||||||
|
except Exception:
|
||||||
|
checks["storage"] = "error"
|
||||||
|
|
||||||
|
if any(state != "ok" for state in checks.values()):
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=503,
|
||||||
|
content={"status": "degraded", "checks": checks},
|
||||||
|
)
|
||||||
|
return {"status": "ok", "app": "Aramesh Building Group API", "checks": checks}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/brand", response_model=BrandOut)
|
@router.get("/brand", response_model=BrandOut)
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ class Settings(BaseSettings):
|
|||||||
database_url: str = "postgresql+psycopg://postgres:postgres@localhost:5432/arameshplaza_api"
|
database_url: str = "postgresql+psycopg://postgres:postgres@localhost:5432/arameshplaza_api"
|
||||||
secret_key: str = "dev-secret-change-me-in-production"
|
secret_key: str = "dev-secret-change-me-in-production"
|
||||||
algorithm: str = "HS256"
|
algorithm: str = "HS256"
|
||||||
access_token_expire_minutes: int = 1440
|
# ۸ ساعت — توکن stateless است و امکان ابطال ندارد؛ عمر کوتاهتر ریسک نشت را کم میکند
|
||||||
|
access_token_expire_minutes: int = 480
|
||||||
cors_origins: list[str] = ["*"]
|
cors_origins: list[str] = ["*"]
|
||||||
admin_username: str = "admin"
|
admin_username: str = "admin"
|
||||||
admin_password: str = "aramesh123"
|
admin_password: str = "aramesh123"
|
||||||
@@ -31,6 +32,10 @@ class Settings(BaseSettings):
|
|||||||
site_url: str = ""
|
site_url: str = ""
|
||||||
# مسیر بیلد فرانتاند (شامل index.html) — برای سرو HTML با متای سئو در صفحات جزئیات
|
# مسیر بیلد فرانتاند (شامل index.html) — برای سرو HTML با متای سئو در صفحات جزئیات
|
||||||
frontend_dist: str = "../frontend/dist"
|
frontend_dist: str = "../frontend/dist"
|
||||||
|
# پیشنمایش خصوصی: سایت روی اینترنت است اما نه عمومی و نه ایندکسشدنی.
|
||||||
|
# گیت اصلی ورود در Traefik است (Basic Auth)؛ این پرچم لایه دوم است:
|
||||||
|
# هدر noindex روی همه پاسخها + robots.txt مسدودکننده.
|
||||||
|
preview_mode: bool = False
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
"""ساخت جداول پایه (baseline) — پیش از مهاجرتهای Alembic.
|
||||||
|
|
||||||
|
روی دیتابیس تازه، همهٔ جدولها از مدلهای فعلی ساخته میشوند؛ روی دیتابیس موجود
|
||||||
|
بیاثر است (create_all جدولهای موجود را دست نمیزند). از اینجا به بعد تغییرات
|
||||||
|
اسکیما فقط با revisionهای Alembic اعمال میشوند.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from . import models # noqa: F401 — ثبت مدلها
|
||||||
|
from .database import Base, engine
|
||||||
|
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
+23
-2
@@ -1,15 +1,17 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.middleware.gzip import GZipMiddleware
|
||||||
|
|
||||||
from . import models # noqa: F401 — ثبت مدلها پیش از ساخت جداول
|
from . import models # noqa: F401 — ثبت مدلها پیش از ساخت جداول
|
||||||
from .api import admin, forms, public
|
from .api import admin, forms, public
|
||||||
from .config import settings
|
from .config import settings
|
||||||
from .database import Base, engine
|
from .database import Base, engine
|
||||||
from .migrations import run_all as run_migrations
|
|
||||||
from .seo_pages import router as seo_router
|
from .seo_pages import router as seo_router
|
||||||
|
|
||||||
|
# ساخت جداول پایه — تغییرات بعدی اسکیما فقط با revisionهای Alembic اعمال میشوند
|
||||||
|
# (مigrations/versions). اجرای اینجا برای اجرای مستقیم uvicorn در محیط توسعه است؛
|
||||||
|
# در کانتینر، CMD همین کار را قبل از alembic upgrade head انجام میدهد.
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
run_migrations()
|
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="Aramesh Building Group API",
|
title="Aramesh Building Group API",
|
||||||
@@ -29,6 +31,19 @@ app.add_middleware(
|
|||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
# پاسخهای JSON /api — فشردهسازی وقتی بدنه قابلتوجه است
|
||||||
|
app.add_middleware(GZipMiddleware, minimum_size=1024)
|
||||||
|
|
||||||
|
|
||||||
|
# پیشنمایش خصوصی — لایه دوم جلوگیری از ایندکس (لایه اول، گیت ورود Traefik است).
|
||||||
|
# روی همه پاسخها هدر noindex مینشیند تا حتی در صورت حذف گیت، گوگل چیزی ذخیره نکند.
|
||||||
|
# همیشه ثبت میشود و پرچم در زمان هر درخواست خوانده میشود (خاموشکردن انتشار = فقط تغییر .env).
|
||||||
|
@app.middleware("http")
|
||||||
|
async def _preview_noindex(request, call_next):
|
||||||
|
response = await call_next(request)
|
||||||
|
if settings.preview_mode:
|
||||||
|
response.headers["X-Robots-Tag"] = "noindex, nofollow, noarchive"
|
||||||
|
return response
|
||||||
|
|
||||||
app.include_router(public.router, prefix="/api", tags=["عمومی"])
|
app.include_router(public.router, prefix="/api", tags=["عمومی"])
|
||||||
app.include_router(forms.router, prefix="/api", tags=["فرمها"])
|
app.include_router(forms.router, prefix="/api", tags=["فرمها"])
|
||||||
@@ -37,6 +52,12 @@ app.include_router(admin.router, prefix="/api/admin", tags=["پنل مدیریت
|
|||||||
# robots.txt، sitemap.xml و HTML صفحات وبلاگ/پروژه با متای سئو — در ریشه دامنه
|
# robots.txt، sitemap.xml و HTML صفحات وبلاگ/پروژه با متای سئو — در ریشه دامنه
|
||||||
app.include_router(seo_router, tags=["سئو"])
|
app.include_router(seo_router, tags=["سئو"])
|
||||||
|
|
||||||
|
# متریکهای Prometheus — فقط Prometheus در شبکه داخلی اسکرپ میکند
|
||||||
|
# (Traefik مسیر /metrics را نمیشناسد، پس از بیرون در دسترس نیست)
|
||||||
|
from prometheus_fastapi_instrumentator import Instrumentator # noqa: E402
|
||||||
|
|
||||||
|
Instrumentator().instrument(app).expose(app, endpoint="/metrics", include_in_schema=False)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def root():
|
def root():
|
||||||
|
|||||||
@@ -145,6 +145,17 @@ def run_ddl_migrations() -> None:
|
|||||||
conn.execute(
|
conn.execute(
|
||||||
text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS seo_keyphrase VARCHAR(128) NOT NULL DEFAULT ''")
|
text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS seo_keyphrase VARCHAR(128) NOT NULL DEFAULT ''")
|
||||||
)
|
)
|
||||||
|
# متادیتای تصویر شاخص مقاله + پرسشوپاسخهای پایان مقاله (سئو)
|
||||||
|
conn.execute(
|
||||||
|
text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS image_alt VARCHAR(256) NOT NULL DEFAULT ''")
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS image_title VARCHAR(256) NOT NULL DEFAULT ''")
|
||||||
|
)
|
||||||
|
conn.execute(text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS faqs JSON"))
|
||||||
|
conn.execute(text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS faqs_en JSON"))
|
||||||
|
# سطرهای موجود ستون تازه را NULL میگیرند — آرایه خالی یعنی «بدون پرسشوپاسخ»
|
||||||
|
conn.execute(text("UPDATE articles SET faqs = '[]'::json WHERE faqs IS NULL"))
|
||||||
# ستونهای انگلیسی — رشتهها با پیشفرض خالی، JSON بدون پیشفرض (NULL = بدون ترجمه)
|
# ستونهای انگلیسی — رشتهها با پیشفرض خالی، JSON بدون پیشفرض (NULL = بدون ترجمه)
|
||||||
for table, column, col_type in EN_COLUMNS:
|
for table, column, col_type in EN_COLUMNS:
|
||||||
if col_type == "JSON":
|
if col_type == "JSON":
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import JSON, Boolean, DateTime, Integer, String, Text
|
from sqlalchemy import JSON, Boolean, DateTime, Integer, String, Text
|
||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
@@ -95,12 +96,18 @@ class Article(Base):
|
|||||||
category: Mapped[str] = mapped_column(String(64), default="")
|
category: Mapped[str] = mapped_column(String(64), default="")
|
||||||
category_en: Mapped[str] = mapped_column(String(64), default="")
|
category_en: Mapped[str] = mapped_column(String(64), default="")
|
||||||
image: Mapped[str] = mapped_column(Text, default="")
|
image: Mapped[str] = mapped_column(Text, default="")
|
||||||
|
# متن جایگزین و عنوان تصویر شاخص — سئو و دسترسپذیری
|
||||||
|
image_alt: Mapped[str] = mapped_column(String(256), default="")
|
||||||
|
image_title: Mapped[str] = mapped_column(String(256), default="")
|
||||||
read: Mapped[str] = mapped_column(String(32), default="")
|
read: Mapped[str] = mapped_column(String(32), default="")
|
||||||
read_en: Mapped[str] = mapped_column(String(32), default="")
|
read_en: Mapped[str] = mapped_column(String(32), default="")
|
||||||
excerpt: Mapped[str] = mapped_column(Text, default="")
|
excerpt: Mapped[str] = mapped_column(Text, default="")
|
||||||
excerpt_en: Mapped[str] = mapped_column(Text, default="")
|
excerpt_en: Mapped[str] = mapped_column(Text, default="")
|
||||||
body: Mapped[list] = mapped_column(JSON, default=list) # سند TipTap (تاریخی: لیست پاراگراف)
|
body: Mapped[list] = mapped_column(JSON, default=list) # سند TipTap (تاریخی: لیست پاراگراف)
|
||||||
body_en: Mapped[dict] = mapped_column(JSON, default=dict) # سند TipTap انگلیسی
|
body_en: Mapped[dict] = mapped_column(JSON, default=dict) # سند TipTap انگلیسی
|
||||||
|
# پرسش و پاسخهای پایان مقاله — [{question, answer}]؛ برای نتایج غنی سئو (FAQPage)
|
||||||
|
faqs: Mapped[list] = mapped_column(JSON, default=list)
|
||||||
|
faqs_en: Mapped[Optional[list]] = mapped_column(JSON) # NULL یعنی بدون ترجمه — برگشت به فارسی
|
||||||
# draft | published — پیشنویس فقط برای ادمین دیده میشود
|
# draft | published — پیشنویس فقط برای ادمین دیده میشود
|
||||||
status: Mapped[str] = mapped_column(String(16), default="published")
|
status: Mapped[str] = mapped_column(String(16), default="published")
|
||||||
# کلمه کلیدی هدف چکلیست سئو — راهنمای نوشتن، در سایت نمایش داده نمیشود
|
# کلمه کلیدی هدف چکلیست سئو — راهنمای نوشتن، در سایت نمایش داده نمیشود
|
||||||
|
|||||||
+66
-15
@@ -1,38 +1,74 @@
|
|||||||
"""محدودسازی تلاش ورود — جلوگیری از حمله brute-force روی /api/auth/login.
|
"""محدودسازی نرخ درخواست — پنجرهٔ لغزان درونحافظهای.
|
||||||
|
|
||||||
پیشفرض: حداکثر ۵ ورود ناموفق در هر ۱۵ دقیقه برای هر IP؛ ورود موفق شمارنده را پاک میکند.
|
برای ورود ادمین (بر پایه تلاش ناموفق) و فرمهای عمومی (بر پایه هر ارسال)
|
||||||
شمارنده در حافظه همین پروسه است — با ریاستارت بکاند پاک میشود و همین کافی است.
|
استفاده میشود. شمارندهها در حافظهٔ همین پروسهاند — با ریاستارت پاک میشوند؛
|
||||||
|
به همین دلیل uvicorn تککارگر نگه داشته شده است.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from fastapi import HTTPException, Request, status
|
from fastapi import HTTPException, Request, status
|
||||||
|
|
||||||
MAX_FAILED_ATTEMPTS = 5
|
|
||||||
WINDOW_SECONDS = 15 * 60
|
|
||||||
|
|
||||||
_failed: dict[str, list[float]] = {}
|
|
||||||
|
|
||||||
|
|
||||||
def client_ip(request: Request) -> str:
|
def client_ip(request: Request) -> str:
|
||||||
|
"""IP واقعی کلاینت — پشت Traefik از X-Forwarded-For خوانده میشود
|
||||||
|
(uvicorn با --proxy-headers هم client.host را درست میکند)."""
|
||||||
forwarded = request.headers.get("x-forwarded-for")
|
forwarded = request.headers.get("x-forwarded-for")
|
||||||
if forwarded:
|
if forwarded:
|
||||||
return forwarded.split(",")[0].strip()
|
return forwarded.split(",")[0].strip()
|
||||||
return request.client.host if request.client else "unknown"
|
return request.client.host if request.client else "unknown"
|
||||||
|
|
||||||
|
|
||||||
def _prune(key: str, now: float) -> list[float]:
|
class RateLimiter:
|
||||||
recent = [t for t in _failed.get(key, []) if now - t < WINDOW_SECONDS]
|
"""پنجرهٔ لغزان بر پایه کلید (عملاً IP) — رویدادها پس از پنجره منقضی میشوند."""
|
||||||
_failed[key] = recent
|
|
||||||
return recent
|
def __init__(self, max_events: int, window_seconds: int, message: str):
|
||||||
|
self.max_events = max_events
|
||||||
|
self.window_seconds = window_seconds
|
||||||
|
self.message = message
|
||||||
|
self._events: dict[str, list[float]] = {}
|
||||||
|
|
||||||
|
def _recent(self, key: str, now: float) -> list[float]:
|
||||||
|
recent = [t for t in self._events.get(key, []) if now - t < self.window_seconds]
|
||||||
|
self._events[key] = recent
|
||||||
|
return recent
|
||||||
|
|
||||||
|
def _reject(self, recent: list[float]) -> None:
|
||||||
|
now = time.monotonic()
|
||||||
|
retry_after = max(1, int(self.window_seconds - (now - recent[0])))
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||||
|
detail=self.message,
|
||||||
|
headers={"Retry-After": str(retry_after)},
|
||||||
|
)
|
||||||
|
|
||||||
|
def hit(self, request: Request) -> None:
|
||||||
|
"""یک رویداد ثبت میکند؛ اگر سهمیه کلید پر باشد 429 برمیگرداند."""
|
||||||
|
key = client_ip(request)
|
||||||
|
now = time.monotonic()
|
||||||
|
recent = self._recent(key, now)
|
||||||
|
if len(recent) >= self.max_events:
|
||||||
|
self._reject(recent)
|
||||||
|
self._events.setdefault(key, []).append(now)
|
||||||
|
|
||||||
|
def clear(self, request: Request) -> None:
|
||||||
|
self._events.pop(client_ip(request), None)
|
||||||
|
|
||||||
|
|
||||||
|
# ورود ادمین: ۵ تلاش ناموفق در ۱۵ دقیقه — فقط شکستها شمرده میشوند
|
||||||
|
_failed: dict[str, list[float]] = {}
|
||||||
|
LOGIN_MAX_FAILED_ATTEMPTS = 5
|
||||||
|
LOGIN_WINDOW_SECONDS = 15 * 60
|
||||||
|
|
||||||
|
|
||||||
def ensure_allowed(request: Request) -> None:
|
def ensure_allowed(request: Request) -> None:
|
||||||
"""اگر این IP سهمیه تلاش ناموفقش پر شده باشد، درخواست با 429 رد میشود."""
|
"""اگر این IP سهمیه تلاش ناموفقش پر شده باشد، درخواست با 429 رد میشود."""
|
||||||
key = client_ip(request)
|
key = client_ip(request)
|
||||||
recent = _prune(key, time.monotonic())
|
now = time.monotonic()
|
||||||
if len(recent) >= MAX_FAILED_ATTEMPTS:
|
recent = [t for t in _failed.get(key, []) if now - t < LOGIN_WINDOW_SECONDS]
|
||||||
retry_after = max(1, int(WINDOW_SECONDS - (time.monotonic() - recent[0])))
|
_failed[key] = recent
|
||||||
|
if len(recent) >= LOGIN_MAX_FAILED_ATTEMPTS:
|
||||||
|
retry_after = max(1, int(LOGIN_WINDOW_SECONDS - (now - recent[0])))
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||||
detail="تلاشهای ناموفق زیاد است — بعداً دوباره امتحان کنید",
|
detail="تلاشهای ناموفق زیاد است — بعداً دوباره امتحان کنید",
|
||||||
@@ -47,3 +83,18 @@ def record_failure(request: Request) -> None:
|
|||||||
def clear(request: Request) -> None:
|
def clear(request: Request) -> None:
|
||||||
"""با ورود موفق، تاریخچه تلاشهای ناموفق IP پاک میشود."""
|
"""با ورود موفق، تاریخچه تلاشهای ناموفق IP پاک میشود."""
|
||||||
_failed.pop(client_ip(request), None)
|
_failed.pop(client_ip(request), None)
|
||||||
|
|
||||||
|
|
||||||
|
# فرم تماس: ۵ ارسال در ۱۵ دقیقه برای هر IP
|
||||||
|
contact_limiter = RateLimiter(
|
||||||
|
max_events=5,
|
||||||
|
window_seconds=15 * 60,
|
||||||
|
message="تعداد پیامهای ارسالی زیاد است — کمی بعد دوباره امتحان کنید",
|
||||||
|
)
|
||||||
|
|
||||||
|
# خبرنامه: ۳ عضویت در ساعت برای هر IP
|
||||||
|
newsletter_limiter = RateLimiter(
|
||||||
|
max_events=3,
|
||||||
|
window_seconds=60 * 60,
|
||||||
|
message="تعداد درخواستها زیاد است — کمی بعد دوباره امتحان کنید",
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
"""پاکسازی ورودیهای غنی در زمان ذخیره.
|
||||||
|
|
||||||
|
بدنه مقالهها سند TipTap (ProseMirror) است و فرانتاند همان را با
|
||||||
|
renderToHTMLString به HTML تبدیل و مستقیم تزریق میکند؛ متن گرهها در آن
|
||||||
|
مسیر escape میشود، اما آدرسهای href/src عیناً وارد HTML میشوند — پس
|
||||||
|
اسکیمهای خطرناک (javascript: و data: و ...) باید در سمت سرور حذف شوند.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
# اسکیمهای مجاز برای href/src — مسیرهای نسبی و لنگر هم بیاسکیماند و میگذرند
|
||||||
|
SAFE_URL_SCHEMES = {"http", "https", "mailto", "tel"}
|
||||||
|
URL_ATTRS = ("href", "src")
|
||||||
|
|
||||||
|
|
||||||
|
def _clean_url(value: Any) -> str | None:
|
||||||
|
"""آدرس امن را برمیگرداند؛ در غیر این صورت None (مشخصه حذف میشود)."""
|
||||||
|
if not isinstance(value, str):
|
||||||
|
return value if value is not None else None
|
||||||
|
stripped = value.strip().replace("\n", "").replace("\r", "").replace("\t", "")
|
||||||
|
if not stripped:
|
||||||
|
return None
|
||||||
|
parsed = urlparse(stripped)
|
||||||
|
if parsed.scheme and parsed.scheme.lower() not in SAFE_URL_SCHEMES:
|
||||||
|
return None
|
||||||
|
return stripped
|
||||||
|
|
||||||
|
|
||||||
|
def _walk(node: Any) -> Any:
|
||||||
|
if isinstance(node, list):
|
||||||
|
return [_walk(item) for item in node]
|
||||||
|
if not isinstance(node, dict):
|
||||||
|
return node
|
||||||
|
cleaned: dict[str, Any] = {}
|
||||||
|
for key, value in node.items():
|
||||||
|
if key == "attrs" and isinstance(value, dict):
|
||||||
|
attrs = dict(value)
|
||||||
|
for attr in URL_ATTRS:
|
||||||
|
if attr in attrs:
|
||||||
|
cleaned_url = _clean_url(attrs[attr])
|
||||||
|
if cleaned_url is None:
|
||||||
|
attrs.pop(attr, None)
|
||||||
|
else:
|
||||||
|
attrs[attr] = cleaned_url
|
||||||
|
cleaned[key] = attrs
|
||||||
|
else:
|
||||||
|
cleaned[key] = _walk(value)
|
||||||
|
return cleaned
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_doc(doc: Any) -> Any:
|
||||||
|
"""سند TipTap را درجا پاک میکند (سایر ساختارها دستنخورده برمیگردند)."""
|
||||||
|
if not isinstance(doc, dict):
|
||||||
|
return doc
|
||||||
|
return _walk(doc)
|
||||||
+22
-2
@@ -3,6 +3,8 @@ from typing import Literal, Optional
|
|||||||
|
|
||||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field, create_model, field_validator
|
from pydantic import BaseModel, ConfigDict, EmailStr, Field, create_model, field_validator
|
||||||
|
|
||||||
|
from .sanitize import sanitize_doc
|
||||||
|
|
||||||
# ---------- پایه ----------
|
# ---------- پایه ----------
|
||||||
|
|
||||||
|
|
||||||
@@ -75,12 +77,17 @@ class ArticleOut(ORMModel):
|
|||||||
category: str
|
category: str
|
||||||
category_en: str = ""
|
category_en: str = ""
|
||||||
image: str
|
image: str
|
||||||
|
image_alt: str = ""
|
||||||
|
image_title: str = ""
|
||||||
read: str
|
read: str
|
||||||
read_en: str = ""
|
read_en: str = ""
|
||||||
excerpt: str
|
excerpt: str
|
||||||
excerpt_en: str = ""
|
excerpt_en: str = ""
|
||||||
body: dict
|
body: dict
|
||||||
body_en: Optional[dict] = None
|
body_en: Optional[dict] = None
|
||||||
|
# پرسشوپاسخهای پایان مقاله — [{question, answer}]؛ سطرهای قدیمی ممکن است NULL باشند
|
||||||
|
faqs: Optional[list[dict]] = None
|
||||||
|
faqs_en: Optional[list[dict]] = None
|
||||||
status: str
|
status: str
|
||||||
seo_keyphrase: str
|
seo_keyphrase: str
|
||||||
|
|
||||||
@@ -386,10 +393,14 @@ class ContactIn(BaseModel):
|
|||||||
name: str = Field(min_length=3, max_length=128)
|
name: str = Field(min_length=3, max_length=128)
|
||||||
phone: str = Field(pattern=r"^[\d۰-۹+\-\s]{8,15}$")
|
phone: str = Field(pattern=r"^[\d۰-۹+\-\s]{8,15}$")
|
||||||
note: str = Field(default="", max_length=2000)
|
note: str = Field(default="", max_length=2000)
|
||||||
|
# honeypot — فیلد پنهان فرم؛ کاربر واقعی هرگز آن را پر نمیکند
|
||||||
|
website: str = Field(default="", max_length=128)
|
||||||
|
|
||||||
|
|
||||||
class NewsletterIn(BaseModel):
|
class NewsletterIn(BaseModel):
|
||||||
email: EmailStr
|
email: EmailStr
|
||||||
|
# honeypot — فیلد پنهان فرم؛ کاربر واقعی هرگز آن را پر نمیکند
|
||||||
|
website: str = Field(default="", max_length=128)
|
||||||
|
|
||||||
|
|
||||||
# ---------- ورودیهای پنل مدیریت ----------
|
# ---------- ورودیهای پنل مدیریت ----------
|
||||||
@@ -460,12 +471,13 @@ EMPTY_DOC = {"type": "doc", "content": [{"type": "paragraph"}]}
|
|||||||
|
|
||||||
|
|
||||||
def validate_doc_body(value: object) -> dict:
|
def validate_doc_body(value: object) -> dict:
|
||||||
"""بدنه مقاله باید یک سند TipTap (ProseMirror) معتبر از نظر ساختار باشد."""
|
"""بدنه مقاله باید یک سند TipTap (ProseMirror) معتبر از نظر ساختار باشد و
|
||||||
|
آدرسهای href/src آن به اسکیمهای امن محدود شوند."""
|
||||||
if not value:
|
if not value:
|
||||||
return dict(EMPTY_DOC)
|
return dict(EMPTY_DOC)
|
||||||
if not isinstance(value, dict) or value.get("type") != "doc" or not isinstance(value.get("content"), list):
|
if not isinstance(value, dict) or value.get("type") != "doc" or not isinstance(value.get("content"), list):
|
||||||
raise ValueError("body must be a TipTap doc: {type: 'doc', content: [...]}")
|
raise ValueError("body must be a TipTap doc: {type: 'doc', content: [...]}")
|
||||||
return value
|
return sanitize_doc(value)
|
||||||
|
|
||||||
|
|
||||||
class ArticleIn(BaseModel):
|
class ArticleIn(BaseModel):
|
||||||
@@ -477,12 +489,16 @@ class ArticleIn(BaseModel):
|
|||||||
category: str = ""
|
category: str = ""
|
||||||
category_en: str = ""
|
category_en: str = ""
|
||||||
image: str = ""
|
image: str = ""
|
||||||
|
image_alt: str = ""
|
||||||
|
image_title: str = ""
|
||||||
read: str = ""
|
read: str = ""
|
||||||
read_en: str = ""
|
read_en: str = ""
|
||||||
excerpt: str = ""
|
excerpt: str = ""
|
||||||
excerpt_en: str = ""
|
excerpt_en: str = ""
|
||||||
body: dict = Field(default_factory=lambda: dict(EMPTY_DOC))
|
body: dict = Field(default_factory=lambda: dict(EMPTY_DOC))
|
||||||
body_en: dict = Field(default_factory=lambda: dict(EMPTY_DOC))
|
body_en: dict = Field(default_factory=lambda: dict(EMPTY_DOC))
|
||||||
|
faqs: list[dict] = []
|
||||||
|
faqs_en: list[dict] = []
|
||||||
status: Literal["draft", "published"] = "draft"
|
status: Literal["draft", "published"] = "draft"
|
||||||
seo_keyphrase: str = Field(default="", max_length=128)
|
seo_keyphrase: str = Field(default="", max_length=128)
|
||||||
_validate_body = field_validator("body")(validate_doc_body)
|
_validate_body = field_validator("body")(validate_doc_body)
|
||||||
@@ -779,12 +795,16 @@ class ArticlePatch(BaseModel):
|
|||||||
category: Optional[str] = None
|
category: Optional[str] = None
|
||||||
category_en: Optional[str] = None
|
category_en: Optional[str] = None
|
||||||
image: Optional[str] = None
|
image: Optional[str] = None
|
||||||
|
image_alt: Optional[str] = None
|
||||||
|
image_title: Optional[str] = None
|
||||||
read: Optional[str] = None
|
read: Optional[str] = None
|
||||||
read_en: Optional[str] = None
|
read_en: Optional[str] = None
|
||||||
excerpt: Optional[str] = None
|
excerpt: Optional[str] = None
|
||||||
excerpt_en: Optional[str] = None
|
excerpt_en: Optional[str] = None
|
||||||
body: Optional[dict] = None
|
body: Optional[dict] = None
|
||||||
body_en: Optional[dict] = None
|
body_en: Optional[dict] = None
|
||||||
|
faqs: Optional[list[dict]] = None
|
||||||
|
faqs_en: Optional[list[dict]] = None
|
||||||
status: Optional[Literal["draft", "published"]] = None
|
status: Optional[Literal["draft", "published"]] = None
|
||||||
seo_keyphrase: Optional[str] = None
|
seo_keyphrase: Optional[str] = None
|
||||||
_validate_body = field_validator("body")(validate_doc_body)
|
_validate_body = field_validator("body")(validate_doc_body)
|
||||||
|
|||||||
@@ -188,6 +188,12 @@ def _meta_for(db: Session, page_key: str, lang: str) -> tuple[str, str]:
|
|||||||
|
|
||||||
@router.get("/robots.txt", response_class=Response)
|
@router.get("/robots.txt", response_class=Response)
|
||||||
def robots(db: Session = Depends(get_db)):
|
def robots(db: Session = Depends(get_db)):
|
||||||
|
# پیشنمایش خصوصی — هیچ خزندهای نباید چیزی ذخیره کند
|
||||||
|
if settings.preview_mode:
|
||||||
|
return Response(
|
||||||
|
content="User-agent: *\nDisallow: /\n",
|
||||||
|
media_type="text/plain; charset=utf-8",
|
||||||
|
)
|
||||||
lines = ["User-agent: *", "Disallow: /admin", "Disallow: /api", ""]
|
lines = ["User-agent: *", "Disallow: /admin", "Disallow: /api", ""]
|
||||||
if settings.site_url:
|
if settings.site_url:
|
||||||
lines.append(f"Sitemap: {_absolute('/sitemap.xml')}")
|
lines.append(f"Sitemap: {_absolute('/sitemap.xml')}")
|
||||||
@@ -206,6 +212,13 @@ def _sitemap_entry(loc: str, fa: str, en: str) -> str:
|
|||||||
|
|
||||||
@router.get("/sitemap.xml", response_class=Response)
|
@router.get("/sitemap.xml", response_class=Response)
|
||||||
def sitemap(db: Session = Depends(get_db)):
|
def sitemap(db: Session = Depends(get_db)):
|
||||||
|
# پیشنمایش خصوصی — نقشه سایت خالی تا هیچ نشانیای اعلام نشود
|
||||||
|
if settings.preview_mode:
|
||||||
|
xml = (
|
||||||
|
'<?xml version="1.0" encoding="UTF-8"?>\n'
|
||||||
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>\n'
|
||||||
|
)
|
||||||
|
return Response(content=xml, media_type="application/xml; charset=utf-8")
|
||||||
fa_urls = list(STATIC_PATHS)
|
fa_urls = list(STATIC_PATHS)
|
||||||
fa_urls += [
|
fa_urls += [
|
||||||
f"/blog/{a.slug}"
|
f"/blog/{a.slug}"
|
||||||
@@ -262,7 +275,7 @@ def blog_detail(slug: str, db: Session = Depends(get_db)):
|
|||||||
brand = _brand_name(db)
|
brand = _brand_name(db)
|
||||||
title, excerpt = _article_pair(article, "fa")
|
title, excerpt = _article_pair(article, "fa")
|
||||||
first_text = _doc_text(article.body) if not isinstance(article.body, list) else " ".join(article.body[:2])
|
first_text = _doc_text(article.body) if not isinstance(article.body, list) else " ".join(article.body[:2])
|
||||||
jsonld = {
|
jsonld: object = {
|
||||||
"@context": "https://schema.org",
|
"@context": "https://schema.org",
|
||||||
"@type": "Article",
|
"@type": "Article",
|
||||||
"headline": title,
|
"headline": title,
|
||||||
@@ -273,6 +286,10 @@ def blog_detail(slug: str, db: Session = Depends(get_db)):
|
|||||||
"publisher": {"@type": "Organization", "name": brand},
|
"publisher": {"@type": "Organization", "name": brand},
|
||||||
"mainEntityOfPage": _absolute(f"/blog/{article.slug}"),
|
"mainEntityOfPage": _absolute(f"/blog/{article.slug}"),
|
||||||
}
|
}
|
||||||
|
# پرسشوپاسخ مقاله → اسکیمای FAQPage در همان بلوک (آرایهای از دو اسکیما)
|
||||||
|
faq_schema = _faq_jsonld(article, "fa")
|
||||||
|
if faq_schema:
|
||||||
|
jsonld = [jsonld, faq_schema]
|
||||||
noscript = f"<h1>{html.escape(title)}</h1><p>{html.escape(first_text)}</p>"
|
noscript = f"<h1>{html.escape(title)}</h1><p>{html.escape(first_text)}</p>"
|
||||||
return _shell(
|
return _shell(
|
||||||
title=f"{title} | {brand}",
|
title=f"{title} | {brand}",
|
||||||
@@ -325,12 +342,35 @@ def project_detail(slug: str, db: Session = Depends(get_db)):
|
|||||||
# ---------- صفحات انگلیسی (/en/...) ----------
|
# ---------- صفحات انگلیسی (/en/...) ----------
|
||||||
|
|
||||||
|
|
||||||
|
def _faq_jsonld(article: models.Article, lang: str) -> dict | None:
|
||||||
|
"""اسکیمای FAQPage برای نتایج غنی گوگل — از پرسشوپاسخهای پایان مقاله.
|
||||||
|
نسخه انگلیسی فقط وقتی ترجمه ثبت شده باشد جایگزین میشود."""
|
||||||
|
faqs = article.faqs if isinstance(article.faqs, list) else []
|
||||||
|
if lang == "en":
|
||||||
|
faqs_en = article.faqs_en if isinstance(article.faqs_en, list) else None
|
||||||
|
if faqs_en:
|
||||||
|
faqs = faqs_en
|
||||||
|
entities = [
|
||||||
|
{"@type": "Question", "name": (f.get("question") or "").strip(),
|
||||||
|
"acceptedAnswer": {"@type": "Answer", "text": (f.get("answer") or "").strip()}}
|
||||||
|
for f in faqs
|
||||||
|
if isinstance(f, dict) and (f.get("question") or "").strip()
|
||||||
|
]
|
||||||
|
if not entities:
|
||||||
|
return None
|
||||||
|
return {
|
||||||
|
"@context": "https://schema.org",
|
||||||
|
"@type": "FAQPage",
|
||||||
|
"mainEntity": entities,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def _en_blog_detail(article: models.Article, db: Session) -> HTMLResponse:
|
def _en_blog_detail(article: models.Article, db: Session) -> HTMLResponse:
|
||||||
brand = _brand_name(db, "en")
|
brand = _brand_name(db, "en")
|
||||||
title, excerpt = _article_pair(article, "en")
|
title, excerpt = _article_pair(article, "en")
|
||||||
body = article.body_en if isinstance(article.body_en, dict) else article.body
|
body = article.body_en if isinstance(article.body_en, dict) else article.body
|
||||||
first_text = _doc_text(body) if not isinstance(body, list) else " ".join(body[:2])
|
first_text = _doc_text(body) if not isinstance(body, list) else " ".join(body[:2])
|
||||||
jsonld = {
|
jsonld: object = {
|
||||||
"@context": "https://schema.org",
|
"@context": "https://schema.org",
|
||||||
"@type": "Article",
|
"@type": "Article",
|
||||||
"headline": title,
|
"headline": title,
|
||||||
@@ -341,6 +381,9 @@ def _en_blog_detail(article: models.Article, db: Session) -> HTMLResponse:
|
|||||||
"publisher": {"@type": "Organization", "name": brand},
|
"publisher": {"@type": "Organization", "name": brand},
|
||||||
"mainEntityOfPage": _absolute(f"/en/blog/{article.slug}"),
|
"mainEntityOfPage": _absolute(f"/en/blog/{article.slug}"),
|
||||||
}
|
}
|
||||||
|
faq_schema = _faq_jsonld(article, "en")
|
||||||
|
if faq_schema:
|
||||||
|
jsonld = [jsonld, faq_schema]
|
||||||
noscript = f"<h1>{html.escape(title)}</h1><p>{html.escape(first_text)}</p>"
|
noscript = f"<h1>{html.escape(title)}</h1><p>{html.escape(first_text)}</p>"
|
||||||
return _shell(
|
return _shell(
|
||||||
title=f"{title} | {brand}",
|
title=f"{title} | {brand}",
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
"""محیط Alembic — متادیتا از مدلهای SQLAlchemy میآید و آدرس دیتابیس از تنظیمات."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from logging.config import fileConfig
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
from sqlalchemy import engine_from_config, pool
|
||||||
|
|
||||||
|
from app import models # noqa: F401 — ثبت مدلها در متادیتا
|
||||||
|
from app.config import settings
|
||||||
|
from app.database import Base
|
||||||
|
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
if config.config_file_name is not None:
|
||||||
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
|
|
||||||
|
def get_url() -> str:
|
||||||
|
return os.environ.get("DATABASE_URL", settings.database_url)
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_offline() -> None:
|
||||||
|
context.configure(
|
||||||
|
url=get_url(),
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
literal_binds=True,
|
||||||
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
)
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_online() -> None:
|
||||||
|
configuration = config.get_section(config.config_ini_section, {})
|
||||||
|
configuration["sqlalchemy.url"] = get_url()
|
||||||
|
connectable = engine_from_config(configuration, prefix="sqlalchemy.", poolclass=pool.NullPool)
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(connection=connection, target_metadata=target_metadata)
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
revision: str = ${repr(up_revision)}
|
||||||
|
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||||
|
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
${downgrades if downgrades else "pass"}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
"""نقطه شروع زنجیره مهاجرتها — جداول پایه با create_all ساخته میشوند.
|
||||||
|
|
||||||
|
ساخت اولیه ۲۹ جدول بر عهده Base.metadata.create_all است (در CMD کانتینر پیش از
|
||||||
|
این مهاجرت اجرا میشود و برای دیتابیسهای موجود بیاثر است). از این نسخه به بعد
|
||||||
|
هر تغییر اسکیما باید یک revision واقعی Alembic باشد — نه create_all و نه
|
||||||
|
تغییر دستی — تا مسیر ارتقا و بازگشت آزمایشپذیر بماند.
|
||||||
|
|
||||||
|
Revision ID: 0001
|
||||||
|
Revises:
|
||||||
|
Create Date: 2026-09-18
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
revision: str = "0001"
|
||||||
|
down_revision: Union[str, None] = None
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
pass
|
||||||
@@ -11,3 +11,11 @@ bcrypt==4.2.1
|
|||||||
email-validator==2.2.0
|
email-validator==2.2.0
|
||||||
boto3==1.35.0
|
boto3==1.35.0
|
||||||
python-multipart==0.0.18
|
python-multipart==0.0.18
|
||||||
|
# پاکسازی HTML سمت سرور (تیپتپ و فیلدهای غنی)
|
||||||
|
nh3==0.2.20
|
||||||
|
# بهینهسازی تصویر در زمان آپلود (تغییر اندازه/فشردهسازی مجدد)
|
||||||
|
Pillow==11.1.0
|
||||||
|
# متریکهای Prometheus برای مانیتورینگ (در /metrics داخلی)
|
||||||
|
prometheus-fastapi-instrumentator==7.0.0
|
||||||
|
# مهاجرتهای نسخهدار اسکیما
|
||||||
|
alembic==1.14.0
|
||||||
|
|||||||
@@ -74,8 +74,12 @@ def reset_rate_limiter():
|
|||||||
from app import rate_limit
|
from app import rate_limit
|
||||||
|
|
||||||
rate_limit._failed.clear()
|
rate_limit._failed.clear()
|
||||||
|
rate_limit.contact_limiter._events.clear()
|
||||||
|
rate_limit.newsletter_limiter._events.clear()
|
||||||
yield
|
yield
|
||||||
rate_limit._failed.clear()
|
rate_limit._failed.clear()
|
||||||
|
rate_limit.contact_limiter._events.clear()
|
||||||
|
rate_limit.newsletter_limiter._events.clear()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
|
|||||||
+311
-2
@@ -1,6 +1,7 @@
|
|||||||
"""تستهای API — مسیرهای عمومی، احراز هویت، CRUD مدیریت، پیشنویس مقاله و سئو."""
|
"""تستهای API — مسیرهای عمومی، احراز هویت، CRUD مدیریت، پیشنویس مقاله و سئو."""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import socket
|
||||||
|
|
||||||
DOC = {"type": "doc", "content": [{"type": "paragraph"}]}
|
DOC = {"type": "doc", "content": [{"type": "paragraph"}]}
|
||||||
|
|
||||||
@@ -8,12 +9,24 @@ DOC = {"type": "doc", "content": [{"type": "paragraph"}]}
|
|||||||
# ---------- عمومی ----------
|
# ---------- عمومی ----------
|
||||||
|
|
||||||
|
|
||||||
def test_health(client):
|
def test_live(client):
|
||||||
res = client.get("/api/health")
|
"""liveness — پروسه زنده است، بدون وابستگی."""
|
||||||
|
res = client.get("/api/live")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
assert res.json()["status"] == "ok"
|
assert res.json()["status"] == "ok"
|
||||||
|
|
||||||
|
|
||||||
|
def test_health_deep(client):
|
||||||
|
"""بررسی عمق — DB (SQLite در تست) سالم است؛ MinIO در محیط تست در دسترس نیست
|
||||||
|
پس /api/health باید 503 و وضعیت degraded با جزئیات هر وابستگی بدهد."""
|
||||||
|
res = client.get("/api/health")
|
||||||
|
assert res.status_code == 503
|
||||||
|
body = res.json()
|
||||||
|
assert body["status"] == "degraded"
|
||||||
|
assert body["checks"]["database"] == "ok"
|
||||||
|
assert body["checks"]["storage"] == "error"
|
||||||
|
|
||||||
|
|
||||||
def test_site_hides_draft_articles(client):
|
def test_site_hides_draft_articles(client):
|
||||||
res = client.get("/api/site")
|
res = client.get("/api/site")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
@@ -268,3 +281,299 @@ def test_en_backfill_fills_only_empty_fields(auth_client, client):
|
|||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
backfill_en_content()
|
backfill_en_content()
|
||||||
assert client.get("/api/site").json()["brand"]["name_en"] == "Custom EN Name"
|
assert client.get("/api/site").json()["brand"]["name_en"] == "Custom EN Name"
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- محافظت فرمها و پاکسازی ----------
|
||||||
|
|
||||||
|
|
||||||
|
def test_contact_honeypot_saves_nothing(client, auth_client):
|
||||||
|
"""دامنهٔ پنهان پرشده یعنی ربات — پاسخ موفقِ قلابی، بدون ذخیره."""
|
||||||
|
res = client.post(
|
||||||
|
"/api/contact",
|
||||||
|
json={"name": "ربات تستی", "phone": "09120000000", "website": "spam"},
|
||||||
|
)
|
||||||
|
assert res.status_code == 201
|
||||||
|
assert res.json()["id"] == 0 # رکورد قلابی — در دیتابیس نیست
|
||||||
|
assert auth_client.get("/api/admin/messages").json() == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_contact_rate_limited(client, auth_client):
|
||||||
|
"""ارسال ششم در پنجرهٔ ۱۵ دقیقهای رد میشود (۵ مجاز برای هر IP)."""
|
||||||
|
for _ in range(5):
|
||||||
|
res = client.post(
|
||||||
|
"/api/contact", json={"name": "کاربر تماس", "phone": "09120000000"}
|
||||||
|
)
|
||||||
|
assert res.status_code == 201, res.text
|
||||||
|
res = client.post("/api/contact", json={"name": "کاربر تماس", "phone": "09120000000"})
|
||||||
|
assert res.status_code == 429
|
||||||
|
assert "Retry-After" in res.headers
|
||||||
|
|
||||||
|
|
||||||
|
def test_newsletter_honeypot_saves_nothing(client, auth_client):
|
||||||
|
res = client.post(
|
||||||
|
"/api/newsletter", json={"email": "bot@example.com", "website": "spam"}
|
||||||
|
)
|
||||||
|
assert res.status_code == 201
|
||||||
|
assert res.json()["id"] == 0
|
||||||
|
assert auth_client.get("/api/admin/subscribers").json() == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_and_subscriber_csv_export(auth_client):
|
||||||
|
auth_client.post("/api/contact", json={"name": "کاربر تماس", "phone": "09120000000"})
|
||||||
|
res = auth_client.get("/api/admin/messages/export.csv")
|
||||||
|
assert res.status_code == 200
|
||||||
|
assert res.headers["content-type"].startswith("text/csv")
|
||||||
|
assert "کاربر تماس" in res.text
|
||||||
|
|
||||||
|
auth_client.post("/api/newsletter", json={"email": "sale@example.com"})
|
||||||
|
res = auth_client.get("/api/admin/subscribers/export.csv")
|
||||||
|
assert res.status_code == 200
|
||||||
|
assert "sale@example.com" in res.text
|
||||||
|
|
||||||
|
|
||||||
|
def test_article_body_url_sanitization(auth_client):
|
||||||
|
"""اسکیمهای خطرناک در href/src سند TipTap در زمان ذخیره حذف میشوند."""
|
||||||
|
res = auth_client.post(
|
||||||
|
"/api/admin/articles",
|
||||||
|
json={
|
||||||
|
"slug": "sanitized-post",
|
||||||
|
"title": "مقاله پاکسازیشده",
|
||||||
|
"status": "published",
|
||||||
|
"body": {
|
||||||
|
"type": "doc",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "paragraph",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"marks": [
|
||||||
|
{"type": "link", "attrs": {"href": "javascript:alert(1)"}},
|
||||||
|
],
|
||||||
|
"text": "لینک خطرناک",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert res.status_code == 201, res.text
|
||||||
|
body = res.json()["body"]
|
||||||
|
marks = body["content"][0]["content"][0].get("marks", [])
|
||||||
|
assert all("href" not in m.get("attrs", {}) for m in marks)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- نامگذاری تصاویر، درونریزی و پرسشوپاسخ مقاله ----------
|
||||||
|
|
||||||
|
|
||||||
|
class _FakeS3:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.objects = {}
|
||||||
|
|
||||||
|
def put_object(self, **kwargs):
|
||||||
|
self.objects[kwargs["Key"]] = kwargs["Body"]
|
||||||
|
|
||||||
|
|
||||||
|
def _mock_storage(monkeypatch):
|
||||||
|
"""MinIO در تست در دسترس نیست — کلاینت S3 و DNS عمومی جعل میشوند."""
|
||||||
|
fake = _FakeS3()
|
||||||
|
monkeypatch.setattr("app.api.admin.boto3.client", lambda *a, **k: fake)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"app.api.admin.socket.getaddrinfo",
|
||||||
|
lambda host, port, *a, **k: [(socket.AF_INET, None, None, "", ("93.184.216.34", 0))],
|
||||||
|
)
|
||||||
|
return fake
|
||||||
|
|
||||||
|
|
||||||
|
def _png_bytes() -> bytes:
|
||||||
|
from PIL import Image
|
||||||
|
import io
|
||||||
|
|
||||||
|
buf = io.BytesIO()
|
||||||
|
Image.new("RGB", (8, 8), (200, 120, 60)).save(buf, "PNG")
|
||||||
|
return buf.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def test_upload_with_readable_name(auth_client, monkeypatch):
|
||||||
|
_mock_storage(monkeypatch)
|
||||||
|
res = auth_client.post(
|
||||||
|
"/api/admin/upload",
|
||||||
|
files={"file": ("raw name.png", _png_bytes(), "image/png")},
|
||||||
|
data={"name": "برج الهیه tower!"},
|
||||||
|
)
|
||||||
|
assert res.status_code == 200, res.text
|
||||||
|
key = res.json()["key"]
|
||||||
|
# نام خوانا (فارسی/لاتین، بدون نویسه ناامن) + پسوند یکتا + پسوند تصویر
|
||||||
|
assert "برج-الهیه-tower" in key
|
||||||
|
assert key.endswith(".png")
|
||||||
|
assert "!" not in key
|
||||||
|
|
||||||
|
|
||||||
|
def test_upload_without_name_uses_filename(auth_client, monkeypatch):
|
||||||
|
_mock_storage(monkeypatch)
|
||||||
|
res = auth_client.post(
|
||||||
|
"/api/admin/upload",
|
||||||
|
files={"file": ("Elahieh Tower.jpg", _png_bytes(), "image/jpeg")},
|
||||||
|
data={"name": ""},
|
||||||
|
)
|
||||||
|
assert res.status_code == 200
|
||||||
|
assert "Elahieh-Tower" in res.json()["key"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_image_rejects_internal_host(auth_client):
|
||||||
|
# SSRF — میزبان حلقهمحلی/خصوصی رد میشود پیش از هر درخواست شبکه
|
||||||
|
res = auth_client.post("/api/admin/import-image", json={"url": "http://127.0.0.1:9000/x.png"})
|
||||||
|
assert res.status_code == 400
|
||||||
|
res = auth_client.post("/api/admin/import-image", json={"url": "http://192.168.1.5/x.png"})
|
||||||
|
assert res.status_code == 400
|
||||||
|
res = auth_client.post("/api/admin/import-image", json={"url": "file:///etc/passwd"})
|
||||||
|
assert res.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_image_rejects_bad_content_type(auth_client, monkeypatch):
|
||||||
|
_mock_storage(monkeypatch)
|
||||||
|
import io as _io
|
||||||
|
|
||||||
|
class FakeResponse:
|
||||||
|
def __init__(self):
|
||||||
|
self.headers = {"Content-Type": "text/html"}
|
||||||
|
|
||||||
|
def read(self, n):
|
||||||
|
return b"<html></html>"
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def fake_urlopen(request, timeout):
|
||||||
|
return FakeResponse()
|
||||||
|
|
||||||
|
monkeypatch.setattr("app.api.admin.urllib.request.urlopen", fake_urlopen)
|
||||||
|
res = auth_client.post("/api/admin/import-image", json={"url": "https://example.com/page"})
|
||||||
|
assert res.status_code == 415
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_image_happy_path(auth_client, monkeypatch):
|
||||||
|
_mock_storage(monkeypatch)
|
||||||
|
class FakeResponse:
|
||||||
|
def __init__(self):
|
||||||
|
self.headers = {"Content-Type": "image/png"}
|
||||||
|
|
||||||
|
def read(self, n):
|
||||||
|
return _png_bytes()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
return False
|
||||||
|
|
||||||
|
monkeypatch.setattr("app.api.admin.urllib.request.urlopen", lambda request, timeout: FakeResponse())
|
||||||
|
res = auth_client.post("/api/admin/import-image", json={"url": "https://wiki.example/Elahieh pic.png?token=1"})
|
||||||
|
assert res.status_code == 200, res.text
|
||||||
|
assert res.json()["url"].startswith("/media/")
|
||||||
|
assert "Elahieh-pic" in res.json()["key"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_article_persists_image_meta_and_faqs(auth_client, client):
|
||||||
|
created = auth_client.post(
|
||||||
|
"/api/admin/articles",
|
||||||
|
json={"slug": "meta-article", "title": "متادیتا", "body": DOC},
|
||||||
|
)
|
||||||
|
assert created.status_code == 201, created.text
|
||||||
|
article_id = created.json()["id"]
|
||||||
|
|
||||||
|
faqs = [{"question": "هزینه چقدر است؟", "answer": "بستگی به پروژه دارد."}]
|
||||||
|
res = auth_client.patch(
|
||||||
|
f"/api/admin/articles/{article_id}",
|
||||||
|
json={
|
||||||
|
"image_alt": "نمای برج الهیه",
|
||||||
|
"image_title": "برج آرامش",
|
||||||
|
"faqs": faqs,
|
||||||
|
"faqs_en": [{"question": "What is the cost?", "answer": "Depends on the project."}],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert res.status_code == 200, res.text
|
||||||
|
assert res.json()["image_alt"] == "نمای برج الهیه"
|
||||||
|
assert res.json()["image_title"] == "برج آرامش"
|
||||||
|
assert res.json()["faqs"] == faqs
|
||||||
|
|
||||||
|
# انتشار و سپس خروجی عمومی — همین مقادیر برمیگردد
|
||||||
|
assert auth_client.patch(f"/api/admin/articles/{article_id}", json={"status": "published"}).status_code == 200
|
||||||
|
public = client.get("/api/articles/meta-article").json()
|
||||||
|
assert public["image_alt"] == "نمای برج الهیه"
|
||||||
|
assert public["faqs"] == faqs
|
||||||
|
|
||||||
|
assert auth_client.delete(f"/api/admin/articles/{article_id}").status_code == 204
|
||||||
|
|
||||||
|
|
||||||
|
def test_blog_shell_includes_faqpage_jsonld(auth_client, client):
|
||||||
|
created = auth_client.post(
|
||||||
|
"/api/admin/articles",
|
||||||
|
json={
|
||||||
|
"slug": "faq-article",
|
||||||
|
"title": "مقاله پرسشوپاسخ",
|
||||||
|
"body": DOC,
|
||||||
|
"status": "published",
|
||||||
|
"faqs": [{"question": "زمان ساخت؟", "answer": "۳۰ ماه."}],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert created.status_code == 201, created.text
|
||||||
|
|
||||||
|
res = client.get("/blog/faq-article")
|
||||||
|
assert res.status_code == 200
|
||||||
|
assert "FAQPage" in res.text
|
||||||
|
assert "زمان ساخت؟" in res.text
|
||||||
|
|
||||||
|
# نسخه انگلیسی بدون ترجمه → همان فارسی؛ با ترجمه → انگلیسی
|
||||||
|
en = client.get("/en/blog/faq-article").text
|
||||||
|
assert "FAQPage" in en
|
||||||
|
article_id = created.json()["id"]
|
||||||
|
auth_client.patch(
|
||||||
|
f"/api/admin/articles/{article_id}",
|
||||||
|
json={"faqs_en": [{"question": "How long?", "answer": "Thirty months."}]},
|
||||||
|
)
|
||||||
|
en = client.get("/en/blog/faq-article").text
|
||||||
|
assert "How long?" in en
|
||||||
|
assert "زمان ساخت؟" not in en
|
||||||
|
|
||||||
|
auth_client.delete(f"/api/admin/articles/{article_id}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_blog_shell_without_faqs_has_no_faqpage(client):
|
||||||
|
res = client.get("/blog/published-post")
|
||||||
|
assert res.status_code == 200
|
||||||
|
assert "FAQPage" not in res.text
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- پیشنمایش خصوصی (برای انتشار عمومی خاموش میشود) ----------
|
||||||
|
|
||||||
|
|
||||||
|
def test_preview_mode_blocks_robots_and_sitemap(client, monkeypatch):
|
||||||
|
from app.config import settings
|
||||||
|
|
||||||
|
monkeypatch.setattr(settings, "preview_mode", True)
|
||||||
|
try:
|
||||||
|
robots = client.get("/robots.txt")
|
||||||
|
assert robots.status_code == 200
|
||||||
|
assert "Disallow: /" in robots.text
|
||||||
|
assert "Sitemap:" not in robots.text
|
||||||
|
|
||||||
|
sitemap = client.get("/sitemap.xml")
|
||||||
|
assert sitemap.status_code == 200
|
||||||
|
assert "published-post" not in sitemap.text
|
||||||
|
|
||||||
|
# لایه دوم: هدر noindex روی همه پاسخها
|
||||||
|
assert "noindex" in client.get("/api/health").headers.get("X-Robots-Tag", "")
|
||||||
|
finally:
|
||||||
|
monkeypatch.setattr(settings, "preview_mode", False)
|
||||||
|
|
||||||
|
|
||||||
|
def test_public_mode_allows_indexing(client):
|
||||||
|
robots = client.get("/robots.txt").text
|
||||||
|
assert "Disallow: /admin" in robots
|
||||||
|
assert "Sitemap:" in robots
|
||||||
|
assert "noindex" not in client.get("/api/health").headers.get("X-Robots-Tag", "")
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# کانتکست بیلد باریک
|
||||||
|
__pycache__/
|
||||||
|
**/__pycache__/
|
||||||
|
*.pyc
|
||||||
|
.venv/
|
||||||
|
venv/
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
README.md
|
||||||
|
Dockerfile
|
||||||
|
.dockerignore
|
||||||
|
test_pure.py
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# سرویس چتبات — این مقادیر را در .env ریشه پروژه بگذارید (نه اینجا)
|
||||||
|
# این فایل فقط مستندسازی متغیرهاست.
|
||||||
|
|
||||||
|
# --- اجباری ---
|
||||||
|
# دیتابیس سایت (فقط خواندن) — در داکر از docker-compose.yml تزریق میشود
|
||||||
|
# DATABASE_URL=postgresql+psycopg://aramesh:PASSWORD@db:5432/arameshplaza
|
||||||
|
|
||||||
|
# LLM سازگار با OpenAI — بدون این چتبات پاسخ نمیدهد
|
||||||
|
# مثال سرور داخلی: LLM_BASE_URL=http://172.16.22.21:4460/v1
|
||||||
|
# مثال سرویس ابری: LLM_BASE_URL=https://api.example.com/v1
|
||||||
|
LLM_BASE_URL=
|
||||||
|
LLM_API_KEY=
|
||||||
|
LLM_MODEL=openai/gpt-oss-120b
|
||||||
|
|
||||||
|
# --- اختیاری ---
|
||||||
|
# توکن محافظت از POST /chat/reindex — اگر خالی باشد مسیر ۴۰۳ میدهد
|
||||||
|
CHAT_REINDEX_TOKEN=
|
||||||
|
# ریرنکر را روی سرور کمرم خاموش کنید (RERANKER_ENABLED=false)
|
||||||
|
RERANKER_ENABLED=true
|
||||||
|
# وزن جستوجوی معنایی در ترکیب نمرهها (۰ تا ۱) — پیشفرض ۰.۵
|
||||||
|
ALPHA_DENSE=0.5
|
||||||
|
# تعداد سند نهایی به LLM — پیشفرض ۳
|
||||||
|
BEST_OF_K=3
|
||||||
|
# بازسازی خودکار ایندکس پس از تغییر محتوا (ثانیه، ۰ = خاموش)
|
||||||
|
INDEX_REFRESH_SECONDS=600
|
||||||
|
# محدودیت نرخ: حداکثر پیام در پنجره زمانی (پیشفرض ۲۰ پیام در ۳۰۰ ثانیه)
|
||||||
|
RATE_LIMIT_MESSAGES=20
|
||||||
|
RATE_LIMIT_WINDOW=300
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# سرویس چتبات — پایتون + مدلهای بازیابی (CPU)
|
||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
WORKDIR /srv
|
||||||
|
|
||||||
|
# همهٔ وابستگیهای سنگین (numpy/scikit-learn/torch/psycopg) ویل prebuilt دارند —
|
||||||
|
# بدون کامپایلر؛ دسترسی apt در شبکهٔ بیلد قابلاعتماد نیست
|
||||||
|
COPY requirements.txt /srv/chatbot/requirements.txt
|
||||||
|
RUN pip install --no-cache-dir -r /srv/chatbot/requirements.txt
|
||||||
|
|
||||||
|
COPY . /srv/chatbot/
|
||||||
|
|
||||||
|
# مدل امبدینگ/ریرنکر در /data کش میشود (والیوم داکر) — بردارها در pgvector میروند و فایل ایندکس جدا وجود ندارد
|
||||||
|
ENV HF_HOME=/data/hf \
|
||||||
|
HF_CACHE_DIR=/data/hf
|
||||||
|
VOLUME /data
|
||||||
|
|
||||||
|
# کاربر بدون دسترسی root — فقط /data باید نوشتنی باشد (کش مدلها)
|
||||||
|
RUN useradd --system --create-home appuser \
|
||||||
|
&& mkdir -p /data \
|
||||||
|
&& chown -R appuser:appuser /srv/chatbot /data
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=180s --retries=5 \
|
||||||
|
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/chat/healthz')" || exit 1
|
||||||
|
|
||||||
|
CMD ["uvicorn", "chatbot.app:api", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips=*"]
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
# دستیار هوشمند سایت (چتبات)
|
||||||
|
|
||||||
|
سرویس گفتگوی محتوای سایت «گروه ساختمانی آرامش» — بازدیدکننده به فارسی سؤال میپرسد و
|
||||||
|
دستیار **فقط از روی محتوای خود سایت** (مقالهها، پروژهها، خدمات، سؤالات متداول، اطلاعات تماس و…)
|
||||||
|
پاسخ میدهد؛ اگر پاسخ در محتوا نباشد، بهجای ساخت جواب، صادقانه اعلام میکند.
|
||||||
|
|
||||||
|
این سرویس پورت خط لولهٔ پروژهٔ `llm-srerver-chatbot` (نسخه v13) با چهار تغییر اصلی برای این سایت است:
|
||||||
|
|
||||||
|
1. **پایگاه sparse بهجای SPLADE از BM25 استفاده میکند** — SPLADE انگلیسیمحور است و روی
|
||||||
|
فارسی عملاً بیفایده؛ BM25 با نرمالسازی فارسی (ی/ک عربی، اعراب، ارقام، نیمفاصله) جایگزین شده است.
|
||||||
|
2. **ذخیرهساز برداری pgvector است، نه FAISS** — بردارها در یک دیتابیس جدا (`aramesh_chatbot`)
|
||||||
|
روی همان نمونه Postgres نگهداری میشوند؛ همگامسازی افزایشی است (فقط سطرهای جدید/تغییریافته
|
||||||
|
امبد میشوند)، بکاپگیری با خود دیتابیس است و فایل ایندکس جدا وجود ندارد.
|
||||||
|
3. **تأخیر کمتر** — مسیریابی `alpha` با LLM و گسترش چندپرسشی (multi-query) بهصورت پیشفرض
|
||||||
|
خاموشاند (با متغیر محیطی قابل فعالسازی)؛ بازنویسی پرسش فقط وقتی تاریخچه گفتگو وجود دارد اجرا میشود.
|
||||||
|
4. **استریم واقعی توکنبهتوکن** با `astream_events` بهجای دیف پیامهای کامل.
|
||||||
|
|
||||||
|
## خط لوله هر پیام
|
||||||
|
|
||||||
|
```
|
||||||
|
پرسش کاربر
|
||||||
|
│
|
||||||
|
├─[اگر تاریخچه هست] بازنویسی پرسش با LLM (حل ضمیرها و اشارهها: «اون دومی» → موضوع واقعی)
|
||||||
|
│
|
||||||
|
├─ بازیابی ترکیبی: pgvector (معنایی، کسینوسی روی HNSW — MiniLM چندزبانه)
|
||||||
|
│ + BM25 (کلیدواژه، متن نرمالشده فارسی)
|
||||||
|
│ نمرهها min-max → ترکیب α×معنایی + (1-α)×کلیدواژه
|
||||||
|
│
|
||||||
|
├─ برش خوشهای (K-Means K=2 + silhouette ≥ 0.55) — مرز طبیعی کیفیت
|
||||||
|
│
|
||||||
|
├─ ریرنک با BAAI/bge-reranker-v2-m3 → نگهداشتن ۳ سند برتر
|
||||||
|
│
|
||||||
|
└─ تولید پاسخ فارسی با LLM (قواعد تکیه بر داده + معرفی صفحه مرتبط + پیام جایگزین مشخص)
|
||||||
|
```
|
||||||
|
|
||||||
|
ایندکس از محتوای PostgreSQL سایت ساخته میشود (`articles` منتشرشده، `projects`، `services`،
|
||||||
|
`faqs`، `brand`، `offices`، `investment_models`، `process_steps`، `why_us`، `team`،
|
||||||
|
`testimonials`، `timeline`، `story`). متنهای بلند به قطعات ~۷۰۰ نویسهای همپوشان برش میخورند.
|
||||||
|
|
||||||
|
- **ایندکس برداری در pgvector**: جدول `chatbot_chunks` در دیتابیس اختصاصی
|
||||||
|
`aramesh_chatbot` (روی همان نمونه Postgres) با ستون `embedding vector(384)` و
|
||||||
|
ایندکس HNSW کسینوسی؛ هر قطعه `doc_id` پایدار و `content_hash` دارد و همگامسازی
|
||||||
|
فقط سطرهای جدید/تغییریافته را امبد میکند. دیتابیس و جدول در شروع سرویس خودکار ساخته میشوند.
|
||||||
|
- **بازسازی خودکار**: یک حلقهٔ پسزمینه هر `INDEX_REFRESH_SECONDS` ثانیه محتوا را چک
|
||||||
|
میکند و اگر ادمین چیزی را در پنل عوض کرده باشد، فقط تغییرات همگام میشود
|
||||||
|
(امبد اجباری همه: `POST /chat/reindex`).
|
||||||
|
- **ماندگاری گفتگو**: در حافظهٔ همان پروسه (MemorySaver لنگگراف) — با ریاستارت پاک میشود.
|
||||||
|
|
||||||
|
## متغیرهای محیطی (در `.env` ریشه پروژه)
|
||||||
|
|
||||||
|
| متغیر | پیشفرض | توضیح |
|
||||||
|
|---|---|---|
|
||||||
|
| `LLM_BASE_URL` | خالی | **اجباری** — نشانی سرویس LLM سازگار با OpenAI. بدون آن چتبات بالا میآید ولی پاسخ نمیدهد |
|
||||||
|
| `LLM_API_KEY` | خالی | کلید API (برای سرور داخلی `not-needed` بگذارید) |
|
||||||
|
| `LLM_MODEL` | `openai/gpt-oss-120b` | نام مدل |
|
||||||
|
| `CHAT_REINDEX_TOKEN` | خالی | توکن `POST /chat/reindex` — اگر خالی باشد آن مسیر ۴۰۳ میدهد |
|
||||||
|
| `CHATBOT_DB_NAME` | `aramesh_chatbot` | نام دیتابیس اختصاصی چتبات روی همان نمونه Postgres (بردارها اینجا میروند) |
|
||||||
|
| `EMBEDDING_DIM` | `384` | بعد بردار مدل امبدینگ — با تعویض مدل امبدینگ باید هماهنگ شود |
|
||||||
|
| `RERANKER_ENABLED` | `true` | روی سرور کمرم (زیر ~۴GB) `false` بگذارید |
|
||||||
|
| `ALPHA_DENSE` | `0.5` | وزن جستوجوی معنایی در ترکیب نمرهها (۰ تا ۱) |
|
||||||
|
| `BEST_OF_K` | `3` | تعداد سند نهایی که به LLM میرود |
|
||||||
|
| `INDEX_REFRESH_SECONDS` | `600` | بازسازی خودکار ایندکس پس از تغییر محتوا (۰ = خاموش) |
|
||||||
|
| `RATE_LIMIT_MESSAGES` / `RATE_LIMIT_WINDOW` | `20` / `300` | سقف پیام هر IP در پنجره زمانی (ثانیه) |
|
||||||
|
|
||||||
|
`DATABASE_URL` در داکر از `docker-compose.yml` تزریق میشود — دیتابیس سایت فقط خوانده
|
||||||
|
میشود و نوشتن (بردارها) فقط در دیتابیس جدا `aramesh_chatbot` انجام میگیرد.
|
||||||
|
|
||||||
|
## پیشنیاز pgvector
|
||||||
|
|
||||||
|
سرویس `db` استک از ایمیج `pgvector/pgvector:pg17` استفاده میکند (همان postgres:17 +
|
||||||
|
افزونهٔ vector). اگر Postgres سایت روی نمونهٔ دیگری است، باید افزونهٔ pgvector روی آن
|
||||||
|
فعال باشد؛ در غیر این صورت شروع چتبات با خطای واضح متوقف میشود.
|
||||||
|
|
||||||
|
## مسیرها
|
||||||
|
|
||||||
|
| مسیر | توضیح |
|
||||||
|
|---|---|
|
||||||
|
| `GET /chat/stream?message=…&thread_id=…` | استریم SSE توکنبهتوکن پاسخ (`{"content": …}` و در پایان `{"done": true}`) |
|
||||||
|
| `GET /chat/suggestions` | ۴ پیشنهاد پرسش (کش ۱۰ دقیقه؛ جایگزین: خود سؤالات متداول) |
|
||||||
|
| `GET /chat/eval?message=…` | مخصوص ارزیابی RAGAS — پاسخ + اسناد بازیابیشده با thread تازه |
|
||||||
|
| `GET /chat/healthz` | وضعیت ایندکس/LLM/تعداد اسناد |
|
||||||
|
| `POST /chat/reindex` | بازسازی اجباری ایندکس (هدر `X-Reindex-Token`) |
|
||||||
|
|
||||||
|
مسیرهای `GET /chat/{scope}/stream|eval|suggestions` هم برای سازگاری با اسکریپت
|
||||||
|
`eval_ragas.py` قدیمی موجود است — کافی است `--url` را به نشانی این سرویس بدهید.
|
||||||
|
|
||||||
|
## اجرا
|
||||||
|
|
||||||
|
با داکر (بخشی از استک اصلی است — `docker compose up -d --build`):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# در .env ریشه:
|
||||||
|
LLM_BASE_URL=http://172.16.22.21:4460/v1 # یا هر سرویس سازگار با OpenAI
|
||||||
|
LLM_API_KEY=not-needed
|
||||||
|
```
|
||||||
|
|
||||||
|
اولین بوت مدلها را دانلود میکند (~۱.۵GB: امبدینگ + ریرنکر) و در والیوم
|
||||||
|
`chatbot_cache` کش میشود؛ بوتهای بعدی چند ثانیه طول میکشد.
|
||||||
|
|
||||||
|
اجرای محلی بدون داکر:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd chatbot
|
||||||
|
python -m venv .venv && source .venv/bin/activate # ویندوز: .venv\Scripts\activate
|
||||||
|
pip install -r requirements.txt
|
||||||
|
export DATABASE_URL="postgresql+psycopg://aramesh:PASS@localhost:5432/arameshplaza"
|
||||||
|
export LLM_BASE_URL="http://…" LLM_MODEL="openai/gpt-oss-120b"
|
||||||
|
uvicorn chatbot.app:api --port 8000 # از ریشه پوشه والد
|
||||||
|
```
|
||||||
|
|
||||||
|
## منابع مورد نیاز
|
||||||
|
|
||||||
|
- **رم**: با ریرنکر ~۴GB (مدل ۵۶۸M پارامتری روی CPU)؛ بدون ریرنکر ~۲GB
|
||||||
|
- **CPU**: امبدینگ و ریرنک روی CPU اجرا میشوند؛ GPU لازم نیست
|
||||||
|
- **شبکه**: دسترسی به PostgreSQL و به نشانی `LLM_BASE_URL`
|
||||||
|
|
||||||
|
## نکات امنیتی
|
||||||
|
|
||||||
|
- دیتابیس سایت فقط **خواندن**؛ نوشتن فقط در دیتابیس اختصاصی `aramesh_chatbot` انجام میشود
|
||||||
|
- محدودسازی نرخ per-IP فعال است (پیشفرض ۲۰ پیام در ۵ دقیقه)
|
||||||
|
- CORS باز است ولی کوکی/اعتبارنامهای رد و بدل نمیشود؛ در تولید همهچیز هممبدأ (پشت Traefik) است
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
"""سرویس چتبات سایت آرامش — بازیابی ترکیبی روی محتوای PostgreSQL."""
|
||||||
+284
@@ -0,0 +1,284 @@
|
|||||||
|
"""سرویس چتبات سایت آرامش — FastAPI با استریم SSE و بازیابی ترکیبی روی محتوای دیتابیس."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
from fastapi import FastAPI, HTTPException, Query, Request
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
|
from langchain_core.messages import AIMessage, HumanMessage
|
||||||
|
|
||||||
|
from . import content as content_mod
|
||||||
|
from . import pipeline
|
||||||
|
from . import vectorstore
|
||||||
|
from .config import settings
|
||||||
|
from .indexing import INDEX, sync_index
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
|
||||||
|
)
|
||||||
|
log = logging.getLogger("chatbot")
|
||||||
|
|
||||||
|
|
||||||
|
# --- چرخه حیات: ساخت ایندکس در شروع + بازسازی خودکار وقتی محتوا عوض شود ---
|
||||||
|
|
||||||
|
_refresh_task: asyncio.Task | None = None
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(_app: FastAPI):
|
||||||
|
try:
|
||||||
|
await asyncio.to_thread(sync_index)
|
||||||
|
except Exception as e:
|
||||||
|
log.critical("Initial index sync failed: %s — chat will be unavailable until reindex", e)
|
||||||
|
global _refresh_task
|
||||||
|
_refresh_task = asyncio.create_task(_refresh_loop())
|
||||||
|
yield
|
||||||
|
_refresh_task.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
async def _refresh_loop() -> None:
|
||||||
|
interval = settings.index_refresh_seconds
|
||||||
|
if interval <= 0:
|
||||||
|
return
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(interval)
|
||||||
|
try:
|
||||||
|
# همگامسازی افزایشی است — فقط سطرهای جدید/تغییریافته امبد میشوند
|
||||||
|
await asyncio.to_thread(sync_index)
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Background index refresh failed: %s", e)
|
||||||
|
|
||||||
|
|
||||||
|
api = FastAPI(
|
||||||
|
title="Aramesh Site Chatbot",
|
||||||
|
version="1.0.0",
|
||||||
|
description="دستیار گفتگوی محتوای سایت — بازیابی ترکیبی (pgvector + BM25) با ریرنکر BGE",
|
||||||
|
lifespan=lifespan,
|
||||||
|
)
|
||||||
|
|
||||||
|
api.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"],
|
||||||
|
allow_credentials=False,
|
||||||
|
allow_methods=["GET", "POST"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# --- محدودسازی نرخ درخواست سبک در حافظه (پنجره لغزان per-IP) ---
|
||||||
|
|
||||||
|
_hits: dict[str, deque] = defaultdict(deque)
|
||||||
|
_last_sweep = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
def _rate_limited(ip: str) -> bool:
|
||||||
|
global _last_sweep
|
||||||
|
now = time.time()
|
||||||
|
# پاکسازی دورهای کلیدهای بیمصرف تا دیکشنری بینهایت بزرگ نشود
|
||||||
|
if now - _last_sweep > settings.rate_limit_window:
|
||||||
|
stale = [k for k, q in _hits.items() if not q or now - q[-1] > settings.rate_limit_window]
|
||||||
|
for k in stale:
|
||||||
|
_hits.pop(k, None)
|
||||||
|
_last_sweep = now
|
||||||
|
window = _hits[ip]
|
||||||
|
while window and now - window[0] > settings.rate_limit_window:
|
||||||
|
window.popleft()
|
||||||
|
if len(window) >= settings.rate_limit_messages:
|
||||||
|
return True
|
||||||
|
window.append(now)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _client_ip(request: Request | None) -> str:
|
||||||
|
if request is None:
|
||||||
|
return "?"
|
||||||
|
forwarded = request.headers.get("x-forwarded-for")
|
||||||
|
return forwarded.split(",")[0].strip() if forwarded else (request.client.host if request.client else "?")
|
||||||
|
|
||||||
|
|
||||||
|
# --- کمکیهای SSE ---
|
||||||
|
|
||||||
|
def _sse(payload: dict) -> str:
|
||||||
|
return f"data: {json.dumps(payload, ensure_ascii=False)}\n\n"
|
||||||
|
|
||||||
|
|
||||||
|
def _chunk_text(chunk) -> str:
|
||||||
|
"""استخراج متن از قطعه استریم LLM (رشته یا لیست بخشها)."""
|
||||||
|
content = getattr(chunk, "content", "")
|
||||||
|
if isinstance(content, str):
|
||||||
|
return content
|
||||||
|
if isinstance(content, list):
|
||||||
|
return "".join(p.get("text", "") for p in content if isinstance(p, dict))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
async def _stream_answer(message: str, thread_id: str):
|
||||||
|
if not INDEX.ready:
|
||||||
|
yield _sse({"error": "ایندکس محتوا هنوز آماده نیست؛ چند لحظه بعد دوباره تلاش کنید.", "done": True})
|
||||||
|
return
|
||||||
|
if pipeline.get_llm() is None:
|
||||||
|
yield _sse({"error": pipeline.LLM_UNAVAILABLE, "done": True})
|
||||||
|
return
|
||||||
|
|
||||||
|
config = {"configurable": {"thread_id": thread_id}}
|
||||||
|
inputs = {"messages": [HumanMessage(content=message)]}
|
||||||
|
try:
|
||||||
|
async for event in pipeline.graph.astream_events(inputs, config=config, version="v2"):
|
||||||
|
# فقط توکنهای پاسخ نهایی — نه فراخوانیهای کمکی (بازنویسی/گسترش پرسش)
|
||||||
|
if event["event"] == "on_chat_model_stream" and "generate" in (event.get("tags") or []):
|
||||||
|
text = _chunk_text(event["data"]["chunk"])
|
||||||
|
if text:
|
||||||
|
yield _sse({"content": text, "done": False})
|
||||||
|
yield _sse({"content": "", "done": True})
|
||||||
|
except Exception as e:
|
||||||
|
log.exception("Streaming failed")
|
||||||
|
yield _sse({"error": f"خطا در تولید پاسخ: {e}", "done": True})
|
||||||
|
|
||||||
|
|
||||||
|
# --- مسیرها ---
|
||||||
|
|
||||||
|
@api.get("/chat/healthz")
|
||||||
|
async def healthz():
|
||||||
|
return {
|
||||||
|
"status": "ok",
|
||||||
|
"index_ready": INDEX.ready,
|
||||||
|
"documents": len(INDEX.docs),
|
||||||
|
"vectors": await asyncio.to_thread(vectorstore.vector_count),
|
||||||
|
"llm_configured": bool(settings.llm_base_url),
|
||||||
|
"reranker_enabled": settings.reranker_enabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@api.get("/chat/suggestions")
|
||||||
|
async def suggestions():
|
||||||
|
return {"questions": _get_suggestions()}
|
||||||
|
|
||||||
|
|
||||||
|
@api.get("/chat/eval")
|
||||||
|
async def eval_question(message: str = Query(...)):
|
||||||
|
"""مخصوص ارزیابی RAGAS — پاسخ + اسناد بازیابیشده، با thread تازه."""
|
||||||
|
if not INDEX.ready:
|
||||||
|
return {"error": "index not ready", "question": message, "answer": "", "contexts": []}
|
||||||
|
if pipeline.get_llm() is None:
|
||||||
|
return {"error": "llm not configured", "question": message, "answer": "", "contexts": []}
|
||||||
|
|
||||||
|
thread_id = f"eval_{uuid.uuid4().hex[:10]}"
|
||||||
|
config = {"configurable": {"thread_id": thread_id}}
|
||||||
|
inputs = {"messages": [HumanMessage(content=message)]}
|
||||||
|
final_state = await pipeline.graph.ainvoke(inputs, config=config)
|
||||||
|
|
||||||
|
answer = ""
|
||||||
|
for msg in reversed(final_state.get("messages", [])):
|
||||||
|
if isinstance(msg, AIMessage):
|
||||||
|
answer = msg.content if isinstance(msg.content, str) else str(msg.content)
|
||||||
|
break
|
||||||
|
return {
|
||||||
|
"question": message,
|
||||||
|
"contextualized_query": final_state.get("contextualized_query") or message,
|
||||||
|
"answer": answer,
|
||||||
|
"contexts": final_state.get("documents", []),
|
||||||
|
"thread_id": thread_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@api.post("/chat/reindex")
|
||||||
|
async def reindex(request: Request):
|
||||||
|
"""همگامسازی اجباری (امبد دوباره همه سطرها) — با توکن محافظت میشود."""
|
||||||
|
if settings.reindex_token and request.headers.get("x-reindex-token") != settings.reindex_token:
|
||||||
|
raise HTTPException(status_code=403, detail="invalid reindex token")
|
||||||
|
stats = await asyncio.to_thread(sync_index, True)
|
||||||
|
return {"status": "ok", "documents": len(INDEX.docs), "stats": stats}
|
||||||
|
|
||||||
|
|
||||||
|
# سازگاری با eval_ragas.py قدیمی که آدرس را /chat/{username}/eval میسازد
|
||||||
|
|
||||||
|
@api.get("/chat/{scope}/stream")
|
||||||
|
async def stream_chat_scoped(scope: str, message: str = Query(...), thread_id: str = Query(default=""), request: Request = None):
|
||||||
|
return await stream_chat(message=message, thread_id=thread_id, request=request)
|
||||||
|
|
||||||
|
|
||||||
|
@api.get("/chat/{scope}/eval")
|
||||||
|
async def eval_scoped(scope: str, message: str = Query(...)):
|
||||||
|
return await eval_question(message=message)
|
||||||
|
|
||||||
|
|
||||||
|
@api.get("/chat/{scope}/suggestions")
|
||||||
|
async def suggestions_scoped(scope: str):
|
||||||
|
return {"questions": _get_suggestions()}
|
||||||
|
|
||||||
|
|
||||||
|
@api.get("/chat/stream")
|
||||||
|
async def stream_chat(message: str = Query(...), thread_id: str = Query(default=""), request: Request = None):
|
||||||
|
"""استریم توکنبهتوکن پاسخ با SSE."""
|
||||||
|
if _rate_limited(_client_ip(request)):
|
||||||
|
raise HTTPException(status_code=429, detail="تعداد پیامهای شما زیاد است؛ کمی بعد دوباره تلاش کنید.")
|
||||||
|
|
||||||
|
thread_id = thread_id or f"t_{uuid.uuid4().hex[:10]}"
|
||||||
|
|
||||||
|
async def generate():
|
||||||
|
async for chunk in _stream_answer(message, thread_id):
|
||||||
|
yield chunk
|
||||||
|
|
||||||
|
return StreamingResponse(
|
||||||
|
generate(),
|
||||||
|
media_type="text/event-stream",
|
||||||
|
headers={"Cache-Control": "no-cache", "Connection": "keep-alive", "X-Accel-Buffering": "no"},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# --- پیشنهاد پرسشها (کششده) ---
|
||||||
|
|
||||||
|
_suggestions_cache: tuple[float, list[str]] = (0.0, [])
|
||||||
|
|
||||||
|
|
||||||
|
def _get_suggestions() -> list[str]:
|
||||||
|
global _suggestions_cache
|
||||||
|
now = time.time()
|
||||||
|
if _suggestions_cache[1] and now - _suggestions_cache[0] < settings.suggestions_ttl:
|
||||||
|
return _suggestions_cache[1]
|
||||||
|
|
||||||
|
questions: list[str] = []
|
||||||
|
if INDEX.ready:
|
||||||
|
faqs = [d for d in INDEX.docs if d["type"] == "faq"][:8]
|
||||||
|
titles = [d["title"] for d in INDEX.docs if d["type"] == "article"][:10]
|
||||||
|
model = pipeline.get_llm()
|
||||||
|
if model and (faqs or titles):
|
||||||
|
material = "\n".join([d["text"][:200] for d in faqs] + titles)
|
||||||
|
try:
|
||||||
|
prompt = (
|
||||||
|
"بر اساس محتوای سایت «گروه ساختمانی آرامش» ۴ پرسش کوتاه و جذاب بنویس که بازدیدکننده "
|
||||||
|
"احتمالاً میپرسد. هر پرسش حداکثر ۱۰ کلمه و به فارسی.\n"
|
||||||
|
'فقط آرایه JSON رشتهای برگردان: ["پرسش ۱", "پرسش ۲", ...]\n\n'
|
||||||
|
f"محتوا:\n{material[:2000]}"
|
||||||
|
)
|
||||||
|
resp = model.invoke([HumanMessage(content=prompt)])
|
||||||
|
text = resp.content if isinstance(resp.content, str) else ""
|
||||||
|
if text.startswith("```"):
|
||||||
|
text = text.split("```")[1]
|
||||||
|
text = text[4:] if text.startswith("json") else text
|
||||||
|
import re
|
||||||
|
match = re.search(r"\[.*\]", text, re.DOTALL)
|
||||||
|
if match:
|
||||||
|
questions = [q.strip() for q in json.loads(match.group()) if isinstance(q, str) and q.strip()][:4]
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Suggestion generation failed: %s", e)
|
||||||
|
if not questions and faqs:
|
||||||
|
# جایگزین: خود پرسشهای متداول
|
||||||
|
questions = [d["text"].split("\n")[0].removeprefix("پرسش: ") for d in faqs[:4]]
|
||||||
|
|
||||||
|
_suggestions_cache = (now, questions)
|
||||||
|
return questions
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
uvicorn.run(api, host="0.0.0.0", port=8000)
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
"""تنظیمات سرویس چتبات — از متغیرهای محیطی یا فایل .env خوانده میشود."""
|
||||||
|
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
||||||
|
|
||||||
|
# دیتابیس سایت (فقط خواندن) — همان DATABASE_URL بکاند
|
||||||
|
database_url: str = "postgresql+psycopg://aramesh:aramesh@localhost:5432/arameshplaza"
|
||||||
|
# دیتابیس اختصاصی چتبات روی همان نمونه Postgres — بردارها اینجا نوشته میشوند
|
||||||
|
# تا دیتابیس سایت فقطخواندنی بماند؛ در شروع سرویس خودکار ساخته میشود
|
||||||
|
chatbot_db_name: str = "aramesh_chatbot"
|
||||||
|
|
||||||
|
# LLM سازگار با OpenAI — اجباری؛ بدون آن پاسخگویی غیرفعال است
|
||||||
|
llm_base_url: str = ""
|
||||||
|
llm_api_key: str = ""
|
||||||
|
llm_model: str = "openai/gpt-oss-120b"
|
||||||
|
|
||||||
|
# مدلهای بازیابی
|
||||||
|
embedding_model: str = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
||||||
|
# بعد بردار خروجی مدل امبدینگ (MiniLM-L12 = 384) — با تغییر مدل باید عوض شود
|
||||||
|
embedding_dim: int = 384
|
||||||
|
reranker_model: str = "BAAI/bge-reranker-v2-m3"
|
||||||
|
reranker_enabled: bool = True
|
||||||
|
reranker_device: str = "cpu"
|
||||||
|
|
||||||
|
# وزن جستوجوی معنایی در ترکیب نمرهها (۰ = فقط کلیدواژه، ۱ = فقط معنایی)
|
||||||
|
alpha_dense: float = 0.5
|
||||||
|
# تعداد سند نهایی که به LLM داده میشود
|
||||||
|
best_of_k: int = 3
|
||||||
|
# تعداد نتیجه هر پایگاه (معنایی و کلیدواژه) برای هر پرسوجو
|
||||||
|
retrieve_k: int = 12
|
||||||
|
# بازنویسی پرسوجو با تاریخچه گفتگو (فقط وقتی تاریخچه هست صدا زده میشود)
|
||||||
|
contextualize_enabled: bool = True
|
||||||
|
# گسترش پرسوجو به چند نوع — برای پیکره کوچک پیشفرض خاموش (تأخیر کمتر)
|
||||||
|
query_expansion: bool = False
|
||||||
|
|
||||||
|
# مسیر کش مدلها (در داکر روی والیوم /data سوار میشود)
|
||||||
|
hf_cache_dir: str = "/data/hf"
|
||||||
|
|
||||||
|
# محدودسازی نرخ درخواست هر IP (تعداد پیام در پنجره زمانی به ثانیه)
|
||||||
|
rate_limit_messages: int = 20
|
||||||
|
rate_limit_window: int = 300
|
||||||
|
|
||||||
|
# توکن بازسازی ایندکس — اگر خالی باشد مسیر reindex فقط از داخل شبکه کار میکند
|
||||||
|
reindex_token: str = ""
|
||||||
|
|
||||||
|
# ماندگاری کش پیشنهادها و زمینه سایت (ثانیه)
|
||||||
|
suggestions_ttl: int = 600
|
||||||
|
site_context_ttl: int = 120
|
||||||
|
|
||||||
|
# بازسازی خودکار ایندکس وقتی محتوای دیتابیس عوض شود (۰ = خاموش)
|
||||||
|
index_refresh_seconds: int = 600
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
@@ -0,0 +1,315 @@
|
|||||||
|
"""خواندن محتوای سایت از PostgreSQL و تبدیل آن به سندهای قابل ایندکس.
|
||||||
|
|
||||||
|
هر سند یک dict با کلیدهای text / type / title / url است. بدنه مقالهها و
|
||||||
|
پروژهها هم میتواند آرایه پاراگراف باشد و هم سند TipTap (JSON) — هر دو
|
||||||
|
پشتیبانی میشوند. مقالههای پیشنویس ایندکس نمیشوند.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from .persian import normalize
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# URL پایه صفحات — چتبات مسیر نسبی میدهد؛ فرانتاند روی همان دامنه است
|
||||||
|
_ARTICLE_URL = "/blog/{slug}"
|
||||||
|
_PROJECT_URL = "/projects/{slug}"
|
||||||
|
|
||||||
|
|
||||||
|
def _psycopg_dsn(database_url: str) -> str:
|
||||||
|
"""DATABASE_URL فرمت SQLAlchemy (postgresql+psycopg://) را به DSN psycopg3 تبدیل میکند."""
|
||||||
|
return database_url.replace("+psycopg", "").replace("+asyncpg", "")
|
||||||
|
|
||||||
|
|
||||||
|
def _fetch_rows(dsn: str, sql: str) -> list[dict[str, Any]]:
|
||||||
|
import psycopg
|
||||||
|
from psycopg.rows import dict_row
|
||||||
|
|
||||||
|
with psycopg.connect(dsn, row_factory=dict_row, connect_timeout=10) as conn:
|
||||||
|
cur = conn.execute(sql)
|
||||||
|
return cur.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
def tiptap_blocks(node: Any) -> list[str]:
|
||||||
|
"""متن بلوکبهبلوک از سند TipTap (یا هر JSON تو در تو) بیرون میکشد."""
|
||||||
|
if not isinstance(node, dict):
|
||||||
|
return []
|
||||||
|
ntype = node.get("type")
|
||||||
|
if ntype in ("paragraph", "heading", "codeBlock"):
|
||||||
|
text = _inline_text(node).strip()
|
||||||
|
return [text] if text else []
|
||||||
|
blocks: list[str] = []
|
||||||
|
for child in node.get("content") or []:
|
||||||
|
blocks.extend(tiptap_blocks(child))
|
||||||
|
return blocks
|
||||||
|
|
||||||
|
|
||||||
|
def _inline_text(node: dict) -> str:
|
||||||
|
parts: list[str] = []
|
||||||
|
for child in node.get("content") or []:
|
||||||
|
ctype = child.get("type")
|
||||||
|
if ctype == "text":
|
||||||
|
parts.append(child.get("text", ""))
|
||||||
|
elif ctype == "hardBreak":
|
||||||
|
parts.append(" ")
|
||||||
|
elif ctype == "image":
|
||||||
|
alt = (child.get("attrs") or {}).get("alt") or ""
|
||||||
|
if alt:
|
||||||
|
parts.append(f"[تصویر: {alt}]")
|
||||||
|
else:
|
||||||
|
parts.append(_inline_text(child))
|
||||||
|
return "".join(parts)
|
||||||
|
|
||||||
|
|
||||||
|
def body_to_text(body: Any) -> str:
|
||||||
|
"""بدنه مقاله/پروژه (آرایه پاراگراف یا سند TipTap) → متن یکپارچه."""
|
||||||
|
if not body:
|
||||||
|
return ""
|
||||||
|
if isinstance(body, list):
|
||||||
|
# فرمت قدیمی: آرایه رشتهای پاراگرافها
|
||||||
|
return "\n".join(str(p).strip() for p in body if str(p).strip())
|
||||||
|
blocks = tiptap_blocks(body)
|
||||||
|
return "\n".join(b for b in blocks if b)
|
||||||
|
|
||||||
|
|
||||||
|
def _doc(text: str, dtype: str, title: str, url: str) -> dict | None:
|
||||||
|
text = (text or "").strip()
|
||||||
|
if not text:
|
||||||
|
return None
|
||||||
|
return {"text": text, "type": dtype, "title": title, "url": url}
|
||||||
|
|
||||||
|
|
||||||
|
def _clean(value: Any) -> str:
|
||||||
|
return normalize(str(value or "")).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_site_documents(database_url: str) -> list[dict]:
|
||||||
|
"""همه محتوای عمومی سایت را به سند تبدیل میکند (بدون وابستگی به ORM بکاند)."""
|
||||||
|
dsn = _psycopg_dsn(database_url)
|
||||||
|
docs: list[dict] = []
|
||||||
|
|
||||||
|
# --- برند و اطلاعات تماس (رکورد یگانه) ---
|
||||||
|
brand_rows = _fetch_rows(dsn, "SELECT name, phone, email, address FROM brand LIMIT 1")
|
||||||
|
if brand_rows:
|
||||||
|
b = brand_rows[0]
|
||||||
|
contact = "\n".join(
|
||||||
|
filter(None, [
|
||||||
|
f"نام مجموعه: {_clean(b['name'])}",
|
||||||
|
f"تلفن تماس: {_clean(b['phone'])}",
|
||||||
|
f"ایمیل: {_clean(b['email'])}",
|
||||||
|
f"آدرس: {_clean(b['address'])}",
|
||||||
|
])
|
||||||
|
)
|
||||||
|
d = _doc(contact, "contact", "اطلاعات تماس", "/contact")
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- دفاتر ---
|
||||||
|
for row in _fetch_rows(dsn, "SELECT city, address, hours FROM offices ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"دفتر {_clean(row['city'])} — آدرس: {_clean(row['address'])} — ساعت کاری: {_clean(row['hours'])}",
|
||||||
|
"office", f"دفتر {_clean(row['city'])}", "/contact",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- خدمات ---
|
||||||
|
for row in _fetch_rows(dsn, "SELECT title, desc FROM services ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"خدمت «{_clean(row['title'])}»: {_clean(row['desc'])}",
|
||||||
|
"service", _clean(row["title"]), "/services",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- پرسشهای متداول ---
|
||||||
|
for row in _fetch_rows(dsn, "SELECT question, answer FROM faqs ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"پرسش: {_clean(row['question'])}\nپاسخ: {_clean(row['answer'])}",
|
||||||
|
"faq", _clean(row["question"]), "/faq",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- پروژهها ---
|
||||||
|
for row in _fetch_rows(
|
||||||
|
dsn,
|
||||||
|
"SELECT slug, title, location, year, status, area, floors, units, client, summary, body FROM projects ORDER BY position",
|
||||||
|
):
|
||||||
|
meta = "\n".join(filter(None, [
|
||||||
|
f"مکان: {_clean(row['location'])}" if row["location"] else "",
|
||||||
|
f"سال: {_clean(row['year'])}" if row["year"] else "",
|
||||||
|
f"وضعیت: {'در حال ساخت' if row['status'] == 'progress' else 'تکمیلشده'}",
|
||||||
|
f"متراژ: {_clean(row['area'])}" if row["area"] else "",
|
||||||
|
f"طبقات: {_clean(row['floors'])}" if row["floors"] else "",
|
||||||
|
f"واحدها: {_clean(row['units'])}" if row["units"] else "",
|
||||||
|
f"کارفرما: {_clean(row['client'])}" if row["client"] else "",
|
||||||
|
]))
|
||||||
|
text = "\n".join(filter(None, [meta, _clean(row["summary"]), body_to_text(row["body"])]))
|
||||||
|
d = _doc(text, "project", _clean(row["title"]), _PROJECT_URL.format(slug=row["slug"]))
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- مقالهها (فقط منتشرشده) ---
|
||||||
|
for row in _fetch_rows(
|
||||||
|
dsn,
|
||||||
|
"SELECT slug, title, date, category, excerpt, body FROM articles WHERE status = 'published' ORDER BY position",
|
||||||
|
):
|
||||||
|
text = "\n".join(filter(None, [
|
||||||
|
f"دستهبندی: {_clean(row['category'])}" if row["category"] else "",
|
||||||
|
_clean(row["excerpt"]),
|
||||||
|
body_to_text(row["body"]),
|
||||||
|
]))
|
||||||
|
d = _doc(text, "article", _clean(row["title"]), _ARTICLE_URL.format(slug=row["slug"]))
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- مدلهای سرمایهگذاری ---
|
||||||
|
for row in _fetch_rows(dsn, "SELECT title, desc, points FROM investment_models ORDER BY position"):
|
||||||
|
points = "؛ ".join(_clean(p) for p in (row["points"] or []))
|
||||||
|
d = _doc(
|
||||||
|
"\n".join(filter(None, [
|
||||||
|
f"«{_clean(row['title'])}»: {_clean(row['desc'])}",
|
||||||
|
f"نکات: {points}" if points else "",
|
||||||
|
])),
|
||||||
|
"investment", _clean(row["title"]), "/investment",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- مراحل همکاری ---
|
||||||
|
for row in _fetch_rows(dsn, "SELECT step, title, desc FROM process_steps ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"مرحله {_clean(row['step'])} — {_clean(row['title'])}: {_clean(row['desc'])}",
|
||||||
|
"process", _clean(row["title"]), "/investment",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
# --- چرا ما / تیم / نظرسنجی / تایملاین / داستان ما ---
|
||||||
|
for row in _fetch_rows(dsn, "SELECT title, desc FROM why_us ORDER BY position"):
|
||||||
|
d = _doc(f"{_clean(row['title'])}: {_clean(row['desc'])}", "about", _clean(row["title"]), "/about")
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
for row in _fetch_rows(dsn, "SELECT name, role, bio FROM team ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"{_clean(row['name'])} — {_clean(row['role'])}. {_clean(row['bio'])}",
|
||||||
|
"team", _clean(row["name"]), "/about",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
for row in _fetch_rows(dsn, "SELECT name, role, quote FROM testimonials ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"نظر {_clean(row['name'])} ({_clean(row['role'])}): {_clean(row['quote'])}",
|
||||||
|
"testimonial", _clean(row["name"]), "/",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
for row in _fetch_rows(dsn, "SELECT year, title, desc FROM timeline ORDER BY position"):
|
||||||
|
d = _doc(
|
||||||
|
f"سال {_clean(row['year'])} — {_clean(row['title'])}: {_clean(row['desc'])}",
|
||||||
|
"timeline", _clean(row["title"]), "/about",
|
||||||
|
)
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
story_rows = _fetch_rows(dsn, "SELECT title, paragraphs, quote FROM story LIMIT 1")
|
||||||
|
if story_rows and story_rows[0]:
|
||||||
|
s = story_rows[0]
|
||||||
|
paragraphs = "\n".join(_clean(p) for p in (s["paragraphs"] or []) if _clean(p))
|
||||||
|
text = "\n".join(filter(None, [_clean(s["title"]), paragraphs, _clean(s["quote"])]))
|
||||||
|
d = _doc(text, "about", _clean(s["title"]) or "درباره ما", "/about")
|
||||||
|
if d:
|
||||||
|
docs.append(d)
|
||||||
|
|
||||||
|
log.info("Fetched %d documents from site database", len(docs))
|
||||||
|
return docs
|
||||||
|
|
||||||
|
|
||||||
|
def chunk_documents(docs: list[dict], chunk_size: int = 700, overlap: int = 120) -> list[dict]:
|
||||||
|
"""متنهای بلند (مقاله/پروژه) را به قطعات همپوشان تقسیم میکند.
|
||||||
|
|
||||||
|
هر قطعه یک doc_id پایدار میگیرد («type:ترتیب در همان url:شماره قطعه») تا
|
||||||
|
همگامسازی افزایشی pgvector بتواند فقط سطرهای تغییر یافته را امبد کند.
|
||||||
|
"""
|
||||||
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||||
|
|
||||||
|
from .vectorstore import chunk_content_hash
|
||||||
|
|
||||||
|
splitter = RecursiveCharacterTextSplitter(
|
||||||
|
chunk_size=chunk_size,
|
||||||
|
chunk_overlap=overlap,
|
||||||
|
separators=["\n", "۔", ".", "،", " ", ""],
|
||||||
|
)
|
||||||
|
|
||||||
|
# شماره ردیف هر سند در میان سندهای همtype و هم-url (برای FAQs که url یکسان دارند)
|
||||||
|
occurrence: dict[str, int] = {}
|
||||||
|
|
||||||
|
out: list[dict] = []
|
||||||
|
for doc in docs:
|
||||||
|
key = f"{doc['type']}|{doc['url']}"
|
||||||
|
occ = occurrence.get(key, 0)
|
||||||
|
occurrence[key] = occ + 1
|
||||||
|
|
||||||
|
pieces = splitter.split_text(doc["text"]) if len(doc["text"]) > chunk_size * 1.3 else [doc["text"]]
|
||||||
|
for i, piece in enumerate(pieces, 1):
|
||||||
|
chunk = {
|
||||||
|
"doc_id": f"{doc['type']}:{occ}:{i}",
|
||||||
|
"text": piece if len(pieces) == 1 else f"[{doc['title']} — بخش {i} از {len(pieces)}]\n{piece}",
|
||||||
|
"type": doc["type"],
|
||||||
|
"title": doc["title"],
|
||||||
|
"url": doc["url"],
|
||||||
|
}
|
||||||
|
chunk["content_hash"] = chunk_content_hash(chunk)
|
||||||
|
out.append(chunk)
|
||||||
|
|
||||||
|
log.info("Chunked %d documents into %d pieces", len(docs), len(out))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def content_fingerprint(docs: list[dict]) -> str:
|
||||||
|
"""هش کل محتوا — فقط برای لاگ؛ تشخیص تغییر ردیفی در vectorstore انجام میشود."""
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
|
||||||
|
payload = json.dumps(docs, ensure_ascii=False, sort_keys=True)
|
||||||
|
return hashlib.sha256(payload.encode("utf-8")).hexdigest()[:16]
|
||||||
|
|
||||||
|
|
||||||
|
# --- زمینه همیشهتازه سایت (بلوک کوتاه تماس/خدمات برای پرامپت) ---
|
||||||
|
_site_context_cache: tuple[float, str] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_site_context(database_url: str, ttl: int = 120) -> str:
|
||||||
|
"""نام برند، تماس و فهرست خدمات — با کش کوتاهمدت تا هر پیام یک کوئری سبک بزند."""
|
||||||
|
global _site_context_cache
|
||||||
|
import time as _time
|
||||||
|
|
||||||
|
now = _time.time()
|
||||||
|
if _site_context_cache and now - _site_context_cache[0] < ttl:
|
||||||
|
return _site_context_cache[1]
|
||||||
|
try:
|
||||||
|
dsn = _psycopg_dsn(database_url)
|
||||||
|
brand = _fetch_rows(dsn, "SELECT name, phone, email, address FROM brand LIMIT 1")
|
||||||
|
services = _fetch_rows(dsn, "SELECT title FROM services ORDER BY position")
|
||||||
|
lines: list[str] = []
|
||||||
|
if brand:
|
||||||
|
b = brand[0]
|
||||||
|
lines += [
|
||||||
|
f"نام مجموعه: {_clean(b['name'])}",
|
||||||
|
f"تلفن تماس: {_clean(b['phone'])}",
|
||||||
|
f"ایمیل: {_clean(b['email'])}",
|
||||||
|
f"آدرس: {_clean(b['address'])}",
|
||||||
|
]
|
||||||
|
if services:
|
||||||
|
lines.append("خدمات سایت: " + "، ".join(_clean(s["title"]) for s in services))
|
||||||
|
ctx = "\n".join(lines)
|
||||||
|
_site_context_cache = (now, ctx)
|
||||||
|
return ctx
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Site context fetch failed: %s", e)
|
||||||
|
return _site_context_cache[1] if _site_context_cache else ""
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
"""مدل امبدینگ — با لود تنبل تا اولین استفاده (برای تستهای سبک، torch لود نمیشود)."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
from .config import settings
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def get_embeddings():
|
||||||
|
from langchain_huggingface import HuggingFaceEmbeddings
|
||||||
|
|
||||||
|
os.makedirs(settings.hf_cache_dir, exist_ok=True)
|
||||||
|
return HuggingFaceEmbeddings(
|
||||||
|
model_name=settings.embedding_model,
|
||||||
|
model_kwargs={"device": "cpu"},
|
||||||
|
encode_kwargs={"normalize_embeddings": False},
|
||||||
|
cache_folder=settings.hf_cache_dir,
|
||||||
|
)
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
"""ایندکس ترکیبی روی pgvector + BM25 در حافظه.
|
||||||
|
|
||||||
|
بردارها در pgvector (دیتابیس اختصاصی چتبات) بهصورت افزایشی همگام میشوند —
|
||||||
|
فقط سطرهای جدید یا تغییر یافته امبد میشوند. BM25 چون سبک است در هر شروع
|
||||||
|
از روی همان سطرها ساخته میشود و نیازی به کش دیسکی ندارد.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
from rank_bm25 import BM25Okapi
|
||||||
|
|
||||||
|
from . import content as content_mod
|
||||||
|
from .config import settings
|
||||||
|
from .persian import tokenize
|
||||||
|
from . import vectorstore
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SiteIndex:
|
||||||
|
"""حالت در حافظهٔ بازیابی — سندها (برای BM25) و وضعیت آماده بودن."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.docs: list[dict] = []
|
||||||
|
self.bm25: BM25Okapi | None = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ready(self) -> bool:
|
||||||
|
return self.bm25 is not None and bool(self.docs)
|
||||||
|
|
||||||
|
|
||||||
|
INDEX = SiteIndex()
|
||||||
|
|
||||||
|
|
||||||
|
def sync_index(force_rebuild: bool = False) -> dict:
|
||||||
|
"""همگامسازی کامل: محتوا → قطعه → pgvector (افزایشی) → ساخت BM25.
|
||||||
|
|
||||||
|
خروجی: آمار همگامسازی. خطای اتصال به دیتابیس بالا فرستاده میشود.
|
||||||
|
"""
|
||||||
|
start = time.time()
|
||||||
|
|
||||||
|
vectorstore.ensure_schema()
|
||||||
|
|
||||||
|
docs = content_mod.fetch_site_documents(settings.database_url)
|
||||||
|
chunks = content_mod.chunk_documents(docs)
|
||||||
|
stats = vectorstore.sync_chunks(chunks, force=force_rebuild)
|
||||||
|
|
||||||
|
INDEX.docs = vectorstore.fetch_all_chunks()
|
||||||
|
corpus_tokens = [tokenize(d["text"]) for d in INDEX.docs]
|
||||||
|
INDEX.bm25 = BM25Okapi(corpus_tokens) if corpus_tokens else None
|
||||||
|
|
||||||
|
log.info("Index ready in %.1fs (%d vectors, %d docs, %s)",
|
||||||
|
time.time() - start, len(INDEX.docs), len(chunks), stats)
|
||||||
|
return stats
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
"""نرمالسازی متن فارسی و توکنسازی برای جستوجوی کلیدواژه (BM25).
|
||||||
|
|
||||||
|
بدون وابستگی سنگین — فقط استانداردسازی نویسهها، حذف اعراب و نیمفاصلههای
|
||||||
|
مشکلی و شکستن کلمههای مرکب روی نیمفاصله برای بهبود بازیابی.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
# نویسههای عربی که در متن فارسی اشتباهاً استفاده میشوند
|
||||||
|
_CHAR_MAP = {
|
||||||
|
"\u064a": "\u06cc", # ي → ی
|
||||||
|
"\u0643": "\u06a9", # ك → ک
|
||||||
|
"\u0623": "\u0627", # أ → ا
|
||||||
|
"\u0625": "\u0627", # إ → ا
|
||||||
|
"\u0671": "\u0627", # ٱ → ا
|
||||||
|
"\u0629": "\u0647", # ة → ه
|
||||||
|
"\u0624": "\u0648", # ؤ → و
|
||||||
|
"\u0626": "\u06cc", # ئ → ی
|
||||||
|
"\u0640": "", # ـ (کشیده) حذف
|
||||||
|
}
|
||||||
|
|
||||||
|
# اعراب و علامتهای اضافی
|
||||||
|
_DIACRITICS = re.compile(r"[\u064b-\u0652\u0670\u0653-\u0655\u0640]")
|
||||||
|
|
||||||
|
# ارقام فارسی و عربی → ASCII
|
||||||
|
_DIGITS = str.maketrans({
|
||||||
|
"\u06f0": "0", "\u06f1": "1", "\u06f2": "2", "\u06f3": "3", "\u06f4": "4",
|
||||||
|
"\u06f5": "5", "\u06f6": "6", "\u06f7": "7", "\u06f8": "8", "\u06f9": "9",
|
||||||
|
"\u0660": "0", "\u0661": "1", "\u0662": "2", "\u0663": "3", "\u0664": "4",
|
||||||
|
"\u0665": "5", "\u0666": "6", "\u0667": "7", "\u0668": "8", "\u0669": "9",
|
||||||
|
})
|
||||||
|
|
||||||
|
# واژههای دستوری پرتکرار — در جستوجو بیمعنا هستند
|
||||||
|
STOPWORDS = frozenset("""
|
||||||
|
و در به از که با این آن است هست بود بودن شد شدن شود کرد کردن کرده کند کنی
|
||||||
|
برای را های ها هایی هم نیز اما یا اگر تا بر روی زیر بین پس قبل بعد همه هر چه
|
||||||
|
چیزی کس کسی کجا چرا چگونه چطور چند یکی دو سه ما شما آنها ایشان من تو او
|
||||||
|
کدام کی چی چهقدر چقدر
|
||||||
|
می نخواهد خواهد باشد باشند هستیم هستند بودم بودیم دارد دارند داشته داد دهد
|
||||||
|
بله خیر آیا فقط نیز خیلی بسیار کمی دیگر دیگری همان همین وقتی زمانی وقتیکه
|
||||||
|
اساساً لطفاً میشه میکنه هستن هستم
|
||||||
|
""".split())
|
||||||
|
|
||||||
|
# علائم نگارشی فارسی/لاتین — قبل از توکنسازی حذف میشوند (نیمفاصله جزو اینها نیست)
|
||||||
|
_PUNCT_RE = re.compile(r"[،؛؟!,;:\?\"'()\[\]{}«»….*#@&%+=/\\|~^$\u066d\u066c\u066b]+")
|
||||||
|
|
||||||
|
# توکن: حروف/ارقام فارسی و لاتین + نیمفاصله در میان کلمه
|
||||||
|
_TOKEN_RE = re.compile(r"[\w\u0600-\u06ff\u200c]+")
|
||||||
|
|
||||||
|
|
||||||
|
def normalize(text: str) -> str:
|
||||||
|
"""نویسههای غیراستاندارد عربی/فارسی را به شکل یکدست فارسی میبرد."""
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
out = []
|
||||||
|
for ch in text:
|
||||||
|
out.append(_CHAR_MAP.get(ch, ch))
|
||||||
|
text = "".join(out)
|
||||||
|
text = _DIACRITICS.sub("", text)
|
||||||
|
text = text.translate(_DIGITS)
|
||||||
|
# فاصلههای تکراری را یکی کن — نیمفاصله عمداً حفظ میشود (میشود)
|
||||||
|
text = re.sub(r"[ \t\r\n]+", " ", text)
|
||||||
|
return text.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def tokenize(text: str) -> list[str]:
|
||||||
|
"""توکنهای نرمالشده برای BM25 — کلمههای مرکب روی نیمفاصله هم جدا ایندکس میشوند."""
|
||||||
|
text = _PUNCT_RE.sub(" ", normalize(text).lower())
|
||||||
|
tokens = []
|
||||||
|
for raw in _TOKEN_RE.findall(text):
|
||||||
|
if raw in STOPWORDS or len(raw) < 2:
|
||||||
|
continue
|
||||||
|
tokens.append(raw)
|
||||||
|
# «میشود» → می + شود (فراخوانی بیشتر برای بازیابی)
|
||||||
|
if "\u200c" in raw:
|
||||||
|
for part in raw.split("\u200c"):
|
||||||
|
if len(part) >= 2 and part not in STOPWORDS:
|
||||||
|
tokens.append(part)
|
||||||
|
return tokens
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
"""گراف گفتگو (LangGraph) — پورت خط لوله v13 در سه گام:
|
||||||
|
|
||||||
|
۱) بازنویسی پرسش با تاریخچه (فقط وقتی تاریخچه هست — صرفهجویی در تأخیر)
|
||||||
|
۲) بازیابی ترکیبی با چند نوعواژه (اختیاری)
|
||||||
|
۳) تولید پاسخ فارسی با قواعد تکیه بر داده
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import json as _json
|
||||||
|
from typing import Annotated, List, Optional
|
||||||
|
|
||||||
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
|
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||||
|
from langchain_openai import ChatOpenAI
|
||||||
|
from langgraph.checkpoint.memory import MemorySaver
|
||||||
|
from langgraph.graph import END, StateGraph
|
||||||
|
from langgraph.graph.message import add_messages
|
||||||
|
|
||||||
|
from . import content as content_mod
|
||||||
|
from .config import settings
|
||||||
|
from .retrieval import retrieve_final
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
FALLBACK_MESSAGE = (
|
||||||
|
"متأسفانه اطلاعات دقیقی در این باره در سایت ثبت نشده است. "
|
||||||
|
"برای راهنمایی بیشتر میتوانید از صفحه «تماس با ما» با ما در ارتباط باشید."
|
||||||
|
)
|
||||||
|
LLM_UNAVAILABLE = (
|
||||||
|
"سرویس هوش مصنوعی هنوز پیکربندی نشده است. "
|
||||||
|
"متغیرهای LLM_BASE_URL و LLM_MODEL را در تنظیمات سرویس چتبات تنظیم کنید."
|
||||||
|
)
|
||||||
|
|
||||||
|
_llm = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_llm() -> ChatOpenAI | None:
|
||||||
|
"""LLM تولید — اگر پیکربندی نشده باشد None برمیگرداند."""
|
||||||
|
global _llm
|
||||||
|
if _llm is None and settings.llm_base_url:
|
||||||
|
_llm = ChatOpenAI(
|
||||||
|
model=settings.llm_model,
|
||||||
|
base_url=settings.llm_base_url,
|
||||||
|
api_key=settings.llm_api_key or "not-needed",
|
||||||
|
temperature=0,
|
||||||
|
streaming=True,
|
||||||
|
)
|
||||||
|
return _llm
|
||||||
|
|
||||||
|
|
||||||
|
class GraphState(TypedDict):
|
||||||
|
messages: Annotated[List, add_messages]
|
||||||
|
documents: List[str]
|
||||||
|
contextualized_query: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
|
# --- گام ۱: بازنویسی پرسش با تاریخچه ---
|
||||||
|
|
||||||
|
def contextualize_query(question: str, history: list) -> str:
|
||||||
|
"""ضمیرها و اشارههای پرسش را با تاریخچه گفتگو مشخص میکند («اون دومی» → موضوع واقعی)."""
|
||||||
|
model = get_llm()
|
||||||
|
if model is None or not history:
|
||||||
|
return question
|
||||||
|
try:
|
||||||
|
history_text = "\n".join(
|
||||||
|
f"{'کاربر' if isinstance(m, HumanMessage) else 'دستیار'}: {str(m.content)[:300]}"
|
||||||
|
for m in history[-6:]
|
||||||
|
)
|
||||||
|
prompt = (
|
||||||
|
"با توجه به تاریخچه گفتگو، پرسش کاربر را به یک پرسش مستقل و کامل فارسی بازنویسی کن.\n"
|
||||||
|
"- ضمیرها و اشارهها (او، آن، همین، دومی، این یکی) را به موضوع واقعیِ ذکرشده در تاریخچه تبدیل کن.\n"
|
||||||
|
"- اگر پرسش از قبل مستقل است، عیناً همان را برگردان.\n"
|
||||||
|
"- فقط متن پرسش بازنویسیشده را برگردان؛ هیچ توضیحی ننویس.\n\n"
|
||||||
|
f"تاریخچه گفتگو:\n{history_text}\n\n"
|
||||||
|
f"پرسش کاربر: {question}\n\n"
|
||||||
|
"پرسش مستقل:"
|
||||||
|
)
|
||||||
|
resp = model.invoke([HumanMessage(content=prompt)], config={"tags": ["utility"]})
|
||||||
|
rewritten = (resp.content or "").strip() if isinstance(resp.content, str) else ""
|
||||||
|
if not rewritten or len(rewritten) > len(question) * 4:
|
||||||
|
return question
|
||||||
|
if rewritten != question:
|
||||||
|
log.info("Contextualized: '%s' → '%s'", question[:60], rewritten[:60])
|
||||||
|
return rewritten
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Contextualization failed: %s", e)
|
||||||
|
return question
|
||||||
|
|
||||||
|
|
||||||
|
def expand_query(query: str) -> list[str]:
|
||||||
|
"""گسترش پرسش به چند نوعواژه (اختیاری — پیشفرض خاموش برای پیکره کوچک)."""
|
||||||
|
model = get_llm()
|
||||||
|
if model is None:
|
||||||
|
return [query]
|
||||||
|
try:
|
||||||
|
prompt = (
|
||||||
|
"برای جستوجو در محتوای سایت یک گروه ساختمانی، ۳ نوعواژه جایگزین برای این پرسش بساز: "
|
||||||
|
"بازنویسی معنایی، استخراج کلیدواژه، گسترش با مفاهیم مرتبط.\n"
|
||||||
|
'فقط JSON برگردان: {"variants": ["۱", "۲", "۳"]}\n\n'
|
||||||
|
f"پرسش: {query}"
|
||||||
|
)
|
||||||
|
resp = model.invoke([HumanMessage(content=prompt)], config={"tags": ["utility"]})
|
||||||
|
text = resp.content if isinstance(resp.content, str) else ""
|
||||||
|
match = re.search(r'\{.*"variants".*\}', text, re.DOTALL)
|
||||||
|
if match:
|
||||||
|
variants = [v for v in _json.loads(match.group()).get("variants", []) if v and v != query][:3]
|
||||||
|
if variants:
|
||||||
|
log.info("Query variants: %s", [query] + variants)
|
||||||
|
return [query] + variants
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Query expansion failed: %s", e)
|
||||||
|
return [query]
|
||||||
|
|
||||||
|
|
||||||
|
# --- گام ۲: بازیابی ---
|
||||||
|
|
||||||
|
def retrieve_node(state: GraphState) -> dict:
|
||||||
|
messages = state.get("messages", [])
|
||||||
|
if not messages:
|
||||||
|
return {"documents": []}
|
||||||
|
question = messages[-1].content
|
||||||
|
history = messages[:-1]
|
||||||
|
|
||||||
|
query = contextualize_query(question, history) if settings.contextualize_enabled else question
|
||||||
|
variants = expand_query(query) if settings.query_expansion else [query]
|
||||||
|
|
||||||
|
docs = retrieve_final(query, variants)
|
||||||
|
doc_texts = [f"{d['text']}\n(منبع: {d['title']} — {d['url']})" for d in docs]
|
||||||
|
log.info("Retrieved %d documents for '%s'", len(doc_texts), question[:60])
|
||||||
|
return {
|
||||||
|
"documents": doc_texts,
|
||||||
|
"contextualized_query": query if query != question else None,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# --- گام ۳: تولید پاسخ ---
|
||||||
|
|
||||||
|
def build_system_prompt(documents: list[str], contextualized_query: str | None) -> str:
|
||||||
|
site_ctx = content_mod.fetch_site_context(settings.database_url, settings.site_context_ttl)
|
||||||
|
docs_block = "\n\n———\n\n".join(documents) if documents else "(سند مرتبطی یافت نشد)"
|
||||||
|
reminder = (
|
||||||
|
f"\n--- یادآوری ---\nپرسش کاربر پس از بازنویسی این است: «{contextualized_query}»\n\n"
|
||||||
|
if contextualized_query else ""
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
"تو دستیار گفتگوی هوشمند سایت «گروه ساختمانی آرامش» هستی و فقط به فارسی پاسخ میدهی.\n"
|
||||||
|
"دانش تو اکیداً محدود به اطلاعات زیر است.\n\n"
|
||||||
|
f"--- اطلاعات پایه سایت (همیشه معتبر) ---\n{site_ctx}\n\n"
|
||||||
|
f"--- اسناد بازیابیشده مرتبط با پرسش ---\n{docs_block}\n"
|
||||||
|
f"{reminder}"
|
||||||
|
"--- قوانین پاسخ ---\n"
|
||||||
|
"۱. فقط بر اساس اطلاعات بالا پاسخ بده؛ بههیچوجه از دانش بیرونی استفاده نکن و چیزی نساز.\n"
|
||||||
|
"۲. پاسخ کوتاه، دقیق و مفید باشد (معمولاً ۲ تا ۵ جمله) و لحن حرفهای و محترمانه داشته باشد.\n"
|
||||||
|
f"۳. اگر اطلاعات کافی در اسناد نیست، فقط همین جمله را بنویس: «{FALLBACK_MESSAGE}»\n"
|
||||||
|
"۴. اگر سند مسیر صفحه مرتبط دارد، در پایان پاسخ آن را معرفی کن (مثلاً: جزئیات کامل در صفحه /blog/... ).\n"
|
||||||
|
"۵. درباره قیمت، زمان دقیق یا هر تعهدی فقط چیزی را بگو که در دادهها آمده است."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_node(state: GraphState) -> dict:
|
||||||
|
messages = state.get("messages", [])
|
||||||
|
question = messages[-1].content if messages else ""
|
||||||
|
|
||||||
|
model = get_llm()
|
||||||
|
if model is None:
|
||||||
|
return {"messages": [AIMessage(content=LLM_UNAVAILABLE)]}
|
||||||
|
|
||||||
|
documents = state.get("documents") or []
|
||||||
|
system = build_system_prompt(documents, state.get("contextualized_query"))
|
||||||
|
log.info("Generating answer with %d docs (system prompt %d chars)", len(documents), len(system))
|
||||||
|
|
||||||
|
history = messages[:-1]
|
||||||
|
reply = model.invoke(
|
||||||
|
[SystemMessage(content=system), *history, HumanMessage(content=question)],
|
||||||
|
config={"tags": ["generate"]},
|
||||||
|
)
|
||||||
|
return {"messages": [reply]}
|
||||||
|
|
||||||
|
|
||||||
|
# --- گراف ---
|
||||||
|
|
||||||
|
memory = MemorySaver()
|
||||||
|
workflow = StateGraph(GraphState)
|
||||||
|
workflow.add_node("retrieve", retrieve_node)
|
||||||
|
workflow.add_node("generate", generate_node)
|
||||||
|
workflow.set_entry_point("retrieve")
|
||||||
|
workflow.add_edge("retrieve", "generate")
|
||||||
|
workflow.add_edge("generate", END)
|
||||||
|
graph = workflow.compile(checkpointer=memory)
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# سرویس چتبات — بازیابی ترکیبی روی محتوای سایت
|
||||||
|
fastapi>=0.115
|
||||||
|
uvicorn[standard]>=0.30
|
||||||
|
pydantic-settings>=2.3
|
||||||
|
|
||||||
|
# گراف گفتگو و LLM
|
||||||
|
langgraph>=0.2.60
|
||||||
|
langchain>=0.3
|
||||||
|
langchain-openai>=0.2
|
||||||
|
langchain-text-splitters>=0.3
|
||||||
|
|
||||||
|
# بازیابی — بردارها در pgvector میروند؛ BM25 و خوشهبندی در حافظه
|
||||||
|
sentence-transformers>=3.0
|
||||||
|
langchain-huggingface>=0.1
|
||||||
|
rank-bm25>=0.2.2
|
||||||
|
scikit-learn>=1.4
|
||||||
|
numpy>=1.26
|
||||||
|
|
||||||
|
# دیتابیس (فقط خواندن)
|
||||||
|
psycopg[binary]>=3.1
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
"""مدل ریرنکر BGE — با لود تنبل و امتیازدهی دستهای."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
from .config import settings
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class _CrossEncoder:
|
||||||
|
"""غلاف سبک روی AutoModelForSequenceClassification — API شبیه rank_bm25."""
|
||||||
|
def __init__(self) -> None:
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
||||||
|
|
||||||
|
os.makedirs(settings.hf_cache_dir, exist_ok=True)
|
||||||
|
self._torch = torch
|
||||||
|
self.tokenizer = AutoTokenizer.from_pretrained(settings.reranker_model, cache_dir=settings.hf_cache_dir)
|
||||||
|
self.model = AutoModelForSequenceClassification.from_pretrained(
|
||||||
|
settings.reranker_model, cache_dir=settings.hf_cache_dir
|
||||||
|
)
|
||||||
|
device = settings.reranker_device if settings.reranker_device == "cuda" and torch.cuda.is_available() else "cpu"
|
||||||
|
self.model.to(device)
|
||||||
|
self.model.eval()
|
||||||
|
self.device = device
|
||||||
|
log.info("Reranker loaded on %s", device)
|
||||||
|
|
||||||
|
def score(self, pairs: list[list[str]], batch_size: int = 16) -> list[float]:
|
||||||
|
torch = self._torch
|
||||||
|
out: list[float] = []
|
||||||
|
with torch.no_grad():
|
||||||
|
for i in range(0, len(pairs), batch_size):
|
||||||
|
inputs = self.tokenizer(
|
||||||
|
pairs[i:i + batch_size],
|
||||||
|
padding=True,
|
||||||
|
truncation=True,
|
||||||
|
max_length=512,
|
||||||
|
return_tensors="pt",
|
||||||
|
).to(self.device)
|
||||||
|
logits = self.model(**inputs).logits.squeeze(-1)
|
||||||
|
out.extend(logits.cpu().tolist())
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def get_reranker() -> _CrossEncoder:
|
||||||
|
return _CrossEncoder()
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
"""بازیابی ترکیبی — پورت خط لوله v13 با سه تغییر برای این سایت:
|
||||||
|
|
||||||
|
۱) پایگاه sparse بهجای SPLADE (انگلیسیمحور و بیفایده روی فارسی) از BM25
|
||||||
|
با نرمالسازی فارسی استفاده میکند.
|
||||||
|
۲) جستوجوی معنایی بهجای FAISS با pgvector و فاصله کسینوسی در SQL انجام
|
||||||
|
میشود (همگامسازی افزایشی، فیلترپذیر، بدون فایل ایندکس جدا).
|
||||||
|
۳) مسیریابی alpha با LLM حذف شده و وزن ثابت تنظیمپذیر است — برای پیکرهٔ
|
||||||
|
کوچک سایت، هزینهٔ یک فراخوانی LLM اضافی بهازای هر پیام نمیارزد.
|
||||||
|
بقیهٔ منطق (نرمالسازی نمره، فیوژن وزندار، برش خوشهبندی، ریرنک) عین v13 است.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
from . import vectorstore
|
||||||
|
from .config import settings
|
||||||
|
from .indexing import INDEX
|
||||||
|
from .persian import tokenize
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def dense_retrieve(query: str, top_k: int) -> list[tuple[dict, float]]:
|
||||||
|
"""جستوجوی معنایی pgvector — خروجی: (سند، شباهت کسینوسی)."""
|
||||||
|
return vectorstore.dense_search(query, top_k)
|
||||||
|
|
||||||
|
|
||||||
|
def keyword_retrieve(query: str, top_k: int) -> list[tuple[dict, float]]:
|
||||||
|
"""جستوجوی کلیدواژه BM25 روی متن نرمالشده فارسی."""
|
||||||
|
if INDEX.bm25 is None:
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
tokens = tokenize(query)
|
||||||
|
if not tokens:
|
||||||
|
return []
|
||||||
|
scores = INDEX.bm25.get_scores(tokens)
|
||||||
|
ranked = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:top_k]
|
||||||
|
return [(INDEX.docs[i], float(scores[i])) for i in ranked if scores[i] > 0]
|
||||||
|
except Exception as e:
|
||||||
|
log.error("BM25 retrieval failed: %s", e)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def _minmax(scores: list[float]) -> list[float]:
|
||||||
|
if not scores:
|
||||||
|
return []
|
||||||
|
lo, hi = min(scores), max(scores)
|
||||||
|
if hi == lo:
|
||||||
|
return [0.5] * len(scores)
|
||||||
|
return [(s - lo) / (hi - lo) for s in scores]
|
||||||
|
|
||||||
|
|
||||||
|
def hybrid_retrieve(query: str, alpha: float | None = None, top_k: int | None = None) -> list[tuple[dict, float]]:
|
||||||
|
"""ترکیب وزندار نمره معنایی و کلیدواژه؛ خروجی مرتبشده بر اساس نمره ترکیبی."""
|
||||||
|
alpha = settings.alpha_dense if alpha is None else alpha
|
||||||
|
top_k = top_k or settings.retrieve_k
|
||||||
|
|
||||||
|
dense = dense_retrieve(query, top_k)
|
||||||
|
keyword = keyword_retrieve(query, top_k)
|
||||||
|
log.info("Retrieval '%s': dense=%d, keyword=%d", query[:60], len(dense), len(keyword))
|
||||||
|
if not dense and not keyword:
|
||||||
|
return []
|
||||||
|
|
||||||
|
dense_norm = _minmax([s for _, s in dense])
|
||||||
|
keyword_norm = _minmax([s for _, s in keyword])
|
||||||
|
|
||||||
|
pool: dict = {} # doc_id → {doc, dense_max, kw_max}
|
||||||
|
for (doc, _), norm in zip(dense, dense_norm):
|
||||||
|
entry = pool.setdefault(doc["doc_id"], {"doc": doc, "dense_max": 0.0, "kw_max": 0.0})
|
||||||
|
entry["dense_max"] = max(entry["dense_max"], norm)
|
||||||
|
for (doc, _), norm in zip(keyword, keyword_norm):
|
||||||
|
entry = pool.setdefault(doc["doc_id"], {"doc": doc, "dense_max": 0.0, "kw_max": 0.0})
|
||||||
|
entry["kw_max"] = max(entry["kw_max"], norm)
|
||||||
|
|
||||||
|
combined = [
|
||||||
|
(entry["doc"], alpha * entry["dense_max"] + (1 - alpha) * entry["kw_max"])
|
||||||
|
for entry in pool.values()
|
||||||
|
]
|
||||||
|
combined.sort(key=lambda x: x[1], reverse=True)
|
||||||
|
return combined
|
||||||
|
|
||||||
|
|
||||||
|
def cluster_cutoff(scored: list[tuple[dict, float]], silhouette_threshold: float = 0.55) -> list[tuple[dict, float]]:
|
||||||
|
"""برش خوشهای v13 — مرز طبیعی کیفیت بهجای top-k دلخواه.
|
||||||
|
|
||||||
|
روی نمرههای ۱بعدی K-Means با K=2 میزند؛ اگر جدایی خوشهها معنادار
|
||||||
|
باشد (silhouette ≥ 0.55) فقط خوشهٔ بالا را نگه میدارد.
|
||||||
|
"""
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
n = len(scored)
|
||||||
|
if n < 4:
|
||||||
|
return scored
|
||||||
|
try:
|
||||||
|
from sklearn.cluster import KMeans
|
||||||
|
from sklearn.metrics import silhouette_score
|
||||||
|
|
||||||
|
scores = np.array([s for _, s in scored], dtype=float).reshape(-1, 1)
|
||||||
|
kmeans = KMeans(n_clusters=2, random_state=42, n_init=10)
|
||||||
|
labels = kmeans.fit_predict(scores)
|
||||||
|
sil = silhouette_score(scores, labels)
|
||||||
|
if sil < silhouette_threshold:
|
||||||
|
log.info("Clustering: silhouette=%.3f < %.2f — keeping all %d docs", sil, silhouette_threshold, n)
|
||||||
|
return scored
|
||||||
|
centers = kmeans.cluster_centers_.flatten()
|
||||||
|
good_label = int(np.argmax(centers))
|
||||||
|
cutoff = int(np.sum(labels == good_label))
|
||||||
|
cutoff = max(cutoff, 1)
|
||||||
|
log.info("Clustering: silhouette=%.3f — cutting at %d/%d docs", sil, cutoff, n)
|
||||||
|
return scored[:cutoff]
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Clustering failed (%s) — keeping all docs", e)
|
||||||
|
return scored
|
||||||
|
|
||||||
|
|
||||||
|
def rerank(query: str, scored: list[tuple[dict, float]], top_k: int) -> list[tuple[dict, float]]:
|
||||||
|
"""ریرنک کراسانکودر BGE — مرتبسازی نهایی بر اساس شباهت واقعی پرسش/سند."""
|
||||||
|
if not scored:
|
||||||
|
return []
|
||||||
|
if not settings.reranker_enabled:
|
||||||
|
return scored[:top_k]
|
||||||
|
|
||||||
|
try:
|
||||||
|
from .reranker import get_reranker
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
pairs = [[query, doc["text"]] for doc, _ in scored]
|
||||||
|
scores = get_reranker().score(pairs)
|
||||||
|
rescored = sorted(
|
||||||
|
((doc, float(s)) for (doc, _), s in zip(scored, scores)),
|
||||||
|
key=lambda x: x[1],
|
||||||
|
reverse=True,
|
||||||
|
)
|
||||||
|
log.info("Reranked %d docs in %.2fs — top scores: %s",
|
||||||
|
len(scored), time.time() - start,
|
||||||
|
[f"{s:.2f}" for _, s in rescored[:3]])
|
||||||
|
return rescored[:top_k]
|
||||||
|
except Exception as e:
|
||||||
|
log.error("Reranking failed (%s) — falling back to fusion order", e)
|
||||||
|
return scored[:top_k]
|
||||||
|
|
||||||
|
|
||||||
|
def retrieve_final(query: str, query_variants: list[str] | None = None) -> list[dict]:
|
||||||
|
"""خط کامل بازیابی برای یک پرسش: هر نوع → فیوژن → برش خوشه → ریرنک → K نهایی."""
|
||||||
|
variants = query_variants or [query]
|
||||||
|
|
||||||
|
pool: dict = {}
|
||||||
|
for variant in variants:
|
||||||
|
for doc, score in hybrid_retrieve(variant):
|
||||||
|
entry = pool.setdefault(doc["doc_id"], {"doc": doc, "score": 0.0})
|
||||||
|
entry["score"] = max(entry["score"], score)
|
||||||
|
|
||||||
|
combined = sorted(((e["doc"], e["score"]) for e in pool.values()),
|
||||||
|
key=lambda x: x[1], reverse=True)
|
||||||
|
if not combined:
|
||||||
|
return []
|
||||||
|
|
||||||
|
filtered = cluster_cutoff(combined)
|
||||||
|
# ریرنک روی نتیجههای برشخورده (سقف retrieve_k تا تأخیر کنترل شود)
|
||||||
|
rerank_candidates = filtered[: settings.retrieve_k]
|
||||||
|
final = rerank(query, rerank_candidates, settings.best_of_k)
|
||||||
|
|
||||||
|
log.info("Final retrieval: %d docs (from %d candidates, %d variants)",
|
||||||
|
len(final), len(combined), len(variants))
|
||||||
|
return [doc for doc, _ in final]
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
"""تستهای خالص (بدون مدل/دیتابیس) برای ماژولهای چتبات — اجرا: python test_pure.py"""
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||||
|
|
||||||
|
from chatbot.content import chunk_documents, tiptap_blocks, body_to_text
|
||||||
|
from chatbot.vectorstore import chunk_content_hash, chatbot_dsn
|
||||||
|
from chatbot.persian import normalize, tokenize
|
||||||
|
|
||||||
|
docs = [
|
||||||
|
{"text": "پاراگراف کوتاه خدمت الف", "type": "service", "title": "الف", "url": "/services"},
|
||||||
|
{"text": "سؤال اول؟", "type": "faq", "title": "س۱", "url": "/faq"},
|
||||||
|
{"text": "سؤال دوم؟", "type": "faq", "title": "س۲", "url": "/faq"},
|
||||||
|
{"text": "آ" * 2000, "type": "article", "title": "مقاله بلند", "url": "/blog/long"},
|
||||||
|
]
|
||||||
|
chunks = chunk_documents(docs)
|
||||||
|
ids = [c["doc_id"] for c in chunks]
|
||||||
|
assert len(ids) == len(set(ids)), f"duplicate ids: {ids}"
|
||||||
|
|
||||||
|
faq_ids = sorted(c["doc_id"] for c in chunks if c["type"] == "faq")
|
||||||
|
assert faq_ids == ["faq:0:1", "faq:1:1"], faq_ids
|
||||||
|
|
||||||
|
art = [c for c in chunks if c["type"] == "article"]
|
||||||
|
assert len(art) >= 2 and art[1]["doc_id"] == "article:0:2", [x["doc_id"] for x in art]
|
||||||
|
assert "بخش 2 از" in art[1]["text"] and "بخش 1 از" in art[0]["text"]
|
||||||
|
|
||||||
|
c = chunks[0]
|
||||||
|
assert c["content_hash"] == chunk_content_hash(c)
|
||||||
|
c2 = dict(c, title="عنوان عوض شد")
|
||||||
|
# hash فیلد کپیشده است — باید با محاسبه دوباره مقایسه شود، نه با فیلد قدیمی
|
||||||
|
assert chunk_content_hash(c2) != c["content_hash"]
|
||||||
|
|
||||||
|
chunks2 = chunk_documents(docs)
|
||||||
|
assert [x["doc_id"] for x in chunks2] == ids
|
||||||
|
assert [x["content_hash"] for x in chunks2] == [x["content_hash"] for x in chunks]
|
||||||
|
|
||||||
|
settings_dsn = chatbot_dsn()
|
||||||
|
assert settings_dsn.endswith("/aramesh_chatbot"), settings_dsn
|
||||||
|
assert "+psycopg" not in settings_dsn
|
||||||
|
|
||||||
|
# نرمالسازی و توکنسازی فارسی
|
||||||
|
assert normalize("كامل يك ۱۲۳") == "کامل یک 123"
|
||||||
|
t = tokenize("کدام شرکت ساختمانی خوب در تهران؟")
|
||||||
|
assert "شرکت" in t and "تهران" in t and "کدام" not in t, t
|
||||||
|
assert "می\u200cسازیم" in tokenize("می\u200cسازیم") and "سازیم" in tokenize("می\u200cسازیم")
|
||||||
|
|
||||||
|
# استخراج متن TipTap و فرمت قدیمی
|
||||||
|
tiptap_doc = {
|
||||||
|
"type": "doc",
|
||||||
|
"content": [
|
||||||
|
{"type": "heading", "attrs": {"level": 2}, "content": [{"type": "text", "text": "معرفی پروژه"}]},
|
||||||
|
{"type": "paragraph", "content": [
|
||||||
|
{"type": "text", "text": "این برج در تهران "},
|
||||||
|
{"type": "text", "marks": [{"type": "bold"}], "text": "ساخته شد"},
|
||||||
|
{"type": "hardBreak"},
|
||||||
|
{"type": "image", "attrs": {"alt": "نمای بیرونی"}},
|
||||||
|
]},
|
||||||
|
{"type": "bulletList", "content": [
|
||||||
|
{"type": "listItem", "content": [{"type": "paragraph", "content": [{"type": "text", "text": "۲۴ طبقه"}]}]},
|
||||||
|
]},
|
||||||
|
{"type": "paragraph", "content": [{"type": "image", "attrs": {"alt": ""}}]},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
blocks = tiptap_blocks(tiptap_doc)
|
||||||
|
assert blocks == ["معرفی پروژه", "این برج در تهران ساخته شد [تصویر: نمای بیرونی]", "۲۴ طبقه"], blocks
|
||||||
|
assert body_to_text(["الف", "ب"]) == "الف\nب"
|
||||||
|
assert body_to_text(None) == ""
|
||||||
|
|
||||||
|
print(f"ALL PURE TESTS OK — {len(chunks)} chunks from {len(docs)} docs")
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
"""ذخیرهساز برداری pgvector — جایگزین FAISS در مهاجرت به pgvector.
|
||||||
|
|
||||||
|
بردارها در یک دیتابیس جدا (aramesh_chatbot) روی همان نمونه Postgres سایت
|
||||||
|
نگهداری میشوند تا دیتابیس سایت فقطخواندنی بماند. همگامسازی افزایشی است:
|
||||||
|
فقط سطرهای جدید/تغییریافته امبد میشوند (کلید: doc_id پایدار + hash محتوا).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
from urllib.parse import urlsplit, urlunsplit
|
||||||
|
|
||||||
|
from .config import settings
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _strip_driver(dsn: str) -> str:
|
||||||
|
return dsn.replace("+psycopg", "").replace("+asyncpg", "")
|
||||||
|
|
||||||
|
|
||||||
|
def chatbot_dsn() -> str:
|
||||||
|
"""DSN دیتابیس اختصاصی چتبات — از DATABASE_URL با تعویض نام دیتابیس ساخته میشود."""
|
||||||
|
parts = urlsplit(_strip_driver(settings.database_url))
|
||||||
|
return urlunsplit(parts._replace(path=f"/{settings.chatbot_db_name}"))
|
||||||
|
|
||||||
|
|
||||||
|
def _admin_dsn() -> str:
|
||||||
|
"""DSN دیتابیس postgres برای ساخت دیتابیس چتبات (فقط یکبار)."""
|
||||||
|
parts = urlsplit(_strip_driver(settings.database_url))
|
||||||
|
return urlunsplit(parts._replace(path="/postgres"))
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_schema() -> None:
|
||||||
|
"""ساخت دیتابیس و جدول بردارها — idempotent و در هر شروع اجرا میشود."""
|
||||||
|
import psycopg
|
||||||
|
from psycopg import errors as pg_errors
|
||||||
|
|
||||||
|
try:
|
||||||
|
with psycopg.connect(_admin_dsn(), autocommit=True, connect_timeout=10) as conn:
|
||||||
|
try:
|
||||||
|
conn.execute(f'CREATE DATABASE "{settings.chatbot_db_name}"')
|
||||||
|
log.info("Created chatbot database '%s'", settings.chatbot_db_name)
|
||||||
|
except pg_errors.DuplicateDatabase:
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(f"Cannot reach Postgres to ensure chatbot database: {e}") from e
|
||||||
|
|
||||||
|
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10) as conn:
|
||||||
|
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
|
||||||
|
conn.execute(
|
||||||
|
f"""
|
||||||
|
CREATE TABLE IF NOT EXISTS chatbot_chunks (
|
||||||
|
doc_id TEXT PRIMARY KEY,
|
||||||
|
content_hash TEXT NOT NULL,
|
||||||
|
model TEXT NOT NULL,
|
||||||
|
content JSONB NOT NULL,
|
||||||
|
embedding vector({settings.embedding_dim}) NOT NULL
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
CREATE INDEX IF NOT EXISTS chatbot_chunks_embedding_hnsw
|
||||||
|
ON chatbot_chunks USING hnsw (embedding vector_cosine_ops)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def sync_chunks(chunks: list[dict], force: bool = False) -> dict:
|
||||||
|
"""همگامسازی افزایشی قطعات با جدول pgvector.
|
||||||
|
|
||||||
|
- سطرهایی که دیگر وجود ندارند حذف میشوند
|
||||||
|
- سطرهای جدید یا تغییر یافته (hash یا مدل امبدینگ عوض شده) امبد و upsert میشوند
|
||||||
|
- force=True همه سطرها را از نو امبد میکند
|
||||||
|
خروجی: آمار عملیات برای لاگ.
|
||||||
|
"""
|
||||||
|
import psycopg
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from .embeddings import get_embeddings
|
||||||
|
|
||||||
|
model = settings.embedding_model
|
||||||
|
wanted = {c["doc_id"]: c for c in chunks}
|
||||||
|
|
||||||
|
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10) as conn:
|
||||||
|
existing: dict[str, str] = {}
|
||||||
|
if not force:
|
||||||
|
for row in conn.execute("SELECT doc_id, content_hash, model FROM chatbot_chunks").fetchall():
|
||||||
|
if row["model"] == model:
|
||||||
|
existing[row["doc_id"]] = row["content_hash"]
|
||||||
|
else:
|
||||||
|
conn.execute("DELETE FROM chatbot_chunks")
|
||||||
|
|
||||||
|
to_delete = [doc_id for doc_id in existing if doc_id not in wanted]
|
||||||
|
if to_delete:
|
||||||
|
conn.execute(
|
||||||
|
"DELETE FROM chatbot_chunks WHERE doc_id = ANY(%s)",
|
||||||
|
[to_delete],
|
||||||
|
)
|
||||||
|
|
||||||
|
to_upsert = [
|
||||||
|
c for c in chunks
|
||||||
|
if existing.get(c["doc_id"]) != c["content_hash"]
|
||||||
|
]
|
||||||
|
inserted = updated = 0
|
||||||
|
|
||||||
|
if to_upsert:
|
||||||
|
vectors = get_embeddings().embed_documents([c["text"] for c in to_upsert])
|
||||||
|
for chunk, vec in zip(to_upsert, vectors):
|
||||||
|
literal = "[" + ",".join(f"{x:.7f}" for x in vec) + "]"
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO chatbot_chunks (doc_id, content_hash, model, content, embedding)
|
||||||
|
VALUES (%s, %s, %s, %s, %s::vector)
|
||||||
|
ON CONFLICT (doc_id) DO UPDATE SET
|
||||||
|
content_hash = EXCLUDED.content_hash,
|
||||||
|
model = EXCLUDED.model,
|
||||||
|
content = EXCLUDED.content,
|
||||||
|
embedding = EXCLUDED.embedding
|
||||||
|
""",
|
||||||
|
(chunk["doc_id"], chunk["content_hash"], model, Jsonb(chunk), literal),
|
||||||
|
)
|
||||||
|
if chunk["doc_id"] in existing:
|
||||||
|
updated += 1
|
||||||
|
else:
|
||||||
|
inserted += 1
|
||||||
|
|
||||||
|
stats = {"inserted": inserted, "updated": updated, "deleted": len(to_delete),
|
||||||
|
"unchanged": len(wanted) - len(to_upsert)}
|
||||||
|
log.info("Vector sync: %s", stats)
|
||||||
|
return stats
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_all_chunks() -> list[dict]:
|
||||||
|
"""همه قطعات ذخیرهشده (برای ساخت BM25 در حافظه)."""
|
||||||
|
import psycopg
|
||||||
|
|
||||||
|
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10) as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT doc_id, content_hash, content FROM chatbot_chunks ORDER BY doc_id"
|
||||||
|
).fetchall()
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"doc_id": r["doc_id"],
|
||||||
|
"content_hash": r["content_hash"],
|
||||||
|
**(r["content"] if isinstance(r["content"], dict) else json.loads(r["content"])),
|
||||||
|
}
|
||||||
|
for r in rows
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def vector_count() -> int:
|
||||||
|
"""تعداد بردارهای ذخیرهشده — برای healthz (خطا → ۰)."""
|
||||||
|
try:
|
||||||
|
import psycopg
|
||||||
|
|
||||||
|
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=5) as conn:
|
||||||
|
return int(conn.execute("SELECT count(*) FROM chatbot_chunks").fetchone()[0])
|
||||||
|
except Exception:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def dense_search(query: str, top_k: int) -> list[tuple[dict, float]]:
|
||||||
|
"""جستوجوی معنایی SQL با فاصله کسینوسی — خروجی: (سند، شباهت).
|
||||||
|
|
||||||
|
خطا (مثلاً دیتابیس هنوز آماده نیست) → فهرست خالی تا بازیابی فقط با BM25 ادامه یابد.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
import psycopg
|
||||||
|
from psycopg.rows import dict_row as _dict_row
|
||||||
|
|
||||||
|
from .embeddings import get_embeddings
|
||||||
|
|
||||||
|
vec = get_embeddings().embed_query(query)
|
||||||
|
literal = "[" + ",".join(f"{x:.7f}" for x in vec) + "]"
|
||||||
|
with psycopg.connect(chatbot_dsn(), row_factory=_dict_row, connect_timeout=10) as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
f"""
|
||||||
|
SELECT doc_id, content, 1 - (embedding <=> %s::vector) AS sim
|
||||||
|
FROM chatbot_chunks
|
||||||
|
ORDER BY embedding <=> %s::vector
|
||||||
|
LIMIT %s
|
||||||
|
""",
|
||||||
|
(literal, literal, top_k),
|
||||||
|
).fetchall()
|
||||||
|
return [(_row_to_doc(r), float(r["sim"])) for r in rows]
|
||||||
|
except Exception as e:
|
||||||
|
log.warning("Dense search failed (%s) — falling back to keyword only", e)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def _row_to_doc(row: Any) -> dict:
|
||||||
|
content = row["content"] if isinstance(row["content"], dict) else json.loads(row["content"])
|
||||||
|
return {
|
||||||
|
"doc_id": row["doc_id"],
|
||||||
|
"text": content.get("text", ""),
|
||||||
|
"type": content.get("type", ""),
|
||||||
|
"title": content.get("title", ""),
|
||||||
|
"url": content.get("url", ""),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def chunk_content_hash(chunk: dict) -> str:
|
||||||
|
"""هش محتوای قطعه — تغییر متن یا متادیتا یعنی سطر باید بهروز شود."""
|
||||||
|
payload = json.dumps(
|
||||||
|
{k: chunk[k] for k in ("text", "type", "title", "url")},
|
||||||
|
ensure_ascii=False, sort_keys=True,
|
||||||
|
)
|
||||||
|
return hashlib.sha256(payload.encode("utf-8")).hexdigest()[:16]
|
||||||
@@ -0,0 +1,189 @@
|
|||||||
|
# لایه مانیتورینگ — Prometheus، Grafana، Loki، Alertmanager و صادرکنندهها
|
||||||
|
#
|
||||||
|
# توسعه: docker compose -f docker-compose.yml -f docker-compose.monitoring.yml up -d
|
||||||
|
# تولید: سه فایل با هم: base + monitoring + prod (بخش مانیتورینگ README)
|
||||||
|
#
|
||||||
|
# داشبورد: grafana.localhost (توسعه) — رمز از GRAFANA_ADMIN_PASSWORD در .env
|
||||||
|
# هشدارها: Prometheus → Alertmanager → تلگرام (ALERT_TELEGRAM_* در .env)
|
||||||
|
# منابع: مجموعاً حدود ۱ تا ۱.۵ گیگابایت RAM — با حذف هر سرویس، بقیه سرپا میمانند.
|
||||||
|
|
||||||
|
x-default-logging: &default-logging
|
||||||
|
driver: json-file
|
||||||
|
options:
|
||||||
|
max-size: "10m"
|
||||||
|
max-file: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
prometheus:
|
||||||
|
image: prom/prometheus:v2.53.0
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
command:
|
||||||
|
- --config.file=/etc/prometheus/prometheus.yml
|
||||||
|
- --storage.tsdb.path=/prometheus
|
||||||
|
- --storage.tsdb.retention.time=15d
|
||||||
|
- --storage.tsdb.retention.size=2GB
|
||||||
|
volumes:
|
||||||
|
- ./monitoring/prometheus:/etc/prometheus:ro
|
||||||
|
- prometheus_data:/prometheus
|
||||||
|
networks: [monitoring, edge]
|
||||||
|
environment:
|
||||||
|
# پروب عمومی — در تولید SITE_URL در .env تنظیم میشود (مثل https://aramesh.example)
|
||||||
|
SITE_URL: ${SITE_URL:-http://frontend/}
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "-q", "--spider", "http://localhost:9090/-/healthy"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
start_period: 15s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 768M
|
||||||
|
|
||||||
|
alertmanager:
|
||||||
|
image: prom/alertmanager:v0.27.0
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
command:
|
||||||
|
- --config.file=/etc/alertmanager/alertmanager.rendered.yml
|
||||||
|
- --storage.path=/alertmanager
|
||||||
|
# فایل رندرشدهٔ alertmanager.rendered.yml را scripts/stack.sh از قالب
|
||||||
|
# alertmanager.yml با مقادیر .env (ALERT_BOT_TOKEN/ALERT_CHAT_ID) میسازد —
|
||||||
|
# Alertmanager خودش متغیر محیطی را در کانفیگ جایگزین نمیکند
|
||||||
|
volumes:
|
||||||
|
- ./monitoring/alertmanager:/etc/alertmanager:ro
|
||||||
|
- alertmanager_data:/alertmanager
|
||||||
|
networks: [monitoring]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
# منابع هر کانتینر — روی Linux سرور کامل کار میکند؛ روی Docker Desktop ویندوز
|
||||||
|
# ممکن است محدود باشد (حذفش بقیه استک را نمیشکند)
|
||||||
|
cadvisor:
|
||||||
|
image: gcr.io/cadvisor/cadvisor:v0.49.1
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
privileged: true
|
||||||
|
volumes:
|
||||||
|
- /:/rootfs:ro
|
||||||
|
- /var/run:/var/run:ro
|
||||||
|
- /sys:/sys:ro
|
||||||
|
- /var/lib/docker/:/var/lib/docker:ro
|
||||||
|
networks: [monitoring]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 192M
|
||||||
|
|
||||||
|
# منابع میزبان + سنجهٔ تازگی پشتیبان (backups/*.prom نوشتهٔ scripts/backup.sh)
|
||||||
|
node-exporter:
|
||||||
|
image: quay.io/prometheus/node-exporter:v1.8.2
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
command:
|
||||||
|
- --path.rootfs=/host
|
||||||
|
- --collector.textfile.directory=/textfile
|
||||||
|
volumes:
|
||||||
|
- /:/host:ro
|
||||||
|
- ./backups:/textfile:ro
|
||||||
|
networks: [monitoring]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 64M
|
||||||
|
|
||||||
|
postgres-exporter:
|
||||||
|
image: quay.io/prometheuscommunity/postgres-exporter:v0.16.0
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
environment:
|
||||||
|
DATA_SOURCE_NAME: postgresql://aramesh:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD را در .env تنظیم کنید}@db:5432/arameshplaza?sslmode=disable
|
||||||
|
networks: [internal, monitoring]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 64M
|
||||||
|
|
||||||
|
blackbox-exporter:
|
||||||
|
image: prom/blackbox-exporter:v0.25.0
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
# edge برای رسیدن به سرویسهایی که پروب میشوند (frontend/minio/umami/chatbot/backend)
|
||||||
|
networks: [monitoring, edge]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 64M
|
||||||
|
|
||||||
|
# لاگ همهٔ کانتینرها — جستوجو در Grafana؛ خطاهای بکاند از همینجا دیده میشوند
|
||||||
|
loki:
|
||||||
|
image: grafana/loki:3.1.1
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
command: -config.file=/etc/loki/loki-config.yml
|
||||||
|
volumes:
|
||||||
|
- ./monitoring/loki:/etc/loki:ro
|
||||||
|
- loki_data:/loki
|
||||||
|
networks: [monitoring]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
promtail:
|
||||||
|
image: grafana/promtail:3.1.1
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
command: -config.file=/etc/promtail/promtail-config.yml
|
||||||
|
volumes:
|
||||||
|
- ./monitoring/promtail:/etc/promtail:ro
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
|
- promtail_positions:/positions
|
||||||
|
networks: [monitoring]
|
||||||
|
depends_on: [loki]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
grafana:
|
||||||
|
image: grafana/grafana:11.1.0
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
environment:
|
||||||
|
GF_SECURITY_ADMIN_USER: ${GRAFANA_ADMIN_USER:-admin}
|
||||||
|
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:?GRAFANA_ADMIN_PASSWORD را در .env تنظیم کنید}
|
||||||
|
GF_USERS_ALLOW_SIGN_UP: "false"
|
||||||
|
volumes:
|
||||||
|
- grafana_data:/var/lib/grafana
|
||||||
|
- ./monitoring/grafana/provisioning:/etc/grafana/provisioning:ro
|
||||||
|
- ./monitoring/grafana/dashboards:/var/lib/grafana/dashboards:ro
|
||||||
|
networks: [monitoring, edge]
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "curl -sf http://localhost:3000/api/health || wget -q --spider http://localhost:3000/api/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
start_period: 30s
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
- traefik.http.routers.grafana.rule=Host(`grafana.localhost`)
|
||||||
|
- traefik.http.routers.grafana.entrypoints=web
|
||||||
|
- traefik.http.services.grafana.loadbalancer.server.port=3000
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 384M
|
||||||
|
|
||||||
|
networks:
|
||||||
|
monitoring:
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
prometheus_data:
|
||||||
|
alertmanager_data:
|
||||||
|
loki_data:
|
||||||
|
grafana_data:
|
||||||
|
promtail_positions:
|
||||||
+98
-16
@@ -1,15 +1,28 @@
|
|||||||
# لایه تولید — TLS با Let's Encrypt و انتشار روی پورتهای استاندارد ۸۰/۴۴۳.
|
# لایه تولید — TLS با Let's Encrypt و انتشار روی پورتهای استاندارد ۸۰/۴۳.
|
||||||
# پیشنیاز: متغیرهای DOMAIN و ACME_EMAIL در .env؛ بهعلاوه رکورد DNS دامنه به این سرور.
|
# پیشنیاز: متغیرهای DOMAIN و ACME_EMAIL در .env؛ بهعلاوه رکورد DNS دامنه به این سرور.
|
||||||
#
|
#
|
||||||
# docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build
|
# bash scripts/stack.sh deploy-prod
|
||||||
|
# (معادل: docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build)
|
||||||
#
|
#
|
||||||
# همه درخواستهای HTTP به HTTPS هدایت میشوند و مسیریابیها فقط روی دامنه DOMAIN پاسخ میدهند.
|
# همه درخواستهای HTTP به HTTPS هدایت میشوند و مسیریابیها فقط روی دامنه DOMAIN پاسخ میدهند.
|
||||||
|
# برای داشبوردها: analytics.DOMAIN (آنالیتیکس) و grafana.DOMAIN (مانیتورینگ، با فایل monitoring).
|
||||||
|
#
|
||||||
|
# ---- پیشنمایش خصوصی و انتشار (scripts/preview-mode.sh) ----
|
||||||
|
# در حالت پیشنمایش (PREVIEW_MODE=on) کل سایت پشت Basic Auth است و noindex میگیرد:
|
||||||
|
# تیم با یک نام کاربری/رمز مشترک وارد میشود، محتوا را ویرایش میکند و هیچکس دیگری —
|
||||||
|
# از جمله گوگل — به سایت دسترسی ندارد. برای انتشار عمومی:
|
||||||
|
# bash scripts/preview-mode.sh off → deploy مجدد → ثبت sitemap در Search Console.
|
||||||
|
#
|
||||||
|
# DOMAIN دامنه اصلی است و DOMAIN_ALIAS دامنه دوم (هر دو مسیر و گواهی میگیرند).
|
||||||
|
# در زمان انتشار، برای جلوگیری از محتوای تکراری، دامنه دوم را به اصلی ریدایرکت کنید:
|
||||||
|
# ALIAS_REDIRECT=on (در .env) — ریدایرکت ۳۰۱ دائمی به دامنه اصلی.
|
||||||
|
|
||||||
services:
|
services:
|
||||||
traefik:
|
traefik:
|
||||||
command: !override
|
command: !override
|
||||||
- --providers.docker=true
|
- --providers.docker=true
|
||||||
- --providers.docker.exposedbydefault=false
|
- --providers.docker.exposedbydefault=false
|
||||||
|
- --providers.docker.constraints=Label(`com.docker.compose.project`, `aramesh-backend`)
|
||||||
- --entrypoints.web.address=:80
|
- --entrypoints.web.address=:80
|
||||||
- --entrypoints.web.http.redirections.entrypoint.to=websecure
|
- --entrypoints.web.http.redirections.entrypoint.to=websecure
|
||||||
- --entrypoints.web.http.redirections.entrypoint.scheme=https
|
- --entrypoints.web.http.redirections.entrypoint.scheme=https
|
||||||
@@ -18,6 +31,9 @@ services:
|
|||||||
- --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
|
- --certificatesresolvers.le.acme.storage=/letsencrypt/acme.json
|
||||||
- --certificatesresolvers.le.acme.httpchallenge=true
|
- --certificatesresolvers.le.acme.httpchallenge=true
|
||||||
- --certificatesresolvers.le.acme.httpchallenge.entrypoint=web
|
- --certificatesresolvers.le.acme.httpchallenge.entrypoint=web
|
||||||
|
- --ping=true
|
||||||
|
- --accesslog=true
|
||||||
|
- --metrics.prometheus=true
|
||||||
ports: !override
|
ports: !override
|
||||||
- "80:80"
|
- "80:80"
|
||||||
- "443:443"
|
- "443:443"
|
||||||
@@ -25,26 +41,40 @@ services:
|
|||||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
- letsencrypt_data:/letsencrypt
|
- letsencrypt_data:/letsencrypt
|
||||||
|
|
||||||
backend:
|
# MinIO کنسول (رمز root) و API مستقیمش فقط از localhost سرور در دسترس باشند —
|
||||||
environment:
|
# از راه دور با تونل SSH: ssh -L 9001:127.0.0.1:9001 server
|
||||||
# سایت هممبدأ است — CORS لازم نیست
|
|
||||||
CORS_ORIGINS: '[]'
|
|
||||||
SITE_URL: https://${DOMAIN:?DOMAIN را در .env تنظیم کنید}
|
|
||||||
labels:
|
|
||||||
- traefik.enable=true
|
|
||||||
- traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)
|
|
||||||
- traefik.http.routers.api.entrypoints=websecure
|
|
||||||
- traefik.http.routers.api.tls.certresolver=le
|
|
||||||
- traefik.http.routers.seo.rule=Host(`${DOMAIN}`) && (PathPrefix(`/blog/`) || Path(`/blog`) || PathPrefix(`/projects/`) || Path(`/projects`) || Path(`/en`) || PathPrefix(`/en/`) || PathPrefix(`/robots.txt`) || PathPrefix(`/sitemap.xml`))
|
|
||||||
- traefik.http.routers.seo.entrypoints=websecure
|
|
||||||
- traefik.http.routers.seo.tls.certresolver=le
|
|
||||||
|
|
||||||
minio:
|
minio:
|
||||||
|
ports: !override
|
||||||
|
- "127.0.0.1:9001:9001"
|
||||||
|
- "127.0.0.1:19000:9000"
|
||||||
labels:
|
labels:
|
||||||
- traefik.enable=true
|
- traefik.enable=true
|
||||||
- traefik.http.routers.media.rule=Host(`${DOMAIN}`) && PathPrefix(`/media`)
|
- traefik.http.routers.media.rule=Host(`${DOMAIN}`) && PathPrefix(`/media`)
|
||||||
- traefik.http.routers.media.entrypoints=websecure
|
- traefik.http.routers.media.entrypoints=websecure
|
||||||
- traefik.http.routers.media.tls.certresolver=le
|
- traefik.http.routers.media.tls.certresolver=le
|
||||||
|
- traefik.http.routers.media.priority=100
|
||||||
|
- traefik.http.routers.media.middlewares=${PREVIEW_MIDDLEWARES:+$PREVIEW_MIDDLEWARES,}media-rewrite,sec-headers
|
||||||
|
- traefik.http.middlewares.media-rewrite.replacepathregex.regex=^/media/(.*)
|
||||||
|
- traefik.http.middlewares.media-rewrite.replacepathregex.replacement=/aramesh-media/$$1
|
||||||
|
- traefik.http.services.media.loadbalancer.server.port=9000
|
||||||
|
|
||||||
|
backend:
|
||||||
|
environment:
|
||||||
|
# سایت هممبدأ است — CORS لازم نیست
|
||||||
|
CORS_ORIGINS: '[]'
|
||||||
|
SITE_URL: https://${DOMAIN:?DOMAIN را در .env تنظیم کنید}
|
||||||
|
# پیشنمایش خصوصی — هدر noindex + robots مسدودکننده (لایه دوم؛ لایه اول گیت Traefik)
|
||||||
|
PREVIEW_MODE: ${PREVIEW_MODE:-off}
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
- traefik.http.routers.api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)
|
||||||
|
- traefik.http.routers.api.entrypoints=websecure
|
||||||
|
- traefik.http.routers.api.tls.certresolver=le
|
||||||
|
- traefik.http.routers.api.middlewares=${PREVIEW_MIDDLEWARES:+$PREVIEW_MIDDLEWARES,}api-flood,sec-headers
|
||||||
|
- traefik.http.routers.seo.rule=Host(`${DOMAIN}`) && (PathPrefix(`/blog/`) || Path(`/blog`) || PathPrefix(`/projects/`) || Path(`/projects`) || Path(`/en`) || PathPrefix(`/en/`) || PathPrefix(`/robots.txt`) || PathPrefix(`/sitemap.xml`))
|
||||||
|
- traefik.http.routers.seo.entrypoints=websecure
|
||||||
|
- traefik.http.routers.seo.tls.certresolver=le
|
||||||
|
- traefik.http.routers.seo.middlewares=${PREVIEW_MIDDLEWARES:+$PREVIEW_MIDDLEWARES,}sec-headers
|
||||||
|
|
||||||
frontend:
|
frontend:
|
||||||
labels:
|
labels:
|
||||||
@@ -52,6 +82,58 @@ services:
|
|||||||
- traefik.http.routers.web.rule=Host(`${DOMAIN}`) && PathPrefix(`/`)
|
- traefik.http.routers.web.rule=Host(`${DOMAIN}`) && PathPrefix(`/`)
|
||||||
- traefik.http.routers.web.entrypoints=websecure
|
- traefik.http.routers.web.entrypoints=websecure
|
||||||
- traefik.http.routers.web.tls.certresolver=le
|
- traefik.http.routers.web.tls.certresolver=le
|
||||||
|
- traefik.http.routers.web.middlewares=${PREVIEW_MIDDLEWARES:+$PREVIEW_MIDDLEWARES,}sec-headers
|
||||||
|
# تعریف یکبارهٔ میانافزار هدرهای امنیتی — روی همهٔ روترهای websecure استفاده میشود
|
||||||
|
- traefik.http.middlewares.sec-headers.headers.stsSeconds=31536000
|
||||||
|
- traefik.http.middlewares.sec-headers.headers.stsIncludeSubdomains=true
|
||||||
|
- traefik.http.middlewares.sec-headers.headers.stsPreload=true
|
||||||
|
- traefik.http.middlewares.sec-headers.headers.contentTypeNosniff=true
|
||||||
|
# ---- گیت پیشنمایش خصوصی ----
|
||||||
|
# ورود با نام کاربری/رمز مشترک (htpasswd) — تا وقتی PREVIEW_MIDDLEWARES تنظیم است فعال میشود
|
||||||
|
- traefik.http.middlewares.preview-auth.basicauth.users=${PREVIEW_AUTH_USERS:-disabled:nope}
|
||||||
|
- traefik.http.middlewares.preview-auth.basicauth.realm=Aramesh private preview
|
||||||
|
# لایه دوم جلوگیری از ایندکس — حتی اگر گیت حذف شد، گوگل چیزی ذخیره نکند
|
||||||
|
- traefik.http.middlewares.preview-noindex.headers.customresponseheaders.X-Robots-Tag=noindex, nofollow, noarchive
|
||||||
|
# ---- ریدایرکت دامنه دوم به اصلی (زمان انتشار: ALIAS_REDIRECT=on) ----
|
||||||
|
- traefik.http.middlewares.to-primary.redirectregex.regex=^https?://[^/]+(.*)
|
||||||
|
- traefik.http.middlewares.to-primary.redirectregex.replacement=https://${DOMAIN}$$1
|
||||||
|
- traefik.http.middlewares.to-primary.redirectregex.permanent=true
|
||||||
|
- traefik.http.routers.alias-redirect.rule=Host(`${DOMAIN_ALIAS:-invalid.invalid}`)
|
||||||
|
- traefik.http.routers.alias-redirect.entrypoints=websecure
|
||||||
|
- traefik.http.routers.alias-redirect.tls.certresolver=le
|
||||||
|
- traefik.http.routers.alias-redirect.priority=1000
|
||||||
|
- traefik.http.routers.alias-redirect.middlewares=${ALIAS_REDIRECT_MIDDLEWARES:-sec-headers}
|
||||||
|
- traefik.http.routers.alias-redirect.service=web
|
||||||
|
|
||||||
|
chatbot:
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
- traefik.http.routers.chat.rule=Host(`${DOMAIN}`) && PathPrefix(`/chat`)
|
||||||
|
- traefik.http.routers.chat.entrypoints=websecure
|
||||||
|
- traefik.http.routers.chat.tls.certresolver=le
|
||||||
|
- traefik.http.routers.chat.priority=100
|
||||||
|
- traefik.http.routers.chat.middlewares=${PREVIEW_MIDDLEWARES:+$PREVIEW_MIDDLEWARES,}api-flood,sec-headers
|
||||||
|
|
||||||
|
umami:
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
- traefik.http.routers.analytics.rule=Host(`analytics.${DOMAIN}`)
|
||||||
|
- traefik.http.routers.analytics.entrypoints=websecure
|
||||||
|
- traefik.http.routers.analytics.tls.certresolver=le
|
||||||
|
- traefik.http.routers.analytics.middlewares=sec-headers
|
||||||
|
- traefik.http.services.analytics.loadbalancer.server.port=3000
|
||||||
|
|
||||||
|
# داشبورد Grafana (docker-compose.monitoring.yml) در تولید با TLS نیاز به روتر
|
||||||
|
# Host(`grafana.DOMAIN`) دارد — چون آن سرویس فقط با فایل monitoring بالا میآید،
|
||||||
|
# برچسبهایش را در override سرور بگذارید (README، بخش مانیتورینگ):
|
||||||
|
# docker-compose.override.yml روی سرور:
|
||||||
|
# services:
|
||||||
|
# grafana:
|
||||||
|
# labels:
|
||||||
|
# - traefik.http.routers.grafana.rule=Host(`grafana.YOURDOMAIN`)
|
||||||
|
# - traefik.http.routers.grafana.entrypoints=websecure
|
||||||
|
# - traefik.http.routers.grafana.tls.certresolver=le
|
||||||
|
# - traefik.http.services.grafana.loadbalancer.server.port=3000
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
letsencrypt_data:
|
letsencrypt_data:
|
||||||
|
|||||||
+230
-20
@@ -1,71 +1,121 @@
|
|||||||
# مقادیر محرمانه از فایل .env ریشه خوانده میشوند — از .env.example کپی کنید:
|
# مقادیر محرمانه از فایل .env ریشه خوانده میشوند — از .env.example کپی کنید:
|
||||||
# cp .env.example .env
|
# cp .env.example .env (یا: bash scripts/gen-env.sh)
|
||||||
# برای انتشار با TLS، روی این فایل docker-compose.prod.yml را هم سوار کنید (README).
|
# برای انتشار با TLS، روی این فایل docker-compose.prod.yml هم سوار شود (README):
|
||||||
|
# bash scripts/stack.sh deploy-prod
|
||||||
|
#
|
||||||
|
# لایههای اختیاری:
|
||||||
|
# docker-compose.monitoring.yml — Prometheus/Grafana/Loki (پروفایل زیرساخت)
|
||||||
|
# پروفایل ops — پشتیبان ساعتی دیتابیس
|
||||||
|
# پروفایل tools — ابزار mc برای پشتیبانگیری دستی
|
||||||
|
|
||||||
|
x-default-logging: &default-logging
|
||||||
|
# بدون این، لاگ json-file بیمرز رشد میکند و دیسک سرور را پر میکند
|
||||||
|
driver: json-file
|
||||||
|
options:
|
||||||
|
max-size: "10m"
|
||||||
|
max-file: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# نقطه ورود واحد — مسیریابی / (سایت استاتیک)، /api (بکاند) و /media (MinIO)
|
# نقطه ورود واحد — مسیریابی / (سایت استاتیک)، /api (بکاند)، /media (MinIO)،
|
||||||
|
# /chat (چتبات)، / (آنالیتیکس روی analytics.localhost — پایین همین فایل)
|
||||||
traefik:
|
traefik:
|
||||||
image: traefik:v3
|
# v3.3+ قدیمیترین کلاینت داکر را دارد که با Engineهای جدید (API 1.5x) مشکل دارد —
|
||||||
|
# با v3.6 پین شده است
|
||||||
|
image: traefik:v3.6
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
command:
|
command:
|
||||||
- --providers.docker=true
|
- --providers.docker=true
|
||||||
- --providers.docker.exposedbydefault=false
|
- --providers.docker.exposedbydefault=false
|
||||||
# فقط کانتینرهای همین استک — با استکهای دیگر هممیزبان تداخل نام روتر پیش نمیآید
|
# فقط کانتینرهای همین استک — با استکهای دیگر هممیزبان تداخل نام روتر پیش نمیآید
|
||||||
- --providers.docker.constraints=Label(`com.docker.compose.project`, `aramesh-backend`)
|
- --providers.docker.constraints=Label(`com.docker.compose.project`, `aramesh-backend`)
|
||||||
- --entrypoints.web.address=:80
|
- --entrypoints.web.address=:80
|
||||||
|
# healthcheck داخلی + متریکها برای Prometheus + لاگ دسترسی
|
||||||
|
- --ping=true
|
||||||
|
- --accesslog=true
|
||||||
|
- --metrics.prometheus=true
|
||||||
ports:
|
ports:
|
||||||
- "${TRAEFIK_HTTP_PORT:-5173}:80"
|
- "${TRAEFIK_HTTP_PORT:-5173}:80"
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
|
networks: [edge]
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "traefik", "healthcheck", "--ping"]
|
||||||
|
interval: 15s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
start_period: 10s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: postgres:17-alpine
|
# pgvector/pgvector:pg17 = همان PostgreSQL 17 + افزونهٔ vector برای چتبات
|
||||||
|
image: pgvector/pgvector:pg17
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: aramesh
|
POSTGRES_USER: aramesh
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD را در .env تنظیم کنید}
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD را در .env تنظیم کنید}
|
||||||
POSTGRES_DB: arameshplaza
|
POSTGRES_DB: arameshplaza
|
||||||
volumes:
|
volumes:
|
||||||
- db_data:/var/lib/postgresql/data
|
- db_data:/var/lib/postgresql/data
|
||||||
|
shm_size: 256m
|
||||||
|
networks: [internal]
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U aramesh -d arameshplaza"]
|
test: ["CMD-SHELL", "pg_isready -U aramesh -d arameshplaza"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 12
|
retries: 12
|
||||||
|
start_period: 10s
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
minio:
|
minio:
|
||||||
image: minio/minio:latest
|
image: minio/minio:RELEASE.2025-04-22T22-12-26Z
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
command: server /data --console-address ":9001"
|
command: server /data --console-address ":9001"
|
||||||
environment:
|
environment:
|
||||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-aramesh}
|
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-aramesh}
|
||||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD را در .env تنظیم کنید}
|
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD را در .env تنظیم کنید}
|
||||||
volumes:
|
volumes:
|
||||||
- minio_data:/data
|
- minio_data:/data
|
||||||
|
networks: [edge, internal]
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
|
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 12
|
retries: 12
|
||||||
|
start_period: 10s
|
||||||
ports:
|
ports:
|
||||||
- "9001:9001" # کنسول MinIO — در محیط واقعی با فایروال ببندید
|
# در تولید، docker-compose.prod.yml اینها را فقط روی 127.0.0.1 میبندد —
|
||||||
- "19000:9000" # دسترسی مستقیم/لوکال به MinIO (اختیاری — تصاویر از مسیر /media سرو میشوند)
|
# کنسول رمز root دارد و نباید به اینترنت باز باشد
|
||||||
|
- "9001:9001"
|
||||||
|
- "19000:9000"
|
||||||
labels:
|
labels:
|
||||||
- traefik.enable=true
|
- traefik.enable=true
|
||||||
|
# Traefik باید IP لبه را بگیرد — سرویس روی دو شبکه است
|
||||||
|
- traefik.docker.network=aramesh-backend_edge
|
||||||
- traefik.http.routers.media.rule=PathPrefix(`/media`)
|
- traefik.http.routers.media.rule=PathPrefix(`/media`)
|
||||||
- traefik.http.routers.media.entrypoints=web
|
- traefik.http.routers.media.entrypoints=web
|
||||||
- traefik.http.routers.media.priority=100
|
- traefik.http.routers.media.priority=100
|
||||||
- traefik.http.routers.media.middlewares=media-rewrite
|
|
||||||
- traefik.http.middlewares.media-rewrite.replacepathregex.regex=^/media/(.*)
|
- traefik.http.middlewares.media-rewrite.replacepathregex.regex=^/media/(.*)
|
||||||
- traefik.http.middlewares.media-rewrite.replacepathregex.replacement=/aramesh-media/$$1
|
- traefik.http.middlewares.media-rewrite.replacepathregex.replacement=/aramesh-media/$$1
|
||||||
|
- traefik.http.routers.media.middlewares=media-rewrite
|
||||||
- traefik.http.services.media.loadbalancer.server.port=9000
|
- traefik.http.services.media.loadbalancer.server.port=9000
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
# یکبار اجرا میشود: ساخت باکت، بازگذاشتن خواندن عمومی و پیشبارگذاری تصویرهای seed.
|
# یکبار اجرا میشود: ساخت باکت، بازگذاشتن خواندن عمومی و پیشبارگذاری تصویرهای seed
|
||||||
# تصویرهای داخل seed-media/ (که با پیشوند /media در دیتابیس ثبتاند) روی یک حجم تازه
|
# فقط وقتی باکت خالی است — تا هر deploy، کپی تکراری تصویرها را تکرار نکند.
|
||||||
# وارد باکت میشوند تا هر نسخه تازهکلون/بیلد، همان عکسها را داشته باشد؛
|
|
||||||
# روی حجم موجود، mc cp فایلهای تکراری را رد میکند و دادهتازه را نمیخورد.
|
|
||||||
minio-setup:
|
minio-setup:
|
||||||
image: minio/mc:latest
|
image: minio/mc:RELEASE.2025-04-08T15-39-49Z
|
||||||
depends_on:
|
depends_on:
|
||||||
minio:
|
minio:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
@@ -76,19 +126,51 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- ./seed-media:/seed-media:ro
|
- ./seed-media:/seed-media:ro
|
||||||
restart: "no"
|
restart: "no"
|
||||||
|
networks: [internal]
|
||||||
|
logging: *default-logging
|
||||||
entrypoint: >
|
entrypoint: >
|
||||||
/bin/sh -c "
|
/bin/sh -c "
|
||||||
mc alias set local http://minio:9000 $$MINIO_ROOT_USER $$MINIO_ROOT_PASSWORD &&
|
mc alias set local http://minio:9000 $$MINIO_ROOT_USER $$MINIO_ROOT_PASSWORD &&
|
||||||
mc mb --ignore-existing local/$$MINIO_BUCKET &&
|
mc mb --ignore-existing local/$$MINIO_BUCKET &&
|
||||||
mc anonymous set download local/$$MINIO_BUCKET &&
|
mc anonymous set download local/$$MINIO_BUCKET &&
|
||||||
echo 'minio-setup: bucket ready' &&
|
COUNT=$$(mc ls --recursive local/$$MINIO_BUCKET | wc -l) &&
|
||||||
cd /seed-media && mc mirror --overwrite . local/$$MINIO_BUCKET/ &&
|
if [ \"$$COUNT\" -eq 0 ]; then
|
||||||
echo 'minio-setup: seed media mirrored'
|
echo 'minio-setup: bucket ready — mirroring seed media';
|
||||||
|
mc mirror --overwrite /seed-media local/$$MINIO_BUCKET/;
|
||||||
|
else
|
||||||
|
echo \"minio-setup: bucket ready — $$COUNT object(s) already present, seed mirror skipped\";
|
||||||
|
fi
|
||||||
|
"
|
||||||
|
|
||||||
|
# یکبار اجرا میشود: دیتابیسهای جانبی روی همان نمونهٔ Postgres
|
||||||
|
# (umami برای آنالیتیکس، aramesh_chatbot برای بردارهای چتبات)
|
||||||
|
db-init:
|
||||||
|
image: postgres:17-alpine
|
||||||
|
restart: "no"
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
PGHOST: db
|
||||||
|
PGUSER: aramesh
|
||||||
|
PGPASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD را در .env تنظیم کنید}
|
||||||
|
networks: [internal]
|
||||||
|
logging: *default-logging
|
||||||
|
entrypoint: >
|
||||||
|
/bin/sh -c "
|
||||||
|
set -e;
|
||||||
|
for DBN in umami aramesh_chatbot; do
|
||||||
|
if ! psql -d arameshplaza -tAc \"SELECT 1 FROM pg_database WHERE datname='$$DBN'\" | grep -q 1; then
|
||||||
|
psql -d arameshplaza -c \"CREATE DATABASE $$DBN\";
|
||||||
|
fi;
|
||||||
|
echo \"db-init: $$DBN ready\";
|
||||||
|
done
|
||||||
"
|
"
|
||||||
|
|
||||||
backend:
|
backend:
|
||||||
build: ./backend
|
build: ./backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
environment:
|
environment:
|
||||||
DATABASE_URL: postgresql+psycopg://aramesh:${POSTGRES_PASSWORD}@db:5432/arameshplaza
|
DATABASE_URL: postgresql+psycopg://aramesh:${POSTGRES_PASSWORD}@db:5432/arameshplaza
|
||||||
SECRET_KEY: ${SECRET_KEY:?SECRET_KEY را در .env تنظیم کنید}
|
SECRET_KEY: ${SECRET_KEY:?SECRET_KEY را در .env تنظیم کنید}
|
||||||
@@ -106,11 +188,14 @@ services:
|
|||||||
FRONTEND_DIST: /srv/frontend
|
FRONTEND_DIST: /srv/frontend
|
||||||
volumes:
|
volumes:
|
||||||
- ./frontend/dist:/srv/frontend:ro
|
- ./frontend/dist:/srv/frontend:ro
|
||||||
|
networks: [edge, internal]
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
minio:
|
minio:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
db-init:
|
||||||
|
condition: service_completed_successfully
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/health')"]
|
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/health')"]
|
||||||
interval: 15s
|
interval: 15s
|
||||||
@@ -119,9 +204,17 @@ services:
|
|||||||
start_period: 20s
|
start_period: 20s
|
||||||
labels:
|
labels:
|
||||||
- traefik.enable=true
|
- traefik.enable=true
|
||||||
|
# Traefik باید IP لبه را بگیرد — سرویس روی دو شبکه است
|
||||||
|
- traefik.docker.network=aramesh-backend_edge
|
||||||
- traefik.http.routers.api.rule=PathPrefix(`/api`)
|
- traefik.http.routers.api.rule=PathPrefix(`/api`)
|
||||||
- traefik.http.routers.api.entrypoints=web
|
- traefik.http.routers.api.entrypoints=web
|
||||||
- traefik.http.routers.api.priority=100
|
- traefik.http.routers.api.priority=100
|
||||||
|
# لایه دوم محافظت از فرمها — سیلاب درخواست را پیش از رسیدن به بکاند میبُرد؛
|
||||||
|
# محدودسازی دقیق هر فرم در خود بکاند است (rate_limit.py)
|
||||||
|
- traefik.http.routers.api.middlewares=api-flood
|
||||||
|
- traefik.http.middlewares.api-flood.ratelimit.average=50
|
||||||
|
- traefik.http.middlewares.api-flood.ratelimit.period=1s
|
||||||
|
- traefik.http.middlewares.api-flood.ratelimit.burst=150
|
||||||
- traefik.http.services.api.loadbalancer.server.port=8000
|
- traefik.http.services.api.loadbalancer.server.port=8000
|
||||||
# robots.txt و sitemap.xml و صفحات وبلاگ/پروژه با متای سئو — اولویت بالاتر از SPA
|
# robots.txt و sitemap.xml و صفحات وبلاگ/پروژه با متای سئو — اولویت بالاتر از SPA
|
||||||
# مسیرهای /en/... هم همه به بکاند میروند تا متای سئوی انگلیسی و html انگلیسی تزریق شود
|
# مسیرهای /en/... هم همه به بکاند میروند تا متای سئوی انگلیسی و html انگلیسی تزریق شود
|
||||||
@@ -129,29 +222,111 @@ services:
|
|||||||
- traefik.http.routers.seo.entrypoints=web
|
- traefik.http.routers.seo.entrypoints=web
|
||||||
- traefik.http.routers.seo.priority=50
|
- traefik.http.routers.seo.priority=50
|
||||||
- traefik.http.routers.seo.service=api
|
- traefik.http.routers.seo.service=api
|
||||||
|
# /metrics فقط برای Prometheus — روتری تعریف نمیشود، از بیرون در دسترس نیست
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 768M
|
||||||
|
|
||||||
# فقط سرو فایلهای استاتیک SPA — بیلد روی هاست (npm run build) انجام شده است
|
# فقط سرو فایلهای استاتیک SPA — بیلد روی هاست (npm run build) انجام شده است
|
||||||
frontend:
|
frontend:
|
||||||
build: ./frontend
|
build: ./frontend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
depends_on:
|
depends_on:
|
||||||
- backend
|
backend:
|
||||||
- minio
|
condition: service_healthy
|
||||||
|
networks: [edge]
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "-q", "--spider", "http://localhost/"]
|
test: ["CMD", "wget", "-q", "--spider", "http://localhost/"]
|
||||||
interval: 15s
|
interval: 15s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
start_period: 5s
|
||||||
labels:
|
labels:
|
||||||
- traefik.enable=true
|
- traefik.enable=true
|
||||||
- traefik.http.routers.web.rule=PathPrefix(`/`)
|
- traefik.http.routers.web.rule=PathPrefix(`/`)
|
||||||
- traefik.http.routers.web.entrypoints=web
|
- traefik.http.routers.web.entrypoints=web
|
||||||
- traefik.http.routers.web.priority=1
|
- traefik.http.routers.web.priority=1
|
||||||
- traefik.http.services.web.loadbalancer.server.port=80
|
- traefik.http.services.web.loadbalancer.server.port=80
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 64M
|
||||||
|
|
||||||
|
# دستیار هوشمند — RAG روی محتوای سایت با pgvector
|
||||||
|
# اولین اجرا مدلها را دانلود میکند (~۵۰۰MB روی حجم chatbot_hf) — کند است، طبیعی است
|
||||||
|
chatbot:
|
||||||
|
build: ./chatbot
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgresql+psycopg://aramesh:${POSTGRES_PASSWORD}@db:5432/arameshplaza
|
||||||
|
LLM_BASE_URL: ${LLM_BASE_URL:-}
|
||||||
|
LLM_API_KEY: ${LLM_API_KEY:-}
|
||||||
|
LLM_MODEL: ${LLM_MODEL:-openai/gpt-oss-120b}
|
||||||
|
CHAT_REINDEX_TOKEN: ${CHAT_REINDEX_TOKEN:-}
|
||||||
|
volumes:
|
||||||
|
- chatbot_hf:/data
|
||||||
|
networks: [edge, internal]
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
db-init:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
# Traefik باید IP لبه را بگیرد — سرویس روی دو شبکه است
|
||||||
|
- traefik.docker.network=aramesh-backend_edge
|
||||||
|
- traefik.http.routers.chat.rule=PathPrefix(`/chat`)
|
||||||
|
- traefik.http.routers.chat.entrypoints=web
|
||||||
|
- traefik.http.routers.chat.priority=100
|
||||||
|
- traefik.http.routers.chat.middlewares=api-flood
|
||||||
|
- traefik.http.services.chat.loadbalancer.server.port=8000
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 2G
|
||||||
|
|
||||||
|
# آنالیتیکس محصول (Umami) — بازدید، ارجاع، رخدادهای سفارشی (فرم/تلفن/چتبات)
|
||||||
|
# داشبورد: analytics.localhost (توسعه) یا analytics.DOMAIN (تولید)
|
||||||
|
umami:
|
||||||
|
image: ghcr.io/umami-software/umami:postgresql-v2.17.0
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgresql://aramesh:${POSTGRES_PASSWORD}@db:5432/umami
|
||||||
|
# اسکریپت ردیاب به ارجاعدهندهٔ واقعی نیاز دارد — پشت Traefik
|
||||||
|
CLIENT_IP_HEADER: x-forwarded-for
|
||||||
|
DISABLE_TELEMETRY: 1
|
||||||
|
networks: [edge, internal]
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
db-init:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
# 127.0.0.1 نه localhost — «localhost» به ::1 حل میشود و umami فقط IPv4 گوش میدهد
|
||||||
|
test: ["CMD-SHELL", "wget -q --spider http://127.0.0.1:3000/api/heartbeat || exit 1"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
start_period: 60s
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
# Traefik باید IP لبه را بگیرد — سرویس روی دو شبکه است
|
||||||
|
- traefik.docker.network=aramesh-backend_edge
|
||||||
|
- traefik.http.routers.analytics.rule=Host(`analytics.localhost`)
|
||||||
|
- traefik.http.routers.analytics.entrypoints=web
|
||||||
|
- traefik.http.services.analytics.loadbalancer.server.port=3000
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
# ابزار پشتیبانگیری — فقط با پروفایل tools اجرا میشود (scripts/backup.sh این کار را میکند)
|
# ابزار پشتیبانگیری — فقط با پروفایل tools اجرا میشود (scripts/backup.sh این کار را میکند)
|
||||||
mc:
|
mc:
|
||||||
image: minio/mc:latest
|
image: minio/mc:RELEASE.2025-04-08T15-39-49Z
|
||||||
profiles: ["tools"]
|
profiles: ["tools"]
|
||||||
depends_on:
|
depends_on:
|
||||||
minio:
|
minio:
|
||||||
@@ -159,7 +334,42 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-aramesh}
|
MINIO_ROOT_USER: ${MINIO_ROOT_USER:-aramesh}
|
||||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD را در .env تنظیم کنید}
|
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?MINIO_ROOT_PASSWORD را در .env تنظیم کنید}
|
||||||
|
networks: [internal]
|
||||||
|
|
||||||
|
# پشتیبان ساعتی خودکار دیتابیس — بدون نیاز به cron میزبان؛ فقط با پروفایل ops
|
||||||
|
# docker compose --profile ops up -d db-backup
|
||||||
|
# RPO را از ۲۴ ساعت (cron شبانه) به ۱ ساعت میرساند؛ scripts/backup.sh برای
|
||||||
|
# پشتیبان کامل (دیتابیس + تصاویر MinIO) سر جایش است.
|
||||||
|
db-backup:
|
||||||
|
image: prodrigestivill/postgres-backup-local:17
|
||||||
|
profiles: ["ops"]
|
||||||
|
restart: unless-stopped
|
||||||
|
logging: *default-logging
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
POSTGRES_HOST: db
|
||||||
|
POSTGRES_DB: arameshplaza
|
||||||
|
POSTGRES_USER: aramesh
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD را در .env تنظیم کنید}
|
||||||
|
SCHEDULE: "@hourly"
|
||||||
|
BACKUP_NUM_KEEP: "48"
|
||||||
|
BACKUP_DIR: /backups
|
||||||
|
volumes:
|
||||||
|
- backups:/backups
|
||||||
|
networks: [internal]
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
networks:
|
||||||
|
edge: # سرویسهایی که Traefik به آنها مسیریابی میکند
|
||||||
|
internal: # دیتابیس و ذخیرهسازی — فرانتاند راهی به این شبکه ندارد
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
db_data:
|
db_data:
|
||||||
minio_data:
|
minio_data:
|
||||||
|
chatbot_hf:
|
||||||
|
backups:
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# فقط Caddyfile و dist/ به کانتکست بیلد میروند — node_modules و سورس عظیماند و لازم نیستند
|
||||||
|
*
|
||||||
|
!Caddyfile
|
||||||
|
!dist
|
||||||
+11
-2
@@ -1,12 +1,21 @@
|
|||||||
|
{
|
||||||
|
# پنل مدیریت Caddy لازم نیست — سطح حمله کمتر
|
||||||
|
admin off
|
||||||
|
}
|
||||||
|
|
||||||
:80 {
|
:80 {
|
||||||
root * /srv
|
root * /srv
|
||||||
encode gzip
|
encode zstd gzip
|
||||||
|
|
||||||
# مسیریابی SPA — مسیرهای ناشناخته به index.html برمیگردند
|
# مسیریابی SPA — مسیرهای ناشناخته به index.html برمیگردند
|
||||||
try_files {path} /index.html
|
try_files {path} /index.html
|
||||||
file_server
|
file_server
|
||||||
|
|
||||||
# کش بلندمدت برای فایلهای هشدار بیلد
|
# فایلهای هشدار بیلد: کش بلندمدت بیرحم
|
||||||
@assets path /assets/*
|
@assets path /assets/*
|
||||||
header @assets Cache-Control "public, immutable, max-age=31536000"
|
header @assets Cache-Control "public, immutable, max-age=31536000"
|
||||||
|
|
||||||
|
# بقیه (از جمله index.html و fallback اسپا): بدون کش مرورگر تا هر استقرار فوری دیده شود
|
||||||
|
@notassets not path /assets/*
|
||||||
|
header @notassets Cache-Control "no-cache"
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-1
@@ -4,7 +4,17 @@
|
|||||||
|
|
||||||
FROM caddy:2-alpine
|
FROM caddy:2-alpine
|
||||||
|
|
||||||
|
# نسخهٔ alpine کاربر caddy ندارد — اینجا ساخته میشود؛ /config و /data مسیرهای
|
||||||
|
# storage سیدی هستند و باید برای کاربر نوشتنی باشند
|
||||||
|
RUN addgroup -S caddy \
|
||||||
|
&& adduser -S -G caddy -s /sbin/nologin caddy \
|
||||||
|
&& mkdir -p /config /data \
|
||||||
|
&& chown -R caddy:caddy /config /data
|
||||||
|
|
||||||
COPY Caddyfile /etc/caddy/Caddyfile
|
COPY Caddyfile /etc/caddy/Caddyfile
|
||||||
COPY dist /srv
|
COPY --chown=caddy:caddy dist /srv
|
||||||
|
|
||||||
|
# بدون root — فقط فایلهای استاتیک سرو میشوند و TLS را Traefik تمام میکند
|
||||||
|
USER caddy
|
||||||
|
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
|
|||||||
@@ -24,6 +24,16 @@
|
|||||||
document.documentElement.setAttribute('dir', 'ltr');
|
document.documentElement.setAttribute('dir', 'ltr');
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
<script type="module">
|
||||||
|
// تحلیل رفتار (Umami) — در زمان بیلد تزریق میشود؛ VITE_UMAMI_SRC خالی یعنی خاموش
|
||||||
|
if (import.meta.env.VITE_UMAMI_SRC) {
|
||||||
|
const s = document.createElement('script');
|
||||||
|
s.defer = true;
|
||||||
|
s.src = import.meta.env.VITE_UMAMI_SRC;
|
||||||
|
s.setAttribute('data-website-id', import.meta.env.VITE_UMAMI_WEBSITE_ID ?? '');
|
||||||
|
document.head.appendChild(s);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
<script type="module" src="/src/index.tsx"></script>
|
<script type="module" src="/src/index.tsx"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { Preloader } from './components/Preloader';
|
|||||||
import { Header } from './components/Header';
|
import { Header } from './components/Header';
|
||||||
import { Footer } from './components/Footer';
|
import { Footer } from './components/Footer';
|
||||||
import { FloatingContact } from './components/FloatingContact';
|
import { FloatingContact } from './components/FloatingContact';
|
||||||
|
import { ChatWidget } from './components/ChatWidget';
|
||||||
import { Home } from './pages/Home';
|
import { Home } from './pages/Home';
|
||||||
import { About } from './pages/About';
|
import { About } from './pages/About';
|
||||||
import { ServicesPage } from './pages/ServicesPage';
|
import { ServicesPage } from './pages/ServicesPage';
|
||||||
@@ -106,6 +107,7 @@ function AppChrome() {
|
|||||||
<AnimatedRoutes />
|
<AnimatedRoutes />
|
||||||
{!isAdminPage && <Footer />}
|
{!isAdminPage && <Footer />}
|
||||||
{!isAdminPage && <FloatingContact />}
|
{!isAdminPage && <FloatingContact />}
|
||||||
|
{!isAdminPage && <ChatWidget />}
|
||||||
{isAuthenticated && !isAdminPage && <AdminBar />}
|
{isAuthenticated && !isAdminPage && <AdminBar />}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -188,6 +188,28 @@ export function AdminBar() {
|
|||||||
رمز
|
رمز
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void adminApi.downloadCsv('messages').catch((e) => {
|
||||||
|
void Swal.fire({ ...SWAL_BASE, icon: 'error', title: 'خروجی پیامها', text: e instanceof Error ? e.message : 'خطا', confirmButtonText: 'باشه' })
|
||||||
|
})}
|
||||||
|
title="دانلود پیامهای فرم تماس بهصورت CSV"
|
||||||
|
className="border border-ivory/20 px-3 py-1.5 text-xs font-light text-ivory/70 transition-colors duration-200 hover:border-copper hover:text-copper"
|
||||||
|
>
|
||||||
|
پیامها (CSV)
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void adminApi.downloadCsv('subscribers').catch((e) => {
|
||||||
|
void Swal.fire({ ...SWAL_BASE, icon: 'error', title: 'خروجی خبرنامه', text: e instanceof Error ? e.message : 'خطا', confirmButtonText: 'باشه' })
|
||||||
|
})}
|
||||||
|
title="دانلود مشترکان خبرنامه بهصورت CSV"
|
||||||
|
className="border border-ivory/20 px-3 py-1.5 text-xs font-light text-ivory/70 transition-colors duration-200 hover:border-copper hover:text-copper"
|
||||||
|
>
|
||||||
|
خبرنامه (CSV)
|
||||||
|
</button>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={logout}
|
onClick={logout}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||||
import { Controller, useController, useForm } from 'react-hook-form'
|
import { Controller, useController, useForm } from 'react-hook-form'
|
||||||
import { Link, Navigate, useNavigate, useParams } from 'react-router-dom'
|
import { Link, Navigate, useNavigate, useParams } from 'react-router-dom'
|
||||||
import { ArrowRightIcon, CheckIcon, EyeIcon, Trash2Icon } from 'lucide-react'
|
import { ArrowRightIcon, CheckIcon, EyeIcon, PlusIcon, Trash2Icon, XIcon } from 'lucide-react'
|
||||||
import Swal from 'sweetalert2'
|
import Swal from 'sweetalert2'
|
||||||
import { useSiteData } from '../api'
|
import { useSiteData } from '../api'
|
||||||
import { useAdmin } from './AdminContext'
|
import { useAdmin } from './AdminContext'
|
||||||
@@ -54,6 +54,75 @@ function normalizeTags(value: unknown): string[] {
|
|||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** یک ردیف پرسشوپاسخ مقاله */
|
||||||
|
type ArticleFaq = { question: string; answer: string }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* پنل پرسشوپاسخهای پایان مقاله — مثل بخش متا دیسکریپشن زیر ویرایشگر.
|
||||||
|
* در زبان فارسی ستون faqs و در زبان انگلیسی faqs_en را ویرایش میکند؛
|
||||||
|
* نسخه انگلیسیِ خالی در سایت به فارسی برمیگردد. ذخیرهسازی خودکار است.
|
||||||
|
*/
|
||||||
|
function FaqPanel({
|
||||||
|
faqs,
|
||||||
|
onChange,
|
||||||
|
}: {
|
||||||
|
faqs: ArticleFaq[]
|
||||||
|
onChange: (next: ArticleFaq[]) => void
|
||||||
|
}) {
|
||||||
|
const update = (index: number, patch: Partial<ArticleFaq>) => {
|
||||||
|
const next = faqs.map((f, i) => (i === index ? { ...f, ...patch } : f))
|
||||||
|
onChange(next)
|
||||||
|
}
|
||||||
|
const remove = (index: number) => onChange(faqs.filter((_, i) => i !== index))
|
||||||
|
const add = () => onChange([...faqs, { question: '', answer: '' }])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mt-4 grid gap-4">
|
||||||
|
{faqs.map((faq, i) => (
|
||||||
|
<div key={i} className="grid gap-3 border border-ivory/10 bg-ink/60 p-4">
|
||||||
|
<div className="flex items-center justify-between gap-3">
|
||||||
|
<span className="text-[11px] font-light text-copper">پرسش {i + 1}</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => remove(i)}
|
||||||
|
aria-label={`حذف پرسش ${i + 1}`}
|
||||||
|
className="flex h-7 w-7 items-center justify-center border border-red-400/40 text-red-300 transition-colors hover:bg-red-500/10"
|
||||||
|
>
|
||||||
|
<XIcon className="h-3.5 w-3.5" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
dir="auto"
|
||||||
|
value={faq.question}
|
||||||
|
onChange={(e) => update(i, { question: e.target.value })}
|
||||||
|
placeholder="پرسش مخاطب…"
|
||||||
|
aria-label={`متن پرسش ${i + 1}`}
|
||||||
|
className="w-full border border-ivory/15 bg-char px-3 py-2 text-sm font-light text-ivory focus:border-copper focus:outline-none"
|
||||||
|
/>
|
||||||
|
<textarea
|
||||||
|
rows={2}
|
||||||
|
dir="auto"
|
||||||
|
value={faq.answer}
|
||||||
|
onChange={(e) => update(i, { answer: e.target.value })}
|
||||||
|
placeholder="پاسخ کوتاه و روشن…"
|
||||||
|
aria-label={`پاسخ پرسش ${i + 1}`}
|
||||||
|
className="w-full resize-y border border-ivory/15 bg-char px-3 py-2 text-sm font-light leading-8 text-ivory focus:border-copper focus:outline-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={add}
|
||||||
|
className="flex w-fit items-center gap-2 border border-dashed border-ivory/25 px-4 py-2 text-xs font-light text-ivory/60 transition-colors hover:border-copper hover:text-copper"
|
||||||
|
>
|
||||||
|
<PlusIcon className="h-3.5 w-3.5" />
|
||||||
|
افزودن پرسشوپاسخ
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const STATUS_TEXT: Record<string, string> = {
|
const STATUS_TEXT: Record<string, string> = {
|
||||||
idle: 'ذخیره شد',
|
idle: 'ذخیره شد',
|
||||||
saving: 'در حال ذخیره…',
|
saving: 'در حال ذخیره…',
|
||||||
@@ -458,6 +527,20 @@ export function ArticleWritePage() {
|
|||||||
/>
|
/>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
{/* پرسشوپاسخهای پایان مقاله — سئوی FAQPage */}
|
||||||
|
<section className="mt-8 border border-ivory/15 bg-char p-6">
|
||||||
|
<h2 className="text-sm font-bold text-copper">پرسشهای متداول مقاله</h2>
|
||||||
|
<p className="mt-1 text-xs font-light text-ivory/50">
|
||||||
|
در پایان مقاله با ظاهر آکاردئونی سایت نمایش داده میشود و با اسکیمای FAQPage
|
||||||
|
شانس نمایش در نتایج غنی گوگل را بالا میبرد.
|
||||||
|
{editLang === 'en' && ' — ویرایش نسخه انگلیسی (خالی = نمایش متن فارسی)'}
|
||||||
|
</p>
|
||||||
|
<FaqPanel
|
||||||
|
faqs={(editLang === 'en' ? draft.faqs_en : draft.faqs) ?? []}
|
||||||
|
onChange={(next) => queueSave({ [suf('faqs')]: next })}
|
||||||
|
/>
|
||||||
|
</section>
|
||||||
|
|
||||||
{/* چکیده و متادیتا */}
|
{/* چکیده و متادیتا */}
|
||||||
<SeoChecklist
|
<SeoChecklist
|
||||||
input={{
|
input={{
|
||||||
|
|||||||
@@ -87,15 +87,40 @@ export const adminApi = {
|
|||||||
reorder: <T = unknown>(resource: string, ids: number[]) =>
|
reorder: <T = unknown>(resource: string, ids: number[]) =>
|
||||||
authFetch<T[]>(`/api/admin/${resource}/reorder`, { method: 'POST', body: JSON.stringify({ ids }) }),
|
authFetch<T[]>(`/api/admin/${resource}/reorder`, { method: 'POST', body: JSON.stringify({ ids }) }),
|
||||||
|
|
||||||
upload: async (file: File): Promise<{ url: string; key: string }> => {
|
upload: async (file: File, name?: string): Promise<{ url: string; key: string }> => {
|
||||||
const fd = new FormData()
|
const fd = new FormData()
|
||||||
fd.append('file', file)
|
fd.append('file', file)
|
||||||
|
if (name && name.trim()) fd.append('name', name.trim())
|
||||||
return authFetch('/api/admin/upload', { method: 'POST', body: fd })
|
return authFetch('/api/admin/upload', { method: 'POST', body: fd })
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/** درونریزی تصویر از نشانی خارجی — دانلود و ذخیره در Object Storage خود سایت */
|
||||||
|
importImage: async (url: string): Promise<{ url: string; key: string }> =>
|
||||||
|
authFetch('/api/admin/import-image', { method: 'POST', body: JSON.stringify({ url }) }),
|
||||||
|
|
||||||
changePassword: (currentPassword: string, newPassword: string) =>
|
changePassword: (currentPassword: string, newPassword: string) =>
|
||||||
authFetch<{ ok: boolean }>('/api/auth/change-password', {
|
authFetch<{ ok: boolean }>('/api/auth/change-password', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }),
|
body: JSON.stringify({ current_password: currentPassword, new_password: newPassword }),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
// خروجی CSV — فایل تولیدی بکاند (با BOM برای Excel) مستقیم دانلود میشود
|
||||||
|
downloadCsv: async (kind: 'messages' | 'subscribers'): Promise<void> => {
|
||||||
|
const path = kind === 'messages' ? 'messages/export.csv' : 'subscribers/export.csv'
|
||||||
|
const token = getToken()
|
||||||
|
const res = await fetch(`${API_URL}/api/admin/${path}`, {
|
||||||
|
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
||||||
|
})
|
||||||
|
if (!res.ok) throw new HttpError(res.status, 'دریافت خروجی CSV ناموفق بود')
|
||||||
|
const blob = await res.blob()
|
||||||
|
const url = URL.createObjectURL(blob)
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = url
|
||||||
|
const disposition = res.headers.get('Content-Disposition') ?? ''
|
||||||
|
a.download = /filename="([^"]+)"/.exec(disposition)?.[1] ?? `${kind}.csv`
|
||||||
|
document.body.appendChild(a)
|
||||||
|
a.click()
|
||||||
|
a.remove()
|
||||||
|
URL.revokeObjectURL(url)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// لایهٔ نازک تحلیل رفتار (Umami) — اگر اسکریپت ردیاب بارگیری نشده باشد
|
||||||
|
// (VITE_UMAMI_SRC خالی)، همهٔ فراخوانیها بیاثر و ارزاناند.
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
umami?: { track: (event?: string, data?: Record<string, unknown>) => void }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** رخداد سفارشی — نام رخداد در داشبورد Umami نمایش داده میشود. */
|
||||||
|
export function track(event: string, data?: Record<string, unknown>) {
|
||||||
|
try {
|
||||||
|
window.umami?.track(event, data)
|
||||||
|
} catch {
|
||||||
|
// تحلیل رفتار هرگز نباید مسیر اصلی را بشکند
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import React from 'react'
|
|
||||||
import { Link } from 'react-router-dom'
|
import { Link } from 'react-router-dom'
|
||||||
import { ArrowLeftIcon, ClockIcon } from 'lucide-react'
|
import { ArrowLeftIcon, ClockIcon } from 'lucide-react'
|
||||||
import type { Article } from '../data/site'
|
import type { Article } from '../data/site'
|
||||||
@@ -11,43 +10,43 @@ export function ArticleCard({ article }: { article: Article }) {
|
|||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
to={href(`/blog/${article.slug}`)}
|
to={href(`/blog/${article.slug}`)}
|
||||||
className="group flex h-full flex-col overflow-hidden rounded-2xl border border-white/10 bg-char/80 backdrop-blur-xl shadow-[0_10px_30px_rgba(0,0,0,0.3)] transition-all duration-500 ease-lux hover:-translate-y-2 hover:border-copper/50 hover:shadow-[0_20px_40px_rgba(0,0,0,0.6),0_0_30px_rgba(197,143,92,0.18)] focus:outline-none focus-visible:ring-2 focus-visible:ring-copper"
|
className="group flex h-full flex-col overflow-hidden rounded-xl md:rounded-2xl border border-white/10 bg-char/80 backdrop-blur-xl shadow-[0_10px_30px_rgba(0,0,0,0.3)] transition-all duration-500 ease-lux hover:-translate-y-2 hover:border-copper/50 hover:shadow-[0_20px_40px_rgba(0,0,0,0.6),0_0_30px_rgba(197,143,92,0.18)] focus:outline-none focus-visible:ring-2 focus-visible:ring-copper"
|
||||||
aria-label={`${t('readArticlePrefix')} ${article.title}`}
|
aria-label={`${t('readArticlePrefix')} ${article.title}`}
|
||||||
>
|
>
|
||||||
<div className="relative overflow-hidden">
|
<div className="relative overflow-hidden aspect-[16/10] w-full">
|
||||||
<img
|
<img
|
||||||
src={article.image}
|
src={article.image}
|
||||||
alt={`${t('articleImagePrefix')} ${article.title}`}
|
alt={`${t('articleImagePrefix')} ${article.title}`}
|
||||||
className="h-52 w-full object-cover transition-transform duration-700 ease-lux group-hover:scale-105"
|
className="h-full w-full object-cover transition-transform duration-700 ease-lux group-hover:scale-105"
|
||||||
/>
|
/>
|
||||||
{/* برچسب دستهبندی کپسولی شیشهای روی تصویر */}
|
{/* برچسب دستهبندی کپسولی شیشهای روی تصویر */}
|
||||||
<div className="absolute top-4 ltr:left-4 rtl:right-4 rounded-full border border-white/20 bg-ink/75 px-3.5 py-1 text-xs font-light text-copper backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.2),0_4px_15px_rgba(0,0,0,0.4)]">
|
<div className="absolute top-3 ltr:left-3 rtl:right-3 md:top-4 md:ltr:left-4 md:rtl:right-4 rounded-full border border-white/20 bg-ink/75 px-2.5 py-0.5 md:px-3.5 md:py-1 text-[11px] md:text-xs font-light text-copper backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.2),0_4px_15px_rgba(0,0,0,0.4)]">
|
||||||
{article.category}
|
{article.category}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-1 flex-col p-8">
|
<div className="flex flex-1 flex-col p-3.5 md:p-8">
|
||||||
<h3 className="mb-4 md:mb-6 text-xl font-bold leading-relaxed text-ivory transition-colors duration-300 group-hover:text-copper">
|
<h3 className="mb-1.5 md:mb-6 text-xs md:text-xl font-bold leading-snug md:leading-relaxed text-ivory line-clamp-2 transition-colors duration-300 group-hover:text-copper">
|
||||||
{article.title}
|
{article.title}
|
||||||
</h3>
|
</h3>
|
||||||
|
|
||||||
<p className="mb-6 md:mb-10 text-[15px] font-light leading-loose text-ivory/60 line-clamp-3">
|
<p className="mb-2.5 md:mb-10 text-[11px] md:text-[15px] font-light leading-relaxed md:leading-loose text-ivory/60 line-clamp-2 md:line-clamp-3">
|
||||||
{article.excerpt}
|
{article.excerpt}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<div className="mt-auto flex items-center justify-between border-t border-white/10 pt-6 md:pt-8 text-xs font-light text-ivory/50">
|
<div className="mt-auto flex items-center justify-between border-t border-white/10 pt-2.5 md:pt-8 text-[11px] md:text-xs font-light text-ivory/50">
|
||||||
<span className="flex items-center gap-4">
|
<span className="flex items-center gap-2.5 md:gap-4">
|
||||||
<span>{formatJalali(article.date)}</span>
|
<span>{formatJalali(article.date)}</span>
|
||||||
<span className="flex items-center gap-2">
|
<span className="flex items-center gap-1.5 md:gap-2">
|
||||||
<ClockIcon className="h-3.5 w-3.5 text-copper/80" />
|
<ClockIcon className="h-3 w-3 md:h-3.5 md:w-3.5 text-copper/80" />
|
||||||
{toPersianDigits(article.read)}
|
{toPersianDigits(article.read)}
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
{/* دکمه کپسولی ادامه مطلب با افکت شیشهای */}
|
{/* دکمه کپسولی ادامه مطلب با افکت شیشهای */}
|
||||||
<span className="inline-flex items-center gap-2 rounded-full border border-copper/40 bg-copper/5 px-3.5 py-1.5 text-xs font-medium text-copper backdrop-blur-md shadow-[inset_0_1px_1px_rgba(255,255,255,0.15)] transition-all duration-300 ease-lux hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_15px_rgba(197,143,92,0.5)] rtl:hover:[&>svg]:-translate-x-1 ltr:hover:[&>svg]:translate-x-1">
|
<span className="inline-flex items-center gap-1.5 md:gap-2 rounded-full border border-copper/40 bg-copper/5 px-2.5 py-1 md:px-3.5 md:py-1.5 text-[11px] md:text-xs font-medium text-copper backdrop-blur-md shadow-[inset_0_1px_1px_rgba(255,255,255,0.15)] transition-all duration-300 ease-lux group-hover:border-copper group-hover:bg-copper group-hover:text-char group-hover:shadow-[0_0_15px_rgba(197,143,92,0.5)] rtl:group-hover:[&>svg]:-translate-x-1 ltr:group-hover:[&>svg]:translate-x-1">
|
||||||
{t('readMore')}
|
{t('readMore')}
|
||||||
<ArrowLeftIcon className="h-3.5 w-3.5 ltr:-scale-x-100 transition-transform duration-300" />
|
<ArrowLeftIcon className="h-3 w-3 md:h-3.5 md:w-3.5 ltr:-scale-x-100 transition-transform duration-300" />
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
import { AnimatePresence, motion } from 'framer-motion'
|
||||||
|
import { PlusIcon } from 'lucide-react'
|
||||||
|
|
||||||
|
/** یک پرسشوپاسخ مقاله — همان شکل داده ستون faqs مقاله در دیتابیس */
|
||||||
|
export type ArticleFaq = { question: string; answer: string }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* پرسشوپاسخهای پایان مقاله — همان رفتار آکاردئونی بخش FAQ سایت، بدون ویرایش مدیریتی.
|
||||||
|
* در سرچ کنسول/نتایج غنی گوگل، اسکیمای FAQPage آن جداگانه تزریق میشود.
|
||||||
|
*/
|
||||||
|
export function ArticleFaq({ faqs }: { faqs: ArticleFaq[] }) {
|
||||||
|
const [open, setOpen] = useState<number | null>(0)
|
||||||
|
|
||||||
|
if (!faqs.length) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section className="mt-16" aria-label="پرسشهای متداول مقاله">
|
||||||
|
<div className="mt-6 h-px w-24 bg-copper/70" />
|
||||||
|
<ul className="mt-2">
|
||||||
|
{faqs.map((faq, i) => {
|
||||||
|
const isOpen = open === i
|
||||||
|
return (
|
||||||
|
<li key={i} className="border-b border-ivory/10">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpen(isOpen ? null : i)}
|
||||||
|
aria-expanded={isOpen}
|
||||||
|
className="flex w-full items-center justify-between gap-6 py-6 text-start transition-colors duration-200 ease-lux hover:text-copper"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={`text-base font-bold md:text-lg ${
|
||||||
|
isOpen ? 'text-copper' : 'text-ivory'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{faq.question}
|
||||||
|
</span>
|
||||||
|
<PlusIcon
|
||||||
|
className={`h-5 w-5 shrink-0 text-copper transition-transform duration-300 ease-lux ${
|
||||||
|
isOpen ? 'rotate-45' : ''
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
<AnimatePresence initial={false}>
|
||||||
|
{isOpen && (
|
||||||
|
<motion.div
|
||||||
|
initial={{ height: 0, opacity: 0 }}
|
||||||
|
animate={{ height: 'auto', opacity: 1 }}
|
||||||
|
exit={{ height: 0, opacity: 0 }}
|
||||||
|
transition={{ duration: 0.28, ease: [0.23, 1, 0.32, 1] }}
|
||||||
|
className="overflow-hidden"
|
||||||
|
>
|
||||||
|
<p className="pb-7 ps-2 text-sm font-light leading-9 text-ivory/60">
|
||||||
|
{faq.answer}
|
||||||
|
</p>
|
||||||
|
</motion.div>
|
||||||
|
)}
|
||||||
|
</AnimatePresence>
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,251 @@
|
|||||||
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { BotIcon, SendIcon, SparklesIcon, XIcon } from 'lucide-react';
|
||||||
|
import { track } from '../analytics';
|
||||||
|
|
||||||
|
type ChatMessage = {
|
||||||
|
role: 'user' | 'assistant';
|
||||||
|
content: string;
|
||||||
|
streaming?: boolean;
|
||||||
|
error?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type SseFrame = {
|
||||||
|
content?: string;
|
||||||
|
error?: string;
|
||||||
|
done?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** شناسه گفتگو برای هر نشست — در sessionStorage میماند تا رفرش صفحه تاریخچه را نگه دارد */
|
||||||
|
function getThreadId(): string {
|
||||||
|
const KEY = 'chat_thread_id';
|
||||||
|
let id = sessionStorage.getItem(KEY);
|
||||||
|
if (!id) {
|
||||||
|
id = `t_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
|
||||||
|
sessionStorage.setItem(KEY, id);
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** خواندن فریمهای SSE از پاسخ fetch (استریم توکنبهتوکن) */
|
||||||
|
async function readSse(response: Response, onFrame: (frame: SseFrame) => void) {
|
||||||
|
const reader = response.body?.getReader();
|
||||||
|
if (!reader) throw new Error('پاسخ استریمی ندارد');
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
let buffer = '';
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
buffer += decoder.decode(value, { stream: true });
|
||||||
|
const lines = buffer.split('\n');
|
||||||
|
buffer = lines.pop() ?? '';
|
||||||
|
for (const line of lines) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (trimmed.startsWith('data: ')) {
|
||||||
|
try {
|
||||||
|
onFrame(JSON.parse(trimmed.slice(6)) as SseFrame);
|
||||||
|
} catch {
|
||||||
|
// فریم ناقص — نادیده بگیر
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ChatWidget() {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||||
|
const [input, setInput] = useState('');
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [suggestions, setSuggestions] = useState<string[]>([]);
|
||||||
|
const scrollEndRef = useRef<HTMLDivElement>(null);
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const assistantIndexRef = useRef<number | null>(null);
|
||||||
|
|
||||||
|
// پیشنهاد پرسشها فقط یک بار برای هر بازشدن اول
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open || suggestions.length > 0) return;
|
||||||
|
fetch('/chat/suggestions')
|
||||||
|
.then((r) => (r.ok ? r.json() : Promise.reject(new Error(String(r.status)))))
|
||||||
|
.then((d) => setSuggestions((d.questions ?? []).slice(0, 4)))
|
||||||
|
.catch(() => setSuggestions([]));
|
||||||
|
}, [open, suggestions.length]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
scrollEndRef.current?.scrollIntoView({ behavior: 'smooth', block: 'end' });
|
||||||
|
}, [messages, open]);
|
||||||
|
|
||||||
|
const sendMessage = async (text: string) => {
|
||||||
|
const question = text.trim();
|
||||||
|
if (!question || isLoading) return;
|
||||||
|
track('chat-question')
|
||||||
|
setInput('');
|
||||||
|
setIsLoading(true);
|
||||||
|
setMessages((prev) => {
|
||||||
|
const next: ChatMessage[] = [
|
||||||
|
...prev,
|
||||||
|
{ role: 'user', content: question },
|
||||||
|
{ role: 'assistant', content: '', streaming: true },
|
||||||
|
];
|
||||||
|
assistantIndexRef.current = next.length - 1;
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
|
||||||
|
const patchAssistant = (patch: Partial<ChatMessage>) => {
|
||||||
|
setMessages((prev) => {
|
||||||
|
const idx = assistantIndexRef.current;
|
||||||
|
if (idx === null || !prev[idx]) return prev;
|
||||||
|
const updated = [...prev];
|
||||||
|
updated[idx] = { ...updated[idx], ...patch };
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const params = new URLSearchParams({ message: question, thread_id: getThreadId() });
|
||||||
|
const response = await fetch(`/chat/stream?${params.toString()}`, {
|
||||||
|
headers: { Accept: 'text/event-stream' },
|
||||||
|
});
|
||||||
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
||||||
|
|
||||||
|
let accumulated = '';
|
||||||
|
await readSse(response, (frame) => {
|
||||||
|
if (frame.error) {
|
||||||
|
patchAssistant({ content: frame.error, streaming: false, error: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (frame.content) {
|
||||||
|
accumulated += frame.content;
|
||||||
|
patchAssistant({ content: accumulated });
|
||||||
|
}
|
||||||
|
if (frame.done) patchAssistant({ content: accumulated || 'پاسخی دریافت نشد.', streaming: false });
|
||||||
|
});
|
||||||
|
patchAssistant({ streaming: false });
|
||||||
|
} catch (err) {
|
||||||
|
patchAssistant({
|
||||||
|
content: 'سرویس گفتگو در دسترس نیست؛ لطفاً کمی بعد دوباره تلاش کنید.',
|
||||||
|
streaming: false,
|
||||||
|
error: true,
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
inputRef.current?.focus();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* دکمه شناور — گوشه راستپایین (دکمه تماس سمت چپ است) */}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setOpen((v) => {
|
||||||
|
if (!v) track('chat-open');
|
||||||
|
return !v;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
aria-label={open ? 'بستن گفتگو' : 'گفتگو با دستیار هوشمند'}
|
||||||
|
className="group fixed bottom-8 right-8 z-40 flex h-14 w-14 items-center justify-center rounded-full bg-ivory text-ink shadow-[0_10px_30px_rgba(0,0,0,0.45)] transition-transform duration-200 ease-lux hover:scale-105">
|
||||||
|
<span className="absolute inset-0 rounded-full border border-ivory/60 opacity-0 transition-transform duration-300 ease-lux group-hover:scale-125 group-hover:opacity-100" />
|
||||||
|
{open ? <XIcon className="h-5 w-5" /> : <BotIcon className="h-5 w-5" />}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{/* پنل گفتگو */}
|
||||||
|
{open && (
|
||||||
|
<div
|
||||||
|
className="fixed bottom-24 right-4 z-40 flex h-[min(560px,72vh)] w-[min(380px,calc(100vw-2rem))] flex-col overflow-hidden rounded-2xl border border-copper/30 bg-char shadow-lux sm:right-8">
|
||||||
|
{/* سربرگ */}
|
||||||
|
<div className="flex items-center gap-3 border-b border-ivory/10 bg-ink px-4 py-3">
|
||||||
|
<span className="flex h-9 w-9 items-center justify-center rounded-full bg-copper/15 text-copper">
|
||||||
|
<SparklesIcon className="h-4 w-4" />
|
||||||
|
</span>
|
||||||
|
<div className="flex-1">
|
||||||
|
<p className="text-sm font-bold text-ivory">دستیار هوشمند آرامش</p>
|
||||||
|
<p className="text-[11px] font-light text-ivory/50">پاسخ از محتوای همین سایت</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpen(false)}
|
||||||
|
aria-label="بستن"
|
||||||
|
className="rounded-full p-1.5 text-ivory/50 transition-colors hover:bg-ivory/10 hover:text-ivory">
|
||||||
|
<XIcon className="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* پیامها */}
|
||||||
|
<div className="flex-1 space-y-3 overflow-y-auto px-4 py-4">
|
||||||
|
{messages.length === 0 && (
|
||||||
|
<div className="mt-6 text-center">
|
||||||
|
<BotIcon className="mx-auto h-8 w-8 text-copper/60" />
|
||||||
|
<p className="mt-3 text-sm font-light leading-7 text-ivory/60">
|
||||||
|
سلام! هر سؤالی درباره خدمات، پروژهها و مقالات
|
||||||
|
<span className="font-bold text-ivory"> گروه ساختمانی آرامش </span>
|
||||||
|
دارید بپرسید.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{messages.map((msg, i) => (
|
||||||
|
<div key={i} className={`flex ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}>
|
||||||
|
<div
|
||||||
|
className={`max-w-[85%] whitespace-pre-wrap rounded-2xl px-3.5 py-2.5 text-[13px] leading-6 ${
|
||||||
|
msg.role === 'user'
|
||||||
|
? 'rounded-bl-md bg-copper text-char'
|
||||||
|
: msg.error
|
||||||
|
? 'rounded-br-md border border-red-400/30 bg-red-400/10 text-red-200'
|
||||||
|
: 'rounded-br-md border border-ivory/10 bg-ink text-ivory'
|
||||||
|
}`}>
|
||||||
|
{msg.content || (msg.streaming ? '…' : '')}
|
||||||
|
{msg.streaming && msg.content && (
|
||||||
|
<span className="ms-1 inline-block h-1.5 w-1.5 animate-pulse rounded-full bg-copper align-middle" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<div ref={scrollEndRef} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* پیشنهاد پرسشها */}
|
||||||
|
{suggestions.length > 0 && messages.length === 0 && (
|
||||||
|
<div className="flex flex-wrap gap-1.5 border-t border-ivory/10 px-3 py-2.5">
|
||||||
|
{suggestions.map((q) => (
|
||||||
|
<button
|
||||||
|
key={q}
|
||||||
|
type="button"
|
||||||
|
onClick={() => sendMessage(q)}
|
||||||
|
className="rounded-full border border-copper/30 px-2.5 py-1 text-[11px] text-copper transition-colors hover:bg-copper hover:text-char">
|
||||||
|
{q}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* ورودی */}
|
||||||
|
<form
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
sendMessage(input);
|
||||||
|
}}
|
||||||
|
className="flex items-center gap-2 border-t border-ivory/10 bg-ink px-3 py-3">
|
||||||
|
<input
|
||||||
|
ref={inputRef}
|
||||||
|
type="text"
|
||||||
|
value={input}
|
||||||
|
onChange={(e) => setInput(e.target.value)}
|
||||||
|
placeholder="پرسش خود را بنویسید…"
|
||||||
|
disabled={isLoading}
|
||||||
|
className="flex-1 rounded-full border border-ivory/15 bg-char px-4 py-2 text-[13px] text-ivory placeholder:text-ivory/30 focus:border-copper/60 focus:outline-none disabled:opacity-50"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={isLoading || !input.trim()}
|
||||||
|
aria-label="ارسال"
|
||||||
|
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-copper text-char transition-transform hover:scale-105 disabled:bg-ivory/15 disabled:text-ivory/40 disabled:hover:scale-100">
|
||||||
|
<SendIcon className="h-4 w-4 -scale-x-100" />
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { CheckCircle2Icon, MailIcon, MapPinIcon, PhoneIcon } from 'lucide-react'
|
import { CheckCircle2Icon, MailIcon, MapPinIcon, PhoneIcon } from 'lucide-react'
|
||||||
import Swal from 'sweetalert2'
|
import Swal from 'sweetalert2'
|
||||||
import { Reveal } from './Reveal'
|
import { Reveal } from './Reveal'
|
||||||
@@ -9,12 +9,13 @@ import { useFieldSaver } from '../admin/AdminContext'
|
|||||||
import { InlineEdit } from './admin/InlineEdit'
|
import { InlineEdit } from './admin/InlineEdit'
|
||||||
import { SingletonPill } from '../admin/Editable'
|
import { SingletonPill } from '../admin/Editable'
|
||||||
import { API_URL } from '../config'
|
import { API_URL } from '../config'
|
||||||
|
import { track } from '../analytics'
|
||||||
|
|
||||||
export function ContactSection() {
|
export function ContactSection() {
|
||||||
const { brand, contact_section: content } = useSite()
|
const { brand, contact_section: content } = useSite()
|
||||||
const { t } = useLang()
|
const { t } = useLang()
|
||||||
const save = useFieldSaver()
|
const save = useFieldSaver()
|
||||||
const [form, setForm] = useState({ name: '', phone: '', note: '' })
|
const [form, setForm] = useState({ name: '', phone: '', note: '', website: '' })
|
||||||
const [sent, setSent] = useState(false)
|
const [sent, setSent] = useState(false)
|
||||||
const [sending, setSending] = useState(false)
|
const [sending, setSending] = useState(false)
|
||||||
|
|
||||||
@@ -79,7 +80,7 @@ export function ContactSection() {
|
|||||||
const res = await fetch(`${API_URL}/api/contact`, {
|
const res = await fetch(`${API_URL}/api/contact`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ name: nameValue, phone: phoneValue, note: form.note.trim() }),
|
body: JSON.stringify({ name: nameValue, phone: phoneValue, note: form.note.trim(), website: form.website }),
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@@ -90,6 +91,7 @@ export function ContactSection() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
track('contact-submit')
|
||||||
setSent(true)
|
setSent(true)
|
||||||
} catch {
|
} catch {
|
||||||
showErrorAlert(t('connectionErrorTitle'), t('connectionErrorText'))
|
showErrorAlert(t('connectionErrorTitle'), t('connectionErrorText'))
|
||||||
@@ -98,18 +100,139 @@ export function ContactSection() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cleanOptionalLabel = `(${content.note_optional_label?.replace(/^\(+|\)+$/g, '') || 'اختیاری'})`
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section id="contact" className="relative overflow-hidden py-24 lg:py-32" aria-labelledby="contact-title">
|
<section id="contact" className="relative w-full max-w-full overflow-x-clip border-t border-copper/15 py-16 md:py-24 lg:py-32" aria-labelledby="contact-title">
|
||||||
<img
|
<img
|
||||||
src={brand.contact_bg}
|
src={brand.contact_bg}
|
||||||
alt=""
|
alt=""
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
className="absolute inset-0 h-full w-full object-cover"
|
className="absolute inset-0 h-full w-full object-cover object-center"
|
||||||
/>
|
/>
|
||||||
<div className="absolute inset-0 bg-ink/75 backdrop-blur-[2px]" />
|
<div className="absolute inset-0 bg-gradient-to-b from-ink/90 via-ink/75 to-ink backdrop-blur-[2px]" />
|
||||||
|
|
||||||
<div className="relative mx-auto max-w-site px-6 lg:px-10">
|
<div className="relative mx-auto max-w-site px-4 md:px-6 lg:px-10">
|
||||||
<div className="mx-auto grid max-w-6xl items-stretch gap-8 lg:grid-cols-12">
|
|
||||||
|
{/* Mobile View: High-Conversion Streamlined Layout */}
|
||||||
|
<div className="block md:hidden px-1 py-1">
|
||||||
|
{/* Header */}
|
||||||
|
<div className="mb-4 text-center">
|
||||||
|
<p className="text-xs font-light tracking-wide text-copper">
|
||||||
|
<InlineEdit value={content.label} onSave={(v) => saveContent('label', v)} />
|
||||||
|
</p>
|
||||||
|
<h2 className="mt-1.5 text-xl font-bold text-ivory">
|
||||||
|
<InlineEdit value={content.title} onSave={(v) => saveContent('title', v)} />
|
||||||
|
</h2>
|
||||||
|
<p className="mt-1.5 text-xs font-light leading-relaxed text-ivory/70 max-w-sm mx-auto">
|
||||||
|
<InlineEdit multiline value={content.lead} onSave={(v) => saveContent('lead', v)} />
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* High-Conversion Form Box */}
|
||||||
|
<div className="rounded-2xl border border-white/10 bg-char/60 p-4 backdrop-blur-xl shadow-lg space-y-3">
|
||||||
|
{sent ? (
|
||||||
|
<div className="flex flex-col items-center justify-center py-6 text-center">
|
||||||
|
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-copper/40 bg-copper/15 text-copper">
|
||||||
|
<CheckCircle2Icon className="h-6 w-6" />
|
||||||
|
</div>
|
||||||
|
<h3 className="mt-4 text-base font-bold text-ivory">
|
||||||
|
{content.success_title}
|
||||||
|
</h3>
|
||||||
|
<p className="mt-2 text-xs font-light leading-relaxed text-ivory/70">
|
||||||
|
{content.success_text}
|
||||||
|
</p>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setSent(false)
|
||||||
|
setForm({ name: '', phone: '', note: '', website: '' })
|
||||||
|
}}
|
||||||
|
className="mt-4 rounded-xl bg-copper px-6 py-2.5 text-xs font-bold text-char transition-all cursor-pointer"
|
||||||
|
>
|
||||||
|
{content.success_button_label}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<form onSubmit={submit} noValidate className="space-y-3">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="website_mobile"
|
||||||
|
value={form.website}
|
||||||
|
onChange={(e) => setForm({ ...form, website: e.target.value })}
|
||||||
|
tabIndex={-1}
|
||||||
|
autoComplete="off"
|
||||||
|
aria-hidden="true"
|
||||||
|
className="pointer-events-none absolute -left-[9999px] h-0 w-0 opacity-0"
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="contact-name-mobile" className="text-[11px] font-semibold text-ivory/80 mb-1 block">
|
||||||
|
{content.name_label}
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="contact-name-mobile"
|
||||||
|
type="text"
|
||||||
|
value={form.name}
|
||||||
|
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
||||||
|
placeholder={content.name_placeholder}
|
||||||
|
className="w-full py-2.5 px-3 text-xs rounded-xl border border-white/10 bg-ink/60 text-ivory placeholder:text-ivory/35 backdrop-blur-md focus:border-copper/70 focus:outline-none"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label htmlFor="contact-phone-mobile" className="text-[11px] font-semibold text-ivory/80 mb-1 block">
|
||||||
|
{content.phone_field_label} <span className="text-copper">*</span>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="contact-phone-mobile"
|
||||||
|
type="tel"
|
||||||
|
dir="ltr"
|
||||||
|
required
|
||||||
|
value={form.phone}
|
||||||
|
onChange={(e) => setForm({ ...form, phone: e.target.value })}
|
||||||
|
placeholder={content.phone_placeholder}
|
||||||
|
className="w-full py-2.5 px-3 text-xs rounded-xl border border-white/10 bg-ink/60 text-ivory placeholder:text-ivory/35 backdrop-blur-md focus:border-copper/70 focus:outline-none text-left font-mono"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label htmlFor="contact-note-mobile" className="text-[11px] font-semibold text-ivory/80 mb-1 block">
|
||||||
|
{content.note_label} <span className="font-light text-ivory/50">{cleanOptionalLabel}</span>
|
||||||
|
</label>
|
||||||
|
<textarea
|
||||||
|
id="contact-note-mobile"
|
||||||
|
rows={2}
|
||||||
|
value={form.note}
|
||||||
|
onChange={(e) => setForm({ ...form, note: e.target.value })}
|
||||||
|
placeholder={content.note_placeholder}
|
||||||
|
className="w-full py-2.5 px-3 text-xs rounded-xl border border-white/10 bg-ink/60 text-ivory placeholder:text-ivory/35 backdrop-blur-md focus:border-copper/70 focus:outline-none resize-none leading-relaxed"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={sending}
|
||||||
|
className="w-full py-3 rounded-xl bg-copper text-char font-bold text-xs shadow-md hover:brightness-110 active:scale-95 transition-all flex items-center justify-center gap-2 cursor-pointer"
|
||||||
|
>
|
||||||
|
{sending ? content.sending_label : content.submit_label}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{/* Direct Call Fallback */}
|
||||||
|
<a
|
||||||
|
href={brand.phone_href}
|
||||||
|
onClick={() => track('phone-click', { from: 'contact-section-mobile-hotline' })}
|
||||||
|
className="flex items-center justify-center gap-1.5 pt-2 text-[11px] text-ivory/60 hover:text-copper transition-colors"
|
||||||
|
>
|
||||||
|
<span>نیاز به گفتگوی فوری دارید؟</span>
|
||||||
|
<span className="font-bold text-copper dir-ltr" dir="ltr">{brand.phone}</span>
|
||||||
|
</a>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Desktop View */}
|
||||||
|
<div className="hidden md:grid mx-auto max-w-6xl items-stretch gap-8 lg:grid-cols-12">
|
||||||
|
|
||||||
{/* باکس اطلاعات تماس */}
|
{/* باکس اطلاعات تماس */}
|
||||||
<div className="rounded-3xl border border-white/10 bg-char/60 p-8 backdrop-blur-2xl shadow-[0_20px_50px_rgba(0,0,0,0.4),inset_0_1px_1px_rgba(255,255,255,0.15)] md:p-10 lg:col-span-5">
|
<div className="rounded-3xl border border-white/10 bg-char/60 p-8 backdrop-blur-2xl shadow-[0_20px_50px_rgba(0,0,0,0.4),inset_0_1px_1px_rgba(255,255,255,0.15)] md:p-10 lg:col-span-5">
|
||||||
@@ -152,6 +275,7 @@ export function ContactSection() {
|
|||||||
<a
|
<a
|
||||||
href={brand.phone_href}
|
href={brand.phone_href}
|
||||||
dir="ltr"
|
dir="ltr"
|
||||||
|
onClick={() => track('phone-click', { from: 'contact-section' })}
|
||||||
className="mt-1 block text-sm font-light text-ivory/70 transition-colors hover:text-copper"
|
className="mt-1 block text-sm font-light text-ivory/70 transition-colors hover:text-copper"
|
||||||
>
|
>
|
||||||
<InlineEdit value={brand.phone} onSave={(v) => saveBrand('phone', v)} />
|
<InlineEdit value={brand.phone} onSave={(v) => saveBrand('phone', v)} />
|
||||||
@@ -197,7 +321,7 @@ export function ContactSection() {
|
|||||||
<PremiumButton
|
<PremiumButton
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setSent(false)
|
setSent(false)
|
||||||
setForm({ name: '', phone: '', note: '' })
|
setForm({ name: '', phone: '', note: '', website: '' })
|
||||||
}}
|
}}
|
||||||
className="mt-8 font-light"
|
className="mt-8 font-light"
|
||||||
>
|
>
|
||||||
@@ -206,6 +330,17 @@ export function ContactSection() {
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<form onSubmit={submit} noValidate className="grid gap-6 md:grid-cols-2">
|
<form onSubmit={submit} noValidate className="grid gap-6 md:grid-cols-2">
|
||||||
|
{/* honeypot — خارج از دید؛ فقط رباتها آن را پر میکنند */}
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="website"
|
||||||
|
value={form.website}
|
||||||
|
onChange={(e) => setForm({ ...form, website: e.target.value })}
|
||||||
|
tabIndex={-1}
|
||||||
|
autoComplete="off"
|
||||||
|
aria-hidden="true"
|
||||||
|
className="pointer-events-none absolute -left-[9999px] h-0 w-0 opacity-0"
|
||||||
|
/>
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="contact-name" className="block text-sm font-medium text-ivory/90">
|
<label htmlFor="contact-name" className="block text-sm font-medium text-ivory/90">
|
||||||
<InlineEdit value={content.name_label} onSave={(v) => saveContent('name_label', v)} />
|
<InlineEdit value={content.name_label} onSave={(v) => saveContent('name_label', v)} />
|
||||||
@@ -238,7 +373,7 @@ export function ContactSection() {
|
|||||||
|
|
||||||
<div className="md:col-span-2">
|
<div className="md:col-span-2">
|
||||||
<label htmlFor="contact-note" className="block text-sm font-medium text-ivory/90">
|
<label htmlFor="contact-note" className="block text-sm font-medium text-ivory/90">
|
||||||
<InlineEdit value={content.note_label} onSave={(v) => saveContent('note_label', v)} /> <span className="font-light text-ivory/50">{content.note_optional_label}</span>
|
<InlineEdit value={content.note_label} onSave={(v) => saveContent('note_label', v)} /> <span className="font-light text-ivory/50">{cleanOptionalLabel}</span>
|
||||||
</label>
|
</label>
|
||||||
<textarea
|
<textarea
|
||||||
id="contact-note"
|
id="contact-note"
|
||||||
@@ -267,6 +402,7 @@ export function ContactSection() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import React from 'react';
|
|||||||
import { PhoneIcon } from 'lucide-react';
|
import { PhoneIcon } from 'lucide-react';
|
||||||
import { useSite } from '../api';
|
import { useSite } from '../api';
|
||||||
import { useLang } from '../i18n/lang';
|
import { useLang } from '../i18n/lang';
|
||||||
|
import { track } from '../analytics';
|
||||||
|
|
||||||
export function FloatingContact() {
|
export function FloatingContact() {
|
||||||
const { brand } = useSite();
|
const { brand } = useSite();
|
||||||
@@ -11,6 +12,7 @@ export function FloatingContact() {
|
|||||||
<a
|
<a
|
||||||
href={brand.phone_href}
|
href={brand.phone_href}
|
||||||
aria-label={t('callBrand')}
|
aria-label={t('callBrand')}
|
||||||
|
onClick={() => track('phone-click', { from: 'floating' })}
|
||||||
className="group fixed bottom-8 ltr:right-8 rtl:left-8 z-40 flex h-14 w-14 items-center justify-center rounded-full border border-copper/50 bg-char/80 text-copper backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.1),0_12px_35px_rgba(0,0,0,0.5),0_0_20px_rgba(197,143,92,0.25)] transition-all duration-500 ease-lux hover:scale-110 hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_35px_rgba(197,143,92,0.7)] active:scale-95"
|
className="group fixed bottom-8 ltr:right-8 rtl:left-8 z-40 flex h-14 w-14 items-center justify-center rounded-full border border-copper/50 bg-char/80 text-copper backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.1),0_12px_35px_rgba(0,0,0,0.5),0_0_20px_rgba(197,143,92,0.25)] transition-all duration-500 ease-lux hover:scale-110 hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_35px_rgba(197,143,92,0.7)] active:scale-95"
|
||||||
>
|
>
|
||||||
{/* پالس هاله نوری در پسزمینه مانند رادار */}
|
{/* پالس هاله نوری در پسزمینه مانند رادار */}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import { useState } from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
import { ArrowUpIcon, InstagramIcon, LinkedinIcon, MailIcon, MapPinIcon, PhoneIcon, SendIcon } from 'lucide-react';
|
import { ArrowUpIcon, InstagramIcon, LinkedinIcon, MailIcon, MapPinIcon, PhoneIcon, SendIcon } from 'lucide-react';
|
||||||
import { useSite } from '../api';
|
import { useSite } from '../api';
|
||||||
@@ -17,6 +17,7 @@ export function Footer() {
|
|||||||
const { brand, services, nav, footer } = useSite();
|
const { brand, services, nav, footer } = useSite();
|
||||||
const { t, href } = useLang();
|
const { t, href } = useLang();
|
||||||
const save = useFieldSaver();
|
const save = useFieldSaver();
|
||||||
|
const [openSection, setOpenSection] = useState<string | null>(null);
|
||||||
|
|
||||||
const saveFooter = (field: string, value: string) =>
|
const saveFooter = (field: string, value: string) =>
|
||||||
save('footer', footer.id, { [field]: value }, (d) => {
|
save('footer', footer.id, { [field]: value }, (d) => {
|
||||||
@@ -34,19 +35,22 @@ export function Footer() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<footer className="relative border-t border-white/10 bg-char/95 backdrop-blur-2xl">
|
<footer className="relative w-full max-w-full overflow-x-clip border-t border-white/10 bg-char/95 backdrop-blur-2xl">
|
||||||
<div className="mx-auto max-w-site px-6 py-16 lg:px-10">
|
<div className="mx-auto max-w-site px-4 py-8 md:px-6 md:py-16 lg:px-10">
|
||||||
<div className="grid gap-12 lg:grid-cols-12">
|
|
||||||
<div className="lg:col-span-4">
|
{/* Mobile View: Collapsible Luxury Accordions */}
|
||||||
|
<div className="block md:hidden">
|
||||||
|
{/* Top Brand Block */}
|
||||||
|
<div className="mb-6 flex flex-col items-center text-center">
|
||||||
<div className="relative w-fit">
|
<div className="relative w-fit">
|
||||||
<img src={brand.logo} alt={brand.name} className="h-20 w-auto object-contain" />
|
<img src={brand.logo} alt={brand.name} className="h-16 w-auto object-contain" />
|
||||||
<SingletonPill resource="brand" label="ویرایش برند، لوگو و تماس" className="absolute top-1/2 -translate-y-1/2 ltr:-right-10 rtl:-left-10" />
|
<SingletonPill resource="brand" label="ویرایش برند" className="absolute top-1/2 -translate-y-1/2 ltr:-right-8 rtl:-left-8" />
|
||||||
</div>
|
</div>
|
||||||
<p className="mt-6 max-w-sm text-sm font-light leading-8 text-ivory/60">
|
<p className="mt-4 text-xs font-light leading-relaxed text-ivory/60 max-w-xs">
|
||||||
<InlineEdit multiline value={footer.about_text} onSave={(v) => saveFooter('about_text', v)} />
|
{footer.about_text}
|
||||||
</p>
|
</p>
|
||||||
{/* آیکونهای شبکههای اجتماعی: گویهای شیشهای شناور */}
|
{/* Social icons */}
|
||||||
<div className="mt-8 flex items-center gap-3">
|
<div className="mt-4 flex items-center justify-center gap-3">
|
||||||
{footer.socials.map(({ label, href, icon }) => {
|
{footer.socials.map(({ label, href, icon }) => {
|
||||||
const Icon = SOCIAL_ICONS[icon] ?? InstagramIcon;
|
const Icon = SOCIAL_ICONS[icon] ?? InstagramIcon;
|
||||||
return (
|
return (
|
||||||
@@ -54,100 +58,234 @@ export function Footer() {
|
|||||||
key={label}
|
key={label}
|
||||||
href={href}
|
href={href}
|
||||||
aria-label={label}
|
aria-label={label}
|
||||||
className="group flex h-11 w-11 items-center justify-center rounded-full border border-white/15 bg-white/[0.04] text-ivory/70 backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.15),0_4px_12px_rgba(0,0,0,0.3)] transition-all duration-300 ease-lux hover:-translate-y-1 hover:border-copper/70 hover:bg-copper/10 hover:text-copper hover:shadow-[0_0_20px_rgba(197,143,92,0.4)] active:scale-95"
|
className="group flex h-10 w-10 items-center justify-center rounded-full border border-white/15 bg-white/[0.04] text-ivory/70 backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.15),0_4px_12px_rgba(0,0,0,0.3)] transition-all duration-300 ease-lux hover:border-copper/70 hover:bg-copper/10 hover:text-copper active:scale-95"
|
||||||
>
|
>
|
||||||
<Icon className="h-4 w-4 transition-transform duration-300 group-hover:scale-110" />
|
<Icon className="h-3.5 w-3.5 transition-transform duration-300 group-hover:scale-110" />
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="lg:col-span-2">
|
{/* Collapsible Link Groups */}
|
||||||
<h3 className="text-sm font-bold text-copper">
|
<div className="border-t border-white/10">
|
||||||
<InlineEdit value={footer.pages_heading} onSave={(v) => saveFooter('pages_heading', v)} />
|
{/* Pages Accordion */}
|
||||||
</h3>
|
<div className="border-b border-white/10">
|
||||||
<ul className="mt-5 space-y-3">
|
<button
|
||||||
{nav.map((item) => (
|
type="button"
|
||||||
<EditableItem key={item.id} resource="nav" item={item}>
|
onClick={() => setOpenSection(openSection === 'pages' ? null : 'pages')}
|
||||||
<li>
|
className="flex w-full items-center justify-between py-3.5 text-xs font-bold text-copper text-start cursor-pointer"
|
||||||
<Link
|
>
|
||||||
to={href(item.href)}
|
<span>{footer.pages_heading}</span>
|
||||||
className="inline-block text-sm font-light text-ivory/60 transition-colors duration-200 ease-lux hover:text-copper"
|
<span className={`text-[10px] text-copper transition-transform duration-300 ${openSection === 'pages' ? 'rotate-180' : ''}`}>
|
||||||
>
|
▼
|
||||||
<InlineEdit
|
</span>
|
||||||
value={item.label}
|
</button>
|
||||||
onSave={(v) => saveNav(item.id, { label: v })}
|
<div className={`overflow-hidden transition-all duration-300 ease-in-out ${openSection === 'pages' ? 'max-h-96 pb-3 opacity-100' : 'max-h-0 opacity-0'}`}>
|
||||||
/>
|
<ul className="space-y-2 rtl:pr-2 ltr:pl-2 text-xs text-ivory/70">
|
||||||
</Link>
|
{nav.map((item) => (
|
||||||
|
<li key={item.id}>
|
||||||
|
<Link
|
||||||
|
to={href(item.href)}
|
||||||
|
className="block py-1 transition-colors hover:text-copper"
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</Link>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Services Accordion */}
|
||||||
|
<div className="border-b border-white/10">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpenSection(openSection === 'services' ? null : 'services')}
|
||||||
|
className="flex w-full items-center justify-between py-3.5 text-xs font-bold text-copper text-start cursor-pointer"
|
||||||
|
>
|
||||||
|
<span>{footer.services_heading}</span>
|
||||||
|
<span className={`text-[10px] text-copper transition-transform duration-300 ${openSection === 'services' ? 'rotate-180' : ''}`}>
|
||||||
|
▼
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
<div className={`overflow-hidden transition-all duration-300 ease-in-out ${openSection === 'services' ? 'max-h-96 pb-3 opacity-100' : 'max-h-0 opacity-0'}`}>
|
||||||
|
<ul className="space-y-2 rtl:pr-2 ltr:pl-2 text-xs text-ivory/70">
|
||||||
|
{services.map((service) => (
|
||||||
|
<li key={service.id} className="py-1">
|
||||||
|
{service.title}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Contact Accordion */}
|
||||||
|
<div className="border-b border-white/10">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setOpenSection(openSection === 'contact' ? null : 'contact')}
|
||||||
|
className="flex w-full items-center justify-between py-3.5 text-xs font-bold text-copper text-start cursor-pointer"
|
||||||
|
>
|
||||||
|
<span>{footer.contact_heading}</span>
|
||||||
|
<span className={`text-[10px] text-copper transition-transform duration-300 ${openSection === 'contact' ? 'rotate-180' : ''}`}>
|
||||||
|
▼
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
<div className={`overflow-hidden transition-all duration-300 ease-in-out ${openSection === 'contact' ? 'max-h-96 pb-3 opacity-100' : 'max-h-0 opacity-0'}`}>
|
||||||
|
<ul className="space-y-3 rtl:pr-2 ltr:pl-2 text-xs text-ivory/70">
|
||||||
|
<li className="flex items-start gap-2.5">
|
||||||
|
<MapPinIcon className="mt-0.5 h-3.5 w-3.5 shrink-0 text-copper" />
|
||||||
|
<span className="leading-relaxed">{brand.address}</span>
|
||||||
</li>
|
</li>
|
||||||
</EditableItem>
|
<li className="flex items-center gap-2.5">
|
||||||
))}
|
<PhoneIcon className="h-3.5 w-3.5 shrink-0 text-copper" />
|
||||||
</ul>
|
<a href={brand.phone_href} dir="ltr" className="transition-colors hover:text-copper">
|
||||||
<div className="mt-4">
|
{brand.phone}
|
||||||
<AddItemButton resource="nav" />
|
</a>
|
||||||
|
</li>
|
||||||
|
<li className="flex items-center gap-2.5">
|
||||||
|
<MailIcon className="h-3.5 w-3.5 shrink-0 text-copper" />
|
||||||
|
<a href={`mailto:${brand.email}`} dir="ltr" className="transition-colors hover:text-copper">
|
||||||
|
{brand.email}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="lg:col-span-3">
|
{/* Bottom Copyright & Meta for Mobile */}
|
||||||
<h3 className="text-sm font-bold text-copper">
|
<div className="mt-6 flex flex-col items-center gap-3 border-t border-white/10 pt-4 text-[11px] text-ivory/50 text-center">
|
||||||
<InlineEdit value={footer.services_heading} onSave={(v) => saveFooter('services_heading', v)} />
|
<button
|
||||||
</h3>
|
type="button"
|
||||||
<ul className="mt-5 space-y-3">
|
onClick={scrollToTop}
|
||||||
{services.map((service) => (
|
className="group flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.04] px-3.5 py-1.5 text-xs font-medium text-ivory/70 backdrop-blur-xl transition-all hover:border-copper hover:bg-copper/10 hover:text-copper active:scale-95 cursor-pointer mb-1"
|
||||||
<li key={service.id} className="text-sm font-light text-ivory/60">
|
>
|
||||||
{service.title}
|
<span>{t('backToTop')}</span>
|
||||||
|
<ArrowUpIcon className="h-3 w-3 text-copper transition-transform duration-300 group-hover:-translate-y-0.5" />
|
||||||
|
</button>
|
||||||
|
<p>{footer.copyright}</p>
|
||||||
|
<p className="text-[10px] text-ivory/40">{footer.tagline}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Desktop View: Multi-Column Layout */}
|
||||||
|
<div className="hidden md:block">
|
||||||
|
<div className="grid gap-12 lg:grid-cols-12">
|
||||||
|
<div className="lg:col-span-4">
|
||||||
|
<div className="relative w-fit">
|
||||||
|
<img src={brand.logo} alt={brand.name} className="h-20 w-auto object-contain" />
|
||||||
|
<SingletonPill resource="brand" label="ویرایش برند، لوگو و تماس" className="absolute top-1/2 -translate-y-1/2 ltr:-right-10 rtl:-left-10" />
|
||||||
|
</div>
|
||||||
|
<p className="mt-6 max-w-sm text-sm font-light leading-8 text-ivory/60">
|
||||||
|
<InlineEdit multiline value={footer.about_text} onSave={(v) => saveFooter('about_text', v)} />
|
||||||
|
</p>
|
||||||
|
{/* آیکونهای شبکههای اجتماعی */}
|
||||||
|
<div className="mt-8 flex items-center gap-3">
|
||||||
|
{footer.socials.map(({ label, href, icon }) => {
|
||||||
|
const Icon = SOCIAL_ICONS[icon] ?? InstagramIcon;
|
||||||
|
return (
|
||||||
|
<a
|
||||||
|
key={label}
|
||||||
|
href={href}
|
||||||
|
aria-label={label}
|
||||||
|
className="group flex h-11 w-11 items-center justify-center rounded-full border border-white/15 bg-white/[0.04] text-ivory/70 backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.15),0_4px_12px_rgba(0,0,0,0.3)] transition-all duration-300 ease-lux hover:-translate-y-1 hover:border-copper/70 hover:bg-copper/10 hover:text-copper hover:shadow-[0_0_20px_rgba(197,143,92,0.4)] active:scale-95"
|
||||||
|
>
|
||||||
|
<Icon className="h-4 w-4 transition-transform duration-300 group-hover:scale-110" />
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="lg:col-span-2">
|
||||||
|
<h3 className="text-sm font-bold text-copper">
|
||||||
|
<InlineEdit value={footer.pages_heading} onSave={(v) => saveFooter('pages_heading', v)} />
|
||||||
|
</h3>
|
||||||
|
<ul className="mt-5 space-y-3">
|
||||||
|
{nav.map((item) => (
|
||||||
|
<EditableItem key={item.id} resource="nav" item={item}>
|
||||||
|
<li>
|
||||||
|
<Link
|
||||||
|
to={href(item.href)}
|
||||||
|
className="inline-block text-sm font-light text-ivory/60 transition-colors duration-200 ease-lux hover:text-copper"
|
||||||
|
>
|
||||||
|
<InlineEdit
|
||||||
|
value={item.label}
|
||||||
|
onSave={(v) => saveNav(item.id, { label: v })}
|
||||||
|
/>
|
||||||
|
</Link>
|
||||||
|
</li>
|
||||||
|
</EditableItem>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
<div className="mt-4">
|
||||||
|
<AddItemButton resource="nav" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="lg:col-span-3">
|
||||||
|
<h3 className="text-sm font-bold text-copper">
|
||||||
|
<InlineEdit value={footer.services_heading} onSave={(v) => saveFooter('services_heading', v)} />
|
||||||
|
</h3>
|
||||||
|
<ul className="mt-5 space-y-3">
|
||||||
|
{services.map((service) => (
|
||||||
|
<li key={service.id} className="text-sm font-light text-ivory/60">
|
||||||
|
{service.title}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="lg:col-span-3">
|
||||||
|
<h3 className="text-sm font-bold text-copper">
|
||||||
|
<InlineEdit value={footer.contact_heading} onSave={(v) => saveFooter('contact_heading', v)} />
|
||||||
|
</h3>
|
||||||
|
<ul className="mt-5 space-y-4 text-sm font-light text-ivory/60">
|
||||||
|
<li className="flex items-start gap-3">
|
||||||
|
<MapPinIcon className="mt-1 h-4 w-4 shrink-0 text-copper" />
|
||||||
|
<span className="leading-7">{brand.address}</span>
|
||||||
</li>
|
</li>
|
||||||
))}
|
<li className="flex items-center gap-3">
|
||||||
</ul>
|
<PhoneIcon className="h-4 w-4 shrink-0 text-copper" />
|
||||||
|
<a href={brand.phone_href} dir="ltr" className="transition-colors hover:text-copper">
|
||||||
|
{brand.phone}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li className="flex items-center gap-3">
|
||||||
|
<MailIcon className="h-4 w-4 shrink-0 text-copper" />
|
||||||
|
<a href={`mailto:${brand.email}`} dir="ltr" className="transition-colors hover:text-copper">
|
||||||
|
{brand.email}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="lg:col-span-3">
|
{/* خط پایانی به همراه دکمه کپسولی بازگشت به بالا */}
|
||||||
<h3 className="text-sm font-bold text-copper">
|
<div className="mt-14 flex flex-col items-center justify-between gap-6 border-t border-white/10 pt-6 text-xs font-light text-ivory/40 md:flex-row">
|
||||||
<InlineEdit value={footer.contact_heading} onSave={(v) => saveFooter('contact_heading', v)} />
|
<p>
|
||||||
</h3>
|
<InlineEdit value={footer.copyright} onSave={(v) => saveFooter('copyright', v)} />
|
||||||
<ul className="mt-5 space-y-4 text-sm font-light text-ivory/60">
|
</p>
|
||||||
<li className="flex items-start gap-3">
|
|
||||||
<MapPinIcon className="mt-1 h-4 w-4 shrink-0 text-copper" />
|
<button
|
||||||
<span className="leading-7">{brand.address}</span>
|
type="button"
|
||||||
</li>
|
onClick={scrollToTop}
|
||||||
<li className="flex items-center gap-3">
|
className="group flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.04] px-4 py-2 text-xs font-medium text-ivory/70 backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.15)] transition-all duration-300 ease-lux hover:border-copper hover:bg-copper/10 hover:text-copper hover:shadow-[0_0_20px_rgba(197,143,92,0.3)] active:scale-95 cursor-pointer"
|
||||||
<PhoneIcon className="h-4 w-4 shrink-0 text-copper" />
|
>
|
||||||
<a href={brand.phone_href} dir="ltr" className="transition-colors hover:text-copper">
|
<span>{t('backToTop')}</span>
|
||||||
{brand.phone}
|
<span className="flex h-5 w-5 items-center justify-center rounded-full bg-white/10 text-copper transition-transform duration-300 group-hover:-translate-y-0.5 group-hover:bg-copper group-hover:text-char">
|
||||||
</a>
|
<ArrowUpIcon className="h-3 w-3" />
|
||||||
</li>
|
</span>
|
||||||
<li className="flex items-center gap-3">
|
</button>
|
||||||
<MailIcon className="h-4 w-4 shrink-0 text-copper" />
|
|
||||||
<a href={`mailto:${brand.email}`} dir="ltr" className="transition-colors hover:text-copper">
|
<p>
|
||||||
{brand.email}
|
<InlineEdit value={footer.tagline} onSave={(v) => saveFooter('tagline', v)} />
|
||||||
</a>
|
</p>
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* خط پایانی به همراه دکمه کپسولی بازگشت به بالا */}
|
|
||||||
<div className="mt-14 flex flex-col items-center justify-between gap-6 border-t border-white/10 pt-6 text-xs font-light text-ivory/40 md:flex-row">
|
|
||||||
<p>
|
|
||||||
<InlineEdit value={footer.copyright} onSave={(v) => saveFooter('copyright', v)} />
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={scrollToTop}
|
|
||||||
className="group flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.04] px-4 py-2 text-xs font-medium text-ivory/70 backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.15)] transition-all duration-300 ease-lux hover:border-copper hover:bg-copper/10 hover:text-copper hover:shadow-[0_0_20px_rgba(197,143,92,0.3)] active:scale-95 cursor-pointer"
|
|
||||||
>
|
|
||||||
<span>{t('backToTop')}</span>
|
|
||||||
<span className="flex h-5 w-5 items-center justify-center rounded-full bg-white/10 text-copper transition-transform duration-300 group-hover:-translate-y-0.5 group-hover:bg-copper group-hover:text-char">
|
|
||||||
<ArrowUpIcon className="h-3 w-3" />
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
<InlineEdit value={footer.tagline} onSave={(v) => saveFooter('tagline', v)} />
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { useLocation, useNavigate } from 'react-router-dom'
|
import { useLocation, useNavigate } from 'react-router-dom'
|
||||||
import { counterpartPath, useLang, writeLangCookie } from '../i18n/lang'
|
import { counterpartPath, useLang, writeLangCookie } from '../i18n/lang'
|
||||||
|
import { track } from '../analytics'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* تنظیمات ثابت هر زبان.
|
* تنظیمات ثابت هر زبان.
|
||||||
@@ -21,7 +22,9 @@ export function LangToggle({ className = '' }: { className?: string }) {
|
|||||||
const target = counterpartPath(pathname, lang)
|
const target = counterpartPath(pathname, lang)
|
||||||
|
|
||||||
const switchLang = () => {
|
const switchLang = () => {
|
||||||
writeLangCookie(lang === 'fa' ? 'en' : 'fa')
|
const next = lang === 'fa' ? 'en' : 'fa'
|
||||||
|
track('lang-switch', { to: next })
|
||||||
|
writeLangCookie(next)
|
||||||
// پرش خودکار بازدید بعدی با انتخاب دستی همینجا گره نمیخورد
|
// پرش خودکار بازدید بعدی با انتخاب دستی همینجا گره نمیخورد
|
||||||
sessionStorage.setItem('langAutoRedirect', '1')
|
sessionStorage.setItem('langAutoRedirect', '1')
|
||||||
navigate(target)
|
navigate(target)
|
||||||
|
|||||||
@@ -45,31 +45,31 @@ export function ProjectCard({ project, imageUrl, detailHref }: ProjectCardProps)
|
|||||||
<img
|
<img
|
||||||
src={finalImageUrl}
|
src={finalImageUrl}
|
||||||
alt={title}
|
alt={title}
|
||||||
className="w-full h-[250px] object-cover transition-transform duration-700 ease-lux group-hover:scale-110"
|
className="w-full h-44 md:h-[250px] object-cover transition-transform duration-700 ease-lux group-hover:scale-110"
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* نشانگر وضعیت کپسولی شیشهای */}
|
{/* نشانگر وضعیت کپسولی شیشهای */}
|
||||||
<div
|
<div
|
||||||
className={`absolute top-4 ltr:left-4 rtl:right-4 flex items-center gap-2 rounded-full border border-white/15 bg-ink/75 px-4 py-1.5 text-xs font-light backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.2),0_4px_15px_rgba(0,0,0,0.4)] ${textColor}`}
|
className={`absolute top-3 ltr:left-3 rtl:right-3 md:top-4 md:ltr:left-4 md:rtl:right-4 flex items-center gap-1.5 md:gap-2 rounded-full border border-white/15 bg-ink/75 px-3 py-1 md:px-4 md:py-1.5 text-xs font-light backdrop-blur-xl shadow-[inset_0_1px_1px_rgba(255,255,255,0.2),0_4px_15px_rgba(0,0,0,0.4)] ${textColor}`}
|
||||||
>
|
>
|
||||||
<span className={`h-2 w-2 rounded-full ${dotColor} ${isOngoing ? 'shadow-[0_0_10px_rgba(197,143,92,0.9)]' : ''}`} />
|
<span className={`h-2 w-2 rounded-full ${dotColor} ${isOngoing ? 'shadow-[0_0_10px_rgba(197,143,92,0.9)]' : ''}`} />
|
||||||
{statusText}
|
{statusText}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-1 flex-col p-7 pb-4">
|
<div className="flex flex-1 flex-col p-4 pb-3 md:p-7 md:pb-4">
|
||||||
<div className="text-center">
|
<div className="text-center">
|
||||||
<h3 className="font-pinar text-xl font-bold text-ivory transition-colors duration-300 group-hover:text-copper">
|
<h3 className="font-pinar text-lg font-bold text-ivory transition-colors duration-300 group-hover:text-copper md:text-xl">
|
||||||
{title}
|
{title}
|
||||||
</h3>
|
</h3>
|
||||||
{location && <p className="mt-4 text-sm font-light text-ivory/60">{location}</p>}
|
{location && <p className="mt-2 text-xs font-light text-ivory/60 md:mt-4 md:text-sm">{location}</p>}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{area && (
|
{area && (
|
||||||
<>
|
<>
|
||||||
<div className="my-6 h-px w-full bg-white/10" />
|
<div className="my-3 h-px w-full bg-white/10 md:my-6" />
|
||||||
<div className="mt-auto flex items-center justify-end text-sm font-light text-ivory/60">
|
<div className="mt-auto flex items-center justify-end text-xs font-light text-ivory/60 md:text-sm">
|
||||||
{/* واحد زیربنا در محتوای انگلیسی داخل خود مقدار است — فقط در فارسی اضافه میشود */}
|
{/* واحد زیربنا در محتوای انگلیسی داخل خود مقدار است — فقط در فارسی اضافه میشود */}
|
||||||
<span>{lang === 'fa' ? `${area} ${t('sqm')}` : area}</span>
|
<span>{lang === 'fa' ? `${area} ${t('sqm')}` : area}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -79,7 +79,7 @@ export function ProjectCard({ project, imageUrl, detailHref }: ProjectCardProps)
|
|||||||
</Link>
|
</Link>
|
||||||
|
|
||||||
{/* پایهٔ کارت: یک دکمه واحد */}
|
{/* پایهٔ کارت: یک دکمه واحد */}
|
||||||
<div className="flex flex-col gap-2 px-7 pb-7">
|
<div className="flex flex-col gap-2 px-4 pb-4 md:px-7 md:pb-7">
|
||||||
<PremiumButton to={finalDetailHref} className="w-full">
|
<PremiumButton to={finalDetailHref} className="w-full">
|
||||||
{t('viewProject')}
|
{t('viewProject')}
|
||||||
</PremiumButton>
|
</PremiumButton>
|
||||||
|
|||||||
@@ -102,6 +102,42 @@ const CLOSED_LINK_MODAL: LinkModalState = {
|
|||||||
/** چند نویسه پیش از نشانگر برای تشخیص نشانیِ تایپشده بررسی میشود */
|
/** چند نویسه پیش از نشانگر برای تشخیص نشانیِ تایپشده بررسی میشود */
|
||||||
const URL_LOOKBACK = 300
|
const URL_LOOKBACK = 300
|
||||||
|
|
||||||
|
/** آیا src تصویر خارج از میزبانی سایت است (مثل چسباندهشده از Outline)؟ */
|
||||||
|
function isExternalImageSrc(src: string): boolean {
|
||||||
|
if (!src || src.startsWith('data:')) return false
|
||||||
|
if (src.startsWith('/')) return false // مسیر نسبی/داخلی
|
||||||
|
try {
|
||||||
|
return new URL(src).origin !== window.location.origin
|
||||||
|
} catch {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* میزبانی مجدد تصاویر خارجی یک HTML — هر <img> با نشانی خارجی از سرور دانلود و در
|
||||||
|
* Object Storage خود سایت ذخیره و src آن بازنویسی میشود (چسباندن از Outline و مانند آن).
|
||||||
|
* تصویری که درونریزیاش شکست بخورد با src اصلی میماند.
|
||||||
|
*/
|
||||||
|
async function rehostExternalImages(html: string): Promise<string> {
|
||||||
|
const parsed = new DOMParser().parseFromString(html, 'text/html')
|
||||||
|
const imgs = Array.from(parsed.querySelectorAll('img[src]'))
|
||||||
|
const externals = imgs.filter((img) => isExternalImageSrc(img.getAttribute('src') ?? ''))
|
||||||
|
if (!externals.length) return html
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
externals.map(async (img) => {
|
||||||
|
const original = img.getAttribute('src') ?? ''
|
||||||
|
try {
|
||||||
|
const { url } = await adminApi.importImage(original)
|
||||||
|
img.setAttribute('src', url)
|
||||||
|
} catch {
|
||||||
|
// نشانی خارجی میماند — شاید خودش کار کند
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
return parsed.body.innerHTML
|
||||||
|
}
|
||||||
|
|
||||||
function ToolButton({
|
function ToolButton({
|
||||||
onClick,
|
onClick,
|
||||||
active = false,
|
active = false,
|
||||||
@@ -144,6 +180,8 @@ export function ArticleEditor({ doc, onChange, contentKey }: ArticleEditorProps)
|
|||||||
const [uploading, setUploading] = useState(false)
|
const [uploading, setUploading] = useState(false)
|
||||||
const [direction, setDirection] = useState<TextDirection>('rtl')
|
const [direction, setDirection] = useState<TextDirection>('rtl')
|
||||||
const [linkModal, setLinkModal] = useState<LinkModalState>(CLOSED_LINK_MODAL)
|
const [linkModal, setLinkModal] = useState<LinkModalState>(CLOSED_LINK_MODAL)
|
||||||
|
// مرحله نامگذاری درج تصویر از نوار ابزار — پیشفرض از نام خود فایل
|
||||||
|
const [pendingImage, setPendingImage] = useState<{ file: File; name: string } | null>(null)
|
||||||
|
|
||||||
const editor = useEditor({
|
const editor = useEditor({
|
||||||
extensions: ARTICLE_EXTENSIONS,
|
extensions: ARTICLE_EXTENSIONS,
|
||||||
@@ -163,6 +201,22 @@ export function ArticleEditor({ doc, onChange, contentKey }: ArticleEditorProps)
|
|||||||
void uploadAndInsert(file)
|
void uploadAndInsert(file)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
// محتوای HTML با تصویر خارجی (Outline و مانند آن) — تصاویر به میزبانی سایت منتقل میشوند
|
||||||
|
const html = event.clipboardData?.getData('text/html') ?? ''
|
||||||
|
if (html.includes('<img')) {
|
||||||
|
const parsed = new DOMParser().parseFromString(html, 'text/html')
|
||||||
|
const hasExternal = Array.from(parsed.querySelectorAll('img[src]')).some((img) =>
|
||||||
|
isExternalImageSrc(img.getAttribute('src') ?? ''),
|
||||||
|
)
|
||||||
|
if (hasExternal) {
|
||||||
|
event.preventDefault()
|
||||||
|
void (async () => {
|
||||||
|
const transformed = await rehostExternalImages(html)
|
||||||
|
editor?.chain().focus().insertContent(transformed).run()
|
||||||
|
})()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
// چسباندن یک نشانی → مودال لینک بلافاصله باز میشود
|
// چسباندن یک نشانی → مودال لینک بلافاصله باز میشود
|
||||||
const text = event.clipboardData?.getData('text/plain')?.trim() ?? ''
|
const text = event.clipboardData?.getData('text/plain')?.trim() ?? ''
|
||||||
if (isUrlLike(text)) {
|
if (isUrlLike(text)) {
|
||||||
@@ -186,6 +240,21 @@ export function ArticleEditor({ doc, onChange, contentKey }: ArticleEditorProps)
|
|||||||
void uploadAndInsert(file)
|
void uploadAndInsert(file)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
const html = event.dataTransfer?.getData('text/html') ?? ''
|
||||||
|
if (html.includes('<img')) {
|
||||||
|
const parsed = new DOMParser().parseFromString(html, 'text/html')
|
||||||
|
const hasExternal = Array.from(parsed.querySelectorAll('img[src]')).some((img) =>
|
||||||
|
isExternalImageSrc(img.getAttribute('src') ?? ''),
|
||||||
|
)
|
||||||
|
if (hasExternal) {
|
||||||
|
event.preventDefault()
|
||||||
|
void (async () => {
|
||||||
|
const transformed = await rehostExternalImages(html)
|
||||||
|
editor?.chain().focus().insertContent(transformed).run()
|
||||||
|
})()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
// پایان تایپ یک نشانی (فاصله بعد از آن) → مودال لینک باز میشود
|
// پایان تایپ یک نشانی (فاصله بعد از آن) → مودال لینک باز میشود
|
||||||
@@ -237,11 +306,11 @@ export function ArticleEditor({ doc, onChange, contentKey }: ArticleEditorProps)
|
|||||||
editor.commands.setContent(incoming, { emitUpdate: false })
|
editor.commands.setContent(incoming, { emitUpdate: false })
|
||||||
}, [editor, contentKey, doc])
|
}, [editor, contentKey, doc])
|
||||||
|
|
||||||
async function uploadAndInsert(file: File) {
|
async function uploadAndInsert(file: File, name?: string) {
|
||||||
if (!editor) return
|
if (!editor) return
|
||||||
setUploading(true)
|
setUploading(true)
|
||||||
await runSave(async () => {
|
await runSave(async () => {
|
||||||
const { url } = await adminApi.upload(file)
|
const { url } = await adminApi.upload(file, name)
|
||||||
// نام پیشفرض از نام فایل ساخته میشود و در نبود آن، مقدار پیشفرض ثابت میماند
|
// نام پیشفرض از نام فایل ساخته میشود و در نبود آن، مقدار پیشفرض ثابت میماند
|
||||||
const defaultAlt = file.name.replace(/\.[^.]+$/, '').replace(/[-_]+/g, ' ').trim()
|
const defaultAlt = file.name.replace(/\.[^.]+$/, '').replace(/[-_]+/g, ' ').trim()
|
||||||
editor
|
editor
|
||||||
@@ -411,12 +480,72 @@ export function ArticleEditor({ doc, onChange, contentKey }: ArticleEditorProps)
|
|||||||
className="hidden"
|
className="hidden"
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const file = e.target.files?.[0]
|
const file = e.target.files?.[0]
|
||||||
if (file) void uploadAndInsert(file)
|
if (file) {
|
||||||
|
setPendingImage({ file, name: file.name.replace(/\.[^.]+$/, '') })
|
||||||
|
}
|
||||||
e.target.value = ''
|
e.target.value = ''
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* مرحله نامگذاری تصویر — نام خوانای ذخیرهسازی با یک کلیک تایید میشود */}
|
||||||
|
{pendingImage && (
|
||||||
|
<div
|
||||||
|
className="fixed inset-0 z-[90] flex items-center justify-center bg-black/60 p-6 backdrop-blur-sm"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-label="نام تصویر"
|
||||||
|
onClick={() => setPendingImage(null)}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
dir="rtl"
|
||||||
|
className="w-full max-w-sm border border-ivory/15 bg-ink p-6 shadow-2xl"
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
>
|
||||||
|
<h2 className="text-sm font-bold text-copper">درج تصویر</h2>
|
||||||
|
<label htmlFor="image-name-input" className="mt-4 block text-xs font-light text-ivory/60">
|
||||||
|
نام فایل تصویر (برای نشانی ذخیرهسازی — اختیاری)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="image-name-input"
|
||||||
|
type="text"
|
||||||
|
dir="auto"
|
||||||
|
autoFocus
|
||||||
|
value={pendingImage.name}
|
||||||
|
onChange={(e) => setPendingImage({ ...pendingImage, name: e.target.value })}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
const { file, name } = pendingImage
|
||||||
|
setPendingImage(null)
|
||||||
|
void uploadAndInsert(file, name)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
className="mt-2 w-full border border-ivory/15 bg-char px-3 py-2 text-sm font-light text-ivory focus:border-copper focus:outline-none"
|
||||||
|
/>
|
||||||
|
<div className="mt-5 flex items-center justify-end gap-3">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setPendingImage(null)}
|
||||||
|
className="border border-ivory/20 px-4 py-2 text-xs font-light text-ivory/70 transition-colors hover:border-copper hover:text-copper"
|
||||||
|
>
|
||||||
|
انصراف
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
const { file, name } = pendingImage
|
||||||
|
setPendingImage(null)
|
||||||
|
void uploadAndInsert(file, name)
|
||||||
|
}}
|
||||||
|
className="bg-copper px-6 py-2 text-xs font-bold text-char transition-transform hover:scale-105"
|
||||||
|
>
|
||||||
|
درج تصویر
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* مودال لینک — انیمیشن ورود/خروج را خودش با کلاسهای Tailwind مدیریت میکند */}
|
{/* مودال لینک — انیمیشن ورود/خروج را خودش با کلاسهای Tailwind مدیریت میکند */}
|
||||||
{linkModal.open && (
|
{linkModal.open && (
|
||||||
<LinkModal
|
<LinkModal
|
||||||
|
|||||||
@@ -48,7 +48,10 @@ export function ImageField({
|
|||||||
const inputRef = useRef<HTMLInputElement>(null)
|
const inputRef = useRef<HTMLInputElement>(null)
|
||||||
const titleId = useId()
|
const titleId = useId()
|
||||||
const altId = useId()
|
const altId = useId()
|
||||||
|
const nameId = useId()
|
||||||
const [uploading, setUploading] = useState(false)
|
const [uploading, setUploading] = useState(false)
|
||||||
|
/** نام دلخواه فایل برای ذخیرهسازی خوانا — پیشفرض از نام فایل انتخابی ساخته میشود */
|
||||||
|
const [fileBaseName, setFileBaseName] = useState('')
|
||||||
|
|
||||||
const showTitle = title !== undefined || !!onSaveTitle
|
const showTitle = title !== undefined || !!onSaveTitle
|
||||||
const showAlt = alt !== undefined || !!onSaveAlt
|
const showAlt = alt !== undefined || !!onSaveAlt
|
||||||
@@ -73,13 +76,16 @@ export function ImageField({
|
|||||||
setUploading(true)
|
setUploading(true)
|
||||||
try {
|
try {
|
||||||
await runSave(async () => {
|
await runSave(async () => {
|
||||||
const { url } = await adminApi.upload(file)
|
// نام ذخیرهسازی: نام تایپشده کاربر، وگرنه نام خود فایل بدون پسوند
|
||||||
|
const name = fileBaseName.trim() || file.name.replace(/\.[^.]+$/, '')
|
||||||
|
const { url } = await adminApi.upload(file, name)
|
||||||
const ok = await onSave(url)
|
const ok = await onSave(url)
|
||||||
if (!ok) throw new Error('ذخیره نشد')
|
if (!ok) throw new Error('ذخیره نشد')
|
||||||
// پیشفرضهای Alt/Title همان لحظه ثبت میشوند تا تصویر بدون متادیتا نماند
|
// پیشفرضهای Alt/Title همان لحظه ثبت میشوند تا تصویر بدون متادیتا نماند
|
||||||
if (showTitle) await titleField.commit()
|
if (showTitle) await titleField.commit()
|
||||||
if (showAlt) await altField.commit()
|
if (showAlt) await altField.commit()
|
||||||
})
|
})
|
||||||
|
setFileBaseName('')
|
||||||
} finally {
|
} finally {
|
||||||
setUploading(false)
|
setUploading(false)
|
||||||
}
|
}
|
||||||
@@ -106,7 +112,7 @@ export function ImageField({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="mt-3">
|
<div className="mt-3 flex flex-wrap items-end gap-3">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => inputRef.current?.click()}
|
onClick={() => inputRef.current?.click()}
|
||||||
@@ -120,6 +126,22 @@ export function ImageField({
|
|||||||
)}
|
)}
|
||||||
{uploading ? 'در حال آپلود…' : value ? 'تغییر تصویر' : 'انتخاب تصویر'}
|
{uploading ? 'در حال آپلود…' : value ? 'تغییر تصویر' : 'انتخاب تصویر'}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
{/* نام ذخیرهسازی فایل — خالی یعنی از نام خود فایل استفاده شود */}
|
||||||
|
<div className="w-56">
|
||||||
|
<label htmlFor={nameId} className="text-[11px] font-light text-ivory/50">
|
||||||
|
نام فایل تصویر (اختیاری)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id={nameId}
|
||||||
|
type="text"
|
||||||
|
dir="auto"
|
||||||
|
value={fileBaseName}
|
||||||
|
onChange={(event) => setFileBaseName(event.target.value)}
|
||||||
|
placeholder="مثلاً: elahieh-tower"
|
||||||
|
className={META_FIELD_CLASS.replace('mt-1.5 ', '')}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{(showTitle || showAlt) && (
|
{(showTitle || showAlt) && (
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { NodeViewWrapper } from '@tiptap/react'
|
import { NodeViewWrapper } from '@tiptap/react'
|
||||||
import type { NodeViewProps } from '@tiptap/react'
|
import type { NodeViewProps } from '@tiptap/react'
|
||||||
import { PlusIcon, XIcon } from 'lucide-react'
|
import { CloudUploadIcon, PlusIcon, XIcon } from 'lucide-react'
|
||||||
|
import { adminApi } from '../../admin/adminClient'
|
||||||
|
import { useAdmin } from '../../admin/AdminContext'
|
||||||
|
|
||||||
type ImageAttrs = {
|
type ImageAttrs = {
|
||||||
src: string
|
src: string
|
||||||
@@ -19,25 +21,39 @@ const INLINE_FIELD_CLASS =
|
|||||||
const ADD_CAPTION_CLASS =
|
const ADD_CAPTION_CLASS =
|
||||||
'mt-1.5 inline-flex items-center gap-1.5 border border-dashed border-ivory/25 px-3 py-1 text-[11px] font-light text-ivory/50 transition-colors duration-150 hover:border-copper hover:text-copper'
|
'mt-1.5 inline-flex items-center gap-1.5 border border-dashed border-ivory/25 px-3 py-1 text-[11px] font-light text-ivory/50 transition-colors duration-150 hover:border-copper hover:text-copper'
|
||||||
|
|
||||||
|
/** تصاویر میزبان خارجی (مثل چسباندهشده از Outline) قابل میزبانی مجدد هستند */
|
||||||
|
function isExternalImage(src: string): boolean {
|
||||||
|
if (!src) return false
|
||||||
|
if (src.startsWith('/media/') || src.startsWith('/')) return false
|
||||||
|
try {
|
||||||
|
return new URL(src, window.location.origin).origin !== window.location.origin
|
||||||
|
} catch {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* نمایش تصویر مقاله در ویرایشگر — دقیقاً با همان ساختار خروجی سایت:
|
* نمایش تصویر مقاله در ویرایشگر — دقیقاً با همان ساختار خروجی سایت:
|
||||||
* <figure><img><figcaption>کپشن</figcaption></figure>
|
* <figure><img><figcaption>کپشن</figcaption></figure>
|
||||||
*
|
*
|
||||||
* ویرایش کاملاً درونخطی است (بدون پاپاور/مودال):
|
* ویرایش کاملاً درونخطی است (بدون پاپاور/مودال):
|
||||||
* - هر تصویر یک «جعبه توضیح/کپشن» همراه خود دارد و متن آن در figcaption ذخیره میشود.
|
* - هر تصویر یک «جعبه توضیح/کپشن» همراه خود دارد و متن آن در figcaption ذخیره میشود.
|
||||||
* - دکمه X کپشن را کامل حذف میکند: صفت کپشن به مقدار پیشفرض برمیگردد و عنصر
|
* - دکمه X کپشن را کامل حذف میکند؛ «افزودن کپشن» دوباره جعبه را باز میکند.
|
||||||
* figcaption با ورودیاش از DOM ویرایشگر (و از خروجی ذخیرهشده) حذف میشود؛
|
* - Alt و Title تصویر (نام تصویر برای سئو/دسترسپذیری) پس از درج هم قابل ویرایشاند.
|
||||||
* فقط تصویر میماند و «افزودن کپشن» دوباره جعبه را باز میکند.
|
* - تصویر میزبان خارجی دکمه «میزبانی مجدد» دارد: تصویر دانلود و در Object Storage
|
||||||
* - ویرایش Alt در ویرایشگر وجود ندارد؛ Alt هنگام درج از نام فایل ثبت میشود.
|
* خود سایت ذخیره میشود (ترمیم تصاویر چسباندهشده از Outline).
|
||||||
* - دکمه X روی تصویر کل تصویر را حذف میکند.
|
* - دکمه X روی تصویر کل تصویر را حذف میکند.
|
||||||
*/
|
*/
|
||||||
export function ImageNodeView({ node, updateAttributes, deleteNode, selected }: NodeViewProps) {
|
export function ImageNodeView({ node, updateAttributes, deleteNode, selected }: NodeViewProps) {
|
||||||
const { src, alt, title, caption } = node.attrs as ImageAttrs
|
const { src, alt, title, caption } = node.attrs as ImageAttrs
|
||||||
|
const { runSave } = useAdmin()
|
||||||
const hasCaption = Boolean((caption ?? '').trim())
|
const hasCaption = Boolean((caption ?? '').trim())
|
||||||
const [captionOpen, setCaptionOpen] = useState(true)
|
const [captionOpen, setCaptionOpen] = useState(true)
|
||||||
|
const [rehosting, setRehosting] = useState(false)
|
||||||
const captionInputRef = useRef<HTMLInputElement>(null)
|
const captionInputRef = useRef<HTMLInputElement>(null)
|
||||||
const focusOnOpen = useRef(false)
|
const focusOnOpen = useRef(false)
|
||||||
const showCaption = captionOpen || hasCaption
|
const showCaption = captionOpen || hasCaption
|
||||||
|
const external = isExternalImage(src)
|
||||||
|
|
||||||
// فقط وقتی کاربر «افزودن کپشن» را میزند ورودی فوکوس میگیرد (نه هنگام بارگذاری مقاله)
|
// فقط وقتی کاربر «افزودن کپشن» را میزند ورودی فوکوس میگیرد (نه هنگام بارگذاری مقاله)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -57,6 +73,16 @@ export function ImageNodeView({ node, updateAttributes, deleteNode, selected }:
|
|||||||
setCaptionOpen(true)
|
setCaptionOpen(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** میزبانی مجدد تصویر خارجی — دانلود از منبع و ذخیره در Object Storage سایت */
|
||||||
|
const rehost = () => {
|
||||||
|
if (rehosting) return
|
||||||
|
setRehosting(true)
|
||||||
|
void runSave(async () => {
|
||||||
|
const { url } = await adminApi.importImage(src)
|
||||||
|
updateAttributes({ src: url })
|
||||||
|
}).finally(() => setRehosting(false))
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NodeViewWrapper
|
<NodeViewWrapper
|
||||||
className={`article-figure-node ${selected ? 'is-selected' : ''}`}
|
className={`article-figure-node ${selected ? 'is-selected' : ''}`}
|
||||||
@@ -77,6 +103,42 @@ export function ImageNodeView({ node, updateAttributes, deleteNode, selected }:
|
|||||||
<XIcon className="h-3.5 w-3.5" />
|
<XIcon className="h-3.5 w-3.5" />
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
{/* تصویر خارجی (مثل Outline) — منتقل به میزبانی سایت */}
|
||||||
|
{external && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
contentEditable={false}
|
||||||
|
onClick={rehost}
|
||||||
|
disabled={rehosting}
|
||||||
|
aria-label="میزبانی مجدد تصویر"
|
||||||
|
title="تصویر از منبع خارجی دانلود و در سایت ذخیره میشود"
|
||||||
|
className="absolute right-2 top-2 z-10 flex items-center gap-1.5 border border-copper/60 bg-ink/85 px-2.5 py-1 text-[11px] font-light text-copper backdrop-blur-sm transition-colors duration-150 hover:bg-copper hover:text-char disabled:opacity-60"
|
||||||
|
>
|
||||||
|
<CloudUploadIcon className={`h-3.5 w-3.5 ${rehosting ? 'animate-pulse' : ''}`} />
|
||||||
|
{rehosting ? 'در حال انتقال…' : 'میزبانی مجدد'}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* نام تصویر — Alt و Title پس از درج هم قابل ویرایشاند */}
|
||||||
|
<div contentEditable={false} className="mt-1.5 grid gap-1.5 sm:grid-cols-2">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={alt ?? ''}
|
||||||
|
onChange={(event) => updateAttributes({ alt: event.target.value })}
|
||||||
|
placeholder="نام تصویر / متن جایگزین (Alt)…"
|
||||||
|
aria-label="متن جایگزین تصویر"
|
||||||
|
className={INLINE_FIELD_CLASS}
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={title ?? ''}
|
||||||
|
onChange={(event) => updateAttributes({ title: event.target.value })}
|
||||||
|
placeholder="عنوان تصویر (Title)…"
|
||||||
|
aria-label="عنوان تصویر"
|
||||||
|
className={INLINE_FIELD_CLASS}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
{showCaption ? (
|
{showCaption ? (
|
||||||
<figcaption className="article-figure__caption" contentEditable={false}>
|
<figcaption className="article-figure__caption" contentEditable={false}>
|
||||||
<input
|
<input
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react';
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
import { Reveal } from '../Reveal';
|
import { Reveal } from '../Reveal';
|
||||||
import { PremiumButton } from '../PremiumButton';
|
import { PremiumButton } from '../PremiumButton';
|
||||||
import { ArticleCard } from '../ArticleCard';
|
import { ArticleCard } from '../ArticleCard';
|
||||||
@@ -8,6 +7,7 @@ import { useLang } from '../../i18n/lang';
|
|||||||
import { useFieldSaver } from '../../admin/AdminContext';
|
import { useFieldSaver } from '../../admin/AdminContext';
|
||||||
import { InlineEdit } from '../admin/InlineEdit';
|
import { InlineEdit } from '../admin/InlineEdit';
|
||||||
import { AddItemButton, EditableItem } from '../../admin/Editable';
|
import { AddItemButton, EditableItem } from '../../admin/Editable';
|
||||||
|
import { formatJalali } from '../../utils/date';
|
||||||
|
|
||||||
export function Articles() {
|
export function Articles() {
|
||||||
const { articles } = useSite();
|
const { articles } = useSite();
|
||||||
@@ -17,6 +17,9 @@ export function Articles() {
|
|||||||
|
|
||||||
// پیشنویسها فقط در صفحه وبلاگ (برای ادمین) دیده میشوند، نه در خانه
|
// پیشنویسها فقط در صفحه وبلاگ (برای ادمین) دیده میشوند، نه در خانه
|
||||||
const published = articles.filter((a) => a.status !== 'draft');
|
const published = articles.filter((a) => a.status !== 'draft');
|
||||||
|
const topArticles = published.slice(0, 3);
|
||||||
|
const spotlight = topArticles[0];
|
||||||
|
const compactList = topArticles.slice(1, 3);
|
||||||
|
|
||||||
const saveHeading = (field: 'label' | 'title' | 'lead' | 'cta_label', value: string) =>
|
const saveHeading = (field: 'label' | 'title' | 'lead' | 'cta_label', value: string) =>
|
||||||
save('section-headings', heading.id, { [field]: value }, (d) => {
|
save('section-headings', heading.id, { [field]: value }, (d) => {
|
||||||
@@ -25,24 +28,97 @@ export function Articles() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="bg-char py-24 lg:py-32" aria-labelledby="articles-title">
|
<section className="w-full max-w-full overflow-x-clip bg-char py-16 md:py-24 lg:py-32" aria-labelledby="articles-title">
|
||||||
<div className="mx-auto max-w-site px-6 lg:px-10">
|
<div className="mx-auto max-w-site px-4 md:px-6 lg:px-10">
|
||||||
<Reveal className="max-w-2xl ltr:text-left rtl:text-right">
|
<Reveal className="max-w-2xl ltr:text-left rtl:text-right">
|
||||||
<p className="text-sm font-light text-copper">
|
<p className="text-sm font-light text-copper">
|
||||||
<InlineEdit value={heading.label} onSave={(v) => saveHeading('label', v)} />
|
<InlineEdit value={heading.label} onSave={(v) => saveHeading('label', v)} />
|
||||||
</p>
|
</p>
|
||||||
<h2 id="articles-title" className="mt-5 text-section font-bold text-ivory">
|
<h2 id="articles-title" className="mt-3 md:mt-5 text-section font-bold text-ivory">
|
||||||
<InlineEdit value={heading.title} onSave={(v) => saveHeading('title', v)} />
|
<InlineEdit value={heading.title} onSave={(v) => saveHeading('title', v)} />
|
||||||
</h2>
|
</h2>
|
||||||
<p className="mt-5 text-base font-light leading-9 text-ivory/60">
|
<p className="mt-3 md:mt-5 text-sm md:text-base font-light leading-7 md:leading-9 text-ivory/60">
|
||||||
<InlineEdit multiline value={heading.lead} onSave={(v) => saveHeading('lead', v)} />
|
<InlineEdit multiline value={heading.lead} onSave={(v) => saveHeading('lead', v)} />
|
||||||
</p>
|
</p>
|
||||||
<div className="mt-6 h-px w-24 bg-copper/70" />
|
<div className="mt-4 md:mt-6 h-px w-24 bg-copper/70" />
|
||||||
</Reveal>
|
</Reveal>
|
||||||
|
|
||||||
<div className="mt-14 grid gap-6 lg:grid-cols-3">
|
{/* Mobile View: Magazine Editorial layout (1 Featured Spotlight + Compact Rows) */}
|
||||||
{published.slice(0, 3).map((article, i) =>
|
<div className="block md:hidden mt-8">
|
||||||
<Reveal key={article.id} delay={0.06 * i} className="h-full">
|
{/* First Article (Spotlight) */}
|
||||||
|
{spotlight && (
|
||||||
|
<EditableItem resource="articles" item={spotlight} editHref={`/admin/write/${spotlight.id}`}>
|
||||||
|
<Link
|
||||||
|
className="block rounded-xl border border-ivory/10 bg-char/40 overflow-hidden mb-3 group"
|
||||||
|
to={href(`/blog/${spotlight.slug}`)}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={spotlight.image}
|
||||||
|
alt={spotlight.title}
|
||||||
|
className="w-full aspect-[16/9] object-cover transition-transform duration-500 group-hover:scale-105"
|
||||||
|
/>
|
||||||
|
<div className="p-3.5">
|
||||||
|
<div className="flex items-center gap-2 text-[11px] text-copper font-medium mb-1">
|
||||||
|
<span>{spotlight.category}</span>
|
||||||
|
<span>•</span>
|
||||||
|
<span>{formatJalali(spotlight.date)}</span>
|
||||||
|
</div>
|
||||||
|
<h4 className="text-sm font-bold text-ivory leading-snug line-clamp-2 mb-1 group-hover:text-copper transition-colors">
|
||||||
|
{spotlight.title}
|
||||||
|
</h4>
|
||||||
|
<p className="text-xs text-ivory/60 line-clamp-2 leading-relaxed">
|
||||||
|
{spotlight.excerpt}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
</EditableItem>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Remaining Articles (Compact List Rows) */}
|
||||||
|
{compactList.length > 0 && (
|
||||||
|
<div className="divide-y divide-ivory/15 border-t border-b border-ivory/15 mb-4">
|
||||||
|
{compactList.map((article) => (
|
||||||
|
<EditableItem key={article.id} resource="articles" item={article} editHref={`/admin/write/${article.id}`}>
|
||||||
|
<Link
|
||||||
|
className="py-2.5 flex items-center gap-3 group"
|
||||||
|
to={href(`/blog/${article.slug}`)}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={article.image}
|
||||||
|
alt={article.title}
|
||||||
|
className="w-14 h-14 rounded-lg object-cover shrink-0 border border-ivory/10"
|
||||||
|
/>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<span className="text-[10px] text-copper block mb-0.5">{formatJalali(article.date)}</span>
|
||||||
|
<h5 className="text-xs font-semibold text-ivory line-clamp-2 leading-snug group-hover:text-copper transition-colors">
|
||||||
|
{article.title}
|
||||||
|
</h5>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
</EditableItem>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Bottom Button ("همه مقالات") */}
|
||||||
|
{heading.cta_label && heading.cta_href && (
|
||||||
|
<Link
|
||||||
|
to={href(heading.cta_href)}
|
||||||
|
className="w-full py-2.5 rounded-xl border border-ivory/20 bg-char/60 hover:bg-copper/10 hover:border-copper/40 text-xs font-semibold text-ivory text-center block transition-all active:scale-[0.98]"
|
||||||
|
>
|
||||||
|
<InlineEdit value={heading.cta_label} onSave={(v) => saveHeading('cta_label', v)} />
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Desktop View: 3-Column Grid */}
|
||||||
|
<div className="hidden md:grid mt-14 grid-cols-3 gap-6">
|
||||||
|
{topArticles.map((article, i) =>
|
||||||
|
<Reveal
|
||||||
|
key={article.id}
|
||||||
|
delay={0.06 * i}
|
||||||
|
className="h-full"
|
||||||
|
>
|
||||||
<EditableItem resource="articles" item={article} className="h-full" editHref={`/admin/write/${article.id}`}>
|
<EditableItem resource="articles" item={article} className="h-full" editHref={`/admin/write/${article.id}`}>
|
||||||
<ArticleCard article={article} />
|
<ArticleCard article={article} />
|
||||||
</EditableItem>
|
</EditableItem>
|
||||||
@@ -52,7 +128,7 @@ export function Articles() {
|
|||||||
|
|
||||||
{heading.cta_label && heading.cta_href && (
|
{heading.cta_label && heading.cta_href && (
|
||||||
<Reveal delay={0.1}>
|
<Reveal delay={0.1}>
|
||||||
<div className="mt-12 flex justify-center">
|
<div className="mt-12 hidden md:flex justify-center">
|
||||||
<PremiumButton to={href(heading.cta_href)} className="font-light">
|
<PremiumButton to={href(heading.cta_href)} className="font-light">
|
||||||
<InlineEdit value={heading.cta_label} onSave={(v) => saveHeading('cta_label', v)} />
|
<InlineEdit value={heading.cta_label} onSave={(v) => saveHeading('cta_label', v)} />
|
||||||
</PremiumButton>
|
</PremiumButton>
|
||||||
@@ -63,6 +139,6 @@ export function Articles() {
|
|||||||
<AddItemButton resource="articles" />
|
<AddItemButton resource="articles" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>);
|
</section>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
|
|
||||||
import { AnimatePresence, motion } from 'framer-motion'
|
import { AnimatePresence, motion } from 'framer-motion'
|
||||||
import { Reveal } from '../Reveal'
|
import { Reveal } from '../Reveal'
|
||||||
import { PremiumButton } from '../PremiumButton'
|
import { PremiumButton } from '../PremiumButton'
|
||||||
@@ -39,18 +38,18 @@ export function FeaturedProjects() {
|
|||||||
const BASE_IMG_URL = "/seed-media/2026/09/";
|
const BASE_IMG_URL = "/seed-media/2026/09/";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="bg-ink py-24 lg:py-32" aria-labelledby="projects-title">
|
<section className="w-full max-w-full overflow-x-clip bg-ink py-16 md:py-24 lg:py-32" aria-labelledby="projects-title">
|
||||||
<div className="mx-auto max-w-site px-6 lg:px-10">
|
<div className="mx-auto max-w-site px-4 md:px-6 lg:px-10">
|
||||||
<SectionHeading title={heading.title} lead={heading.lead} headingKey="featured_projects" />
|
<SectionHeading title={heading.title} lead={heading.lead} headingKey="featured_projects" />
|
||||||
|
|
||||||
{/* دکمههای فیلتر مینیمال و شیشهای (بدون کادر) */}
|
{/* دکمههای فیلتر مینیمال و شیشهای (بدون کادر) */}
|
||||||
<div className="mt-10 flex items-center justify-center gap-4 md:gap-6" role="tablist" aria-label={t('projectFilterAria')}>
|
<div className="mt-8 flex flex-wrap items-center justify-center gap-3 md:mt-10 md:gap-6" role="tablist" aria-label={t('projectFilterAria')}>
|
||||||
{FILTERS.map((filter, index) => {
|
{FILTERS.map((filter, index) => {
|
||||||
const isSelected = status === filter.key
|
const isSelected = status === filter.key
|
||||||
return (
|
return (
|
||||||
<React.Fragment key={filter.key}>
|
<React.Fragment key={filter.key}>
|
||||||
{index > 0 && (
|
{index > 0 && (
|
||||||
<span className="h-6 w-[2px] rounded-full bg-ivory/10" aria-hidden="true" />
|
<span className="hidden h-6 w-[2px] rounded-full bg-ivory/10 sm:inline-block" aria-hidden="true" />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<button
|
<button
|
||||||
@@ -58,7 +57,7 @@ export function FeaturedProjects() {
|
|||||||
role="tab"
|
role="tab"
|
||||||
aria-selected={isSelected}
|
aria-selected={isSelected}
|
||||||
onClick={() => setStatus(filter.key)}
|
onClick={() => setStatus(filter.key)}
|
||||||
className={`group relative rounded-lg px-6 py-2.5 text-sm font-medium transition-all duration-300 ease-lux active:scale-95 cursor-pointer select-none backdrop-blur-md ${
|
className={`group relative rounded-lg px-4 py-2 text-xs font-medium transition-all duration-300 ease-lux active:scale-95 cursor-pointer select-none backdrop-blur-md md:px-6 md:py-2.5 md:text-sm ${
|
||||||
isSelected
|
isSelected
|
||||||
? 'bg-copper/15 text-copper shadow-[0_4px_15px_rgba(197,143,92,0.1)]'
|
? 'bg-copper/15 text-copper shadow-[0_4px_15px_rgba(197,143,92,0.1)]'
|
||||||
: 'bg-transparent text-ivory/60 hover:bg-white/5 hover:text-ivory'
|
: 'bg-transparent text-ivory/60 hover:bg-white/5 hover:text-ivory'
|
||||||
@@ -76,7 +75,7 @@ export function FeaturedProjects() {
|
|||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="mt-10 min-h-[420px]">
|
<div className="mt-8 min-h-[380px] md:mt-10 md:min-h-[420px]">
|
||||||
<AnimatePresence mode="wait">
|
<AnimatePresence mode="wait">
|
||||||
<motion.div
|
<motion.div
|
||||||
key={status}
|
key={status}
|
||||||
@@ -84,7 +83,7 @@ export function FeaturedProjects() {
|
|||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
exit={{ opacity: 0, y: -15 }}
|
exit={{ opacity: 0, y: -15 }}
|
||||||
transition={{ duration: 0.3, ease: [0.23, 1, 0.32, 1] }}
|
transition={{ duration: 0.3, ease: [0.23, 1, 0.32, 1] }}
|
||||||
className="grid gap-6 lg:grid-cols-3"
|
className="flex gap-4 overflow-x-auto pb-4 snap-x snap-mandatory [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none] md:grid md:grid-cols-3 md:gap-6 md:overflow-visible md:pb-0"
|
||||||
>
|
>
|
||||||
{list.map((project, i) => {
|
{list.map((project, i) => {
|
||||||
const dummyImageName = `${(i % 6) + 1}.png`;
|
const dummyImageName = `${(i % 6) + 1}.png`;
|
||||||
@@ -96,7 +95,7 @@ export function FeaturedProjects() {
|
|||||||
initial={{ opacity: 0, y: 20 }}
|
initial={{ opacity: 0, y: 20 }}
|
||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
transition={{ duration: 0.5, delay: i * 0.1, ease: [0.23, 1, 0.32, 1] }}
|
transition={{ duration: 0.5, delay: i * 0.1, ease: [0.23, 1, 0.32, 1] }}
|
||||||
className="h-full"
|
className="h-full w-[82vw] max-w-[320px] shrink-0 snap-center md:w-auto md:max-w-none md:shrink"
|
||||||
>
|
>
|
||||||
<ProjectCard
|
<ProjectCard
|
||||||
project={project}
|
project={project}
|
||||||
@@ -113,7 +112,7 @@ export function FeaturedProjects() {
|
|||||||
{/* دکمه مشاهده همه پروژهها */}
|
{/* دکمه مشاهده همه پروژهها */}
|
||||||
{heading.cta_label && heading.cta_href && (
|
{heading.cta_label && heading.cta_href && (
|
||||||
<Reveal delay={0.1}>
|
<Reveal delay={0.1}>
|
||||||
<div className="mt-12 flex justify-center">
|
<div className="mt-10 flex justify-center md:mt-12">
|
||||||
<PremiumButton to={href(heading.cta_href)}>
|
<PremiumButton to={href(heading.cta_href)}>
|
||||||
<InlineEdit
|
<InlineEdit
|
||||||
value={heading.cta_label}
|
value={heading.cta_label}
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ import { useFieldSaver } from '../../admin/AdminContext'
|
|||||||
import { InlineEdit } from '../admin/InlineEdit'
|
import { InlineEdit } from '../admin/InlineEdit'
|
||||||
import { SingletonPill } from '../../admin/Editable'
|
import { SingletonPill } from '../../admin/Editable'
|
||||||
import { API_URL } from '../../config'
|
import { API_URL } from '../../config'
|
||||||
|
import { track } from '../../analytics'
|
||||||
|
|
||||||
export function Newsletter() {
|
export function Newsletter() {
|
||||||
const { newsletter } = useSite()
|
const { newsletter } = useSite()
|
||||||
const { t } = useLang()
|
const { t } = useLang()
|
||||||
const save = useFieldSaver()
|
const save = useFieldSaver()
|
||||||
const [email, setEmail] = useState('')
|
const [email, setEmail] = useState('')
|
||||||
|
const [website, setWebsite] = useState('')
|
||||||
const [status, setStatus] = useState<'idle' | 'error' | 'done'>('idle')
|
const [status, setStatus] = useState<'idle' | 'error' | 'done'>('idle')
|
||||||
const [sending, setSending] = useState(false)
|
const [sending, setSending] = useState(false)
|
||||||
|
|
||||||
@@ -75,7 +77,7 @@ export function Newsletter() {
|
|||||||
const res = await fetch(`${API_URL}/api/newsletter`, {
|
const res = await fetch(`${API_URL}/api/newsletter`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ email: emailValue }),
|
body: JSON.stringify({ email: emailValue, website }),
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@@ -87,6 +89,7 @@ export function Newsletter() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
track('newsletter-signup')
|
||||||
setStatus('done')
|
setStatus('done')
|
||||||
setEmail('')
|
setEmail('')
|
||||||
} catch {
|
} catch {
|
||||||
@@ -117,6 +120,17 @@ export function Newsletter() {
|
|||||||
|
|
||||||
<Reveal delay={0.08}>
|
<Reveal delay={0.08}>
|
||||||
<form onSubmit={submit} className="flex flex-col gap-3.5 sm:flex-row" noValidate>
|
<form onSubmit={submit} className="flex flex-col gap-3.5 sm:flex-row" noValidate>
|
||||||
|
{/* honeypot — خارج از دید؛ فقط رباتها آن را پر میکنند */}
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="website"
|
||||||
|
value={website}
|
||||||
|
onChange={(e) => setWebsite(e.target.value)}
|
||||||
|
tabIndex={-1}
|
||||||
|
autoComplete="off"
|
||||||
|
aria-hidden="true"
|
||||||
|
className="pointer-events-none absolute -left-[9999px] h-0 w-0 opacity-0"
|
||||||
|
/>
|
||||||
<div className="flex-1">
|
<div className="flex-1">
|
||||||
<label htmlFor="newsletter-email" className="sr-only">
|
<label htmlFor="newsletter-email" className="sr-only">
|
||||||
{newsletter.placeholder}
|
{newsletter.placeholder}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import React from 'react'
|
|
||||||
import { Reveal } from '../Reveal'
|
import { Reveal } from '../Reveal'
|
||||||
import { ServiceIcon } from '../ServiceIcon'
|
import { ServiceIcon } from '../ServiceIcon'
|
||||||
import { useSectionHeading, useSite } from '../../api'
|
import { useSectionHeading, useSite } from '../../api'
|
||||||
@@ -18,32 +17,35 @@ export function Services() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="bg-ink py-24 lg:py-32" aria-labelledby="services-title">
|
<section className="w-full max-w-full overflow-hidden bg-ink py-12 md:py-24 lg:py-32" aria-labelledby="services-title">
|
||||||
<div className="mx-auto max-w-site px-6 lg:px-10">
|
<div className="mx-auto max-w-site px-4 md:px-6 lg:px-10">
|
||||||
<Reveal className="max-w-2xl text-right">
|
<Reveal className="max-w-2xl text-start">
|
||||||
|
|
||||||
<h2 id="services-title" className="text-section font-bold text-ivory">
|
<h2 id="services-title" className="text-section font-bold text-ivory">
|
||||||
<InlineEdit value={heading.title} onSave={(v) => saveHeading('title', v)} />
|
<InlineEdit value={heading.title} onSave={(v) => saveHeading('title', v)} />
|
||||||
</h2>
|
</h2>
|
||||||
<p className="mt-5 text-base font-light leading-9 text-ivory/60">
|
<p className="mt-3 text-sm font-light leading-7 text-ivory/60 md:mt-5 md:text-base md:leading-9">
|
||||||
<InlineEdit multiline value={heading.lead} onSave={(v) => saveHeading('lead', v)} />
|
<InlineEdit multiline value={heading.lead} onSave={(v) => saveHeading('lead', v)} />
|
||||||
</p>
|
</p>
|
||||||
<div className="mt-6 h-px w-24 bg-copper/70" />
|
<div className="mt-4 h-px w-24 bg-copper/70 md:mt-6" />
|
||||||
</Reveal>
|
</Reveal>
|
||||||
|
|
||||||
<div className="mt-14 grid gap-5 sm:grid-cols-2 lg:grid-cols-4">
|
<div className="mt-8 grid grid-cols-2 gap-3 md:mt-14 md:gap-5 md:grid-cols-2 lg:grid-cols-4">
|
||||||
{services.map((service, i) => (
|
{services.map((service, i) => (
|
||||||
<Reveal key={service.id} delay={0.04 * i} className="h-full">
|
<Reveal key={service.id} delay={0.04 * i} className="h-full">
|
||||||
<EditableItem resource="services" item={service} className="h-full">
|
<EditableItem resource="services" item={service} className="h-full">
|
||||||
<article className="group flex h-full flex-col items-center border border-ivory/10 bg-char p-8 text-center transition-all duration-300 ease-out hover:-translate-y-2 hover:border-copper/40 hover:shadow-lift transform-gpu will-change-transform">
|
<article className="group flex h-full flex-col items-center rounded-xl border border-ivory/10 bg-char p-3.5 text-center transition-all duration-300 ease-out hover:-translate-y-2 hover:border-copper/40 hover:shadow-lift transform-gpu will-change-transform md:rounded-2xl md:p-8">
|
||||||
<ServiceIcon
|
<div className="flex h-10 w-10 items-center justify-center md:h-14 md:w-14">
|
||||||
name={service.icon}
|
<ServiceIcon
|
||||||
className="h-12 w-12 text-copper/85 transition-colors duration-300 ease-out group-hover:text-copper"
|
name={service.icon}
|
||||||
/>
|
className="h-10 w-10 text-copper/85 transition-colors duration-300 ease-out group-hover:text-copper md:h-14 md:w-14"
|
||||||
<h3 className="mt-6 text-lg font-bold text-ivory transition-colors duration-300 ease-out group-hover:text-copper">
|
/>
|
||||||
|
</div>
|
||||||
|
<h3 className="mt-3 text-sm font-bold text-ivory transition-colors duration-300 ease-out group-hover:text-copper md:mt-6 md:text-lg">
|
||||||
{service.title}
|
{service.title}
|
||||||
</h3>
|
</h3>
|
||||||
<p className="mt-3 text-sm font-light leading-8 text-ivory/55">{service.desc}</p>
|
<p className="mt-2 text-xs font-light leading-relaxed text-ivory/55 line-clamp-2 md:mt-3 md:text-sm md:leading-8 md:line-clamp-none">
|
||||||
|
{service.desc}
|
||||||
|
</p>
|
||||||
</article>
|
</article>
|
||||||
</EditableItem>
|
</EditableItem>
|
||||||
</Reveal>
|
</Reveal>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useSite } from '../../api';
|
import { useSite } from '../../api';
|
||||||
import { useLang, formatNumber } from '../../i18n/lang';
|
import { useLang, formatNumber } from '../../i18n/lang';
|
||||||
import { useFieldSaver } from '../../admin/AdminContext';
|
import { useFieldSaver } from '../../admin/AdminContext';
|
||||||
@@ -71,12 +71,12 @@ export function Stats() {
|
|||||||
const save = useFieldSaver();
|
const save = useFieldSaver();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="bg-char py-16" aria-label={t('statsAria')}>
|
<section className="w-full max-w-full overflow-x-clip bg-char py-8 md:py-16" aria-label={t('statsAria')}>
|
||||||
<div className="mx-auto grid max-w-site grid-cols-2 gap-y-12 px-6 lg:grid-cols-4 lg:px-10">
|
<div className="mx-auto grid max-w-site grid-cols-2 gap-3.5 p-4 md:grid-cols-4 md:gap-y-12 md:px-10 md:py-0">
|
||||||
{stats.map((stat) =>
|
{stats.map((stat) =>
|
||||||
<EditableItem key={stat.id} resource="stats" item={stat}>
|
<EditableItem key={stat.id} resource="stats" item={stat}>
|
||||||
<div className="text-center">
|
<div className="p-3.5 text-center md:p-0">
|
||||||
<p className="text-4xl font-bold text-copper md:text-5xl">
|
<p className="text-2xl font-bold text-copper md:text-5xl">
|
||||||
<Counter value={stat.value} />
|
<Counter value={stat.value} />
|
||||||
<InlineEdit
|
<InlineEdit
|
||||||
value={stat.suffix}
|
value={stat.suffix}
|
||||||
@@ -84,10 +84,10 @@ export function Stats() {
|
|||||||
const s = d.stats.find((x) => x.id === stat.id);
|
const s = d.stats.find((x) => x.id === stat.id);
|
||||||
if (s) s.suffix = v;
|
if (s) s.suffix = v;
|
||||||
})}
|
})}
|
||||||
className="ltr:ml-1 rtl:mr-1 text-2xl"
|
className="ltr:ml-1 rtl:mr-1 text-lg md:text-2xl"
|
||||||
/>
|
/>
|
||||||
</p>
|
</p>
|
||||||
<p className="mt-3 text-sm font-light text-ivory/55">
|
<p className="mt-2 text-xs font-light text-ivory/55 md:mt-3 md:text-sm">
|
||||||
<InlineEdit
|
<InlineEdit
|
||||||
value={stat.label}
|
value={stat.label}
|
||||||
onSave={(v) => save('stats', stat.id, { label: v }, (d) => {
|
onSave={(v) => save('stats', stat.id, { label: v }, (d) => {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import React from 'react'
|
|
||||||
import { Reveal } from '../Reveal'
|
import { Reveal } from '../Reveal'
|
||||||
import { SectionHeading } from '../SectionHeading'
|
import { SectionHeading } from '../SectionHeading'
|
||||||
import { useSite } from '../../api'
|
import { useSite } from '../../api'
|
||||||
@@ -9,8 +8,8 @@ export function Team() {
|
|||||||
const { team } = useSite()
|
const { team } = useSite()
|
||||||
const { t } = useLang()
|
const { t } = useLang()
|
||||||
return (
|
return (
|
||||||
<section className="bg-ink py-24 lg:py-32" aria-labelledby="team-title" >
|
<section className="w-full max-w-full overflow-x-clip bg-ink py-16 md:py-24 lg:py-32" aria-labelledby="team-title">
|
||||||
<div className="mx-auto max-w-site px-6 lg:px-10 ">
|
<div className="mx-auto max-w-site px-4 md:px-6 lg:px-10">
|
||||||
<SectionHeading
|
<SectionHeading
|
||||||
title={t('fallbackTeamTitle')}
|
title={t('fallbackTeamTitle')}
|
||||||
lead={t('fallbackTeamLead')}
|
lead={t('fallbackTeamLead')}
|
||||||
@@ -18,18 +17,17 @@ export function Team() {
|
|||||||
align="center"
|
align="center"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="mt-16 grid gap-8 sm:grid-cols-2 lg:grid-cols-4">
|
<div className="mt-8 grid grid-cols-2 gap-3 sm:grid-cols-2 lg:grid-cols-4 md:mt-16 md:gap-8">
|
||||||
{team.map((member, i) => (
|
{team.map((member, i) => (
|
||||||
<Reveal key={member.id} delay={0.06 * i} className="h-full">
|
<Reveal key={member.id} delay={0.06 * i} className="h-full">
|
||||||
<EditableItem resource="team" item={member} className="h-full">
|
<EditableItem resource="team" item={member} className="h-full">
|
||||||
{/* اضافه شدن rounded-2xl برای گرد شدن گوشهها */}
|
<article className="group flex h-full flex-col items-center rounded-xl border border-ivory/10 bg-char p-3 text-center transition-[transform,box-shadow,border-color] duration-500 ease-out hover:-translate-y-3 hover:border-copper/50 hover:shadow-lift md:rounded-2xl md:p-10">
|
||||||
<article className="group flex h-full flex-col items-center rounded-2xl border border-ivory/10 bg-char p-10 text-center transition-[transform,box-shadow,border-color] duration-500 ease-out hover:-translate-y-3 hover:border-copper/50 hover:shadow-lift">
|
<div className="flex h-12 w-12 items-center justify-center rounded-full border border-copper/30 text-sm font-bold text-copper transition-colors duration-500 group-hover:border-copper md:h-24 md:w-24 md:text-3xl">
|
||||||
<div className="flex h-24 w-24 items-center justify-center rounded-full border border-copper/30 text-3xl font-bold text-copper transition-colors duration-500 group-hover:border-copper">
|
|
||||||
{member.name.trim().charAt(0)}
|
{member.name.trim().charAt(0)}
|
||||||
</div>
|
</div>
|
||||||
<h3 className="mt-8 text-lg font-bold text-ivory">{member.name}</h3>
|
<h3 className="mt-2 text-xs font-bold text-ivory md:mt-8 md:text-lg">{member.name}</h3>
|
||||||
<p className="mt-2 text-sm font-light text-copper">{member.role}</p>
|
<p className="mt-0.5 text-[11px] font-medium text-copper line-clamp-1 md:mt-2 md:text-sm md:font-light md:line-clamp-none">{member.role}</p>
|
||||||
<p className="mt-6 border-t border-ivory/10 pt-6 text-sm font-light text-ivory/60">
|
<p className="mt-2 w-full border-t border-ivory/10 pt-2 text-[10px] font-light leading-relaxed text-ivory/60 line-clamp-2 md:mt-6 md:pt-6 md:text-sm md:line-clamp-none">
|
||||||
{member.bio}
|
{member.bio}
|
||||||
</p>
|
</p>
|
||||||
</article>
|
</article>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { AnimatePresence, motion } from 'framer-motion';
|
import { AnimatePresence, motion, PanInfo } from 'framer-motion';
|
||||||
import { ChevronLeftIcon, ChevronRightIcon, QuoteIcon } from 'lucide-react';
|
import { ChevronLeftIcon, ChevronRightIcon, QuoteIcon } from 'lucide-react';
|
||||||
import { SectionHeading } from '../SectionHeading';
|
import { SectionHeading } from '../SectionHeading';
|
||||||
import { useSite } from '../../api';
|
import { useSite } from '../../api';
|
||||||
@@ -17,39 +17,57 @@ export function Testimonials() {
|
|||||||
setIndex((prev) => (prev + dir + testimonials.length) % testimonials.length);
|
setIndex((prev) => (prev + dir + testimonials.length) % testimonials.length);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleDragEnd = (_: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) => {
|
||||||
|
if (info.offset.x > 50) {
|
||||||
|
go(1); // سوایپ به راست
|
||||||
|
} else if (info.offset.x < -50) {
|
||||||
|
go(-1); // سوایپ به چپ
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="border-y border-ivory/10 bg-char py-24 lg:py-32" aria-labelledby="testimonials-title">
|
<section className="border-y border-ivory/10 bg-char py-14 lg:py-28" aria-labelledby="testimonials-title">
|
||||||
<div className="mx-auto max-w-site px-6 lg:px-10">
|
<div className="mx-auto max-w-site px-6 lg:px-10">
|
||||||
<SectionHeading headingKey="testimonials" align="center" />
|
<SectionHeading headingKey="testimonials" align="center" />
|
||||||
|
|
||||||
<div className="relative mx-auto mt-14 max-w-3xl text-center">
|
<div className="relative mx-auto mt-8 max-w-3xl text-center select-none">
|
||||||
<QuoteIcon className="mx-auto h-8 w-8 text-copper/60" aria-hidden="true" />
|
<QuoteIcon className="mx-auto h-6 w-6 text-copper/60 md:h-8 md:w-8" aria-hidden="true" />
|
||||||
<AnimatePresence mode="wait">
|
|
||||||
<motion.blockquote
|
|
||||||
key={current.id}
|
|
||||||
initial={{ opacity: 0, y: 16 }}
|
|
||||||
animate={{ opacity: 1, y: 0 }}
|
|
||||||
exit={{ opacity: 0, y: -12 }}
|
|
||||||
transition={{ duration: 0.28, ease: [0.23, 1, 0.32, 1] }}>
|
|
||||||
|
|
||||||
<EditableItem resource="testimonials" item={current}>
|
<div className="relative min-h-[170px] overflow-hidden md:min-h-[190px]">
|
||||||
<p className="mt-8 text-lg font-light leading-[2.4rem] text-ivory/85 md:text-xl">«{current.quote}»</p>
|
<AnimatePresence mode="wait">
|
||||||
<footer className="mt-8">
|
<motion.blockquote
|
||||||
<p className="text-base font-bold text-copper">{current.name}</p>
|
key={current.id}
|
||||||
<p className="mt-1 text-sm font-light text-ivory/45">{current.role}</p>
|
drag="x"
|
||||||
</footer>
|
dragConstraints={{ left: 0, right: 0 }}
|
||||||
</EditableItem>
|
dragElastic={0.2}
|
||||||
</motion.blockquote>
|
onDragEnd={handleDragEnd}
|
||||||
</AnimatePresence>
|
initial={{ opacity: 0, y: 12 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
exit={{ opacity: 0, y: -12 }}
|
||||||
|
transition={{ duration: 0.28, ease: [0.23, 1, 0.32, 1] }}
|
||||||
|
className="cursor-grab active:cursor-grabbing"
|
||||||
|
>
|
||||||
|
<EditableItem resource="testimonials" item={current}>
|
||||||
|
<p className="mt-5 text-base font-light leading-8 text-ivory/85 md:mt-8 md:text-xl md:leading-[2.4rem]">
|
||||||
|
«{current.quote}»
|
||||||
|
</p>
|
||||||
|
<footer className="mt-5 md:mt-8">
|
||||||
|
<p className="text-sm font-bold text-copper md:text-base">{current.name}</p>
|
||||||
|
<p className="mt-0.5 text-xs font-light text-ivory/45 md:text-sm">{current.role}</p>
|
||||||
|
</footer>
|
||||||
|
</EditableItem>
|
||||||
|
</motion.blockquote>
|
||||||
|
</AnimatePresence>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="mt-12 flex items-center justify-center gap-5">
|
<div className="mt-6 flex items-center justify-center gap-5 md:mt-10">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => go(1)}
|
onClick={() => go(1)}
|
||||||
aria-label={t('prevTestimonial')}
|
aria-label={t('prevTestimonial')}
|
||||||
className="group flex h-12 w-12 items-center justify-center rounded-full border border-copper/35 bg-char/80 text-ivory/80 backdrop-blur-md shadow-[0_4px_20px_rgba(0,0,0,0.35)] transition-all duration-300 ease-lux hover:scale-110 hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_25px_rgba(197,143,92,0.45)] active:scale-95"
|
className="group flex h-10 w-10 items-center justify-center rounded-full border border-copper/35 bg-char/80 text-ivory/80 shadow-[0_4px_20px_rgba(0,0,0,0.35)] backdrop-blur-md transition-all duration-300 ease-lux hover:scale-110 hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_25px_rgba(197,143,92,0.45)] active:scale-95 md:h-12 md:w-12"
|
||||||
>
|
>
|
||||||
<ChevronRightIcon className="h-5 w-5 ltr:rotate-180 transition-transform duration-300 ease-lux rtl:group-hover:translate-x-0.5 ltr:group-hover:-translate-x-0.5" />
|
<ChevronRightIcon className="h-4 w-4 transition-transform duration-300 ease-lux ltr:rotate-180 rtl:group-hover:translate-x-0.5 ltr:group-hover:-translate-x-0.5 md:h-5 md:w-5" />
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<div className="flex items-center gap-2" role="tablist" aria-label={t('selectTestimonialAria')}>
|
<div className="flex items-center gap-2" role="tablist" aria-label={t('selectTestimonialAria')}>
|
||||||
@@ -63,8 +81,8 @@ export function Testimonials() {
|
|||||||
onClick={() => setIndex(i)}
|
onClick={() => setIndex(i)}
|
||||||
className={`h-2 rounded-full transition-all duration-300 ease-lux ${
|
className={`h-2 rounded-full transition-all duration-300 ease-lux ${
|
||||||
i === index
|
i === index
|
||||||
? 'w-8 bg-copper shadow-[0_0_12px_rgba(197,143,92,0.6)]'
|
? 'w-7 bg-copper shadow-[0_0_12px_rgba(197,143,92,0.6)] md:w-8'
|
||||||
: 'w-2.5 bg-ivory/20 hover:bg-ivory/40'
|
: 'w-2 bg-ivory/20 hover:bg-ivory/40 md:w-2.5'
|
||||||
}`}
|
}`}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@@ -74,9 +92,9 @@ export function Testimonials() {
|
|||||||
type="button"
|
type="button"
|
||||||
onClick={() => go(-1)}
|
onClick={() => go(-1)}
|
||||||
aria-label={t('nextTestimonial')}
|
aria-label={t('nextTestimonial')}
|
||||||
className="group flex h-12 w-12 items-center justify-center rounded-full border border-copper/35 bg-char/80 text-ivory/80 backdrop-blur-md shadow-[0_4px_20px_rgba(0,0,0,0.35)] transition-all duration-300 ease-lux hover:scale-110 hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_25px_rgba(197,143,92,0.45)] active:scale-95"
|
className="group flex h-10 w-10 items-center justify-center rounded-full border border-copper/35 bg-char/80 text-ivory/80 shadow-[0_4px_20px_rgba(0,0,0,0.35)] backdrop-blur-md transition-all duration-300 ease-lux hover:scale-110 hover:border-copper hover:bg-copper hover:text-char hover:shadow-[0_0_25px_rgba(197,143,92,0.45)] active:scale-95 md:h-12 md:w-12"
|
||||||
>
|
>
|
||||||
<ChevronLeftIcon className="h-5 w-5 ltr:rotate-180 transition-transform duration-300 ease-lux rtl:group-hover:-translate-x-0.5 ltr:group-hover:translate-x-0.5" />
|
<ChevronLeftIcon className="h-4 w-4 transition-transform duration-300 ease-lux ltr:rotate-180 rtl:group-hover:-translate-x-0.5 ltr:group-hover:translate-x-0.5 md:h-5 md:w-5" />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useRef } from 'react'
|
import { useRef } from 'react'
|
||||||
import { motion, useScroll, useTransform, useInView } from 'framer-motion'
|
import { motion, useScroll, useTransform, useInView } from 'framer-motion'
|
||||||
import { useSite, type TimelineItem } from '../../api'
|
import { useSite, type TimelineItem } from '../../api'
|
||||||
import { useLang } from '../../i18n/lang'
|
import { useLang } from '../../i18n/lang'
|
||||||
@@ -9,17 +9,17 @@ const ease = [0.23, 1, 0.32, 1] as const
|
|||||||
|
|
||||||
type Milestone = TimelineItem
|
type Milestone = TimelineItem
|
||||||
|
|
||||||
function Item({ item, index }: { item: Milestone; index: number }) {
|
function DesktopTimelineItem({ item, index }: { item: Milestone; index: number }) {
|
||||||
const ref = useRef<HTMLLIElement>(null)
|
const ref = useRef<HTMLLIElement>(null)
|
||||||
const active = useInView(ref, { once: true, amount: 0.6 })
|
const active = useInView(ref, { once: true, amount: 0.6 })
|
||||||
const onRight = index % 2 === 0
|
const onRight = index % 2 === 0
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li ref={ref} className="relative md:grid md:grid-cols-2 md:gap-16">
|
<li ref={ref} className="relative md:grid md:grid-cols-2 md:gap-16">
|
||||||
{/* دایرهها: مرکز دایره در موبایل و دسکتاپ دقیقاً روی مرکز خط 2px است */}
|
{/* دایرهها: مرکز دایره دقیقاً روی مرکز خط 2px است */}
|
||||||
<span
|
<span
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
className={`absolute right-[-5px] top-8 z-10 h-3 w-3 rounded-full transition-all duration-500 ease-lux md:right-1/2 md:translate-x-1/2 ${
|
className={`absolute right-1/2 top-8 z-10 h-3 w-3 translate-x-1/2 rounded-full transition-all duration-500 ease-lux ${
|
||||||
active
|
active
|
||||||
? 'bg-copper shadow-[0_0_15px_rgba(197,143,92,0.6),0_0_0_6px_rgba(213,135,76,0.15)]'
|
? 'bg-copper shadow-[0_0_15px_rgba(197,143,92,0.6),0_0_0_6px_rgba(213,135,76,0.15)]'
|
||||||
: 'bg-ivory/20'
|
: 'bg-ivory/20'
|
||||||
@@ -27,15 +27,13 @@ function Item({ item, index }: { item: Milestone; index: number }) {
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<motion.div
|
<motion.div
|
||||||
className={`pr-8 md:pr-0 ${
|
className={onRight ? 'md:col-start-1 md:pl-16 md:text-left' : 'md:col-start-2 md:pr-16'}
|
||||||
onRight ? 'md:col-start-1 md:pl-16 md:text-left' : 'md:col-start-2 md:pr-16'
|
|
||||||
}`}
|
|
||||||
initial={{ opacity: 0, y: 24 }}
|
initial={{ opacity: 0, y: 24 }}
|
||||||
animate={active ? { opacity: 1, y: 0 } : { opacity: 0, y: 24 }}
|
animate={active ? { opacity: 1, y: 0 } : { opacity: 0, y: 24 }}
|
||||||
transition={{ duration: 0.55, ease }}
|
transition={{ duration: 0.55, ease }}
|
||||||
>
|
>
|
||||||
<EditableItem resource="timeline" item={item}>
|
<EditableItem resource="timeline" item={item}>
|
||||||
<div className="group relative overflow-hidden rounded-2xl border border-ivory/10 bg-ink/40 p-6 backdrop-blur-md transition-all duration-500 ease-lux hover:-translate-y-1.5 hover:border-copper/50 hover:bg-ink/60 hover:shadow-[0_12px_40px_-10px_rgba(197,143,92,0.35)] md:p-8">
|
<div className="group relative overflow-hidden rounded-2xl border border-ivory/10 bg-ink/40 p-8 backdrop-blur-md transition-all duration-500 ease-lux hover:-translate-y-1.5 hover:border-copper/50 hover:bg-ink/60 hover:shadow-[0_12px_40px_-10px_rgba(197,143,92,0.35)]">
|
||||||
<div className="absolute inset-0 bg-gradient-to-br from-white/[0.03] to-transparent opacity-0 transition-opacity duration-500 group-hover:opacity-100" />
|
<div className="absolute inset-0 bg-gradient-to-br from-white/[0.03] to-transparent opacity-0 transition-opacity duration-500 group-hover:opacity-100" />
|
||||||
|
|
||||||
<span className="relative z-10 text-sm font-medium tracking-widest text-copper">{item.year}</span>
|
<span className="relative z-10 text-sm font-medium tracking-widest text-copper">{item.year}</span>
|
||||||
@@ -51,18 +49,19 @@ function Item({ item, index }: { item: Milestone; index: number }) {
|
|||||||
export function Timeline() {
|
export function Timeline() {
|
||||||
const { timeline } = useSite()
|
const { timeline } = useSite()
|
||||||
const { t } = useLang()
|
const { t } = useLang()
|
||||||
const ref = useRef<HTMLDivElement>(null)
|
|
||||||
|
const desktopRef = useRef<HTMLDivElement>(null)
|
||||||
const { scrollYProgress } = useScroll({
|
const { scrollYProgress } = useScroll({
|
||||||
target: ref,
|
target: desktopRef,
|
||||||
offset: ['start 70%', 'end 60%'],
|
offset: ['start 70%', 'end 60%'],
|
||||||
})
|
})
|
||||||
const scaleY = useTransform(scrollYProgress, [0, 1], [0, 1])
|
const scaleY = useTransform(scrollYProgress, [0, 1], [0, 1])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="bg-char py-24 md:py-32" aria-labelledby="timeline-title">
|
<section className="w-full max-w-full overflow-x-clip bg-char py-16 md:py-24 lg:py-32" aria-labelledby="timeline-title">
|
||||||
<div className="mx-auto w-full max-w-site px-6 lg:px-10">
|
<div className="mx-auto w-full max-w-site px-4 md:px-6 lg:px-10">
|
||||||
|
|
||||||
<div className="mb-16">
|
<div className="mb-8 md:mb-16">
|
||||||
<SectionHeading
|
<SectionHeading
|
||||||
title={t('fallbackTimelineTitle')}
|
title={t('fallbackTimelineTitle')}
|
||||||
lead={t('fallbackTimelineLead')}
|
lead={t('fallbackTimelineLead')}
|
||||||
@@ -71,28 +70,65 @@ export function Timeline() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div ref={ref} className="relative mx-auto mt-20 max-w-4xl">
|
{/* Mobile View: Minimalist Editorial Spine */}
|
||||||
|
<div className="block md:hidden px-5 py-4">
|
||||||
|
<div className="relative rtl:pr-6 rtl:border-r ltr:pl-6 ltr:border-l border-copper/25 space-y-6 my-2">
|
||||||
|
{timeline.map((item) => (
|
||||||
|
<EditableItem key={item.id} resource="timeline" item={item}>
|
||||||
|
<div className="relative group">
|
||||||
|
{/* Timeline Node on the vertical spine */}
|
||||||
|
<span
|
||||||
|
aria-hidden="true"
|
||||||
|
className="absolute rtl:-right-[29px] ltr:-left-[29px] top-1.5 h-2.5 w-2.5 rounded-full bg-copper ring-4 ring-copper/15"
|
||||||
|
/>
|
||||||
|
{/* Milestone Content */}
|
||||||
|
<div className="p-0">
|
||||||
|
<div className="flex items-center gap-2 mb-1">
|
||||||
|
<span className="text-[11px] font-bold tracking-wider text-copper bg-copper/10 px-2 py-0.5 rounded-md">
|
||||||
|
{item.year}
|
||||||
|
</span>
|
||||||
|
<h3 className="text-sm font-semibold text-ivory">
|
||||||
|
{item.title}
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-ivory/60 leading-relaxed line-clamp-2 rtl:pr-0.5 ltr:pl-0.5">
|
||||||
|
{item.desc}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</EditableItem>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mt-6">
|
||||||
|
<AddItemButton resource="timeline" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Desktop View: Original Vertical Timeline */}
|
||||||
|
<div ref={desktopRef} className="relative mx-auto mt-20 hidden max-w-4xl md:block">
|
||||||
{/* خط پسزمینه ثابت - عرض 2px و همراستایی دقیق با مرکز */}
|
{/* خط پسزمینه ثابت - عرض 2px و همراستایی دقیق با مرکز */}
|
||||||
<span
|
<span
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
className="absolute inset-y-0 right-0 w-[2px] bg-ivory/10 md:right-1/2 md:translate-x-1/2"
|
className="absolute inset-y-0 right-1/2 w-[2px] translate-x-1/2 bg-ivory/10"
|
||||||
/>
|
/>
|
||||||
{/* خط متحرک - عرض 2px و همراستایی دقیق با مرکز */}
|
{/* خط متحرک - عرض 2px و همراستایی دقیق با مرکز */}
|
||||||
<motion.span
|
<motion.span
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
className="absolute inset-y-0 right-0 w-[2px] origin-top bg-gradient-to-b from-copper/80 via-copper to-copper/20 shadow-[0_0_10px_rgba(197,143,92,0.5)] md:right-1/2 md:translate-x-1/2"
|
className="absolute inset-y-0 right-1/2 w-[2px] origin-top translate-x-1/2 bg-gradient-to-b from-copper/80 via-copper to-copper/20 shadow-[0_0_10px_rgba(197,143,92,0.5)]"
|
||||||
style={{ scaleY }}
|
style={{ scaleY }}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ol className="flex flex-col gap-12 md:gap-16">
|
<ol className="flex flex-col gap-16">
|
||||||
{timeline.map((item, i) => (
|
{timeline.map((item, i) => (
|
||||||
<Item key={item.id} item={item} index={i} />
|
<DesktopTimelineItem key={item.id} item={item} index={i} />
|
||||||
))}
|
))}
|
||||||
</ol>
|
</ol>
|
||||||
<div className="mt-12">
|
<div className="mt-12">
|
||||||
<AddItemButton resource="timeline" />
|
<AddItemButton resource="timeline" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import React from 'react';
|
|
||||||
import { Reveal } from '../Reveal';
|
import { Reveal } from '../Reveal';
|
||||||
import { SectionHeading } from '../SectionHeading';
|
import { SectionHeading } from '../SectionHeading';
|
||||||
import { useSectionHeading, useSite } from '../../api';
|
import { useSectionHeading, useSite } from '../../api';
|
||||||
import { AddItemButton, EditableItem, SingletonPill } from '../../admin/Editable';
|
import { AddItemButton, EditableItem } from '../../admin/Editable';
|
||||||
import { InlineEdit } from '../admin/InlineEdit';
|
import { InlineEdit } from '../admin/InlineEdit';
|
||||||
import { useFieldSaver } from '../../admin/AdminContext';
|
import { useFieldSaver } from '../../admin/AdminContext';
|
||||||
import { useLang, formatNumber } from '../../i18n/lang';
|
import { useLang, formatNumber } from '../../i18n/lang';
|
||||||
@@ -19,9 +18,15 @@ export function WhyUs() {
|
|||||||
if (it) Object.assign(it, changes);
|
if (it) Object.assign(it, changes);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const formatIndex = (index: number) => {
|
||||||
|
return lang === 'fa'
|
||||||
|
? `۰${formatNumber(index + 1, lang)}`
|
||||||
|
: String(index + 1).padStart(2, '0');
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="bg-ink py-24 lg:py-32" aria-labelledby="why-title">
|
<section className="w-full max-w-full overflow-x-clip bg-ink py-16 md:py-24 lg:py-32" aria-labelledby="why-title">
|
||||||
<div className="mx-auto grid max-w-site gap-14 px-6 lg:grid-cols-12 lg:px-10">
|
<div className="mx-auto grid max-w-site gap-8 px-4 md:gap-14 md:px-6 lg:grid-cols-12 lg:px-10">
|
||||||
<div className="lg:col-span-5">
|
<div className="lg:col-span-5">
|
||||||
<SectionHeading
|
<SectionHeading
|
||||||
title={heading.title}
|
title={heading.title}
|
||||||
@@ -33,42 +38,84 @@ export function WhyUs() {
|
|||||||
<img
|
<img
|
||||||
src={heading.image}
|
src={heading.image}
|
||||||
alt={heading.title}
|
alt={heading.title}
|
||||||
className="mt-10 h-80 w-full object-cover" />
|
className="mt-6 hidden h-80 w-full object-cover md:mt-10 md:block" />
|
||||||
|
|
||||||
</Reveal>
|
</Reveal>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="lg:col-span-7">
|
<div className="lg:col-span-7">
|
||||||
<div className="grid gap-x-10 gap-y-px sm:grid-cols-2">
|
{/* Mobile 2-Column Luxury Micro-Card Grid */}
|
||||||
{why_us.map((item, i) =>
|
<div className="block md:hidden px-3.5 py-2">
|
||||||
<Reveal key={item.id} delay={0.05 * i}>
|
<div className="grid grid-cols-2 gap-2.5 items-stretch">
|
||||||
<EditableItem resource="why_us" item={item}>
|
{why_us.map((item, i) => (
|
||||||
<div className="flex h-full flex-col border-t border-ivory/10 py-8">
|
<EditableItem key={item.id} resource="why_us" item={item} className="h-full">
|
||||||
<span className="text-xs font-light text-copper">{lang === 'fa' ? `۰${formatNumber(i + 1, lang)}` : formatNumber(i + 1, lang).padStart(2, '0')}</span>
|
<div className="h-full p-3 rounded-xl border border-ivory/10 bg-char/40 flex flex-col justify-between hover:border-copper/40 transition-all duration-200">
|
||||||
<h3 className="mt-4 text-lg font-bold text-ivory">
|
<div>
|
||||||
<InlineEdit
|
{/* Top Row: Index Badge */}
|
||||||
value={item.title}
|
<div className="flex items-center justify-between mb-2">
|
||||||
onSave={(v) => saveItem(item.id, { title: v })}
|
<span className="text-[11px] font-mono font-bold text-copper tracking-wider px-1.5 py-0.5 rounded bg-copper/10 border border-copper/20">
|
||||||
/>
|
{`0${i + 1}`}
|
||||||
</h3>
|
</span>
|
||||||
<p className="mt-3 text-sm font-light leading-8 text-ivory/55">
|
</div>
|
||||||
<InlineEdit
|
|
||||||
multiline
|
{/* Title */}
|
||||||
value={item.desc}
|
<h4 className="text-xs font-bold text-ivory mb-1 line-clamp-1 leading-snug">
|
||||||
onSave={(v) => saveItem(item.id, { desc: v })}
|
<InlineEdit
|
||||||
/>
|
value={item.title}
|
||||||
</p>
|
onSave={(v) => saveItem(item.id, { title: v })}
|
||||||
</div>
|
/>
|
||||||
</EditableItem>
|
</h4>
|
||||||
</Reveal>
|
|
||||||
)}
|
{/* Description */}
|
||||||
|
<p className="text-[10px] text-ivory/60 leading-relaxed line-clamp-2">
|
||||||
|
<InlineEdit
|
||||||
|
multiline
|
||||||
|
value={item.desc}
|
||||||
|
onSave={(v) => saveItem(item.id, { desc: v })}
|
||||||
|
/>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</EditableItem>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="mt-6">
|
||||||
|
<AddItemButton resource="why_us" />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-8">
|
|
||||||
<AddItemButton resource="why_us" />
|
{/* Desktop Grid Layout */}
|
||||||
|
<div className="hidden md:block">
|
||||||
|
<div className="grid gap-x-10 gap-y-px sm:grid-cols-2">
|
||||||
|
{why_us.map((item, i) => (
|
||||||
|
<Reveal key={item.id} delay={0.05 * i}>
|
||||||
|
<EditableItem resource="why_us" item={item}>
|
||||||
|
<div className="flex h-full flex-col border-t border-ivory/10 py-8">
|
||||||
|
<span className="text-xs font-light text-copper">{formatIndex(i)}</span>
|
||||||
|
<h3 className="mt-4 text-lg font-bold text-ivory">
|
||||||
|
<InlineEdit
|
||||||
|
value={item.title}
|
||||||
|
onSave={(v) => saveItem(item.id, { title: v })}
|
||||||
|
/>
|
||||||
|
</h3>
|
||||||
|
<p className="mt-3 text-sm font-light leading-8 text-ivory/55">
|
||||||
|
<InlineEdit
|
||||||
|
multiline
|
||||||
|
value={item.desc}
|
||||||
|
onSave={(v) => saveItem(item.id, { desc: v })}
|
||||||
|
/>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</EditableItem>
|
||||||
|
</Reveal>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="mt-8">
|
||||||
|
<AddItemButton resource="why_us" />
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>);
|
</section>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,11 @@ export type ArticleStatus = 'draft' | 'published';
|
|||||||
export type Article = {
|
export type Article = {
|
||||||
id: number;
|
id: number;
|
||||||
slug: string; title: string; title_en?: string; date: string; date_en?: string; category: string; category_en?: string;
|
slug: string; title: string; title_en?: string; date: string; date_en?: string; category: string; category_en?: string;
|
||||||
image: string; read: string; read_en?: string; excerpt: string; excerpt_en?: string;
|
image: string;
|
||||||
|
read: string; read_en?: string; excerpt: string; excerpt_en?: string;
|
||||||
|
/** پرسشوپاسخهای پایان مقاله — سئوی FAQPage (faqs_en خالی → برگشت به فارسی) */
|
||||||
|
faqs?: { question: string; answer: string }[];
|
||||||
|
faqs_en?: { question: string; answer: string }[] | null;
|
||||||
/** سند TipTap — ردیفهای قدیمی ممکن است هنوز لیست پاراگراف باشند */
|
/** سند TipTap — ردیفهای قدیمی ممکن است هنوز لیست پاراگراف باشند */
|
||||||
body: ArticleBodyDoc | string[];
|
body: ArticleBodyDoc | string[];
|
||||||
/** بدنه انگلیسی — سند TipTap؛ null یعنی بدون ترجمه */
|
/** بدنه انگلیسی — سند TipTap؛ null یعنی بدون ترجمه */
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ export function localizeSite(data: SiteData, lang: Lang): SiteData {
|
|||||||
read: s(a.read, a.read_en),
|
read: s(a.read, a.read_en),
|
||||||
excerpt: s(a.excerpt, a.excerpt_en),
|
excerpt: s(a.excerpt, a.excerpt_en),
|
||||||
body: docHasText(a.body_en) ? a.body_en! : a.body,
|
body: docHasText(a.body_en) ? a.body_en! : a.body,
|
||||||
|
faqs: list(a.faqs ?? [], a.faqs_en),
|
||||||
})),
|
})),
|
||||||
testimonials: data.testimonials.map((tm) => ({
|
testimonials: data.testimonials.map((tm) => ({
|
||||||
...tm,
|
...tm,
|
||||||
|
|||||||
+27
-5
@@ -130,21 +130,42 @@ html[lang='en'] .article-body h3 {
|
|||||||
}
|
}
|
||||||
.article-body h2 {
|
.article-body h2 {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
padding-inline-start: 0.85rem;
|
/* نوار مسی ندارد: نوار عمودی مسی نشانه اختصاصی نقلقول است و تیتر فقط با
|
||||||
border-inline-start: 3px solid #d5874c;
|
اندازه و وزن متمایز میشود تا با نقلقول اشتباه گرفته نشود. */
|
||||||
}
|
}
|
||||||
.article-body h3 {
|
.article-body h3 {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
}
|
}
|
||||||
|
/* متن مقاله در هر دو زبان همتراز (justify) است: لبههای راست و چپ یکدست میمانند.
|
||||||
|
text-align: justify با direction درست، در فارسی و انگلیسی هر دو کار میکند. */
|
||||||
[data-dir='rtl'] .article-body {
|
[data-dir='rtl'] .article-body {
|
||||||
direction: rtl;
|
direction: rtl;
|
||||||
text-align: right;
|
text-align: justify;
|
||||||
|
text-justify: inter-word;
|
||||||
}
|
}
|
||||||
[data-dir='ltr'] .article-body {
|
[data-dir='ltr'] .article-body {
|
||||||
direction: ltr;
|
direction: ltr;
|
||||||
text-align: left;
|
text-align: justify;
|
||||||
|
text-justify: inter-word;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* تیترها و فهرستها همتراز نمیشوند — فقط متن پاراگراف تراز میشود
|
||||||
|
(تیتر کوتاه با justify کشیده و بدقلق میشود و فهرست نقطهگذاریاش بههم میریزد). */
|
||||||
|
[data-dir='rtl'] .article-body h1,
|
||||||
|
[data-dir='rtl'] .article-body h2,
|
||||||
|
[data-dir='rtl'] .article-body h3,
|
||||||
|
[data-dir='rtl'] .article-body li,
|
||||||
|
[data-dir='rtl'] .article-body figcaption {
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
[data-dir='ltr'] .article-body h1,
|
||||||
|
[data-dir='ltr'] .article-body h2,
|
||||||
|
[data-dir='ltr'] .article-body h3,
|
||||||
|
[data-dir='ltr'] .article-body li,
|
||||||
|
[data-dir='ltr'] .article-body figcaption {
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
[data-dir='ltr'] .article-body h1 {
|
[data-dir='ltr'] .article-body h1 {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
}
|
}
|
||||||
@@ -259,7 +280,8 @@ html[lang='en'] .article-body h3 {
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 0.5rem;
|
gap: 0.5rem;
|
||||||
margin-top: 0.6rem;
|
margin-top: 0.6rem;
|
||||||
text-align: right;
|
/* تراز از جهت بلوک پیروی میکند — در مقاله انگلیسی چپ و در فارسی راست */
|
||||||
|
text-align: start;
|
||||||
}
|
}
|
||||||
[data-wysiwyg] .article-figure-node.is-selected .article-figure {
|
[data-wysiwyg] .article-figure-node.is-selected .article-figure {
|
||||||
outline: 1px dashed rgba(213, 135, 76, 0.8);
|
outline: 1px dashed rgba(213, 135, 76, 0.8);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { Reveal } from '../components/Reveal';
|
|||||||
import { PremiumButton } from '../components/PremiumButton';
|
import { PremiumButton } from '../components/PremiumButton';
|
||||||
import { ArticleCard } from '../components/ArticleCard';
|
import { ArticleCard } from '../components/ArticleCard';
|
||||||
import { ArticleMeta } from '../components/ArticleMeta';
|
import { ArticleMeta } from '../components/ArticleMeta';
|
||||||
|
import { ArticleFaq } from '../components/ArticleFaq';
|
||||||
import { TableOfContents, extractToc, type TocItem } from '../components/TableOfContents';
|
import { TableOfContents, extractToc, type TocItem } from '../components/TableOfContents';
|
||||||
import { Newsletter } from '../components/home/Newsletter';
|
import { Newsletter } from '../components/home/Newsletter';
|
||||||
import { useSite } from '../api';
|
import { useSite } from '../api';
|
||||||
@@ -47,6 +48,35 @@ export function ArticleDetail() {
|
|||||||
// عنوان تب، توضیح متا، OG و JSON-LD از خود مقاله
|
// عنوان تب، توضیح متا، OG و JSON-LD از خود مقاله
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!article) return;
|
if (!article) return;
|
||||||
|
let jsonLd: Record<string, unknown> | Record<string, unknown>[] = {
|
||||||
|
'@context': 'https://schema.org',
|
||||||
|
'@type': 'Article',
|
||||||
|
headline: article.title,
|
||||||
|
description: article.meta_description || article.excerpt,
|
||||||
|
image: article.image ? new URL(article.image, window.location.origin).href : undefined,
|
||||||
|
inLanguage: lang,
|
||||||
|
author: { '@type': 'Organization', name: article.author?.trim() || brand.name },
|
||||||
|
publisher: { '@type': 'Organization', name: brand.name },
|
||||||
|
mainEntityOfPage: new URL(href(`/blog/${article.slug}`), window.location.origin).href,
|
||||||
|
};
|
||||||
|
// پرسشوپاسخ مقاله → اسکیمای FAQPage در همان بلوک (آرایهای از دو اسکیما)
|
||||||
|
const articleFaqs = article.faqs ?? [];
|
||||||
|
if (articleFaqs.length) {
|
||||||
|
jsonLd = [
|
||||||
|
jsonLd,
|
||||||
|
{
|
||||||
|
'@context': 'https://schema.org',
|
||||||
|
'@type': 'FAQPage',
|
||||||
|
mainEntity: articleFaqs
|
||||||
|
.filter((f) => f.question.trim())
|
||||||
|
.map((f) => ({
|
||||||
|
'@type': 'Question',
|
||||||
|
name: f.question,
|
||||||
|
acceptedAnswer: { '@type': 'Answer', text: f.answer },
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
applySeo({
|
applySeo({
|
||||||
title: `${article.title} | ${brand.name}`,
|
title: `${article.title} | ${brand.name}`,
|
||||||
// توضیحات سئوی اختصاصی در اولویت است؛ در نبود آن، چکیده
|
// توضیحات سئوی اختصاصی در اولویت است؛ در نبود آن، چکیده
|
||||||
@@ -55,17 +85,7 @@ export function ArticleDetail() {
|
|||||||
image: article.image,
|
image: article.image,
|
||||||
type: 'article',
|
type: 'article',
|
||||||
lang,
|
lang,
|
||||||
jsonLd: {
|
jsonLd,
|
||||||
'@context': 'https://schema.org',
|
|
||||||
'@type': 'Article',
|
|
||||||
headline: article.title,
|
|
||||||
description: article.meta_description || article.excerpt,
|
|
||||||
image: article.image ? new URL(article.image, window.location.origin).href : undefined,
|
|
||||||
inLanguage: lang,
|
|
||||||
author: { '@type': 'Organization', name: article.author?.trim() || brand.name },
|
|
||||||
publisher: { '@type': 'Organization', name: brand },
|
|
||||||
mainEntityOfPage: new URL(href(`/blog/${article.slug}`), window.location.origin).href,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}, [article?.id, lang, href]);
|
}, [article?.id, lang, href]);
|
||||||
|
|
||||||
@@ -170,6 +190,14 @@ export function ArticleDetail() {
|
|||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
|
{/* پرسشوپاسخهای پایان مقاله — همان ظاهر آکاردئونی FAQ سایت */}
|
||||||
|
<div className="bg-ink pb-4">
|
||||||
|
<div className="mx-auto max-w-5xl px-6 lg:px-10">
|
||||||
|
<h2 className="text-section font-bold text-ivory">{t('fallbackFaqTitle')}</h2>
|
||||||
|
<ArticleFaq faqs={article.faqs ?? []} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<section className="bg-char py-20 lg:py-28" aria-label={t('relatedArticlesAria')}>
|
<section className="bg-char py-20 lg:py-28" aria-label={t('relatedArticlesAria')}>
|
||||||
<div className="mx-auto max-w-site px-6 lg:px-10">
|
<div className="mx-auto max-w-site px-6 lg:px-10">
|
||||||
<h2 className="text-section font-bold text-ivory">{t('relatedArticles')}</h2>
|
<h2 className="text-section font-bold text-ivory">{t('relatedArticles')}</h2>
|
||||||
|
|||||||
@@ -1,22 +1,16 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { PageHero } from '../components/PageHero'
|
import { PageHero } from '../components/PageHero'
|
||||||
import { ContactSection } from '../components/ContactSection'
|
import { ContactSection } from '../components/ContactSection'
|
||||||
import { Reveal } from '../components/Reveal'
|
|
||||||
import { useSite } from '../api'
|
|
||||||
import { useLang } from '../i18n/lang'
|
import { useLang } from '../i18n/lang'
|
||||||
import { AddItemButton, EditableItem } from '../admin/Editable'
|
|
||||||
|
|
||||||
export function ContactPage() {
|
export function ContactPage() {
|
||||||
const { offices } = useSite()
|
|
||||||
const { t } = useLang()
|
const { t } = useLang()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<PageHero pageKey="contact" />
|
<PageHero pageKey="contact" />
|
||||||
|
|
||||||
<section className="bg-ink py-2" aria-label={t('officesAria')}>
|
<section className="bg-ink py-2" aria-label={t('officesAria')} />
|
||||||
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<ContactSection />
|
<ContactSection />
|
||||||
</>
|
</>
|
||||||
|
|||||||
+4
-3
@@ -72,10 +72,10 @@ function upsertAlternates(path: string): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setJsonLd(data: Record<string, unknown> | null): void {
|
function setJsonLd(data: Record<string, unknown> | Record<string, unknown>[] | null): void {
|
||||||
const id = 'seo-jsonld'
|
const id = 'seo-jsonld'
|
||||||
let el = document.getElementById(id)
|
let el = document.getElementById(id)
|
||||||
if (!data) {
|
if (!data || (Array.isArray(data) && data.length === 0)) {
|
||||||
el?.remove()
|
el?.remove()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,8 @@ export type SeoInput = {
|
|||||||
type?: 'website' | 'article'
|
type?: 'website' | 'article'
|
||||||
/** زبان جاری — برای og:locale و لینکهای hreflang */
|
/** زبان جاری — برای og:locale و لینکهای hreflang */
|
||||||
lang?: Lang
|
lang?: Lang
|
||||||
jsonLd?: Record<string, unknown> | null
|
/** اسکیمای JSON-LD — یک شیء یا آرایهای از چند اسکیما (مثل Article + FAQPage) */
|
||||||
|
jsonLd?: Record<string, unknown> | Record<string, unknown>[] | null
|
||||||
}
|
}
|
||||||
|
|
||||||
/** تنظیم همه تگهای سئوی صفحه در یک فراخوانی */
|
/** تنظیم همه تگهای سئوی صفحه در یک فراخوانی */
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
# Alertmanager — تحویل هشدارها به بله (API بات بله همان شکل تلگرام است)
|
||||||
|
# توکن و شناسه چت از .env (ALERT_BOT_TOKEN / ALERT_CHAT_ID) — این فایل قالب است؛
|
||||||
|
# نسخهٔ رندرشده (alertmanager.rendered.yml) را scripts/stack.sh میسازد و در گیت نمیرود.
|
||||||
|
# تا وقتی توکن/چت واقعی تنظیم نشده، ارسال شکست میخورد و هشدارها در
|
||||||
|
# http://<prometheus>:9090/alerts دیده میشوند.
|
||||||
|
|
||||||
|
global:
|
||||||
|
resolve_timeout: 5m
|
||||||
|
|
||||||
|
route:
|
||||||
|
receiver: bale
|
||||||
|
group_by: [alertname, job]
|
||||||
|
group_wait: 30s
|
||||||
|
group_interval: 5m
|
||||||
|
repeat_interval: 4h
|
||||||
|
|
||||||
|
receivers:
|
||||||
|
- name: bale
|
||||||
|
telegram_configs:
|
||||||
|
# تنها تفاوت با تلگرام: نشانی API بات بله
|
||||||
|
- api_url: https://tapi.bale.ai
|
||||||
|
bot_token: "${ALERT_BOT_TOKEN}"
|
||||||
|
chat_id: ${ALERT_CHAT_ID}
|
||||||
|
send_resolved: true
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
{
|
||||||
|
"uid": "aramesh-app",
|
||||||
|
"title": "آرامش — اپلیکیشن و ترافیک",
|
||||||
|
"tags": ["aramesh"],
|
||||||
|
"timezone": "browser",
|
||||||
|
"schemaVersion": 39,
|
||||||
|
"refresh": "30s",
|
||||||
|
"time": { "from": "now-6h", "to": "now" },
|
||||||
|
"panels": [
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "درخواستها بر ثانیه (هر روتر Traefik)",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "sum(rate(traefik_service_requests_total[5m])) by (service)",
|
||||||
|
"legendFormat": "{{ service }}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "خطاهای 5xx (بکاند)",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "sum(rate(http_request_duration_seconds_count{status=~\"5..\"}[5m]))",
|
||||||
|
"legendFormat": "5xx/s"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "تأخیر بکاند — p50 و p95",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 8 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "histogram_quantile(0.5, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))",
|
||||||
|
"legendFormat": "p50"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))",
|
||||||
|
"legendFormat": "p95"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "s" }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "stat",
|
||||||
|
"title": "سلامت /api/health (پروب عمق)",
|
||||||
|
"gridPos": { "h": 8, "w": 4, "x": 12, "y": 8 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{ "expr": "probe_success{job=\"blackbox-internal-health\"}", "legendFormat": "health" }
|
||||||
|
],
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"mappings": [
|
||||||
|
{ "type": "value", "options": { "1": { "text": "سالم", "color": "green" }, "0": { "text": "خراب", "color": "red" } } }
|
||||||
|
],
|
||||||
|
"min": 0,
|
||||||
|
"max": 1
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "stat",
|
||||||
|
"title": "تازگی آخرین پشتیبان (ثانیه)",
|
||||||
|
"gridPos": { "h": 8, "w": 4, "x": 16, "y": 8 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{ "expr": "time() - backup_success_time", "legendFormat": "age" }
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "s", "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null }, { "color": "orange", "value": 72000 }, { "color": "red", "value": 93600 } ] } }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "اتصالهای Postgres",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 16 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{ "expr": "sum(pg_stat_activity_count)", "legendFormat": "اتصالها" }
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "short" }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "logs",
|
||||||
|
"title": "لاگ زندهٔ سرویسها (Loki)",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 16 },
|
||||||
|
"datasource": { "type": "loki", "uid": "loki" },
|
||||||
|
"targets": [
|
||||||
|
{ "expr": "{service=~\"backend|traefik|chatbot|umami\"}" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
{
|
||||||
|
"uid": "aramesh-infra",
|
||||||
|
"title": "آرامش — زیرساخت (میزبان و کانتینرها)",
|
||||||
|
"tags": ["aramesh"],
|
||||||
|
"timezone": "browser",
|
||||||
|
"schemaVersion": 39,
|
||||||
|
"refresh": "30s",
|
||||||
|
"time": { "from": "now-6h", "to": "now" },
|
||||||
|
"panels": [
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "CPU میزبان",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "100 - (avg by (instance) (rate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
|
||||||
|
"legendFormat": "CPU %"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "percent", "max": 100 }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "حافظه میزبان",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{ "expr": "node_memory_MemTotal_bytes", "legendFormat": "کل" },
|
||||||
|
{ "expr": "node_memory_MemAvailable_bytes", "legendFormat": "آزاد" }
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "bytes" }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "دیسک — درصد پرشدگی",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 0, "y": 8 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "100 * (1 - (node_filesystem_avail_bytes{fstype!~\"tmpfs|overlay|squashfs\"} / node_filesystem_size_bytes{fstype!~\"tmpfs|overlay|squashfs\"}))",
|
||||||
|
"legendFormat": "{{ mountpoint }}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "percent", "max": 100 }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "مصرف RAM هر کانتینر",
|
||||||
|
"gridPos": { "h": 8, "w": 12, "x": 12, "y": 8 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "topk(10, container_memory_usage_bytes{image!=\"\", container!=\"\"})",
|
||||||
|
"legendFormat": "{{ container }}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "bytes" }, "overrides": [] }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "timeseries",
|
||||||
|
"title": "CPU هر کانتینر",
|
||||||
|
"gridPos": { "h": 8, "w": 24, "x": 0, "y": 16 },
|
||||||
|
"datasource": { "type": "prometheus", "uid": "prom" },
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "topk(10, rate(container_cpu_usage_seconds_total{image!=\"\", container!=\"\"}[5m]))",
|
||||||
|
"legendFormat": "{{ container }}"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"fieldConfig": { "defaults": { "unit": "percentunit" }, "overrides": [] }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
apiVersion: 1
|
||||||
|
|
||||||
|
providers:
|
||||||
|
- name: aramesh
|
||||||
|
folder: Aramesh
|
||||||
|
type: file
|
||||||
|
disableDeletion: false
|
||||||
|
updateIntervalSeconds: 30
|
||||||
|
options:
|
||||||
|
path: /var/lib/grafana/dashboards
|
||||||
|
foldersFromFilesStructure: false
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
apiVersion: 1
|
||||||
|
|
||||||
|
datasources:
|
||||||
|
- name: Prometheus
|
||||||
|
type: prometheus
|
||||||
|
uid: prom
|
||||||
|
access: proxy
|
||||||
|
url: http://prometheus:9090
|
||||||
|
isDefault: true
|
||||||
|
editable: false
|
||||||
|
|
||||||
|
- name: Loki
|
||||||
|
type: loki
|
||||||
|
uid: loki
|
||||||
|
access: proxy
|
||||||
|
url: http://loki:3100
|
||||||
|
editable: false
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# Loki — ذخیرهٔ لاگها روی دیسک (۱۴ روز)
|
||||||
|
auth_enabled: false
|
||||||
|
|
||||||
|
server:
|
||||||
|
http_listen_port: 3100
|
||||||
|
|
||||||
|
common:
|
||||||
|
instance_addr: 127.0.0.1
|
||||||
|
path_prefix: /loki
|
||||||
|
storage:
|
||||||
|
filesystem:
|
||||||
|
chunks_directory: /loki/chunks
|
||||||
|
rules_directory: /loki/rules
|
||||||
|
replication_factor: 1
|
||||||
|
ring:
|
||||||
|
kvstore:
|
||||||
|
store: inmemory
|
||||||
|
|
||||||
|
schema_config:
|
||||||
|
configs:
|
||||||
|
- from: 2024-01-01
|
||||||
|
store: tsdb
|
||||||
|
object_store: filesystem
|
||||||
|
schema: v13
|
||||||
|
index:
|
||||||
|
prefix: index_
|
||||||
|
period: 24h
|
||||||
|
|
||||||
|
limits_config:
|
||||||
|
retention_period: 336h
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
# قواعد هشدار — از Prometheus به Alertmanager (تلگرام) میروند.
|
||||||
|
# آستانهها محافظهکارانهاند؛ پس از یک هفته داده، دقیقتر تنظیم کنید.
|
||||||
|
|
||||||
|
groups:
|
||||||
|
- name: aramesh-availability
|
||||||
|
rules:
|
||||||
|
- alert: ServiceDown
|
||||||
|
# سرویسهای دارای متریک (backend/traefik/exporters) — بقیه با ProbeFailing پوشش میشوند
|
||||||
|
expr: up == 0
|
||||||
|
for: 2m
|
||||||
|
labels:
|
||||||
|
severity: critical
|
||||||
|
annotations:
|
||||||
|
summary: "{{ $labels.job }} پاسخ نمیدهد"
|
||||||
|
description: "سرویس {{ $labels.job }} ({{ $labels.instance }}) حداقل ۲ دقیقه پایین است."
|
||||||
|
|
||||||
|
- alert: ProbeFailing
|
||||||
|
expr: probe_success == 0
|
||||||
|
for: 2m
|
||||||
|
labels:
|
||||||
|
severity: critical
|
||||||
|
annotations:
|
||||||
|
summary: "پروب {{ $labels.job }} ناموفق است"
|
||||||
|
description: "سرویسِ پشت پروب {{ $labels.instance }} حداقل ۲ دقیقه پاسخ نمیدهد."
|
||||||
|
|
||||||
|
- alert: BackendHealthFailing
|
||||||
|
expr: probe_success{job="blackbox-internal-health"} == 0
|
||||||
|
for: 2m
|
||||||
|
labels:
|
||||||
|
severity: critical
|
||||||
|
annotations:
|
||||||
|
summary: "بررسی عمق سلامت بکاند شکست میخورد"
|
||||||
|
description: "/api/health اتصال DB یا MinIO را آزموده و 503 برگردانده — وابستگیها را ببینید."
|
||||||
|
|
||||||
|
- alert: PublicSiteDown
|
||||||
|
expr: probe_success{job="blackbox-public"} == 0
|
||||||
|
for: 2m
|
||||||
|
labels:
|
||||||
|
severity: critical
|
||||||
|
annotations:
|
||||||
|
summary: "سایت از مسیر عمومی در دسترس نیست"
|
||||||
|
description: "پروب {{ $labels.instance }} ناموفق است — Traefik/TLS/DNS را بررسی کنید."
|
||||||
|
|
||||||
|
- name: aramesh-resources
|
||||||
|
rules:
|
||||||
|
- alert: HostDiskAlmostFull
|
||||||
|
# Docker Desktop/WSL سوارهای تکراری میسازد — فقط مسیرهای واقعی دیده میشوند
|
||||||
|
expr: |
|
||||||
|
1 - (node_filesystem_avail_bytes{fstype!~"tmpfs|overlay|squashfs", mountpoint!~"/run/desktop.*|/parent-distro.*|/usr/lib/wsl.*|/mnt/wsl.*"} / node_filesystem_size_bytes{fstype!~"tmpfs|overlay|squashfs", mountpoint!~"/run/desktop.*|/parent-distro.*|/usr/lib/wsl.*|/mnt/wsl.*"}) > 0.85
|
||||||
|
for: 5m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "دیسک میزبان پر میشود"
|
||||||
|
description: "پرشدن بیش از ۸۵٪ روی {{ $labels.mountpoint }} — پشتیبانها و لاگها را بررسی کنید."
|
||||||
|
|
||||||
|
- alert: HostMemoryPressure
|
||||||
|
expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes < 0.1
|
||||||
|
for: 5m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "RAM میزبان رو به تمامشدن است"
|
||||||
|
description: "کمتر از ۱۰٪ حافظه باقی مانده است."
|
||||||
|
|
||||||
|
- alert: ContainerMemoryNearLimit
|
||||||
|
expr: |
|
||||||
|
(container_memory_usage_bytes{image!="", container!=""}
|
||||||
|
/ container_spec_memory_limit_bytes{image!="", container!=""} > 0.9)
|
||||||
|
and (container_spec_memory_limit_bytes{image!="", container!=""} > 0)
|
||||||
|
for: 10m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "کانتینر {{ $labels.container }} به سقف حافظه نزدیک است"
|
||||||
|
|
||||||
|
- name: aramesh-app
|
||||||
|
rules:
|
||||||
|
- alert: Http5xxRateHigh
|
||||||
|
expr: |
|
||||||
|
sum(rate(http_request_duration_seconds_count{status=~"5.."}[5m])) by (instance)
|
||||||
|
/ sum(rate(http_request_duration_seconds_count[5m])) by (instance) > 0.05
|
||||||
|
for: 5m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "نرخ خطای 5xx بکاند بالاست"
|
||||||
|
|
||||||
|
- alert: Proxy5xxRateHigh
|
||||||
|
expr: |
|
||||||
|
sum(rate(traefik_service_requests_total{code=~"5.."}[5m])) by (service)
|
||||||
|
/ sum(rate(traefik_service_requests_total[5m])) by (service) > 0.05
|
||||||
|
for: 5m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "نرخ خطای 5xx روی سرویس {{ $labels.service }} پشت Traefik بالاست"
|
||||||
|
|
||||||
|
- alert: HighBackendLatency
|
||||||
|
expr: histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le)) > 2
|
||||||
|
for: 10m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "p95 تأخیر بکاند بالای ۲ ثانیه است"
|
||||||
|
|
||||||
|
- alert: PostgresTooManyConnections
|
||||||
|
expr: sum(pg_stat_activity_count) > 80
|
||||||
|
for: 5m
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "تعداد اتصالهای Postgres نزدیک سقف است"
|
||||||
|
|
||||||
|
- alert: TlsCertExpiringSoon
|
||||||
|
expr: traefik_tls_certs_not_after - time() < 1209600
|
||||||
|
labels:
|
||||||
|
severity: warning
|
||||||
|
annotations:
|
||||||
|
summary: "گواهی TLS کمتر از ۱۴ روز اعتبار دارد"
|
||||||
|
description: "تمدید خودکار Let's Encrypt کار نکرده — ACME/TLS را بررسی کنید."
|
||||||
|
|
||||||
|
- alert: BackupStale
|
||||||
|
expr: time() - backup_success_time > 93600
|
||||||
|
for: 30m
|
||||||
|
labels:
|
||||||
|
severity: critical
|
||||||
|
annotations:
|
||||||
|
summary: "آخرین پشتیبان موفق بیش از ۲۶ ساعت پیش است"
|
||||||
|
description: "کرون/اسکریپت پشتیبانگیری یا سرویس db-backup خفته — فوراً بررسی کنید."
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
# تنظیمات Prometheus — اسکرپ همهٔ سرویسهای استک از شبکههای داخلی
|
||||||
|
global:
|
||||||
|
scrape_interval: 30s
|
||||||
|
evaluation_interval: 30s
|
||||||
|
|
||||||
|
alerting:
|
||||||
|
alertmanagers:
|
||||||
|
- static_configs:
|
||||||
|
- targets: ["alertmanager:9093"]
|
||||||
|
|
||||||
|
rule_files:
|
||||||
|
- /etc/prometheus/alerts.yml
|
||||||
|
|
||||||
|
scrape_configs:
|
||||||
|
- job_name: prometheus
|
||||||
|
static_configs:
|
||||||
|
- targets: ["localhost:9090"]
|
||||||
|
|
||||||
|
# بکاند FastAPI — /metrics با instrumentator (فقط از شبکه داخلی)
|
||||||
|
- job_name: backend
|
||||||
|
static_configs:
|
||||||
|
- targets: ["backend:8000"]
|
||||||
|
|
||||||
|
# پراکسی — RPS هر روتر، کدهای وضعیت، انقضای گواهی TLS
|
||||||
|
- job_name: traefik
|
||||||
|
static_configs:
|
||||||
|
- targets: ["traefik:8080"]
|
||||||
|
|
||||||
|
# منابع کانتینرها و میزبان
|
||||||
|
- job_name: cadvisor
|
||||||
|
static_configs:
|
||||||
|
- targets: ["cadvisor:8080"]
|
||||||
|
|
||||||
|
- job_name: node
|
||||||
|
static_configs:
|
||||||
|
- targets: ["node-exporter:9100"]
|
||||||
|
|
||||||
|
- job_name: postgres
|
||||||
|
static_configs:
|
||||||
|
- targets: ["postgres-exporter:9187"]
|
||||||
|
|
||||||
|
# سلامت سرویسهای بدون متریک — با پروب blackbox (پاسخ JSON/HTML قابل اسکرپ نیست)
|
||||||
|
# نتیجه: probe_success (۱ = سالم، ۰ = پایین) — هشدار ProbeFailing پوشش میدهد
|
||||||
|
- job_name: blackbox-frontend
|
||||||
|
metrics_path: /probe
|
||||||
|
params:
|
||||||
|
module: [http_2xx]
|
||||||
|
static_configs:
|
||||||
|
- targets:
|
||||||
|
- http://frontend/
|
||||||
|
relabel_configs: &blackbox_relabel
|
||||||
|
- source_labels: [__address__]
|
||||||
|
target_label: __param_target
|
||||||
|
- source_labels: [__param_target]
|
||||||
|
target_label: instance
|
||||||
|
- target_label: __address__
|
||||||
|
replacement: blackbox-exporter:9115
|
||||||
|
|
||||||
|
- job_name: blackbox-minio
|
||||||
|
metrics_path: /probe
|
||||||
|
params:
|
||||||
|
module: [http_2xx]
|
||||||
|
static_configs:
|
||||||
|
- targets:
|
||||||
|
- http://minio:9000/minio/health/live
|
||||||
|
relabel_configs: *blackbox_relabel
|
||||||
|
|
||||||
|
- job_name: blackbox-umami
|
||||||
|
metrics_path: /probe
|
||||||
|
params:
|
||||||
|
module: [http_2xx]
|
||||||
|
static_configs:
|
||||||
|
- targets:
|
||||||
|
- http://umami:3000/api/heartbeat
|
||||||
|
relabel_configs: *blackbox_relabel
|
||||||
|
|
||||||
|
- job_name: blackbox-chatbot
|
||||||
|
metrics_path: /probe
|
||||||
|
params:
|
||||||
|
module: [http_2xx]
|
||||||
|
static_configs:
|
||||||
|
- targets:
|
||||||
|
- http://chatbot:8000/chat/healthz
|
||||||
|
relabel_configs: *blackbox_relabel
|
||||||
|
|
||||||
|
# پروب بیروننگر — سلامت واقعی /api/health (بخش عمق: DB و MinIO را میآزماید)
|
||||||
|
# و در تولید، آدرس عمومی سایت (SITE_URL از .env — پشتیبانی متغیر محیطی Prometheus)
|
||||||
|
- job_name: blackbox-internal-health
|
||||||
|
metrics_path: /probe
|
||||||
|
params:
|
||||||
|
module: [http_2xx]
|
||||||
|
static_configs:
|
||||||
|
- targets:
|
||||||
|
- http://backend:8000/api/health
|
||||||
|
relabel_configs: *blackbox_relabel
|
||||||
|
|
||||||
|
- job_name: blackbox-public
|
||||||
|
metrics_path: /probe
|
||||||
|
params:
|
||||||
|
module: [http_2xx]
|
||||||
|
static_configs:
|
||||||
|
- targets:
|
||||||
|
# مقدار پیشفرض در docker-compose.monitoring.yml تنظیم میشود
|
||||||
|
# (Prometheus سینتکس ${VAR:-default} را پشتیبانی نمیکند)
|
||||||
|
- ${SITE_URL}
|
||||||
|
relabel_configs: *blackbox_relabel
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# Promtail — لاگ همهٔ کانتینرهای استک آرامش را از Docker socket میخواند
|
||||||
|
server:
|
||||||
|
http_listen_port: 9080
|
||||||
|
|
||||||
|
positions:
|
||||||
|
filename: /positions/positions.yaml
|
||||||
|
|
||||||
|
clients:
|
||||||
|
- url: http://loki:3100/loki/api/v1/push
|
||||||
|
|
||||||
|
scrape_configs:
|
||||||
|
- job_name: docker
|
||||||
|
docker_sd_configs:
|
||||||
|
- host: unix:///var/run/docker.sock
|
||||||
|
refresh_interval: 5s
|
||||||
|
# فقط کانتینرهای همین استک — نه بقیهٔ کانتینرهای میزبان
|
||||||
|
filters:
|
||||||
|
- name: label
|
||||||
|
values: ["com.docker.compose.project=aramesh-backend"]
|
||||||
|
relabel_configs:
|
||||||
|
- source_labels: ["__meta_docker_container_name"]
|
||||||
|
regex: "/(.*)"
|
||||||
|
target_label: container
|
||||||
|
- source_labels: ["__meta_docker_container_label_com_docker_compose_service"]
|
||||||
|
target_label: service
|
||||||
|
- source_labels: ["__meta_docker_container_log_stream"]
|
||||||
|
target_label: stream
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# ممیزی وابستگیها — آسیبپذیریهای شناختهشده در پکیجهای پایتون/نود و ایمیجهای داکر.
|
||||||
|
# bash scripts/audit.sh
|
||||||
|
# ابزارها اگر نصب نباشند رد میشوند (پیام هشدار)؛ خروجی nonzero یعنی مشکل پیدا شد.
|
||||||
|
|
||||||
|
set -uo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
STATUS=0
|
||||||
|
|
||||||
|
echo "== pip-audit (بکاند) =="
|
||||||
|
if command -v pip-audit >/dev/null 2>&1; then
|
||||||
|
(cd backend && pip-audit -r requirements.txt --progress-spinner off) || STATUS=1
|
||||||
|
else
|
||||||
|
echo "رد شد: pip-audit نصب نیست (pip install pip-audit)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== npm audit (فرانتاند) =="
|
||||||
|
if [ -d frontend/node_modules ]; then
|
||||||
|
(cd frontend && npm audit --omit=dev) || STATUS=1
|
||||||
|
else
|
||||||
|
echo "رد شد: frontend/node_modules نیست (npm install)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== trivy (ایمیجهای داکر) =="
|
||||||
|
if command -v trivy >/dev/null 2>&1; then
|
||||||
|
for img in aramesh-backend-backend aramesh-backend-frontend aramesh-backend-chatbot; do
|
||||||
|
if docker image inspect "$img" >/dev/null 2>&1; then
|
||||||
|
trivy image --severity HIGH,CRITICAL "$img" || STATUS=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo "رد شد: trivy نصب نیست"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$STATUS" -eq 0 ]; then
|
||||||
|
echo "✔ ممیزی تمام شد — مشکل شناختهشدهای پیدا نشد."
|
||||||
|
else
|
||||||
|
echo "✗ ممیزی مشکل پیدا کرد — خروجی بالا را بررسی کنید." >&2
|
||||||
|
fi
|
||||||
|
exit "$STATUS"
|
||||||
+32
-3
@@ -4,8 +4,12 @@
|
|||||||
# اجرا: bash scripts/backup.sh
|
# اجرا: bash scripts/backup.sh
|
||||||
# زمانبندی: cron (لینوکس) یا Task Scheduler (ویندوز)، مثلا هر شب ساعت ۳:
|
# زمانبندی: cron (لینوکس) یا Task Scheduler (ویندوز)، مثلا هر شب ساعت ۳:
|
||||||
# 0 3 * * * cd /path/to/aramesh-backend && bash scripts/backup.sh >> backups/backup.log 2>&1
|
# 0 3 * * * cd /path/to/aramesh-backend && bash scripts/backup.sh >> backups/backup.log 2>&1
|
||||||
|
# یا سرویس db-backup در پروفایل ops (پشتیبان ساعتی DB بدون cron میزبان).
|
||||||
#
|
#
|
||||||
# نگهداری: فایلهای قدیمیتر از RETENTION_DAYS (پیشفرض ۱۴) حذف میشوند.
|
# نگهداری: فایلهای قدیمیتر از RETENTION_DAYS (پیشفرض ۱۴) حذف میشوند.
|
||||||
|
# انتشار خارج از سرور: اگر OFFSITE_RCLONE_REMOTE تنظیم شده باشد (در .env، مثل
|
||||||
|
# OFFSITE_RCLONE_REMOTE=storage:aramesh-backups)، بعد از هر پشتیبان rclone copy انجام میشود.
|
||||||
|
# تازگی پشتیبان: backups/backup_freshness.prom برای هشدار «پشتیبان کهنه» در Grafana نوشته میشود.
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
cd "$(dirname "$0")/.."
|
cd "$(dirname "$0")/.."
|
||||||
@@ -14,7 +18,7 @@ RETENTION_DAYS="${RETENTION_DAYS:-14}"
|
|||||||
STAMP="$(date +%Y%m%d-%H%M%S)"
|
STAMP="$(date +%Y%m%d-%H%M%S)"
|
||||||
mkdir -p backups
|
mkdir -p backups
|
||||||
|
|
||||||
# متغیرهای .env (برای MINIO_ROOT_*) در دسترس قرار میگیرند
|
# متغیرهای .env (برای MINIO_ROOT_* و OFFSITE_RCLONE_REMOTE) در دسترس قرار میگیرند
|
||||||
if [ -f .env ]; then
|
if [ -f .env ]; then
|
||||||
set -a
|
set -a
|
||||||
# shellcheck disable=SC1091
|
# shellcheck disable=SC1091
|
||||||
@@ -26,7 +30,18 @@ echo "[backup] $STAMP"
|
|||||||
|
|
||||||
# ۱) دیتابیس — همه متنها، مقالهها و تنظیمات سایت
|
# ۱) دیتابیس — همه متنها، مقالهها و تنظیمات سایت
|
||||||
DB_FILE="backups/db-${STAMP}.sql.gz"
|
DB_FILE="backups/db-${STAMP}.sql.gz"
|
||||||
docker compose exec -T db pg_dump -U aramesh arameshplaza | gzip > "$DB_FILE"
|
# --exit-on-error: اگر pg_dump وسط کار خطا بگیرد، پشتیبان ناقص تولید نمیشود
|
||||||
|
docker compose exec -T db pg_dump --exit-on-error -U aramesh arameshplaza | gzip > "$DB_FILE"
|
||||||
|
|
||||||
|
# پشتیبان خالی/کوچک یعنی خرابی — زودتر بفهمیم تا «روز نیاز»
|
||||||
|
MIN_DB_BYTES=1024
|
||||||
|
DB_BYTES=$(wc -c < "$DB_FILE" | tr -d ' ')
|
||||||
|
if [ "$DB_BYTES" -lt "$MIN_DB_BYTES" ]; then
|
||||||
|
echo "[backup] ✗ پشتیبان دیتابیس شکسته است (${DB_BYTES} بایت)" >&2
|
||||||
|
rm -f "$DB_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sha256sum "$DB_FILE" > "${DB_FILE}.sha256"
|
||||||
echo "[backup] db: $DB_FILE ($(du -h "$DB_FILE" | cut -f1))"
|
echo "[backup] db: $DB_FILE ($(du -h "$DB_FILE" | cut -f1))"
|
||||||
|
|
||||||
# ۲) تصاویر MinIO — باکت aramesh-media
|
# ۲) تصاویر MinIO — باکت aramesh-media
|
||||||
@@ -36,9 +51,23 @@ docker compose run --rm -v "$(pwd)/backups:/backups" --entrypoint sh mc -c "
|
|||||||
"
|
"
|
||||||
echo "[backup] images: backups/images-${STAMP}"
|
echo "[backup] images: backups/images-${STAMP}"
|
||||||
|
|
||||||
# ۳) پاکسازی نسخههای قدیمیتر
|
# ۳) انتشار خارج از سرور (اختیاری) — هر شب کپی به حافظهٔ دیگری
|
||||||
|
if [ -n "${OFFSITE_RCLONE_REMOTE:-}" ]; then
|
||||||
|
if command -v rclone >/dev/null 2>&1; then
|
||||||
|
rclone copy backups "${OFFSITE_RCLONE_REMOTE}" --max-age 48h
|
||||||
|
echo "[backup] offsite: ${OFFSITE_RCLONE_REMOTE}"
|
||||||
|
else
|
||||||
|
echo "[backup] هشدار: OFFSITE_RCLONE_REMOTE تنظیم است اما rclone نصب نیست" >&2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# ۴) پاکسازی نسخههای قدیمیتر
|
||||||
find backups -name 'db-*.sql.gz' -mtime "+${RETENTION_DAYS}" -delete
|
find backups -name 'db-*.sql.gz' -mtime "+${RETENTION_DAYS}" -delete
|
||||||
|
find backups -name 'db-*.sql.gz.sha256' -mtime "+${RETENTION_DAYS}" -delete
|
||||||
find backups -maxdepth 1 -type d -name 'images-*' -mtime "+${RETENTION_DAYS}" -exec rm -rf {} +
|
find backups -maxdepth 1 -type d -name 'images-*' -mtime "+${RETENTION_DAYS}" -exec rm -rf {} +
|
||||||
echo "[backup] پاکسازی نسخههای قدیمیتر از ${RETENTION_DAYS} روز انجام شد"
|
echo "[backup] پاکسازی نسخههای قدیمیتر از ${RETENTION_DAYS} روز انجام شد"
|
||||||
|
|
||||||
|
# ۵) سنجهٔ تازگی پشتیبان — Prometheus آن را میخواند؛ کهنهشدن یعنی کرون خفتی است
|
||||||
|
printf 'backup_success_time %s\n' "$(date +%s)" > backups/backup_freshness.prom
|
||||||
|
|
||||||
echo "[backup] ✔ تمام شد"
|
echo "[backup] ✔ تمام شد"
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# تستها + بیلد ایمیجها — قابل اجرا محلی یا در هر CI (Gitea Actions/GitLab CI سازگار است)
|
||||||
|
# bash scripts/ci.sh
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
echo "== تستهای بکاند =="
|
||||||
|
if [ -d backend/.venv ]; then
|
||||||
|
# shellcheck disable=SC1091
|
||||||
|
source backend/.venv/bin/activate 2>/dev/null || source backend/.venv/Scripts/activate
|
||||||
|
(cd backend && pytest -q)
|
||||||
|
elif command -v pytest >/dev/null 2>&1; then
|
||||||
|
(cd backend && pytest -q)
|
||||||
|
else
|
||||||
|
echo "رد شد: pytest در دسترس نیست (backend/.venv را بسازید)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "== بیلد ایمیجهای داکر =="
|
||||||
|
docker compose -f docker-compose.yml build backend frontend chatbot
|
||||||
|
|
||||||
|
echo "== ممیزی وابستگیها =="
|
||||||
|
bash scripts/audit.sh || true
|
||||||
|
|
||||||
|
echo "✔ CI تمام شد"
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# ساخت .env با مقادیر قوی تصادفی — پیش از اولین استقرار تولید یکبار اجرا کنید.
|
||||||
|
#
|
||||||
|
# bash scripts/gen-env.sh # فایل .env میسازد (موجود را بازنویسی نمیکند)
|
||||||
|
# bash scripts/gen-env.sh --force # .env موجود را بازنویسی میکند (احتیاط!)
|
||||||
|
#
|
||||||
|
# توجه: تغییر SECRET_KEY همه ادمینها را از پنل خارج میکند؛ تغییر MINIO_ROOT_PASSWORD
|
||||||
|
# فقط با همگامسازی S3_SECRET_KEY بکاند اعتبار دارد.
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
if [ -f .env ] && [ "${1:-}" != "--force" ]; then
|
||||||
|
echo "خطا: .env از قبل موجود است. برای بازنویسی: bash scripts/gen-env.sh --force" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
rand() { openssl rand -"$1" 2>/dev/null || head -c 64 /dev/urandom | od -An -tx1 | tr -d ' \n'; }
|
||||||
|
|
||||||
|
SECRET_KEY="$(rand hex 48)"
|
||||||
|
POSTGRES_PASSWORD="$(rand hex 16)"
|
||||||
|
MINIO_ROOT_PASSWORD="$(rand hex 16)"
|
||||||
|
ADMIN_PASSWORD="$(rand hex 12)"
|
||||||
|
|
||||||
|
cat > .env <<EOF
|
||||||
|
# تولیدشده با scripts/gen-env.sh — این فایل هرگز به گیت نمیرود.
|
||||||
|
# پیش از انتشار واقعی، ADMIN_PASSWORD را به مقداری که به یاد میسپارید اما قوی است تغییر دهید:
|
||||||
|
# docker compose exec backend python -c "from app.auth import hash_password; print(hash_password('رمز-جدید'))"
|
||||||
|
# سپس در پنل مدیریت از تغییر رمز (change-password) استفاده کنید.
|
||||||
|
SECRET_KEY=${SECRET_KEY}
|
||||||
|
POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
|
||||||
|
MINIO_ROOT_USER=aramesh
|
||||||
|
MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD}
|
||||||
|
ADMIN_USERNAME=admin
|
||||||
|
ADMIN_PASSWORD=${ADMIN_PASSWORD}
|
||||||
|
TRAEFIK_HTTP_PORT=18081
|
||||||
|
SITE_URL=
|
||||||
|
# --- فقط تولید ---
|
||||||
|
# DOMAIN=example.com
|
||||||
|
# ACME_EMAIL=you@example.com
|
||||||
|
# --- تحلیل رفتار (Umami) ---
|
||||||
|
# VITE_API_URL=
|
||||||
|
# VITE_UMAMI_SRC=
|
||||||
|
# VITE_UMAMI_WEBSITE_ID=
|
||||||
|
# --- چتبات ---
|
||||||
|
# LLM_BASE_URL=
|
||||||
|
# LLM_API_KEY=
|
||||||
|
# LLM_MODEL=openai/gpt-oss-120b
|
||||||
|
# CHAT_REINDEX_TOKEN=
|
||||||
|
# --- مانیتورینگ ---
|
||||||
|
# GRAFANA_ADMIN_PASSWORD=
|
||||||
|
# ALERT_BOT_TOKEN=
|
||||||
|
# ALERT_CHAT_ID=
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "✔ .env ساخته شد با مقادیر قوی تصادفی."
|
||||||
|
echo " ADMIN_PASSWORD فعلی: ${ADMIN_PASSWORD}"
|
||||||
|
echo " (آن را امن نگه دارید — پس از ورود به پنل، رمز را عوض کنید.)"
|
||||||
@@ -27,11 +27,23 @@ import uuid
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
|
# اگر .env ریشه هست، متغیرهایش (ADMIN_PASSWORD و ...) خوانده میشوند
|
||||||
|
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
_ENV_FILE = os.path.join(_REPO_ROOT, ".env")
|
||||||
|
if os.path.exists(_ENV_FILE):
|
||||||
|
with open(_ENV_FILE, encoding="utf-8") as _f:
|
||||||
|
for _line in _f:
|
||||||
|
_line = _line.strip()
|
||||||
|
if _line and not _line.startswith("#") and "=" in _line:
|
||||||
|
_k, _, _v = _line.partition("=")
|
||||||
|
os.environ.setdefault(_k.strip(), _v.strip())
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
API = os.environ.get("VITE_API_URL", "http://localhost:5173")
|
API = os.environ.get("VITE_API_URL", "http://localhost:5173")
|
||||||
USER = os.environ.get("ADMIN_USERNAME", "admin")
|
USER = os.environ.get("ADMIN_USERNAME", "admin")
|
||||||
PASS = os.environ.get("ADMIN_PASSWORD", "aramesh123")
|
# رمز پیشفرض ندارد — از .env یا متغیر محیطی خوانده میشود؛ نبودش یعنی توقف با خطا
|
||||||
|
PASS = os.environ["ADMIN_PASSWORD"]
|
||||||
FORCE = "--force" in sys.argv
|
FORCE = "--force" in sys.argv
|
||||||
|
|
||||||
print(f"API بکاند: {API}")
|
print(f"API بکاند: {API}")
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# پیشنمایش خصوصی و انتشار عمومی سایت.
|
||||||
|
#
|
||||||
|
# bash scripts/preview-mode.sh status # وضعیت فعلی
|
||||||
|
# bash scripts/preview-mode.sh on # سایت آنلاین ولی خصوصی (گیت ورود + noindex)
|
||||||
|
# bash scripts/preview-mode.sh off # انتشار عمومی (حذف گیت + اجازه ایندکس)
|
||||||
|
#
|
||||||
|
# در حالت «on» کل سایت پشت Basic Auth است: تیم با نام کاربری/رمز مشترک وارد میشود،
|
||||||
|
# محتوا را در همان سایت آنلاین ویرایش میکند (ویرایش درجای ایمیل/تلفن و بقیه محتوا)،
|
||||||
|
# اما نه عموم به سایت دسترسی دارند و نه گوگل چیزی ایندکس میکند.
|
||||||
|
#
|
||||||
|
# پس از هر تغییر، استک را دوباره بالا بیاورید:
|
||||||
|
# bash scripts/stack.sh deploy-prod
|
||||||
|
#
|
||||||
|
# با «فعال شدن» اولین بار، نام کاربری/رمز ساخته و در .env ذخیره میشود
|
||||||
|
# (پیشفرض: preview / یک رمز تصادفی — در خروجی چاپ میشود).
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
ENV_FILE=.env
|
||||||
|
[ -f "$ENV_FILE" ] || { echo "فایل .env پیدا نشد — اول bash scripts/gen-env.sh" >&2; exit 1; }
|
||||||
|
|
||||||
|
# مقدار فعلی یک کلید در .env (یا خالی)
|
||||||
|
env_get() {
|
||||||
|
local key="$1"
|
||||||
|
sed -n "s/^${key}=//p" "$ENV_FILE" | tail -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# تنظیم/جایگزینی یک کلید در .env (بدون آسیب به بقیه خطوط)
|
||||||
|
env_set() {
|
||||||
|
local key="$1" value="$2"
|
||||||
|
if grep -q "^${key}=" "$ENV_FILE"; then
|
||||||
|
# جداکننده | تا کاراکترهای ویژه مقدار (مثل / و $) مشکلی نسازند
|
||||||
|
python - "$ENV_FILE" "$key" "$value" <<'PY'
|
||||||
|
import io, sys
|
||||||
|
path, key, value = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||||
|
lines = io.open(path, encoding="utf-8").read().split("\n")
|
||||||
|
out = [f"{key}={value}" if line.startswith(f"{key}=") else line for line in lines]
|
||||||
|
io.open(path, "w", encoding="utf-8", newline="\n").write("\n".join(out))
|
||||||
|
PY
|
||||||
|
else
|
||||||
|
printf '\n%s=%s\n' "$key" "$value" >> "$ENV_FILE"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# ساخت هش htpasswd — اول با openssl، در نبودش با ایمیج httpd (Docker لازم است)
|
||||||
|
make_htpasswd() {
|
||||||
|
local user="$1" pass="$2" hash
|
||||||
|
if command -v openssl >/dev/null 2>&1; then
|
||||||
|
hash=$(openssl passwd -apr1 "$pass")
|
||||||
|
else
|
||||||
|
hash=$(docker run --rm httpd:2.4-alpine htpasswd -nbB "$user" "$pass" | cut -d: -f2)
|
||||||
|
fi
|
||||||
|
printf '%s:%s' "$user" "$hash"
|
||||||
|
}
|
||||||
|
|
||||||
|
# رمز تصادفی خوانا (بدون نویسههای گیجکننده)
|
||||||
|
random_password() {
|
||||||
|
python - <<'PY'
|
||||||
|
import secrets, string
|
||||||
|
alphabet = string.ascii_letters + string.digits
|
||||||
|
print("".join(secrets.choice(alphabet) for _ in range(16)))
|
||||||
|
PY
|
||||||
|
}
|
||||||
|
|
||||||
|
PREVIEW_MIDDLEWARES_VALUE="preview-auth@docker,preview-noindex@docker"
|
||||||
|
|
||||||
|
case "${1:-status}" in
|
||||||
|
on)
|
||||||
|
users="$(env_get PREVIEW_AUTH_USERS)"
|
||||||
|
if [ -z "$users" ]; then
|
||||||
|
password="$(random_password)"
|
||||||
|
users="$(make_htpasswd preview "$password")"
|
||||||
|
env_set PREVIEW_AUTH_USERS "$users"
|
||||||
|
echo "نام کاربری/رمز پیشنمایش ساخته شد:"
|
||||||
|
echo " کاربر: preview"
|
||||||
|
echo " رمز: $password"
|
||||||
|
echo " (این رمز فقط همین یکبار چاپ میشود — در .env ذخیره شده است)"
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
env_set PREVIEW_MODE on
|
||||||
|
env_set PREVIEW_MIDDLEWARES "$PREVIEW_MIDDLEWARES_VALUE"
|
||||||
|
# در پیشنمایش، دامنه دوم به اصلی ریدایرکت نمیشود تا هر دو قابل بررسی باشند
|
||||||
|
env_set ALIAS_REDIRECT_MIDDLEWARES ""
|
||||||
|
echo "پیشنمایش خصوصی فعال شد — سایت آنلاین است، ورود با نام کاربری/رمز، بدون ایندکس."
|
||||||
|
echo "برای اعمال: bash scripts/stack.sh deploy-prod"
|
||||||
|
;;
|
||||||
|
|
||||||
|
off)
|
||||||
|
env_set PREVIEW_MODE off
|
||||||
|
env_set PREVIEW_MIDDLEWARES ""
|
||||||
|
# در انتشار، دامنه دوم به اصلی ریدایرکت میشود تا محتوای تکراری ایجاد نشود
|
||||||
|
env_set ALIAS_REDIRECT_MIDDLEWARES "to-primary@docker"
|
||||||
|
echo "حالت انتشار عمومی فعال شد — گیت ورود حذف و ایندکس مجاز میشود."
|
||||||
|
echo "پس از deploy، نقشه سایت را در Search Console ثبت کنید:"
|
||||||
|
echo " https://\$(grep '^DOMAIN=' .env | cut -d= -f2)/sitemap.xml"
|
||||||
|
echo "برای اعمال: bash scripts/stack.sh deploy-prod"
|
||||||
|
;;
|
||||||
|
|
||||||
|
status)
|
||||||
|
mode="$(env_get PREVIEW_MODE)"
|
||||||
|
mws="$(env_get PREVIEW_MIDDLEWARES)"
|
||||||
|
alias_mws="$(env_get ALIAS_REDIRECT_MIDDLEWARES)"
|
||||||
|
if [ "$mode" = "on" ]; then
|
||||||
|
echo "وضعیت: پیشنمایش خصوصی (آنلاین، غیرعمومی، noindex)"
|
||||||
|
elif [ "$mode" = "off" ]; then
|
||||||
|
echo "وضعیت: انتشار عمومی"
|
||||||
|
else
|
||||||
|
echo "وضعیت: تعییننشده (PREVIEW_MODE=${mode:-خالی}) — پیشفرض compose: off"
|
||||||
|
fi
|
||||||
|
echo " PREVIEW_MIDDLEWARES=${mws:-(خالی — بدون گیت)}"
|
||||||
|
echo " ALIAS_REDIRECT_MIDDLEWARES=${alias_mws:-(خالی — بدون ریدایرکت دامنه دوم)}"
|
||||||
|
if [ -n "$(env_get PREVIEW_AUTH_USERS)" ]; then
|
||||||
|
echo " کاربر پیشنمایش: $(env_get PREVIEW_AUTH_USERS | cut -d: -f1)"
|
||||||
|
else
|
||||||
|
echo " کاربر پیشنمایش: ساخته نشده (اولین «on» میسازد)"
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
echo "استفاده: bash scripts/preview-mode.sh [on|off|status]" >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# بازگردانی پشتیبان دیتابیس — از بازگردانی در محیط واقعی قبلش تمرین کنید!
|
||||||
|
#
|
||||||
|
# bash scripts/restore.sh backups/db-20260918-030000.sql.gz
|
||||||
|
#
|
||||||
|
# احتیاط: محتوای فعلی دیتابیس با محتوای پشتیبان جایگزین میشود.
|
||||||
|
# تصاویر MinIO هم اگر پوشه images-* همان زمان را دارید:
|
||||||
|
# docker compose run --rm -v "$(pwd)/backups:/backups" --entrypoint sh mc -c \
|
||||||
|
# "mc alias set local http://minio:9000 \"\$MINIO_ROOT_USER\" \"\$MINIO_ROOT_PASSWORD\" && \
|
||||||
|
# mc mirror --overwrite /backups/images-20260918-030000 local/aramesh-media"
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "استفاده: bash scripts/restore.sh backups/db-<زمان>.sql.gz" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
SQL_GZ="$1"
|
||||||
|
if [ ! -f "$SQL_GZ" ]; then
|
||||||
|
echo "خطا: فایل ${SQL_GZ} پیدا نشد" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# راستیآزمایی چکسام اگر کنار فایل هست
|
||||||
|
if [ -f "${SQL_GZ}.sha256" ]; then
|
||||||
|
sha256sum -c "${SQL_GZ}.sha256"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "[restore] هشدار: محتوای فعلی arameshplaza جایگزین میشود. ۵ ثانیه فرصت لغو (Ctrl+C)…"
|
||||||
|
sleep 5
|
||||||
|
|
||||||
|
gunzip -c "$SQL_GZ" | docker compose exec -T db psql -v ON_ERROR_STOP=1 -U aramesh -d arameshplaza
|
||||||
|
|
||||||
|
echo "[restore] ✔ بازگردانی تمام شد — حالا: docker compose restart backend"
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# ابزار orchestration استک — یک نقطه ورود برای همه عملیات رایج.
|
||||||
|
#
|
||||||
|
# bash scripts/stack.sh deploy # بیلد فرانتاند (روی هاست) + بیلد و بالا آوردن استک
|
||||||
|
# bash scripts/stack.sh deploy-prod # همین، با لایه TLS/دامنه (docker-compose.prod.yml)
|
||||||
|
# bash scripts/stack.sh logs [svc] # دنبالکردن لاگ (همه یا یک سرویس)
|
||||||
|
# bash scripts/stack.sh ps # وضعیت سرویسها
|
||||||
|
# bash scripts/stack.sh health # وضعیت healthcheck همه سرویسها
|
||||||
|
# bash scripts/stack.sh backup # پشتیبانگیری کامل (scripts/backup.sh)
|
||||||
|
# bash scripts/stack.sh restore FILE # بازگردانی دیتابیس (scripts/restore.sh)
|
||||||
|
# bash scripts/stack.sh update # کشیدن ایمیجهای پینشده و بالا آوردن مجدد
|
||||||
|
# bash scripts/stack.sh monitoring # لایه مانیتورینگ (Prometheus/Grafana/Loki/بله)
|
||||||
|
# bash scripts/stack.sh seed-force # seed مجدد محتوا (--force — دادههای جدولها ریست میشود)
|
||||||
|
#
|
||||||
|
# متغیرهای بیلد فرانتاند (VITE_API_URL، VITE_UMAMI_SRC، VITE_UMAMI_WEBSITE_ID)
|
||||||
|
# از .env ریشه خوانده و به npm run build پاس میشوند — متغیرهای Vite در زمان بیلد
|
||||||
|
# روی هاست معنا دارند، نه در ایمیج Caddy.
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
DEV_FILES=(-f docker-compose.yml)
|
||||||
|
[ -f docker-compose.override.yml ] && DEV_FILES+=(-f docker-compose.override.yml)
|
||||||
|
|
||||||
|
# compose dev ... | compose prod ... — انتخاب فایلهای لایهای
|
||||||
|
compose() {
|
||||||
|
local mode="$1"
|
||||||
|
shift
|
||||||
|
if [ "$mode" = "prod" ]; then
|
||||||
|
docker compose -f docker-compose.yml -f docker-compose.prod.yml "$@"
|
||||||
|
else
|
||||||
|
docker compose "${DEV_FILES[@]}" "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
load_env_vars() {
|
||||||
|
# فقط متغیرهای بیلد فرانتاند — بدون خروجی
|
||||||
|
if [ -f .env ]; then
|
||||||
|
set -a
|
||||||
|
# shellcheck disable=SC1091
|
||||||
|
. ./.env
|
||||||
|
set +a
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
render_alertmanager() {
|
||||||
|
# قالب alertmanager.yml با مقادیر .env رندر میشود — Alertmanager متغیر محیطی
|
||||||
|
# را در کانفیگ جایگزین نمیکند؛ خروجی (rendered) در گیت نمیآید.
|
||||||
|
load_env_vars
|
||||||
|
local tpl="monitoring/alertmanager/alertmanager.yml"
|
||||||
|
local out="monitoring/alertmanager/alertmanager.rendered.yml"
|
||||||
|
local bot="${ALERT_BOT_TOKEN:-0000000000:PLACEHOLDER-TOKEN}"
|
||||||
|
local chat="${ALERT_CHAT_ID:-0}"
|
||||||
|
sed -e "s|\${ALERT_BOT_TOKEN}|${bot}|g" \
|
||||||
|
-e "s|\${ALERT_CHAT_ID}|${chat}|g" \
|
||||||
|
"$tpl" > "$out"
|
||||||
|
if [ -n "${ALERT_BOT_TOKEN:-}" ]; then
|
||||||
|
echo "[stack] alertmanager config rendered (bale token set)"
|
||||||
|
else
|
||||||
|
echo "[stack] alertmanager config rendered (no bale token — alerts will not deliver)"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
build_frontend() {
|
||||||
|
load_env_vars
|
||||||
|
echo "[stack] بیلد فرانتاند روی هاست…"
|
||||||
|
(cd frontend \
|
||||||
|
&& VITE_API_URL="${VITE_API_URL:-}" \
|
||||||
|
VITE_UMAMI_SRC="${VITE_UMAMI_SRC:-}" \
|
||||||
|
VITE_UMAMI_WEBSITE_ID="${VITE_UMAMI_WEBSITE_ID:-}" \
|
||||||
|
npm run build)
|
||||||
|
if [ ! -f frontend/dist/index.html ]; then
|
||||||
|
echo "[stack] ✗ بیلد فرانتاند خروجی نداد (frontend/dist)" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_healthy() {
|
||||||
|
local mode="$1"
|
||||||
|
echo "[stack] منتظر سلامت سرویسها (حداکثر ۱۸۰ ثانیه)…"
|
||||||
|
local deadline=$((SECONDS + 180))
|
||||||
|
local unhealthy=""
|
||||||
|
while [ $SECONDS -lt $deadline ]; do
|
||||||
|
unhealthy=$(compose "$mode" ps --format '{{.Service}} {{.Health}}' 2>/dev/null \
|
||||||
|
| awk '$2=="starting" || $2=="unhealthy" {print $1":"$2}')
|
||||||
|
if [ -z "$unhealthy" ]; then
|
||||||
|
echo "[stack] ✔ همه سرویسها سالماند"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
sleep 5
|
||||||
|
done
|
||||||
|
echo "[stack] ✗ برخی سرویسها سالم نشدند: ${unhealthy:-نامشخص}" >&2
|
||||||
|
compose "$mode" ps
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
do_deploy() {
|
||||||
|
local mode="$1"
|
||||||
|
build_frontend
|
||||||
|
echo "[stack] بیلد و بالا آوردن استک…"
|
||||||
|
compose "$mode" up -d --build
|
||||||
|
wait_healthy "$mode"
|
||||||
|
docker system prune -f >/dev/null 2>&1 || true
|
||||||
|
echo "[stack] ✔ استقرار تمام شد"
|
||||||
|
if [ -z "${VITE_UMAMI_WEBSITE_ID:-}" ]; then
|
||||||
|
echo "[stack] یادآوری: VITE_UMAMI_WEBSITE_ID در .env خالی است — تحلیل رفتار غیرفعال میماند."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
case "${1:-}" in
|
||||||
|
deploy)
|
||||||
|
do_deploy dev
|
||||||
|
;;
|
||||||
|
deploy-prod)
|
||||||
|
load_env_vars
|
||||||
|
if [ -z "${DOMAIN:-}" ] || [ -z "${ACME_EMAIL:-}" ]; then
|
||||||
|
echo "[stack] ✗ برای deploy-prod، DOMAIN و ACME_EMAIL باید در .env تنظیم باشند" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
do_deploy prod
|
||||||
|
;;
|
||||||
|
logs)
|
||||||
|
shift || true
|
||||||
|
compose dev logs -f --tail=100 "$@"
|
||||||
|
;;
|
||||||
|
ps)
|
||||||
|
compose dev ps
|
||||||
|
;;
|
||||||
|
health)
|
||||||
|
compose dev ps --format '{{.Service}}: {{.Status}}'
|
||||||
|
;;
|
||||||
|
backup)
|
||||||
|
bash scripts/backup.sh
|
||||||
|
;;
|
||||||
|
restore)
|
||||||
|
shift
|
||||||
|
bash scripts/restore.sh "$@"
|
||||||
|
;;
|
||||||
|
update)
|
||||||
|
compose dev pull
|
||||||
|
compose dev up -d
|
||||||
|
wait_healthy dev
|
||||||
|
;;
|
||||||
|
monitoring)
|
||||||
|
# لایه مانیتورینگ — Prometheus/Grafana/Loki/Alertmanager (+ رندر کانفیگ بله)
|
||||||
|
render_alertmanager
|
||||||
|
echo "[stack] بالا آوردن لایه مانیتورینگ…"
|
||||||
|
docker compose "${DEV_FILES[@]}" -f docker-compose.monitoring.yml up -d --build
|
||||||
|
wait_healthy dev
|
||||||
|
echo "[stack] ✔ مانیتورینگ بالا آمد — داشبورد: http://grafana.localhost:${TRAEFIK_HTTP_PORT:-5173}"
|
||||||
|
;;
|
||||||
|
seed-force)
|
||||||
|
compose dev run --rm backend python -m app.seed --force
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
grep '^# ' "$0" | sed 's/^# //'
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
Binary file not shown.
Reference in New Issue
Block a user