move folder and add supavisor
This commit is contained in:
358
02-supabase/README.md
Normal file
358
02-supabase/README.md
Normal file
@@ -0,0 +1,358 @@
|
||||
# Supabase - Backend as a Service
|
||||
|
||||
Supabase เป็น open-source Firebase alternative ที่ให้บริการ:
|
||||
- PostgreSQL Database พร้อม Realtime subscriptions
|
||||
- Authentication & Authorization
|
||||
- RESTful API (PostgREST)
|
||||
- Storage สำหรับไฟล์
|
||||
- Edge Functions
|
||||
- Studio UI สำหรับจัดการ
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
### 1. Setup Configuration Files
|
||||
|
||||
```bash
|
||||
cd 02-supabase
|
||||
bash setup.sh
|
||||
```
|
||||
|
||||
Script นี้จะ:
|
||||
- สร้าง directories ที่จำเป็น
|
||||
- ดาวน์โหลด config files จาก Supabase repository
|
||||
- สร้าง `.env` file จาก template
|
||||
|
||||
### 2. Configure Environment Variables
|
||||
|
||||
แก้ไขไฟล์ `.env`:
|
||||
|
||||
```bash
|
||||
nano .env
|
||||
```
|
||||
|
||||
**สิ่งที่ต้องแก้ไข:**
|
||||
|
||||
```bash
|
||||
# Generate secure passwords and secrets
|
||||
POSTGRES_PASSWORD=<your-secure-password>
|
||||
JWT_SECRET=$(openssl rand -base64 32)
|
||||
SECRET_KEY_BASE=$(openssl rand -base64 32)
|
||||
VAULT_ENC_KEY=$(openssl rand -base64 32)
|
||||
PG_META_CRYPTO_KEY=$(openssl rand -base64 32)
|
||||
LOGFLARE_PUBLIC_ACCESS_TOKEN=$(openssl rand -base64 32)
|
||||
LOGFLARE_PRIVATE_ACCESS_TOKEN=$(openssl rand -base64 32)
|
||||
|
||||
# Dashboard credentials
|
||||
DASHBOARD_USERNAME=admin
|
||||
DASHBOARD_PASSWORD=<your-secure-password>
|
||||
|
||||
# Update URLs if needed
|
||||
SUPABASE_PUBLIC_URL=http://localhost:8100
|
||||
SITE_URL=http://localhost:3010
|
||||
```
|
||||
|
||||
### 3. Start Supabase
|
||||
|
||||
```bash
|
||||
# Ensure network exists
|
||||
cd ../00-network
|
||||
bash create-network.sh
|
||||
|
||||
# Start Supabase
|
||||
cd ../02-supabase
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
### 4. Verify Services
|
||||
|
||||
```bash
|
||||
docker compose ps
|
||||
```
|
||||
|
||||
คุณควรเห็น containers:
|
||||
- `supabase-studio` - Web UI
|
||||
- `supabase-kong` - API Gateway
|
||||
- `supabase-auth` - Authentication service
|
||||
- `supabase-rest` - PostgREST API
|
||||
- `supabase-realtime` - Realtime subscriptions
|
||||
- `supabase-storage` - File storage
|
||||
- `supabase-db` - PostgreSQL database
|
||||
- `supabase-meta` - Database metadata
|
||||
- `supabase-analytics` - Logflare analytics
|
||||
- และอื่นๆ
|
||||
|
||||
## 🔑 Access Points
|
||||
|
||||
| Service | URL | Port | Description |
|
||||
|---------|-----|------|-------------|
|
||||
| **Studio** | http://localhost:3010 | 3010 | Web UI สำหรับจัดการ |
|
||||
| **API Gateway** | http://localhost:8100 | 8100 | REST API endpoint |
|
||||
| **PostgreSQL** | localhost:5434 | 5434 | Database (internal) |
|
||||
| **Pooler** | localhost:6544 | 6544 | Connection pooler |
|
||||
|
||||
## 📝 Port Configuration
|
||||
|
||||
Supabase ใช้ port ที่ไม่ชนกับ services อื่นใน stack:
|
||||
|
||||
- **3010**: Studio (แทน default 3000)
|
||||
- **8100**: Kong HTTP (แทน default 8000)
|
||||
- **8443**: Kong HTTPS
|
||||
- **5434**: PostgreSQL (แทน default 5432, เพราะ 5435 ใช้โดย main PostgreSQL)
|
||||
- **6544**: Pooler (แทน default 6543)
|
||||
|
||||
## 🔐 Authentication
|
||||
|
||||
### Default API Keys
|
||||
|
||||
ใน `.env` จะมี API keys 2 ประเภท:
|
||||
|
||||
1. **ANON_KEY** - ใช้สำหรับ client-side (public)
|
||||
2. **SERVICE_ROLE_KEY** - ใช้สำหรับ server-side (private, bypass RLS)
|
||||
|
||||
**⚠️ สำคัญ:** ใน production ต้อง generate keys ใหม่ด้วย Supabase CLI:
|
||||
|
||||
```bash
|
||||
# Install Supabase CLI
|
||||
npm install -g supabase
|
||||
|
||||
# Generate new JWT secrets
|
||||
supabase gen keys jwt
|
||||
```
|
||||
|
||||
### Dashboard Access
|
||||
|
||||
เข้า Studio ที่ http://localhost:3010 แล้วใช้:
|
||||
- Username: `DASHBOARD_USERNAME` จาก `.env`
|
||||
- Password: `DASHBOARD_PASSWORD` จาก `.env`
|
||||
|
||||
## 📊 Database Management
|
||||
|
||||
### Connect to PostgreSQL
|
||||
|
||||
```bash
|
||||
# Using psql
|
||||
psql -h localhost -p 5434 -U postgres -d postgres
|
||||
|
||||
# Using connection string
|
||||
postgresql://postgres:<POSTGRES_PASSWORD>@localhost:5434/postgres
|
||||
```
|
||||
|
||||
### Database Roles
|
||||
|
||||
Supabase สร้าง roles หลายตัวอัตโนมัติ:
|
||||
- `postgres` - Superuser
|
||||
- `authenticator` - สำหรับ PostgREST
|
||||
- `anon` - Anonymous access
|
||||
- `authenticated` - Authenticated users
|
||||
- `service_role` - Service role (bypass RLS)
|
||||
- `supabase_admin` - Admin operations
|
||||
- `supabase_auth_admin` - Auth service
|
||||
- `supabase_storage_admin` - Storage service
|
||||
|
||||
## 🔄 Realtime Subscriptions
|
||||
|
||||
Supabase Realtime ให้ subscribe การเปลี่ยนแปลงใน database:
|
||||
|
||||
```javascript
|
||||
import { createClient } from '@supabase/supabase-js'
|
||||
|
||||
const supabase = createClient(
|
||||
'http://localhost:8100',
|
||||
'YOUR_ANON_KEY'
|
||||
)
|
||||
|
||||
// Subscribe to changes
|
||||
const channel = supabase
|
||||
.channel('table-changes')
|
||||
.on('postgres_changes',
|
||||
{ event: '*', schema: 'public', table: 'your_table' },
|
||||
(payload) => console.log(payload)
|
||||
)
|
||||
.subscribe()
|
||||
```
|
||||
|
||||
## 📦 Storage
|
||||
|
||||
### Upload Files
|
||||
|
||||
```javascript
|
||||
const { data, error } = await supabase
|
||||
.storage
|
||||
.from('bucket-name')
|
||||
.upload('file-path', file)
|
||||
```
|
||||
|
||||
### Download Files
|
||||
|
||||
```javascript
|
||||
const { data, error } = await supabase
|
||||
.storage
|
||||
.from('bucket-name')
|
||||
.download('file-path')
|
||||
```
|
||||
|
||||
## 🔧 Edge Functions
|
||||
|
||||
Edge Functions อยู่ใน `volumes/functions/`:
|
||||
|
||||
```bash
|
||||
# Create new function
|
||||
mkdir -p volumes/functions/hello
|
||||
cat > volumes/functions/hello/index.ts << 'EOF'
|
||||
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
|
||||
|
||||
serve(async (req) => {
|
||||
return new Response(
|
||||
JSON.stringify({ message: "Hello from Supabase Edge Functions!" }),
|
||||
{ headers: { "Content-Type": "application/json" } },
|
||||
)
|
||||
})
|
||||
EOF
|
||||
|
||||
# Restart functions service
|
||||
docker compose restart functions
|
||||
```
|
||||
|
||||
## 🔍 Monitoring & Logs
|
||||
|
||||
### View Logs
|
||||
|
||||
```bash
|
||||
# All services
|
||||
docker compose logs -f
|
||||
|
||||
# Specific service
|
||||
docker compose logs -f studio
|
||||
docker compose logs -f auth
|
||||
docker compose logs -f db
|
||||
```
|
||||
|
||||
### Analytics
|
||||
|
||||
Supabase Analytics (Logflare) รวบรวม logs และ metrics ที่:
|
||||
- http://localhost:3010 (ใน Studio UI)
|
||||
|
||||
## 🛠️ Maintenance
|
||||
|
||||
### Backup Database
|
||||
|
||||
```bash
|
||||
# Backup
|
||||
docker exec supabase-db pg_dump -U postgres postgres > backup_$(date +%Y%m%d).sql
|
||||
|
||||
# Restore
|
||||
docker exec -i supabase-db psql -U postgres postgres < backup_20260218.sql
|
||||
```
|
||||
|
||||
### Update Supabase
|
||||
|
||||
```bash
|
||||
# Pull latest images
|
||||
docker compose pull
|
||||
|
||||
# Restart services
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
### Reset Everything
|
||||
|
||||
```bash
|
||||
# Stop and remove containers
|
||||
docker compose down
|
||||
|
||||
# Remove volumes (⚠️ This deletes all data!)
|
||||
docker compose down -v
|
||||
rm -rf volumes/db/data
|
||||
|
||||
# Start fresh
|
||||
bash setup.sh
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## 🔗 Integration with Other Services
|
||||
|
||||
### Connect from API Service (FastAPI)
|
||||
|
||||
```python
|
||||
from supabase import create_client, Client
|
||||
|
||||
supabase: Client = create_client(
|
||||
"http://supabase-kong:8000", # Internal network
|
||||
"YOUR_SERVICE_ROLE_KEY"
|
||||
)
|
||||
|
||||
# Query data
|
||||
response = supabase.table('users').select("*").execute()
|
||||
```
|
||||
|
||||
### Connect via Nginx Proxy Manager
|
||||
|
||||
เพิ่ม Proxy Host:
|
||||
- Domain: `supabase.sriphat.local`
|
||||
- Forward Hostname: `supabase-kong`
|
||||
- Forward Port: `8000`
|
||||
|
||||
## 📚 Documentation
|
||||
|
||||
- [Supabase Official Docs](https://supabase.com/docs)
|
||||
- [PostgREST API Reference](https://postgrest.org/)
|
||||
- [Supabase JavaScript Client](https://supabase.com/docs/reference/javascript/introduction)
|
||||
- [Self-Hosting Guide](https://supabase.com/docs/guides/self-hosting)
|
||||
|
||||
## 🐛 Troubleshooting
|
||||
|
||||
### Services not starting
|
||||
|
||||
```bash
|
||||
# Check logs
|
||||
docker compose logs
|
||||
|
||||
# Check network
|
||||
docker network inspect shared_data_network
|
||||
|
||||
# Verify .env file
|
||||
cat .env | grep -v '^#' | grep -v '^$'
|
||||
```
|
||||
|
||||
### Database connection issues
|
||||
|
||||
```bash
|
||||
# Check database is ready
|
||||
docker exec supabase-db pg_isready -U postgres
|
||||
|
||||
# Check database logs
|
||||
docker compose logs db
|
||||
```
|
||||
|
||||
### Port conflicts
|
||||
|
||||
ถ้า port ชน ให้แก้ไขใน `.env`:
|
||||
|
||||
```bash
|
||||
STUDIO_PORT=3011
|
||||
KONG_HTTP_PORT=8101
|
||||
```
|
||||
|
||||
แล้ว restart:
|
||||
|
||||
```bash
|
||||
docker compose down
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## 🔒 Security Best Practices
|
||||
|
||||
1. **เปลี่ยน default passwords** ทั้งหมดใน `.env`
|
||||
2. **Generate JWT secrets ใหม่** สำหรับ production
|
||||
3. **Enable Row Level Security (RLS)** สำหรับทุก table
|
||||
4. **ใช้ HTTPS** ใน production (ผ่าน Nginx Proxy Manager)
|
||||
5. **Backup database** เป็นประจำ
|
||||
6. **Monitor logs** สำหรับ suspicious activities
|
||||
7. **Update images** เป็นประจำเพื่อ security patches
|
||||
|
||||
## 📞 Support
|
||||
|
||||
หากมีปัญหาหรือคำถาม:
|
||||
1. ตรวจสอบ logs: `docker compose logs`
|
||||
2. ดู [Supabase Discussions](https://github.com/supabase/supabase/discussions)
|
||||
3. อ่าน [Self-Hosting Troubleshooting](https://supabase.com/docs/guides/self-hosting/docker#troubleshooting)
|
||||
Reference in New Issue
Block a user