Merge branch 'database-url' into 'main'

Introduce DATABASE_URL

See merge request soapbox-pub/ditto!164
This commit is contained in:
Alex Gleason 2024-04-19 03:14:53 +00:00
commit 6555675ef3
2 changed files with 34 additions and 2 deletions

View File

@ -5,7 +5,7 @@
"start": "deno run -A --unstable-ffi src/server.ts", "start": "deno run -A --unstable-ffi src/server.ts",
"dev": "deno run -A --unstable-ffi --watch src/server.ts", "dev": "deno run -A --unstable-ffi --watch src/server.ts",
"debug": "deno run -A --unstable-ffi --inspect src/server.ts", "debug": "deno run -A --unstable-ffi --inspect src/server.ts",
"test": "DB_PATH=\":memory:\" deno test -A --unstable-ffi", "test": "DATABASE_URL=\"sqlite://:memory:\" deno test -A --unstable-ffi",
"check": "deno check src/server.ts", "check": "deno check src/server.ts",
"relays:sync": "deno run -A --unstable-ffi scripts/relays.ts sync", "relays:sync": "deno run -A --unstable-ffi scripts/relays.ts sync",
"nsec": "deno run scripts/nsec.ts" "nsec": "deno run scripts/nsec.ts"

View File

@ -1,3 +1,5 @@
import url from 'node:url';
import { dotenv, getPublicKey, nip19, z } from '@/deps.ts'; import { dotenv, getPublicKey, nip19, z } from '@/deps.ts';
/** Load environment config from `.env` */ /** Load environment config from `.env` */
@ -54,7 +56,37 @@ class Conf {
} }
/** Path to the main SQLite database which stores users, events, and more. */ /** Path to the main SQLite database which stores users, events, and more. */
static get dbPath() { static get dbPath() {
return Deno.env.get('DB_PATH') || 'data/db.sqlite3'; if (Deno.env.get('DATABASE_URL') === 'sqlite://:memory:') {
return ':memory:';
}
const { host, pathname } = Conf.databaseUrl;
if (!pathname) return '';
// Get relative path.
if (host === '') {
return pathname;
} else if (host === '.') {
return pathname;
} else if (host) {
return host + pathname;
}
return '';
}
/**
* Heroku-style database URL. This is used in production to connect to the
* database.
*
* Follows the format:
*
* ```txt
* protocol://username:password@host:port/database_name
* ```
*/
static get databaseUrl(): url.UrlWithStringQuery {
return url.parse(Deno.env.get('DATABASE_URL') ?? 'sqlite://data/db.sqlite3');
} }
/** Character limit to enforce for posts made through Mastodon API. */ /** Character limit to enforce for posts made through Mastodon API. */
static get postCharLimit() { static get postCharLimit() {