Merge branch 'config-class' into 'main'

Make Conf a static class, cache pubkey

See merge request soapbox-pub/ditto!125
This commit is contained in:
Alex Gleason 2024-03-31 04:12:55 +00:00
commit 1549398f83
1 changed files with 41 additions and 43 deletions

View File

@ -8,9 +8,9 @@ await dotenv.load({
}); });
/** Application-wide configuration. */ /** Application-wide configuration. */
const Conf = { class Conf {
/** Ditto admin secret key in nip19 format. This is the way it's configured by an admin. */ /** Ditto admin secret key in nip19 format. This is the way it's configured by an admin. */
get nsec() { static get nsec() {
const value = Deno.env.get('DITTO_NSEC'); const value = Deno.env.get('DITTO_NSEC');
if (!value) { if (!value) {
throw new Error('Missing DITTO_NSEC'); throw new Error('Missing DITTO_NSEC');
@ -19,17 +19,15 @@ const Conf = {
throw new Error('Invalid DITTO_NSEC'); throw new Error('Invalid DITTO_NSEC');
} }
return value as `nsec1${string}`; return value as `nsec1${string}`;
}, }
/** Ditto admin secret key in hex format. */ /** Ditto admin secret key in hex format. */
get seckey() { static get seckey() {
return nip19.decode(Conf.nsec).data; return nip19.decode(Conf.nsec).data;
}, }
/** Ditto admin public key in hex format. */ /** Ditto admin public key in hex format. */
get pubkey() { static pubkey = getPublicKey(Conf.seckey);
return getPublicKey(Conf.seckey);
},
/** Ditto admin secret key as a Web Crypto key. */ /** Ditto admin secret key as a Web Crypto key. */
get cryptoKey() { static get cryptoKey() {
return crypto.subtle.importKey( return crypto.subtle.importKey(
'raw', 'raw',
Conf.seckey, Conf.seckey,
@ -37,29 +35,29 @@ const Conf = {
false, false,
['sign', 'verify'], ['sign', 'verify'],
); );
}, }
get relay(): `wss://${string}` | `ws://${string}` { static get relay(): `wss://${string}` | `ws://${string}` {
const { protocol, host } = Conf.url; const { protocol, host } = Conf.url;
return `${protocol === 'https:' ? 'wss:' : 'ws:'}//${host}/relay`; return `${protocol === 'https:' ? 'wss:' : 'ws:'}//${host}/relay`;
}, }
/** Relay to use for NIP-50 `search` queries. */ /** Relay to use for NIP-50 `search` queries. */
get searchRelay() { static get searchRelay() {
return Deno.env.get('SEARCH_RELAY'); return Deno.env.get('SEARCH_RELAY');
}, }
/** Origin of the Ditto server, including the protocol and port. */ /** Origin of the Ditto server, including the protocol and port. */
get localDomain() { static get localDomain() {
return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000'; return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
}, }
/** Path to the main SQLite database which stores users, events, and more. */ /** Path to the main SQLite database which stores users, events, and more. */
get dbPath() { static get dbPath() {
return Deno.env.get('DB_PATH') || 'data/db.sqlite3'; return Deno.env.get('DB_PATH') || 'data/db.sqlite3';
}, }
/** Character limit to enforce for posts made through Mastodon API. */ /** Character limit to enforce for posts made through Mastodon API. */
get postCharLimit() { static get postCharLimit() {
return Number(Deno.env.get('POST_CHAR_LIMIT') || 5000); return Number(Deno.env.get('POST_CHAR_LIMIT') || 5000);
}, }
/** S3 media storage configuration. */ /** S3 media storage configuration. */
s3: { static s3 = {
get endPoint() { get endPoint() {
return Deno.env.get('S3_ENDPOINT')!; return Deno.env.get('S3_ENDPOINT')!;
}, },
@ -87,20 +85,20 @@ const Conf = {
get useSSL() { get useSSL() {
return optionalBooleanSchema.parse(Deno.env.get('S3_USE_SSL')); return optionalBooleanSchema.parse(Deno.env.get('S3_USE_SSL'));
}, },
}, };
/** IPFS uploader configuration. */ /** IPFS uploader configuration. */
ipfs: { static ipfs = {
/** Base URL for private IPFS API calls. */ /** Base URL for private IPFS API calls. */
get apiUrl() { get apiUrl() {
return Deno.env.get('IPFS_API_URL') || 'http://localhost:5001'; return Deno.env.get('IPFS_API_URL') || 'http://localhost:5001';
}, },
}, };
/** Module to upload files with. */ /** Module to upload files with. */
get uploader() { static get uploader() {
return Deno.env.get('DITTO_UPLOADER'); return Deno.env.get('DITTO_UPLOADER');
}, }
/** Media base URL for uploads. */ /** Media base URL for uploads. */
get mediaDomain() { static get mediaDomain() {
const value = Deno.env.get('MEDIA_DOMAIN'); const value = Deno.env.get('MEDIA_DOMAIN');
if (!value) { if (!value) {
@ -110,13 +108,13 @@ const Conf = {
} }
return value; return value;
}, }
/** Max upload size for files in number of bytes. Default 100MiB. */ /** Max upload size for files in number of bytes. Default 100MiB. */
get maxUploadSize() { static get maxUploadSize() {
return Number(Deno.env.get('MAX_UPLOAD_SIZE') || 100 * 1024 * 1024); return Number(Deno.env.get('MAX_UPLOAD_SIZE') || 100 * 1024 * 1024);
}, }
/** Usernames that regular users cannot sign up with. */ /** Usernames that regular users cannot sign up with. */
get forbiddenUsernames() { static get forbiddenUsernames() {
return Deno.env.get('FORBIDDEN_USERNAMES')?.split(',') || [ return Deno.env.get('FORBIDDEN_USERNAMES')?.split(',') || [
'_', '_',
'admin', 'admin',
@ -125,27 +123,27 @@ const Conf = {
'sysadmin', 'sysadmin',
'system', 'system',
]; ];
}, }
/** Proof-of-work configuration. */ /** Proof-of-work configuration. */
pow: { static pow = {
get registrations() { get registrations() {
return Number(Deno.env.get('DITTO_POW_REGISTRATIONS') ?? 20); return Number(Deno.env.get('DITTO_POW_REGISTRATIONS') ?? 20);
}, },
}, };
/** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */ /** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */
get url() { static get url() {
return new URL(Conf.localDomain); return new URL(Conf.localDomain);
}, }
/** Merges the path with the localDomain. */ /** Merges the path with the localDomain. */
local(path: string): string { static local(path: string): string {
return mergePaths(Conf.localDomain, path); return mergePaths(Conf.localDomain, path);
}, }
/** URL to send Sentry errors to. */ /** URL to send Sentry errors to. */
get sentryDsn() { static get sentryDsn() {
return Deno.env.get('SENTRY_DSN'); return Deno.env.get('SENTRY_DSN');
}, }
/** SQLite settings. */ /** SQLite settings. */
sqlite: { static sqlite = {
/** /**
* Number of bytes to use for memory-mapped IO. * Number of bytes to use for memory-mapped IO.
* https://www.sqlite.org/pragma.html#pragma_mmap_size * https://www.sqlite.org/pragma.html#pragma_mmap_size
@ -158,8 +156,8 @@ const Conf = {
return 1024 * 1024 * 1024; return 1024 * 1024 * 1024;
} }
}, },
}, };
}; }
const optionalBooleanSchema = z const optionalBooleanSchema = z
.enum(['true', 'false']) .enum(['true', 'false'])