- server: Go 单二进制骨架,embed 内嵌前端,配置/迁移/HTTP 路由 - store: system.db + event.db 两套迁移,业务表 + 种子数据 - domain: 积分引擎(并列三口径/计分通道/流水/排名/破纪录)+ 68 个测试 - web: Vite + Vue 三入口骨架 + shared 层(类型/API/WebSocket 客户端) - console: Electron 宿主骨架(选屏/防熄屏/进程守护/托盘)
96 lines
2.9 KiB
JavaScript
96 lines
2.9 KiB
JavaScript
'use strict'
|
||
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
// 默认值写死在代码里:config.json 只覆盖要改的项。
|
||
// 这样升级新增字段时老配置依然能用,现场也不容易配出半成品。
|
||
const DEFAULTS = {
|
||
server: {
|
||
exe: 'scoreboard.exe',
|
||
args: ['-config', 'config.yaml'],
|
||
host: '127.0.0.1',
|
||
port: 8080,
|
||
autoStart: true,
|
||
healthIntervalMs: 5000,
|
||
restartDelayMs: 3000,
|
||
// 0 表示无限重启;服务端是现场唯一进程,崩了必须拉起来
|
||
maxRestarts: 0
|
||
},
|
||
display: {
|
||
// all-secondary | all | specified
|
||
selection: 'all-secondary',
|
||
displays: [],
|
||
pages: ['standings', 'medals', 'individual', 'results', 'records'],
|
||
pageParam: 'page',
|
||
kiosk: true,
|
||
ignoreMouse: true,
|
||
reloadEveryHours: 24,
|
||
// 服务端迟迟没就绪时的兜底:超时后也把大屏开出来,避免现场一片黑
|
||
waitForServerMs: 20000,
|
||
backgroundColor: '#0b0f1a'
|
||
},
|
||
admin: {
|
||
width: 1040,
|
||
height: 760
|
||
},
|
||
log: {
|
||
maxLines: 2000,
|
||
file: 'logs/console.log',
|
||
maxFileBytes: 4 * 1024 * 1024
|
||
}
|
||
}
|
||
|
||
function isPlainObject(value) {
|
||
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
||
}
|
||
|
||
function merge(base, override) {
|
||
const out = isPlainObject(base) ? { ...base } : {}
|
||
if (!isPlainObject(override)) return out
|
||
for (const [key, value] of Object.entries(override)) {
|
||
if (value === undefined) continue
|
||
out[key] = isPlainObject(value) && isPlainObject(base[key]) ? merge(base[key], value) : value
|
||
}
|
||
return out
|
||
}
|
||
|
||
function readJson(file) {
|
||
try {
|
||
const raw = fs.readFileSync(file, 'utf8')
|
||
return { values: JSON.parse(raw), warning: null }
|
||
} catch (err) {
|
||
if (err.code === 'ENOENT') return { values: {}, warning: `配置文件不存在,使用内置默认值:${file}` }
|
||
return { values: {}, warning: `配置文件无法解析,使用内置默认值:${file}(${err.message})` }
|
||
}
|
||
}
|
||
|
||
function loadConfig(configPath) {
|
||
const { values, warning } = readJson(configPath)
|
||
return {
|
||
path: configPath,
|
||
values: merge(DEFAULTS, values),
|
||
warning
|
||
}
|
||
}
|
||
|
||
// 相对路径的 exe 需要按「部署包根目录」理解,而部署包根目录在不同形态下不同:
|
||
// 开发时是 console/ 的上级,打包后是 exe 所在目录。逐个候选位置探测,避免写死。
|
||
function resolveExecutable(exe, configPath) {
|
||
if (!exe) return { path: null, candidates: [] }
|
||
if (path.isAbsolute(exe)) return { path: exe, candidates: [exe] }
|
||
|
||
const configDir = path.dirname(configPath)
|
||
const candidates = [
|
||
configDir,
|
||
process.cwd(),
|
||
path.resolve(configDir, '..'),
|
||
path.resolve(configDir, '..', 'dist')
|
||
].map((dir) => path.resolve(dir, exe))
|
||
|
||
const found = candidates.find((candidate) => fs.existsSync(candidate))
|
||
return { path: found || candidates[0], candidates: [...new Set(candidates)] }
|
||
}
|
||
|
||
module.exports = { DEFAULTS, loadConfig, merge, resolveExecutable }
|