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