env
Some checks failed
Build & Deploy KLZ Cables / build-and-deploy (push) Failing after 1m46s

This commit is contained in:
2026-01-27 23:43:14 +01:00
parent ad6bfe1457
commit 5a5c10ca36
5 changed files with 69 additions and 73 deletions

1
.env
View File

@@ -15,7 +15,6 @@ UMAMI_SCRIPT_URL=https://analytics.infra.mintel.me/script.js
# GlitchTip (Sentry protocol) # GlitchTip (Sentry protocol)
SENTRY_DSN=https://c10957d0182245b1a2a806ac2d34a197@errors.infra.mintel.me/1 SENTRY_DSN=https://c10957d0182245b1a2a806ac2d34a197@errors.infra.mintel.me/1
NEXT_PUBLIC_SENTRY_DSN=https://c10957d0182245b1a2a806ac2d34a197@klz-cables.com/errors/1
# Redis Cache # Redis Cache
REDIS_URL= REDIS_URL=

View File

@@ -1,8 +1,19 @@
import * as Sentry from '@sentry/nextjs'; import * as Sentry from '@sentry/nextjs';
import { getServerAppServices } from '@/lib/services/create-services.server';
// Next.js will call this on boot for the active runtime. /**
// We dynamically import the correct Sentry config file. * Next.js will call this on boot for the active runtime.
*
* NEXT_RUNTIME is an environment variable automatically set by Next.js:
* - 'nodejs' when running in the standard Node.js runtime
* - 'edge' when running in the Edge runtime (e.g. Middleware, Edge API Routes)
*/
export async function register() { export async function register() {
// Initialize server services on boot
if (process.env.NEXT_RUNTIME === 'nodejs') {
getServerAppServices();
}
if (process.env.NEXT_RUNTIME === 'nodejs') { if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./sentry.server.config'); await import('./sentry.server.config');
} }

View File

@@ -2,64 +2,76 @@
* Centralized configuration management for the application. * Centralized configuration management for the application.
* This file defines the schema and provides a type-safe way to access environment variables. * This file defines the schema and provides a type-safe way to access environment variables.
*/ */
import dotenv from 'dotenv';
import path from 'path';
// Load .env file in development or if not already loaded
if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') {
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
}
const getEnv = (key: string, defaultValue?: string): string | undefined => {
if (typeof process === 'undefined') return defaultValue;
return process.env[key] || defaultValue;
};
export const config = { export const config = {
env: process.env.NODE_ENV || 'development', env: getEnv('NODE_ENV', 'development'),
isProduction: process.env.NODE_ENV === 'production', isProduction: getEnv('NODE_ENV') === 'production',
isDevelopment: process.env.NODE_ENV === 'development', isDevelopment: getEnv('NODE_ENV') === 'development',
isTest: process.env.NODE_ENV === 'test', isTest: getEnv('NODE_ENV') === 'test',
baseUrl: process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000', baseUrl: getEnv('NEXT_PUBLIC_BASE_URL', 'http://localhost:3000'),
analytics: { analytics: {
umami: { umami: {
websiteId: process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID, websiteId: getEnv('NEXT_PUBLIC_UMAMI_WEBSITE_ID'),
scriptUrl: process.env.UMAMI_SCRIPT_URL || 'https://analytics.infra.mintel.me/script.js', scriptUrl: getEnv('UMAMI_SCRIPT_URL', 'https://analytics.infra.mintel.me/script.js'),
// The proxied path used in the frontend // The proxied path used in the frontend
proxyPath: '/stats/script.js', proxyPath: '/stats/script.js',
enabled: Boolean(process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID), enabled: Boolean(getEnv('NEXT_PUBLIC_UMAMI_WEBSITE_ID')),
}, },
}, },
errors: { errors: {
glitchtip: { glitchtip: {
// Use SENTRY_DSN for both server and client (proxied) // Use SENTRY_DSN for both server and client (proxied)
dsn: process.env.SENTRY_DSN, dsn: getEnv('SENTRY_DSN'),
// The proxied origin used in the frontend // The proxied origin used in the frontend
proxyPath: '/errors', proxyPath: '/errors',
enabled: Boolean(process.env.SENTRY_DSN), enabled: Boolean(getEnv('SENTRY_DSN')),
}, },
}, },
cache: { cache: {
redis: { redis: {
url: process.env.REDIS_URL, url: getEnv('REDIS_URL'),
keyPrefix: process.env.REDIS_KEY_PREFIX || 'klz:', keyPrefix: getEnv('REDIS_KEY_PREFIX', 'klz:'),
enabled: Boolean(process.env.REDIS_URL), enabled: Boolean(getEnv('REDIS_URL')),
}, },
}, },
logging: { logging: {
level: process.env.LOG_LEVEL || 'info', level: getEnv('LOG_LEVEL', 'info'),
}, },
mail: { mail: {
host: process.env.MAIL_HOST, host: getEnv('MAIL_HOST'),
port: parseInt(process.env.MAIL_PORT || '587', 10), port: parseInt(getEnv('MAIL_PORT', '587')!, 10),
user: process.env.MAIL_USERNAME, user: getEnv('MAIL_USERNAME'),
pass: process.env.MAIL_PASSWORD, pass: getEnv('MAIL_PASSWORD'),
from: process.env.MAIL_FROM, from: getEnv('MAIL_FROM'),
recipients: process.env.MAIL_RECIPIENTS?.split(',') || [], recipients: getEnv('MAIL_RECIPIENTS', '')?.split(',').filter(Boolean) || [],
}, },
woocommerce: { woocommerce: {
url: process.env.WOOCOMMERCE_URL, url: getEnv('WOOCOMMERCE_URL'),
consumerKey: process.env.WOOCOMMERCE_CONSUMER_KEY, consumerKey: getEnv('WOOCOMMERCE_CONSUMER_KEY'),
consumerSecret: process.env.WOOCOMMERCE_CONSUMER_SECRET, consumerSecret: getEnv('WOOCOMMERCE_CONSUMER_SECRET'),
}, },
wordpress: { wordpress: {
appPassword: process.env.WORDPRESS_APP_PASSWORD, appPassword: getEnv('WORDPRESS_APP_PASSWORD'),
}, },
} as const; } as const;

View File

@@ -2,14 +2,15 @@ import nodemailer from "nodemailer";
import { render } from "@react-email/components"; import { render } from "@react-email/components";
import { ReactElement } from "react"; import { ReactElement } from "react";
import { getServerAppServices } from "@/lib/services/create-services.server"; import { getServerAppServices } from "@/lib/services/create-services.server";
import { config } from "../config";
const transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
host: process.env.MAIL_HOST, host: config.mail.host,
port: Number(process.env.MAIL_PORT), port: config.mail.port,
secure: Number(process.env.MAIL_PORT) === 465, secure: config.mail.port === 465,
auth: { auth: {
user: process.env.MAIL_USERNAME, user: config.mail.user,
pass: process.env.MAIL_PASSWORD, pass: config.mail.pass,
}, },
}); });
@@ -22,10 +23,10 @@ interface SendEmailOptions {
export async function sendEmail({ to, subject, template }: SendEmailOptions) { export async function sendEmail({ to, subject, template }: SendEmailOptions) {
const html = await render(template); const html = await render(template);
const recipients = to || process.env.MAIL_RECIPIENTS?.split(",") || []; const recipients = to || config.mail.recipients;
const mailOptions = { const mailOptions = {
from: process.env.MAIL_FROM, from: config.mail.from,
to: recipients, to: recipients,
subject, subject,
html, html,

View File

@@ -5,6 +5,7 @@ import { GlitchtipErrorReportingService } from './errors/glitchtip-error-reporti
import { NoopErrorReportingService } from './errors/noop-error-reporting-service'; import { NoopErrorReportingService } from './errors/noop-error-reporting-service';
import { NoopLoggerService } from './logging/noop-logger-service'; import { NoopLoggerService } from './logging/noop-logger-service';
import { PinoLoggerService } from './logging/pino-logger-service'; import { PinoLoggerService } from './logging/pino-logger-service';
import { config, getMaskedConfig } from '../config';
/** /**
* Singleton instance of AppServices. * Singleton instance of AppServices.
@@ -74,48 +75,28 @@ export function getAppServices(): AppServices {
? new PinoLoggerService('server') ? new PinoLoggerService('server')
: new NoopLoggerService(); : new NoopLoggerService();
// Log environment variables (safely masked)
const envLog = {
// Mask sensitive values - show only last 4 characters or '***' for empty
NEXT_PUBLIC_UMAMI_WEBSITE_ID: process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID
? `***${process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID.slice(-4)}`
: 'not set',
UMAMI_SCRIPT_URL: process.env.UMAMI_SCRIPT_URL ?? 'not set',
NEXT_PUBLIC_SENTRY_DSN: process.env.NEXT_PUBLIC_SENTRY_DSN
? `***${process.env.NEXT_PUBLIC_SENTRY_DSN.slice(-4)}`
: 'not set',
SENTRY_DSN: process.env.SENTRY_DSN
? `***${process.env.SENTRY_DSN.slice(-4)}`
: 'not set',
// Safe to show - no sensitive data
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL ?? 'not set',
NODE_ENV: process.env.NODE_ENV ?? 'not set',
};
// Log initialization // Log initialization
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
// Server-side // Server-side
logger.info('Initializing server application services', { logger.info('Initializing server application services', {
environment: envLog, environment: getMaskedConfig(),
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
}); });
} else { } else {
// Client-side // Client-side
logger.info('Initializing client application services', { logger.info('Initializing client application services', {
environment: envLog, environment: getMaskedConfig(),
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
}); });
} }
// Determine which services to enable based on environment variables // Determine which services to enable based on environment variables
const umamiEnabled = Boolean(process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID); const umamiEnabled = config.analytics.umami.enabled;
const sentryClientEnabled = Boolean(process.env.NEXT_PUBLIC_SENTRY_DSN); const sentryEnabled = config.errors.glitchtip.enabled;
const sentryServerEnabled = Boolean(process.env.SENTRY_DSN);
logger.info('Service configuration', { logger.info('Service configuration', {
umamiEnabled, umamiEnabled,
sentryClientEnabled, sentryEnabled,
sentryServerEnabled,
isServer: typeof window === 'undefined', isServer: typeof window === 'undefined',
}); });
@@ -135,20 +116,12 @@ export function getAppServices(): AppServices {
} }
// Create error reporting service (GlitchTip/Sentry or no-op) // Create error reporting service (GlitchTip/Sentry or no-op)
// Server-side and client-side have separate DSNs const errors = sentryEnabled
const errors = ? new GlitchtipErrorReportingService({ enabled: true })
typeof window === 'undefined' : new NoopErrorReportingService();
? sentryServerEnabled
? new GlitchtipErrorReportingService({ enabled: true })
: new NoopErrorReportingService()
: sentryClientEnabled
? new GlitchtipErrorReportingService({ enabled: true })
: new NoopErrorReportingService();
if (typeof window === 'undefined' && sentryServerEnabled) { if (sentryEnabled) {
logger.info('GlitchTip error reporting service initialized (server)'); logger.info(`GlitchTip error reporting service initialized (${typeof window === 'undefined' ? 'server' : 'client'})`);
} else if (typeof window !== 'undefined' && sentryClientEnabled) {
logger.info('GlitchTip error reporting service initialized (client)');
} else { } else {
logger.info('Noop error reporting service initialized (error reporting disabled)'); logger.info('Noop error reporting service initialized (error reporting disabled)');
} }
@@ -161,7 +134,7 @@ export function getAppServices(): AppServices {
logger.info('Pino logger service initialized', { logger.info('Pino logger service initialized', {
name: typeof window === 'undefined' ? 'server' : 'client', name: typeof window === 'undefined' ? 'server' : 'client',
level: process.env.LOG_LEVEL ?? 'info', level: config.logging.level,
}); });
// Create and cache the singleton // Create and cache the singleton