Skip to content

An AI-powered search engine with a generative UI

Require remote object storage for uploads in Docker

Yoshiki Miura3149cafee380
diff --git a/.env.local.example b/.env.local.example
index be7c0481..f90546e4 100644
--- a/.env.local.example
+++ b/.env.local.example
@@ -111,14 +111,16 @@ ANONYMOUS_USER_ID=anonymous-user
# MORPHIC_CLOUD_DEPLOYMENT=true
# -----------------------------------------------------------------------------
-# File Upload Support (Cloudflare R2)
+# File Upload Support (Cloudflare R2 / S3-Compatible)
# Enable file and image upload functionality
# -----------------------------------------------------------------------------
+# If not configured, file upload is disabled and /api/upload returns 400.
# R2_ACCOUNT_ID=[YOUR_R2_ACCOUNT_ID]
+# S3_ENDPOINT=[YOUR_S3_ENDPOINT] # Optional: generic S3-compatible endpoint
# R2_ACCESS_KEY_ID=[YOUR_R2_ACCESS_KEY_ID]
# R2_SECRET_ACCESS_KEY=[YOUR_R2_SECRET_ACCESS_KEY]
# R2_BUCKET_NAME=user-uploads # Default: user-uploads
-# R2_PUBLIC_URL=[YOUR_R2_PUBLIC_URL] # e.g., https://pub-xxx.r2.dev
+# R2_PUBLIC_URL=[YOUR_R2_PUBLIC_URL] # Publicly reachable base URL (e.g., https://pub-xxx.r2.dev)
# -----------------------------------------------------------------------------
# Alternative Search Providers
diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts
index 8f344296..fdbf7954 100644
--- a/app/api/upload/route.ts
+++ b/app/api/upload/route.ts
@@ -14,6 +14,17 @@ const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'application/pdf']
export async function POST(req: NextRequest) {
try {
+ if (!isObjectStorageConfigured()) {
+ return NextResponse.json(
+ {
+ error: 'File upload storage is not configured',
+ message:
+ 'Set R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_PUBLIC_URL, and either R2_ACCOUNT_ID or S3_ENDPOINT.'
+ },
+ { status: 400 }
+ )
+ }
+
const userId = await getCurrentUserId()
if (!userId) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
@@ -79,7 +90,7 @@ async function uploadFileToR2(file: File, userId: string, chatId: string) {
})
)
- const publicUrl = `${R2_PUBLIC_URL}/${filePath}`
+ const publicUrl = `${R2_PUBLIC_URL.replace(/\/+$/, '')}/${filePath}`
return {
filename: file.name,
@@ -91,3 +102,13 @@ async function uploadFileToR2(file: File, userId: string, chatId: string) {
throw new Error('Upload failed: ' + error.message)
}
}
+
+function isObjectStorageConfigured() {
+ const hasCredentials =
+ !!process.env.R2_ACCESS_KEY_ID && !!process.env.R2_SECRET_ACCESS_KEY
+ const hasEndpointOrAccount =
+ !!process.env.S3_ENDPOINT || !!process.env.R2_ACCOUNT_ID
+ const hasPublicUrl = !!R2_PUBLIC_URL
+
+ return hasCredentials && hasEndpointOrAccount && hasPublicUrl
+}
diff --git a/docker-compose.yaml b/docker-compose.yaml
index ed20046e..738ed0a8 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -19,6 +19,11 @@ services:
# Authentication settings (default: disabled for personal Docker use)
ENABLE_AUTH: ${ENABLE_AUTH:-false}
ANONYMOUS_USER_ID: ${ANONYMOUS_USER_ID:-docker-anonymous}
+ MORPHIC_CLOUD_DEPLOYMENT: 'false'
+ # Docker-internal service URLs
+ LOCAL_REDIS_URL: ${LOCAL_REDIS_URL:-redis://redis:6379}
+ SEARCH_API: ${SEARCH_API:-searxng}
+ SEARXNG_API_URL: ${SEARXNG_API_URL:-http://searxng:8080}
ports:
- '3000:3000' # Maps port 3000 on the host to port 3000 in the container.
depends_on:
diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md
index f5523764..4e7054be 100644
--- a/docs/CONFIGURATION.md
+++ b/docs/CONFIGURATION.md
@@ -353,15 +353,19 @@ LANGFUSE_HOST=https://cloud.langfuse.com
### File Upload
-Enable file upload with Cloudflare R2:
+Enable file upload with Cloudflare R2 or S3-compatible storage:
```bash
-CLOUDFLARE_R2_ACCESS_KEY_ID=[YOUR_ACCESS_KEY]
-CLOUDFLARE_R2_SECRET_ACCESS_KEY=[YOUR_SECRET_KEY]
-CLOUDFLARE_R2_ACCOUNT_ID=[YOUR_ACCOUNT_ID]
-CLOUDFLARE_R2_BUCKET_NAME=[YOUR_BUCKET_NAME]
+R2_ACCESS_KEY_ID=[YOUR_ACCESS_KEY]
+R2_SECRET_ACCESS_KEY=[YOUR_SECRET_KEY]
+R2_ACCOUNT_ID=[YOUR_ACCOUNT_ID] # For Cloudflare R2
+# S3_ENDPOINT=[YOUR_S3_ENDPOINT] # Optional: generic S3-compatible endpoint
+R2_BUCKET_NAME=[YOUR_BUCKET_NAME]
+R2_PUBLIC_URL=[YOUR_PUBLIC_BASE_URL]
```
+If storage variables are not configured, `/api/upload` returns `400` and uploads are disabled.
+
### Alternative Fetch Tool
Use Jina for enhanced content extraction:
diff --git a/docs/DOCKER.md b/docs/DOCKER.md
index a261fc95..11bde118 100644
--- a/docs/DOCKER.md
+++ b/docs/DOCKER.md
@@ -15,10 +15,11 @@ Edit `.env.local` and set the required variables:
```bash
DATABASE_URL=postgresql://morphic:morphic@postgres:5432/morphic
OPENAI_API_KEY=your_openai_key
-TAVILY_API_KEY=your_tavily_key
-BRAVE_SEARCH_API_KEY=your_brave_key
```
+If you switch `SEARCH_API` to `tavily`, set `TAVILY_API_KEY`.
+`BRAVE_SEARCH_API_KEY` is optional and only needed when using Brave as a dedicated general-search provider.
+
**Note**: Authentication is disabled by default (`ENABLE_AUTH=false` in `.env.local.example`).
**Optional**: Customize PostgreSQL credentials by setting environment variables in `.env.local`:
@@ -43,10 +44,35 @@ The application will:
- Wait for the database to be ready
- Run database migrations automatically
- Start the Morphic application
-- Start SearXNG (optional search provider)
+- Start SearXNG and connect Morphic to it automatically
3. Visit http://localhost:3000 in your browser.
+### Built-in Docker Defaults
+
+When running with `docker compose up -d`, Morphic is automatically configured to use internal Docker services:
+
+- `LOCAL_REDIS_URL=redis://redis:6379`
+- `SEARCH_API=searxng`
+- `SEARXNG_API_URL=http://searxng:8080`
+- `MORPHIC_CLOUD_DEPLOYMENT=false`
+
+Important precedence note:
+
+- Values defined in `docker-compose.yaml` under `services.morphic.environment` take precedence over `.env.local` (`env_file`) values.
+- To override a default from compose, export the variable before startup. Example:
+ - `SEARCH_API=tavily docker compose up -d`
+ - `SEARXNG_API_URL=http://custom-searxng:8080 docker compose up -d`
+
+### File Upload in Docker
+
+File upload requires externally reachable object storage. Docker Compose does not provision storage by default.
+
+- Required: `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_PUBLIC_URL`
+- Required (one of): `R2_ACCOUNT_ID` (Cloudflare R2) or `S3_ENDPOINT` (generic S3-compatible endpoint)
+
+If these are not set, `/api/upload` returns `400` and upload is disabled.
+
**Note**: Database data is persisted in a Docker volume. To reset the database, run:
```bash
diff --git a/lib/storage/r2-client.ts b/lib/storage/r2-client.ts
index 8289d3c3..e1c2c79b 100644
--- a/lib/storage/r2-client.ts
+++ b/lib/storage/r2-client.ts
@@ -10,17 +10,25 @@ export function getR2Client(): S3Client {
return _r2Client
}
+ const s3Endpoint = process.env.S3_ENDPOINT?.replace(/\/+$/, '')
const accountId = process.env.R2_ACCOUNT_ID
const accessKeyId = process.env.R2_ACCESS_KEY_ID
const secretAccessKey = process.env.R2_SECRET_ACCESS_KEY
- if (!accountId || !accessKeyId || !secretAccessKey) {
- throw new Error('R2 configuration missing')
+ if (!accessKeyId || !secretAccessKey) {
+ throw new Error('S3 configuration missing: R2_ACCESS_KEY_ID or R2_SECRET_ACCESS_KEY')
+ }
+
+ if (!s3Endpoint && !accountId) {
+ throw new Error(
+ 'S3 configuration missing: set S3_ENDPOINT (generic S3) or R2_ACCOUNT_ID (Cloudflare R2)'
+ )
}
_r2Client = new S3Client({
region: 'auto',
- endpoint: `https://${accountId}.r2.cloudflarestorage.com`,
+ endpoint: s3Endpoint || `https://${accountId}.r2.cloudflarestorage.com`,
+ forcePathStyle: !!s3Endpoint,
credentials: {
accessKeyId,
secretAccessKey