refact
This commit is contained in:
65
src/config/index.js
Executable file
65
src/config/index.js
Executable file
@@ -0,0 +1,65 @@
|
||||
// src/config/index.js
|
||||
require('dotenv').config();
|
||||
|
||||
function must(name) {
|
||||
const v = process.env[name];
|
||||
if (!v) throw new Error(`${name} não definido no .env`);
|
||||
return v;
|
||||
}
|
||||
|
||||
function mustInProd(name, fallback = '') {
|
||||
const v = process.env[name];
|
||||
if ((process.env.NODE_ENV || 'development') === 'production') {
|
||||
if (!v) throw new Error(`${name} não definido (obrigatório em production)`);
|
||||
return v;
|
||||
}
|
||||
return v || fallback;
|
||||
}
|
||||
|
||||
const env = process.env.NODE_ENV || 'development';
|
||||
|
||||
const config = {
|
||||
env,
|
||||
|
||||
port: Number(process.env.PORT || 4000),
|
||||
|
||||
// ✅ sempre obrigatório
|
||||
jwtSecret: must('JWT_SECRET'),
|
||||
|
||||
// ✅ em produção deve estar explícito e restrito
|
||||
corsOrigins: (mustInProd('CORS_ORIGIN', 'http://localhost:5173'))
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean),
|
||||
|
||||
mqtt: {
|
||||
// ✅ em produção não usar defaults
|
||||
url: mustInProd('MQTT_URL', 'mqtt://localhost:1883'),
|
||||
user: mustInProd('MQTT_USER', 'admin'),
|
||||
pass: mustInProd('MQTT_PASS', '123QWEasd'),
|
||||
subTopics: (process.env.MQTT_SUB_TOPICS || '')
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean),
|
||||
},
|
||||
|
||||
chargerOfflineMinutes: Number(process.env.CHARGER_OFFLINE_MINUTES || 5),
|
||||
chargerCacheTtlMs: Number(process.env.CHARGER_CACHE_TTL_MS || 30000),
|
||||
|
||||
vapid: {
|
||||
publicKey: process.env.VAPID_PUBLIC_KEY || '',
|
||||
privateKey: process.env.VAPID_PRIVATE_KEY || '',
|
||||
subject: process.env.VAPID_SUBJECT || 'mailto:admin@evstation.local',
|
||||
},
|
||||
|
||||
// ✅ para remover o hardcode de localhost:7000
|
||||
mosquittoMgmt: {
|
||||
baseUrl: process.env.MOSQUITTO_MGMT_URL || '', // ex: http://mosquitto-mgmt:7000
|
||||
timeoutMs: Number(process.env.MOSQUITTO_MGMT_TIMEOUT_MS || 5000),
|
||||
// se quiseres no futuro: user/pass/token
|
||||
// user: process.env.MOSQUITTO_MGMT_USER || '',
|
||||
// pass: process.env.MOSQUITTO_MGMT_PASS || '',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
Reference in New Issue
Block a user