fixed setting env managment
This commit is contained in:
parent
de4e8486c4
commit
6bb9bd40dd
6 changed files with 100 additions and 54 deletions
|
|
@ -61,10 +61,10 @@ const config = {
|
||||||
|
|
||||||
// Site configuration (domain and protocol based on environment)
|
// Site configuration (domain and protocol based on environment)
|
||||||
site: {
|
site: {
|
||||||
domain: process.env.ENVIRONMENT === 'prod' ? 'rocks.2many.ca' : 'localhost:3000',
|
domain: process.env.ENVIRONMENT === 'prod' ? (process.env.APP_PROD_URL || 'rocks.2many.ca') : 'localhost:3000',
|
||||||
protocol: process.env.ENVIRONMENT === 'prod' ? 'https' : 'http',
|
protocol: process.env.ENVIRONMENT === 'prod' ? 'https' : 'http',
|
||||||
apiDomain: process.env.ENVIRONMENT === 'prod' ? 'api.rocks.2many.ca' : 'localhost:4000',
|
apiDomain: process.env.ENVIRONMENT === 'prod' ? (process.env.API_PROD_URL || 'api.rocks.2many.ca') : 'localhost:4000',
|
||||||
analyticApiKey: '',
|
analyticApiKey: process.env.SITE_ANALYTIC_API || '',
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ module.exports = (pool, query) => {
|
||||||
[email, firstName, lastName]
|
[email, firstName, lastName]
|
||||||
);
|
);
|
||||||
console.log("REGISTERED NEW USER ", email)
|
console.log("REGISTERED NEW USER ", email)
|
||||||
|
// If First User, promot to Admin
|
||||||
if(isFirstUser){
|
if(isFirstUser){
|
||||||
console.log("First User, promoting ", email, " to admin")
|
console.log("First User, promoting ", email, " to admin")
|
||||||
await query(
|
await query(
|
||||||
|
|
|
||||||
|
|
@ -217,21 +217,50 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
const { key, value, category } = setting;
|
const { key, value, category } = setting;
|
||||||
|
|
||||||
// Map database settings to config structure
|
// Map database settings to config structure
|
||||||
if (category === 'email') {
|
if (category === 'server') {
|
||||||
|
if (key === 'port') config.port = parseInt(value, 10);
|
||||||
|
if (key === 'node_env') config.nodeEnv = value;
|
||||||
|
if (key === 'environment') config.environment = value;
|
||||||
|
} else if (category === 'database') {
|
||||||
|
if (key === 'db_host') config.db.host = value;
|
||||||
|
if (key === 'db_user') config.db.user = value;
|
||||||
|
if (key === 'db_password') config.db.password = value;
|
||||||
|
if (key === 'db_name') config.db.database = value;
|
||||||
|
if (key === 'db_port') config.db.port = parseInt(value, 10);
|
||||||
|
} else if (category === 'email') {
|
||||||
if (key === 'smtp_host') config.email.host = value;
|
if (key === 'smtp_host') config.email.host = value;
|
||||||
if (key === 'smtp_port') config.email.port = parseInt(value, 10);
|
if (key === 'smtp_port') config.email.port = parseInt(value, 10);
|
||||||
if (key === 'smtp_user') config.email.user = value;
|
if (key === 'smtp_user') config.email.user = value;
|
||||||
if (key === 'smtp_password') config.email.pass = value;
|
if (key === 'smtp_password') config.email.pass = value;
|
||||||
if (key === 'smtp_from_email') config.email.reply = value;
|
if (key === 'smtp_from_email') config.email.reply = value;
|
||||||
|
} else if (category === 'payment') {
|
||||||
|
if (key === 'stripe_enabled') config.payment.stripeEnabled = value === 'true';
|
||||||
|
if (key === 'stripe_public_key') config.payment.stripePublicKey = value;
|
||||||
|
if (key === 'stripe_secret_key') config.payment.stripeSecretKey = value;
|
||||||
|
if (key === 'stripe_webhook_secret') config.payment.stripeWebhookSecret = value;
|
||||||
|
} else if (category === 'shipping') {
|
||||||
|
if (key === 'shipping_enabled') config.shipping.enabled = value === 'true';
|
||||||
|
if (key === 'easypost_enabled') config.shipping.easypostEnabled = value === 'true';
|
||||||
|
if (key === 'easypost_api_key') config.shipping.easypostApiKey = value;
|
||||||
|
if (key === 'shipping_flat_rate') config.shipping.flatRate = parseFloat(value);
|
||||||
|
if (key === 'shipping_free_threshold') config.shipping.freeThreshold = parseFloat(value);
|
||||||
|
if (key === 'shipping_origin_street') config.shipping.originAddress.street = value;
|
||||||
|
if (key === 'shipping_origin_city') config.shipping.originAddress.city = value;
|
||||||
|
if (key === 'shipping_origin_state') config.shipping.originAddress.state = value;
|
||||||
|
if (key === 'shipping_origin_zip') config.shipping.originAddress.zip = value;
|
||||||
|
if (key === 'shipping_origin_country') config.shipping.originAddress.country = value;
|
||||||
|
if (key === 'shipping_default_package_length') config.shipping.defaultPackage.length = parseFloat(value);
|
||||||
|
if (key === 'shipping_default_package_width') config.shipping.defaultPackage.width = parseFloat(value);
|
||||||
|
if (key === 'shipping_default_package_height') config.shipping.defaultPackage.height = parseFloat(value);
|
||||||
|
if (key === 'shipping_default_package_unit') config.shipping.defaultPackage.unit = value;
|
||||||
|
if (key === 'shipping_default_weight_unit') config.shipping.defaultPackage.weightUnit = value;
|
||||||
|
if (key === 'shipping_carriers_allowed') config.shipping.carriersAllowed = value.split(',');
|
||||||
} else if (category === 'site') {
|
} else if (category === 'site') {
|
||||||
if (key === 'site_name') config.site.name = value;
|
|
||||||
if (key === 'site_domain') config.site.domain = value;
|
if (key === 'site_domain') config.site.domain = value;
|
||||||
if (key === 'site_api_domain') config.site.apiDomain = value;
|
if (key === 'site_api_domain') config.site.apiDomain = value;
|
||||||
if (key === 'site_protocol') config.site.protocol = value;
|
if (key === 'site_analytics_api_key') config.site.analyticApiKey = value;
|
||||||
if (key === 'site_environment') config.environment = value;
|
if (key === 'site_environment') config.environment = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// You can add more mappings for other categories here
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -241,16 +270,21 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
try {
|
try {
|
||||||
// Get all settings from database
|
// Get all settings from database
|
||||||
const allSettings = await SystemSettings.getAllSettings(pool, query);
|
const allSettings = await SystemSettings.getAllSettings(pool, query);
|
||||||
config.updateFromDatabase(allSettings)
|
|
||||||
|
// First update the config with the database settings
|
||||||
|
config.updateFromDatabase(allSettings);
|
||||||
|
|
||||||
// Build environment variables string
|
// Build environment variables string
|
||||||
let envContent = '';
|
let envContent = '';
|
||||||
|
|
||||||
// Add standard environment variables
|
// Server configuration
|
||||||
envContent += `PORT=${process.env.PORT || config.port || 4000}\n`;
|
envContent += '# Server configuration\n';
|
||||||
envContent += `NODE_ENV=${process.env.NODE_ENV || 'development'}\n\n`;
|
envContent += `PORT=${config.port}\n`;
|
||||||
|
envContent += `NODE_ENV=${config.nodeEnv}\n`;
|
||||||
|
envContent += `ENVIRONMENT=${config.environment}\n\n`;
|
||||||
|
|
||||||
// Add database configuration - use existing config values as fallbacks
|
// Database configuration
|
||||||
envContent += `# Database connection\n`;
|
envContent += '# Database configuration\n';
|
||||||
envContent += `DB_HOST=${config.db.host}\n`;
|
envContent += `DB_HOST=${config.db.host}\n`;
|
||||||
envContent += `DB_USER=${config.db.user}\n`;
|
envContent += `DB_USER=${config.db.user}\n`;
|
||||||
envContent += `DB_PASSWORD=${config.db.password}\n`;
|
envContent += `DB_PASSWORD=${config.db.password}\n`;
|
||||||
|
|
@ -258,45 +292,54 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
envContent += `DB_PORT=${config.db.port}\n`;
|
envContent += `DB_PORT=${config.db.port}\n`;
|
||||||
envContent += `POSTGRES_USER=${config.db.user}\n`;
|
envContent += `POSTGRES_USER=${config.db.user}\n`;
|
||||||
envContent += `POSTGRES_PASSWORD=${config.db.password}\n`;
|
envContent += `POSTGRES_PASSWORD=${config.db.password}\n`;
|
||||||
envContent += `POSTGRES_DB=${config.db.database}\n`;
|
envContent += `POSTGRES_DB=${config.db.database}\n\n`;
|
||||||
|
|
||||||
// Get site environment from settings or use existing config
|
// Email configuration
|
||||||
const siteEnvSetting = allSettings.find(s => s.key === 'site_environment');
|
envContent += '# Email configuration\n';
|
||||||
const currentEnvironment = siteEnvSetting?.value || config.environment || process.env.ENVIRONMENT || 'beta';
|
envContent += `EMAIL_HOST=${config.email.host}\n`;
|
||||||
envContent += `ENVIRONMENT=${currentEnvironment}\n\n`;
|
envContent += `EMAIL_PORT=${config.email.port}\n`;
|
||||||
|
envContent += `EMAIL_USER=${config.email.user}\n`;
|
||||||
|
envContent += `EMAIL_PASS=${config.email.pass}\n`;
|
||||||
|
envContent += `EMAIL_REPLY=${config.email.reply}\n\n`;
|
||||||
|
|
||||||
// Add email configuration with fallbacks to existing config
|
// Payment configuration
|
||||||
envContent += `# Email configuration\n`;
|
envContent += '# Payment configuration\n';
|
||||||
const emailSettings = allSettings.filter(s => s.category === 'email');
|
envContent += `STRIPE_ENABLED=${config.payment.stripeEnabled}\n`;
|
||||||
|
envContent += `STRIPE_PUBLIC_KEY=${config.payment.stripePublicKey}\n`;
|
||||||
|
envContent += `STRIPE_SECRET_KEY=${config.payment.stripeSecretKey}\n`;
|
||||||
|
envContent += `STRIPE_WEBHOOK_SECRET=${config.payment.stripeWebhookSecret}\n\n`;
|
||||||
|
|
||||||
const smtpHost = emailSettings.find(s => s.key === 'smtp_host');
|
// Shipping configuration
|
||||||
const smtpPort = emailSettings.find(s => s.key === 'smtp_port');
|
envContent += '# Shipping configuration\n';
|
||||||
const smtpUser = emailSettings.find(s => s.key === 'smtp_user');
|
envContent += `SHIPPING_ENABLED=${config.shipping.enabled}\n`;
|
||||||
const smtpPass = emailSettings.find(s => s.key === 'smtp_password');
|
envContent += `EASYPOST_ENABLED=${config.shipping.easypostEnabled}\n`;
|
||||||
const smtpReply = emailSettings.find(s => s.key === 'smtp_from_email');
|
envContent += `EASYPOST_API_KEY=${config.shipping.easypostApiKey}\n`;
|
||||||
|
envContent += `SHIPPING_FLAT_RATE=${config.shipping.flatRate}\n`;
|
||||||
|
envContent += `SHIPPING_FREE_THRESHOLD=${config.shipping.freeThreshold}\n`;
|
||||||
|
envContent += `SHIPPING_ORIGIN_STREET=${config.shipping.originAddress.street}\n`;
|
||||||
|
envContent += `SHIPPING_ORIGIN_CITY=${config.shipping.originAddress.city}\n`;
|
||||||
|
envContent += `SHIPPING_ORIGIN_STATE=${config.shipping.originAddress.state}\n`;
|
||||||
|
envContent += `SHIPPING_ORIGIN_ZIP=${config.shipping.originAddress.zip}\n`;
|
||||||
|
envContent += `SHIPPING_ORIGIN_COUNTRY=${config.shipping.originAddress.country}\n`;
|
||||||
|
envContent += `SHIPPING_DEFAULT_PACKAGE_LENGTH=${config.shipping.defaultPackage.length}\n`;
|
||||||
|
envContent += `SHIPPING_DEFAULT_PACKAGE_WIDTH=${config.shipping.defaultPackage.width}\n`;
|
||||||
|
envContent += `SHIPPING_DEFAULT_PACKAGE_HEIGHT=${config.shipping.defaultPackage.height}\n`;
|
||||||
|
envContent += `SHIPPING_DEFAULT_PACKAGE_UNIT=${config.shipping.defaultPackage.unit}\n`;
|
||||||
|
envContent += `SHIPPING_DEFAULT_WEIGHT_UNIT=${config.shipping.defaultPackage.weightUnit}\n`;
|
||||||
|
envContent += `SHIPPING_CARRIERS_ALLOWED=${config.shipping.carriersAllowed.join(',')}\n\n`;
|
||||||
|
|
||||||
// Use existing config values as fallbacks in this order: database setting → config value → env value → default
|
// Site configuration
|
||||||
envContent += `EMAIL_HOST=${smtpHost?.value || config.email.host || process.env.EMAIL_HOST || 'smtp.postmarkapp.com'}\n`;
|
envContent += '# Site configuration\n';
|
||||||
envContent += `EMAIL_PORT=${smtpPort?.value || config.email.port || process.env.EMAIL_PORT || '587'}\n`;
|
if (config.environment === 'prod') {
|
||||||
envContent += `EMAIL_USER=${smtpUser?.value || config.email.user || process.env.EMAIL_USER || ''}\n`;
|
// For production, use the actual domain values
|
||||||
envContent += `EMAIL_PASS=${smtpPass?.value || config.email.pass || process.env.EMAIL_PASS || ''}\n`;
|
envContent += `APP_PROD_URL=${config.site.domain}\n`;
|
||||||
envContent += `EMAIL_REPLY=${smtpReply?.value || config.email.reply || process.env.EMAIL_REPLY || 'noreply@2many.ca'}\n\n`;
|
envContent += `API_PROD_URL=${config.site.apiDomain}\n`;
|
||||||
|
} else {
|
||||||
// Add payment configuration with fallbacks to existing values
|
// For beta/development, still include these but they won't be used
|
||||||
const paymentSettings = allSettings.filter(s => s.category === 'payment');
|
envContent += `APP_PROD_URL=${config.site.domain === 'localhost:3000' ? '' : config.site.domain}\n`;
|
||||||
if (paymentSettings.length > 0 || process.env.STRIPE_PUBLIC_KEY || process.env.STRIPE_SECRET_KEY) {
|
envContent += `API_PROD_URL=${config.site.apiDomain === 'localhost:4000' ? '' : config.site.apiDomain}\n`;
|
||||||
envContent += `# Payment configuration\n`;
|
|
||||||
const stripePublic = paymentSettings.find(s => s.key === 'stripe_public_key');
|
|
||||||
const stripeSecret = paymentSettings.find(s => s.key === 'stripe_secret_key');
|
|
||||||
|
|
||||||
// Include payment settings if they exist in either DB or environment
|
|
||||||
if (stripePublic?.value || process.env.STRIPE_PUBLIC_KEY) {
|
|
||||||
envContent += `STRIPE_PUBLIC_KEY=${stripePublic?.value || process.env.STRIPE_PUBLIC_KEY}\n`;
|
|
||||||
}
|
|
||||||
if (stripeSecret?.value || process.env.STRIPE_SECRET_KEY) {
|
|
||||||
envContent += `STRIPE_SECRET_KEY=${stripeSecret?.value || process.env.STRIPE_SECRET_KEY}\n`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
envContent += `SITE_ANALYTIC_API=${config.site.analyticApiKey}\n`;
|
||||||
|
|
||||||
// Write to .env file
|
// Write to .env file
|
||||||
const envPath = path.join(__dirname, '../../.env');
|
const envPath = path.join(__dirname, '../../.env');
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ const config = {
|
||||||
|
|
||||||
// Site configuration (domain and protocol based on environment)
|
// Site configuration (domain and protocol based on environment)
|
||||||
site: {
|
site: {
|
||||||
domain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'rocks.2many.ca' : 'localhost:3000',
|
domain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? import.meta.env.VITE_APP_PROD_URL : 'localhost:3000',
|
||||||
protocol: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'https' : 'http',
|
protocol: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'https' : 'http',
|
||||||
apiDomain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? 'api.rocks.2many.ca' : 'localhost:4000'
|
apiDomain: import.meta.env.VITE_ENVIRONMENT === 'prod' ? import.meta.env.VITE_API_PROD_URL : 'localhost:4000'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -400,7 +400,6 @@ const EmailTemplatesPage = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const onEditorReady = () => {
|
const onEditorReady = () => {
|
||||||
// You can perform any setup actions here when the editor is loaded
|
|
||||||
console.log('Email editor is ready');
|
console.log('Email editor is ready');
|
||||||
|
|
||||||
// If there's a template being edited, load its design
|
// If there's a template being edited, load its design
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,15 @@ export default defineConfig(({ mode }) => {
|
||||||
// 1. Check env loaded by Vite
|
// 1. Check env loaded by Vite
|
||||||
// 2. Check process.env (for Node.js environment)
|
// 2. Check process.env (for Node.js environment)
|
||||||
// 3. Try to read from .env file directly as fallback
|
// 3. Try to read from .env file directly as fallback
|
||||||
let environment = env.ENVIRONMENT;
|
let environment = env.VITE_ENVIRONMENT;
|
||||||
|
|
||||||
if (!environment && process.env.ENVIRONMENT) {
|
if (!environment && process.env.ENVIRONMENT) {
|
||||||
|
console.log(`No environment found at VITE_ENVIRONMENT checking ENVIRONMENT${environment}`);
|
||||||
environment = process.env.ENVIRONMENT;
|
environment = process.env.ENVIRONMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!environment) {
|
if (!environment) {
|
||||||
|
console.log(`No environment trying to manually read env ${environment}`);
|
||||||
try {
|
try {
|
||||||
const envContent = fs.readFileSync('.env', 'utf8');
|
const envContent = fs.readFileSync('.env', 'utf8');
|
||||||
const envMatch = envContent.match(/ENVIRONMENT\s*=\s*(\w+)/);
|
const envMatch = envContent.match(/ENVIRONMENT\s*=\s*(\w+)/);
|
||||||
|
|
@ -33,6 +35,7 @@ export default defineConfig(({ mode }) => {
|
||||||
|
|
||||||
// Default to 'beta' if still not found
|
// Default to 'beta' if still not found
|
||||||
if (!environment) {
|
if (!environment) {
|
||||||
|
console.log(`No environment set to beta manually: ${environment}`);
|
||||||
environment = 'beta';
|
environment = 'beta';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,7 +49,7 @@ export default defineConfig(({ mode }) => {
|
||||||
if (isProduction) {
|
if (isProduction) {
|
||||||
// Production: use the domain name without clientPort
|
// Production: use the domain name without clientPort
|
||||||
hmrConfig = {
|
hmrConfig = {
|
||||||
host: 'rocks.2many.ca',
|
host: env.VITE_APP_PROD_URL,
|
||||||
protocol: 'https'
|
protocol: 'https'
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -75,7 +78,7 @@ export default defineConfig(({ mode }) => {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
allowedHosts: ['localhost', 'rocks.2many.ca'],
|
allowedHosts: ['localhost', env.VITE_APP_PROD_URL],
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
port: 3000,
|
port: 3000,
|
||||||
watch: {
|
watch: {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue