1- import dotenv from 'dotenv' ;
1+ import dotenv from 'dotenv'
22
33export interface Config {
4- GithubToken : string ;
5- GitlabToken : string ;
6-
7- PostgresHost : string ;
8- PostgresPort : number ;
9- PostgresDBName : string ;
10- PostgresUsername : string ;
11- PostgresPassword : string ;
12- PostgresUrl : string ;
13-
14- RedisHost : string ;
15- RedisPort : number ;
16- RedisUsername : string ;
17- RedisPassword : string ;
18- RedisDB : number ; // This is the Redis database index, not a URL.
19- RedisUrl : string ;
20-
21- BatchSize : number ;
22- BatchDelayMs : number ;
4+ GithubToken : string
5+ GitlabToken : string
6+
7+ PostgresHost : string
8+ PostgresPort : number
9+ PostgresDBName : string
10+ PostgresUsername : string
11+ PostgresPassword : string
12+ PostgresUrl : string
13+
14+ RedisHost : string
15+ RedisPort : number
16+ RedisUsername : string
17+ RedisPassword : string
18+ RedisDB : number // This is the Redis database index, not a URL.
19+ RedisUrl : string
20+
21+ BatchSize : number
2322}
2423
25- // These environment variables are required for the application to function correctly. The remaining ones should have
26- // sensible defaults.
24+ // These environment variables are required for the application to function correctly.
25+ // The remaining ones should have sensible defaults.
2726const requiredEnvVars = [
2827 'ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN' ,
2928 'ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN' ,
30-
29+
3130 'CROWD_DB_WRITE_HOST' ,
3231 'CROWD_DB_READ_HOST' ,
3332 'CROWD_DB_PORT' ,
3433 'CROWD_DB_USERNAME' ,
3534 'CROWD_DB_PASSWORD' ,
3635 'CROWD_DB_DATABASE' ,
37-
36+
3837 'CROWD_REDIS_USERNAME' ,
3938 'CROWD_REDIS_PASSWORD' ,
4039 'CROWD_REDIS_HOST' ,
4140 'CROWD_REDIS_PORT' ,
4241 'ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB' ,
43- ] ;
42+ ]
4443
4544const defaults = {
46- ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE : 100 ,
47- ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS : 5000 ,
45+ ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE : 10000 ,
4846}
4947
48+ /**
49+ * Helper that returns a validated environment variable as a string.
50+ * Throws an error if the variable is required and missing, or if an optional variable is missing
51+ * and has no default.
52+ */
53+ const getEnv = ( name : string ) : string => {
54+ const val = process . env [ name ]
55+
56+ // If we have the value, just return it.
57+ if ( val !== undefined ) {
58+ return val
59+ }
60+
61+ // If it's a required env var, throw.
62+ if ( requiredEnvVars . includes ( name ) ) {
63+ throw new Error ( `Missing required environment variable: ${ name } ` )
64+ }
65+
66+ // Optional env var: fall back to defaults if present, otherwise undefined.
67+ const defaultVal = ( defaults as Record < string , unknown > ) [ name ]
68+ if ( defaultVal !== undefined ) {
69+ return String ( defaultVal )
70+ }
71+
72+ // If we reach this point, an optional env var is missing and has no default set.
73+ throw new Error ( `Missing default value for optional environment variable: ${ name } ` )
74+ }
5075
5176/**
5277 * Loads configuration from environment variables or a .env file.
@@ -55,38 +80,39 @@ const defaults = {
5580 * we load the configuration in the future if needed (e.g. using a secrets store).
5681 */
5782export function getConfig ( ) : Config {
58- dotenv . config ( ) ;
83+ dotenv . config ( )
5984
6085 // Check if all required environment variables are set
6186 for ( const envVar of requiredEnvVars ) {
6287 if ( ! process . env [ envVar ] ) {
63- throw new Error ( `Missing required environment variable: ${ envVar } ` ) ;
88+ throw new Error ( `Missing required environment variable: ${ envVar } ` )
6489 }
6590 }
6691
6792 // Assemble database connection URL strings for convenience
68- const postgresURL = `postgresql://${ process . env . CROWD_DB_USERNAME } :${ process . env . CROWD_DB_PASSWORD } @${ process . env . CROWD_DB_WRITE_HOST } :${ process . env . CROWD_DB_PORT } /${ process . env . CROWD_DB_DATABASE } ?ssl=true` ;
69- const redisURL = `redis://${ process . env . CROWD_REDIS_USERNAME } :${ process . env . CROWD_REDIS_PASSWORD } @${ process . env . CROWD_REDIS_HOST } :${ process . env . CROWD_REDIS_PORT } /${ process . env . ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB } ` ;
93+ const postgresURL = `postgresql://${ getEnv ( ' CROWD_DB_USERNAME' ) } :${ getEnv ( ' CROWD_DB_PASSWORD' ) } @${ getEnv ( ' CROWD_DB_WRITE_HOST' ) } :${ getEnv ( ' CROWD_DB_PORT' ) } /${ getEnv ( ' CROWD_DB_DATABASE' ) } ?ssl=true`
94+ const redisURL = `redis://${ getEnv ( ' CROWD_REDIS_USERNAME' ) } :${ getEnv ( ' CROWD_REDIS_PASSWORD' ) } @${ getEnv ( ' CROWD_REDIS_HOST' ) } :${ getEnv ( ' CROWD_REDIS_PORT' ) } /${ getEnv ( ' ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB' ) } `
7095
7196 return {
72- GithubToken : process . env . ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN ! ,
73- GitlabToken : process . env . ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN ! ,
74-
75- PostgresHost : process . env . CROWD_DB_WRITE_HOST ! ,
76- PostgresPort : parseInt ( process . env . CROWD_DB_PORT ! , 10 ) ,
77- PostgresDBName : process . env . CROWD_DB_DATABASE ! ,
78- PostgresUsername : process . env . CROWD_DB_USERNAME ! ,
79- PostgresPassword : process . env . CROWD_DB_PASSWORD ! ,
97+ GithubToken : getEnv ( ' ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN' ) ,
98+ GitlabToken : getEnv ( ' ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN' ) ,
99+
100+ PostgresHost : getEnv ( ' CROWD_DB_WRITE_HOST' ) ,
101+ PostgresPort : parseInt ( getEnv ( ' CROWD_DB_PORT' ) , 10 ) ,
102+ PostgresDBName : getEnv ( ' CROWD_DB_DATABASE' ) ,
103+ PostgresUsername : getEnv ( ' CROWD_DB_USERNAME' ) ,
104+ PostgresPassword : getEnv ( ' CROWD_DB_PASSWORD' ) ,
80105 PostgresUrl : postgresURL ,
81106
82- RedisHost : process . env . CROWD_REDIS_HOST ! ,
83- RedisPort : parseInt ( process . env . CROWD_REDIS_PORT ! , 10 ) ,
84- RedisUsername : process . env . CROWD_REDIS_USERNAME ! ,
85- RedisPassword : process . env . CROWD_REDIS_PASSWORD ! ,
86- RedisDB : parseInt ( process . env . REDIS_DB ! , 10 ) ,
107+ RedisHost : getEnv ( ' CROWD_REDIS_HOST' ) ,
108+ RedisPort : parseInt ( getEnv ( ' CROWD_REDIS_PORT' ) , 10 ) ,
109+ RedisUsername : getEnv ( ' CROWD_REDIS_USERNAME' ) ,
110+ RedisPassword : getEnv ( ' CROWD_REDIS_PASSWORD' ) ,
111+ RedisDB : parseInt ( getEnv ( 'ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB' ) , 10 ) ,
87112 RedisUrl : redisURL ,
88113
89- BatchSize : parseInt ( process . env . ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE ! , 10 ) || defaults . ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE ,
90- BatchDelayMs : parseInt ( process . env . ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS ! , 10 ) || defaults . ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS ,
91- } ;
114+ BatchSize :
115+ parseInt ( getEnv ( 'ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE' ) , 10 ) ||
116+ defaults . ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE ,
117+ }
92118}
0 commit comments