2026-06-17 09:45:59 -07:00
import { z } from "zod" ;
2026-07-09 09:49:45 -07:00
import { DbTypes , LogLevels , QueueModes } from "@/domain/app-settings/app-settings.type.js" ;
2026-06-09 12:34:15 -07:00
import { booleanCoercion } from "@/api/validation/shared.js" ;
2026-06-24 10:53:03 -07:00
import { ILogger } from "@/utils/logger.js" ;
2026-03-02 21:52:22 +00:00
2026-09-10 10:31:41 -07:00
// Padded standard base64 of exactly 32 bytes, as produced by `openssl rand -base64 32`
const ENCRYPTION_KEY_ENTRY = /^[A-Za-z0-9+/]{43}=$/ ;
export const encryptionKeyList = z
. string ()
. optional ()
. transform (( raw , ctx ) => {
if ( raw === undefined ) return [];
const entries = raw
. split ( "," )
. map (( entry , index ) => ({ entry : entry.trim (), slot : index + 1 }))
. filter (({ entry }) => entry . length > 0 );
const seen = new Set < string >();
const keys : Buffer [] = [];
entries . forEach (({ entry , slot }) => {
if ( ! ENCRYPTION_KEY_ENTRY . test ( entry )) {
ctx . addIssue ({
code : "custom" ,
message : `ENCRYPTION_KEY entry ${ slot } must be 32 bytes as padded standard base64 (openssl rand -base64 32)` ,
});
return ;
}
const decoded = Buffer . from ( entry , "base64" );
const fingerprint = decoded . toString ( "hex" );
if ( seen . has ( fingerprint )) {
ctx . addIssue ({ code : "custom" , message : `ENCRYPTION_KEY entry ${ slot } duplicates an earlier entry` });
}
seen . add ( fingerprint );
keys . push ( decoded );
});
return keys ;
});
2026-03-02 21:52:22 +00:00
const envSchema = z . object ({
// Server Configuration
2026-03-06 12:07:18 -08:00
PORT : z.string (). default ( "52345" ),
2026-06-25 10:36:53 -07:00
// Port for worker health checks
HEALTH_PORT : z.string (). default ( "52346" ),
2026-03-02 21:52:22 +00:00
NODE_ENV : z.enum ([ "development" , "production" , "test" ]). default ( "development" ),
2026-07-09 09:49:45 -07:00
LOG_LEVEL : z.enum ( LogLevels ). default ( "debug" ),
2026-03-02 21:52:22 +00:00
// Database
DB_CONNECTION_STRING : z.string (). min ( 1 , "Database connection string is required" ),
2026-03-26 09:47:10 -07:00
DB_TYPE : z.enum ( DbTypes ). default ( "mongodb" ),
2026-06-01 10:44:18 -07:00
QUEUE_MODE : z.enum ( QueueModes ). default ( "primary" ),
2026-06-15 11:02:46 -07:00
QUEUE_PRIMARY_PROCESSES : booleanCoercion.default ( true ),
2026-03-02 21:52:22 +00:00
// JWT Authentication
JWT_SECRET : z.string (). min ( 1 , "JWT_SECRET is required" ),
TOKEN_TTL : z.string (). default ( "99d" ),
// Client Configuration
CLIENT_HOST : z.string (). url ( "CLIENT_HOST must be a valid URL" ),
2026-07-09 09:49:45 -07:00
// Client runtime config overrides, rendered into GET /config.js; unset keys are
// omitted and the client uses its same-origin defaults
CLIENT_CONFIG_API_BASE_URL : z.string (). optional (),
CLIENT_CONFIG_CLIENT_HOST : z.string (). url ( "CLIENT_CONFIG_CLIENT_HOST must be a valid URL" ). optional (),
CLIENT_CONFIG_LOG_LEVEL : z.enum ( LogLevels ). optional (),
2026-03-02 21:52:22 +00:00
// Optional
ORIGIN : z.string (). optional (),
2026-04-23 19:57:43 -04:00
// Feature flags
STATUS_PAGE_THEMES_ENABLED : booleanCoercion.default ( true ),
2026-09-10 10:31:41 -07:00
// Encryption
ENCRYPTION_KEY : encryptionKeyList ,
2026-03-02 21:52:22 +00:00
});
export type ValidatedEnv = z . infer < typeof envSchema >;
2026-06-24 10:53:03 -07:00
export const validateEnv = ( logger : ILogger ) : ValidatedEnv => {
2026-03-02 21:52:22 +00:00
const result = envSchema . safeParse ( process . env );
if ( ! result . success ) {
2026-06-24 10:53:03 -07:00
logger . error ({
message : "Env validation failed" ,
method : "validateEnv" ,
service : "Server" ,
});
2026-03-02 21:52:22 +00:00
const errors = result . error . format ();
for ( const [ key , value ] of Object . entries ( errors )) {
if ( key !== "_errors" && value && typeof value === "object" && "_errors" in value ) {
2026-06-24 10:53:03 -07:00
logger . error ({
message : ` ${ key } : ${ value . _errors . join ( ", " ) } ` ,
method : "validateEnv" ,
service : "Server" ,
});
2026-03-02 21:52:22 +00:00
}
}
2026-06-24 10:53:03 -07:00
logger . error ({
message : "Please check your .env file and ensure all required variables are set." ,
method : "validateEnv" ,
service : "Server" ,
});
2026-03-02 21:52:22 +00:00
process . exit ( 1 );
}
2026-07-13 09:48:02 -07:00
const legacyClientVars = Object . keys ( process . env ). filter (( key ) => key . startsWith ( "UPTIME_APP_" ));
if ( legacyClientVars . length > 0 ) {
logger . warn ({
message : ` ${ legacyClientVars . join ( ", " ) } no longer configure the client and will be ignored. The client defaults to the origin it is served from; to override, use CLIENT_CONFIG_API_BASE_URL, CLIENT_CONFIG_CLIENT_HOST, or CLIENT_CONFIG_LOG_LEVEL.` ,
method : "validateEnv" ,
service : "Server" ,
});
}
2026-06-24 10:53:03 -07:00
logger . info ({
message : "Environment variables validated successfully" ,
method : "validateEnv" ,
service : "Server" ,
});
2026-03-02 21:52:22 +00:00
return result . data ;
};