diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..64140e7 --- /dev/null +++ b/.env.example @@ -0,0 +1,36 @@ +# EmDash Environment Variables +# Copy this file to .env and fill in your values + +# =========================================== +# DATABASE (choose one) +# =========================================== + +# SQLite (default - local file) +# DATABASE_URL=file:./storage/data.db + +# PostgreSQL +# DATABASE_URL=postgresql://user:password@host:5432/emdash + +# MySQL +# DATABASE_URL=mysql://user:password@host:3306/emdash + +# =========================================== +# SESSION SECRET (required for production) +# =========================================== +# Generate with: openssl rand -base64 32 +# SESSION_SECRET=your-secret-key-here + +# =========================================== +# S3 STORAGE (optional - for cloud storage) +# =========================================== +# AWS S3 +# S3_BUCKET=my-uploads +# S3_REGION=us-east-1 +# AWS_ACCESS_KEY_ID=xxx +# AWS_SECRET_ACCESS_KEY=xxx + +# Cloudflare R2 +# R2_ACCOUNT_ID=xxx +# R2_ACCESS_KEY_ID=xxx +# R2_SECRET_ACCESS_KEY=xxx +# R2_BUCKET=my-uploads diff --git a/astro.config.mjs b/astro.config.mjs index 448765e..8cdbbdd 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -3,7 +3,7 @@ import react from "@astrojs/react"; import { auditLogPlugin } from "@emdash-cms/plugin-audit-log"; import { defineConfig, fontProviders } from "astro/config"; import emdash, { local } from "emdash/astro"; -import { sqlite } from "emdash/db"; +import { getDatabaseConfig } from "./src/lib/db.ts"; export default defineConfig({ output: "server", @@ -17,7 +17,7 @@ export default defineConfig({ integrations: [ react(), emdash({ - database: sqlite({ url: "file:./storage/data.db" }), + database: getDatabaseConfig(), storage: local({ directory: "./storage/uploads", baseUrl: "/_emdash/api/media/file", diff --git a/src/lib/db.ts b/src/lib/db.ts new file mode 100644 index 0000000..96e7b2b --- /dev/null +++ b/src/lib/db.ts @@ -0,0 +1,37 @@ +import { sqlite, postgres, mysql } from "emdash/db"; + +/** + * Database configuration from environment variables. + * + * Supports: + * - SQLite: DATABASE_URL=file:./storage/data.db + * - PostgreSQL: DATABASE_URL=postgresql://user:pass@host:5432/db + * - MySQL: DATABASE_URL=mysql://user:pass@host:3306/db + */ +export function getDatabaseConfig() { + const url = process.env.DATABASE_URL; + + if (!url) { + // Default to SQLite + return sqlite({ url: "file:./storage/data.db" }); + } + + if (url.startsWith("file:")) { + // SQLite + return sqlite({ url: url }); + } + + if (url.startsWith("postgresql:")) { + // PostgreSQL + return postgres({ connectionString: url }); + } + + if (url.startsWith("mysql:")) { + // MySQL + return mysql({ connectionString: url }); + } + + // Fallback to SQLite + console.warn(`Unknown DATABASE_URL protocol, falling back to SQLite`); + return sqlite({ url: "file:./storage/data.db" }); +}