mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {envInt, envList, envStr} from '@atproto/common'
2
3export type Config = {
4 service: ServiceConfig
5 db: DbConfig
6}
7
8export type ServiceConfig = {
9 port: number
10 version?: string
11 hostnames: string[]
12 appHostname: string
13}
14
15export type DbConfig = {
16 url: string
17 migrationUrl?: string
18 pool: DbPoolConfig
19 schema?: string
20}
21
22export type DbPoolConfig = {
23 size: number
24 maxUses: number
25 idleTimeoutMs: number
26}
27
28export type Environment = {
29 port?: number
30 version?: string
31 hostnames: string[]
32 appHostname?: string
33 dbPostgresUrl?: string
34 dbPostgresMigrationUrl?: string
35 dbPostgresSchema?: string
36 dbPostgresPoolSize?: number
37 dbPostgresPoolMaxUses?: number
38 dbPostgresPoolIdleTimeoutMs?: number
39}
40
41export const readEnv = (): Environment => {
42 return {
43 port: envInt('LINK_PORT'),
44 version: envStr('LINK_VERSION'),
45 hostnames: envList('LINK_HOSTNAMES'),
46 appHostname: envStr('LINK_APP_HOSTNAME'),
47 dbPostgresUrl: envStr('LINK_DB_POSTGRES_URL'),
48 dbPostgresMigrationUrl: envStr('LINK_DB_POSTGRES_MIGRATION_URL'),
49 dbPostgresSchema: envStr('LINK_DB_POSTGRES_SCHEMA'),
50 dbPostgresPoolSize: envInt('LINK_DB_POSTGRES_POOL_SIZE'),
51 dbPostgresPoolMaxUses: envInt('LINK_DB_POSTGRES_POOL_MAX_USES'),
52 dbPostgresPoolIdleTimeoutMs: envInt(
53 'LINK_DB_POSTGRES_POOL_IDLE_TIMEOUT_MS',
54 ),
55 }
56}
57
58export const envToCfg = (env: Environment): Config => {
59 const serviceCfg: ServiceConfig = {
60 port: env.port ?? 3000,
61 version: env.version,
62 hostnames: env.hostnames,
63 appHostname: env.appHostname || 'bsky.app',
64 }
65 if (!env.dbPostgresUrl) {
66 throw new Error('Must configure postgres url (LINK_DB_POSTGRES_URL)')
67 }
68 const dbCfg: DbConfig = {
69 url: env.dbPostgresUrl,
70 migrationUrl: env.dbPostgresMigrationUrl,
71 schema: env.dbPostgresSchema,
72 pool: {
73 idleTimeoutMs: env.dbPostgresPoolIdleTimeoutMs ?? 10000,
74 maxUses: env.dbPostgresPoolMaxUses ?? Infinity,
75 size: env.dbPostgresPoolSize ?? 10,
76 },
77 }
78 return {
79 service: serviceCfg,
80 db: dbCfg,
81 }
82}