ditto/src/config.ts
2023-08-14 14:40:30 -05:00

76 lines
2 KiB
TypeScript

import { dotenv, nip19, secp } from '@/deps.ts';
/** Load environment config from `.env` */
await dotenv.load({
export: true,
defaultsPath: null,
examplePath: null,
});
/** Application-wide configuration. */
const Conf = {
get nsec() {
const value = Deno.env.get('DITTO_NSEC');
if (!value) {
throw new Error('Missing DITTO_NSEC');
}
if (!value.startsWith('nsec1')) {
throw new Error('Invalid DITTO_NSEC');
}
return value as `nsec1${string}`;
},
get seckey() {
const result = nip19.decode(Conf.nsec);
if (result.type !== 'nsec') {
throw new Error('Invalid DITTO_NSEC');
}
return result.data;
},
get cryptoKey() {
return crypto.subtle.importKey(
'raw',
secp.etc.hexToBytes(Conf.seckey),
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign', 'verify'],
);
},
get relay(): `wss://${string}` | `ws://${string}` {
const { protocol, host } = Conf.url;
return `${protocol === 'https:' ? 'wss:' : 'ws:'}//${host}/relay`;
},
get localDomain() {
return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
},
get dbPath() {
return Deno.env.get('DB_PATH') || 'data/db.sqlite3';
},
get postCharLimit() {
return Number(Deno.env.get('POST_CHAR_LIMIT') || 5000);
},
get adminEmail() {
return Deno.env.get('ADMIN_EMAIL') || 'webmaster@localhost';
},
get poolRelays() {
return (Deno.env.get('RELAY_POOL') || '').split(',').filter(Boolean);
},
get publishRelays() {
return ['wss://relay.mostr.pub'];
},
get url() {
return new URL(Conf.localDomain);
},
/** Merges the path with the localDomain. */
local(path: string): string {
if (path.startsWith('/')) {
// Path is a path.
return new URL(path, Conf.localDomain).toString();
} else {
// Path is possibly a full URL. Replace the domain.
const { pathname } = new URL(path);
return new URL(pathname, Conf.localDomain).toString();
}
},
};
export { Conf };