1
This commit is contained in:
63
server/config/app_config.js
Normal file
63
server/config/app_config.js
Normal file
@@ -0,0 +1,63 @@
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { get_env } from './env.js';
|
||||
|
||||
function must_get(key) {
|
||||
const v = get_env(key);
|
||||
if (v === undefined || v === null || v === '') {
|
||||
throw new Error(`缺少配置 ${key}`);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
function get_bool(key, default_value) {
|
||||
const v = get_env(key);
|
||||
if (v === undefined || v === null || v === '') {
|
||||
return default_value;
|
||||
}
|
||||
return String(v).toLowerCase() === 'true';
|
||||
}
|
||||
|
||||
function get_int(key, default_value) {
|
||||
const v = get_env(key);
|
||||
if (v === undefined || v === null || v === '') {
|
||||
return default_value;
|
||||
}
|
||||
const n = Number(v);
|
||||
if (Number.isNaN(n)) {
|
||||
throw new Error(`配置 ${key} 必须是数字`);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
let cached = null;
|
||||
|
||||
export function get_app_config() {
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
cached = {
|
||||
mysql: {
|
||||
host: must_get('MYSQL_HOST'),
|
||||
port: get_int('MYSQL_PORT', 3306),
|
||||
user: must_get('MYSQL_USER'),
|
||||
password: must_get('MYSQL_PASSWORD'),
|
||||
database: must_get('MYSQL_DATABASE')
|
||||
},
|
||||
server: {
|
||||
port: get_int('SERVER_PORT', 38080)
|
||||
},
|
||||
crawler: {
|
||||
crx_src_path: must_get('CRX_SRC_PATH'),
|
||||
action_timeout_ms: get_int('ACTION_TIMEOUT_MS', 300000),
|
||||
puppeteer_headless: get_bool('PUPPETEER_HEADLESS', false),
|
||||
chrome_executable_path: (get_env('CHROME_EXECUTABLE_PATH') || '').trim() || path.resolve(__dirname, '../../chrome-win/chrome.exe')
|
||||
}
|
||||
};
|
||||
|
||||
return cached;
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
import { get_app_config } from './app_config.js';
|
||||
|
||||
export function get_sequelize_options() {
|
||||
const cfg = get_app_config();
|
||||
|
||||
return {
|
||||
host: process.env.MYSQL_HOST || '127.0.0.1',
|
||||
port: Number(process.env.MYSQL_PORT || 3306),
|
||||
username: process.env.MYSQL_USER || 'root',
|
||||
password: process.env.MYSQL_PASSWORD || '',
|
||||
database: process.env.MYSQL_DATABASE || 'ecom_crawl',
|
||||
host: cfg.mysql.host,
|
||||
port: cfg.mysql.port,
|
||||
username: cfg.mysql.user,
|
||||
password: cfg.mysql.password,
|
||||
database: cfg.mysql.database,
|
||||
dialect: 'mysql',
|
||||
logging: false,
|
||||
define: {
|
||||
|
||||
74
server/config/env.js
Normal file
74
server/config/env.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
let loaded = false;
|
||||
let env_map = {};
|
||||
|
||||
function unquote(value) {
|
||||
const v = String(value);
|
||||
if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) {
|
||||
return v.slice(1, -1);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
function parse_env_text(text) {
|
||||
const out = {};
|
||||
const lines = String(text).split(/\r?\n/);
|
||||
|
||||
for (const raw_line of lines) {
|
||||
const line = raw_line.trim();
|
||||
if (!line) continue;
|
||||
if (line.startsWith('#')) continue;
|
||||
|
||||
const idx = line.indexOf('=');
|
||||
if (idx <= 0) continue;
|
||||
|
||||
const key = line.slice(0, idx).trim();
|
||||
let value = line.slice(idx + 1).trim();
|
||||
|
||||
// 去掉行尾注释:仅在未被引号包裹时生效
|
||||
const quoted = (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"));
|
||||
if (!quoted) {
|
||||
const sharp = value.indexOf('#');
|
||||
if (sharp >= 0) {
|
||||
value = value.slice(0, sharp).trim();
|
||||
}
|
||||
}
|
||||
|
||||
out[key] = unquote(value);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function load_env() {
|
||||
if (loaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const env_path = path.resolve(__dirname, '../.env');
|
||||
|
||||
const text = fs.readFileSync(env_path, 'utf8');
|
||||
env_map = parse_env_text(text);
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
export function get_env(key) {
|
||||
if (!loaded) {
|
||||
load_env();
|
||||
}
|
||||
return env_map[key];
|
||||
}
|
||||
|
||||
export function get_all_env() {
|
||||
if (!loaded) {
|
||||
load_env();
|
||||
}
|
||||
return { ...env_map };
|
||||
}
|
||||
|
||||
load_env();
|
||||
Reference in New Issue
Block a user