All files / shared/config serverConfig.ts

100% Statements 20/20
90.47% Branches 38/42
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221                                                                                  437x   1311x       1311x             437x 437x 437x 437x   437x                                                                                                         437x                                                       437x                                                 437x                                                             437x 442x   5x   2x     370x         65x   2x       63x       437x  
import { EmailNotificationTrigger } from "shared/types/emailNotification";
 
export interface ServerConfig {
  port: number;
  logLevel: string;
  dbConnString: string;
  dbName: string;
  jwtSecretKey: string;
  jwtSecretKeyAdmin: string;
  jwtSecretKeyHelp: string;
  allowedCorsOrigins: readonly string[];
  padgAssignmentRounds: number;
  randomAssignmentRounds: number;
  bundleCompression: boolean;
  autoUpdateProgramEnabled: boolean;
  programUpdateInterval: string;
  updateProgramItemPopularityEnabled: boolean;
  useLocalProgramFile: boolean;
  autoAssignAttendeesEnabled: boolean;
  firstSignupBonus: number;
  additionalFirstSignupBonus: number;
  statsDataDir: string;
  autoAssignDelay: number;
  autoAssignInterval: string;
  useTestTime: boolean;
  localKompassiFile: string;
  consoleLogFormatJson: boolean;
  enableLoggingInTests: boolean;
  onlyCronjobs: boolean;
  cronjobsAndBackendSameInstance: boolean;
  logInvalidStartTimes: boolean;
  logMissingScheduleItems: boolean; // If scheduleItems is missing, program item is ignored
  emailNotificationQueueWorkerCount: number;
  emailNotificationTrigger: readonly EmailNotificationTrigger[];
  emailSendFromAddress: string;
  emailSMTPHost: string;
  emailSMTPPort: number;
  emailSMTPUsername: string;
  emailSMTPPassword: string;
}
 
const getAllowedCorsOrigins = (localOrigins: string[] = []): string[] => {
  const envVariableOrigins =
    typeof process.env.CORS_WHITELIST === "string"
      ? process.env.CORS_WHITELIST.split(";")
      : [];
 
  return [...envVariableOrigins, ...localOrigins];
};
 
// PORT_OFFSET lets several local instances (e.g. one per git worktree) run side
// by side without colliding: it shifts the server and client ports by the same
// amount and picks a per-instance dev database name. Default 0 keeps the classic
// 5000/8000 ports
const portOffset = Number(process.env.PORT_OFFSET) || 0;
const defaultServerPort = 5000 + portOffset;
const clientPort = 8000 + portOffset;
const devDbName = portOffset === 0 ? "konsti" : `konsti-${portOffset}`;
 
const commonConfig = {
  // Server settings
  port:
    typeof process.env.PORT === "string"
      ? Number(process.env.PORT)
      : defaultServerPort,
  onlyCronjobs: process.env.ONLY_CRONJOBS === "true" ? true : false,
  cronjobsAndBackendSameInstance: false, // Set this to run cronjobs and http/api server on same instance
 
  // App settings
  bundleCompression: true,
 
  // Event settings
  padgAssignmentRounds: 5,
  randomAssignmentRounds: 300,
  firstSignupBonus: 20,
  additionalFirstSignupBonus: 5,
  useLocalProgramFile: false,
  localKompassiFile: "program-ropecon-2025.json",
 
  // Statistics
  statsDataDir: "src/features/statistics/datafiles",
 
  // Logging: LOG_LEVEL caps winston console output, e.g. "debug" enables the
  // debug logs and the Playwright scripts set "warn" to hide the per-request
  // info logs
  logLevel: process.env.LOG_LEVEL ?? "info",
 
  // Tests
  enableLoggingInTests: false,
 
  // Data checks
  logInvalidStartTimes: false,
  logMissingScheduleItems: false,
 
  // Email notifications
  emailNotificationTrigger: [
    EmailNotificationTrigger.ACCEPTED,
    EmailNotificationTrigger.REJECTED,
    EmailNotificationTrigger.PROGRAM_ITEM_CANCELLED,
    EmailNotificationTrigger.PROGRAM_ITEM_DELETED,
    EmailNotificationTrigger.PROGRAM_ITEM_NO_KONSTI_SIGNUP_ANYMORE,
    EmailNotificationTrigger.PROGRAM_ITEM_NO_LOTTERY_ANYMORE,
    EmailNotificationTrigger.PROGRAM_ITEM_TIME_CHANGED,
  ],
  emailNotificationQueueWorkerCount: 1,
  emailSendFromAddress: "Konsti <konsti@kompassi.eu>",
  emailSMTPHost: "smtp.ethereal.email",
  emailSMTPPort: 587,
  emailSMTPUsername: "",
  emailSMTPPassword: "",
};
 
const prodConfig = {
  dbConnString: process.env.CONN_STRING ?? "",
  dbName: "konsti",
  jwtSecretKey: process.env.JWT_SECRET_KEY ?? "",
  jwtSecretKeyAdmin: process.env.JWT_SECRET_KEY_ADMIN ?? "",
  jwtSecretKeyHelp: process.env.JWT_SECRET_KEY_HELP ?? "",
  allowedCorsOrigins: getAllowedCorsOrigins(),
  consoleLogFormatJson: true,
 
  // Dev
  useTestTime: false,
 
  // Program update cron
  autoUpdateProgramEnabled: true,
  programUpdateInterval: "5,10,15,20,25,35,40,45,50,55 * * * *",
 
  // Program update
  updateProgramItemPopularityEnabled: true,
 
  // Attendee assign cron
  autoAssignAttendeesEnabled: true,
  autoAssignInterval: "0,30 * * * *",
  autoAssignDelay: 1000 * 5,
  emailSendFromAddress: "Konsti <konsti@kompassi.eu>",
  emailSMTPHost: "sr1.pahaip.fi",
  emailSMTPPort: 25,
};
 
const stagingConfig = {
  dbConnString: process.env.CONN_STRING ?? "",
  dbName: "konsti",
  jwtSecretKey: process.env.JWT_SECRET_KEY ?? "",
  jwtSecretKeyAdmin: process.env.JWT_SECRET_KEY_ADMIN ?? "",
  jwtSecretKeyHelp: process.env.JWT_SECRET_KEY_HELP ?? "",
  allowedCorsOrigins: getAllowedCorsOrigins(),
  consoleLogFormatJson: true,
 
  // Dev
  useTestTime: true,
 
  // Program update cron
  autoUpdateProgramEnabled: true,
  programUpdateInterval: "5,10,15,20,25,35,40,45,50,55 * * * *",
 
  // Program update
  updateProgramItemPopularityEnabled: true,
 
  // Attendee assign cron
  autoAssignAttendeesEnabled: true,
  autoAssignInterval: "0,30 * * * *",
  autoAssignDelay: 1000 * 5,
};
 
const devConfig = {
  dbConnString: process.env.CONN_STRING ?? "mongodb://localhost:27017",
  dbName: process.env.DB_NAME ?? devDbName,
  jwtSecretKey: "secret",
  jwtSecretKeyAdmin: "admin secret",
  jwtSecretKeyHelp: "help secret",
  allowedCorsOrigins: getAllowedCorsOrigins([
    `http://localhost:${clientPort}`,
    `http://localhost:${defaultServerPort}`,
    `http://127.0.0.1:${clientPort}`,
    `http://127.0.0.1:${defaultServerPort}`,
    "http://server:5000",
  ]),
  consoleLogFormatJson: false,
 
  // Dev
  useTestTime: true,
 
  // Program update cron
  autoUpdateProgramEnabled: false,
  programUpdateInterval: "5,10,15,20,25,35,40,45,50,55 * * * *",
 
  // Program update
  updateProgramItemPopularityEnabled: true,
 
  // Attendee assign cron
  autoAssignAttendeesEnabled: false,
  autoAssignInterval: "0,30 * * * *",
  autoAssignDelay: 1000 * 1,
};
 
export const combineConfig = (): ServerConfig => {
  switch (process.env.SETTINGS) {
    case "production":
      return { ...commonConfig, ...prodConfig };
    case "staging":
      return { ...commonConfig, ...stagingConfig };
    case "development":
    case "ci":
      return { ...commonConfig, ...devConfig };
    default:
      // Fail closed: deployed pods always run with NODE_ENV=production, so an unrecognized
      // or missing SETTINGS there must not silently fall back to devConfig's public JWT
      // secrets — refuse to start instead. Local/test runs (any other NODE_ENV) keep devConfig
      if (process.env.NODE_ENV === "production") {
        // eslint-disable-next-line no-restricted-syntax -- fail fast on startup misconfiguration
        throw new Error(
          `Refusing to start: SETTINGS="${process.env.SETTINGS ?? ""}" is not a valid config profile for a production deployment`,
        );
      }
      return { ...commonConfig, ...devConfig };
  }
};
 
export const serverConfig: ServerConfig = combineConfig();