init: PLC data collection system
This commit is contained in:
commit
ae90bc9368
38 files changed
+5234
No files matched your search
@@ -0,0 +1,95 @@
|
||||
import { serve } from '@hono/node-server'
|
||||
import { Hono } from 'hono'
|
||||
import { logger } from './logger.js'
|
||||
import configApi from './api/config.js'
|
||||
import queryApi from './api/query.js'
|
||||
import monitorApi from './api/monitor.js'
|
||||
import { ensureDatabase } from './database/schema.js'
|
||||
import * as scanner from './plc/scanner.js'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const WEB_ROOT = (() => {
|
||||
const candidates = [
|
||||
path.resolve(process.cwd(), 'web/dist'),
|
||||
path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../web/dist'),
|
||||
]
|
||||
for (const p of candidates) {
|
||||
if (fs.existsSync(p)) return p
|
||||
}
|
||||
return candidates[0]
|
||||
})()
|
||||
|
||||
logger.info('Server', `前端目录: ${WEB_ROOT}`)
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
app.route('/api/config', configApi)
|
||||
app.route('/api/query', queryApi)
|
||||
app.route('/api/monitor', monitorApi)
|
||||
|
||||
app.get('/api/scanner/status', (c) => {
|
||||
return c.json({ running: scanner.isRunning() })
|
||||
})
|
||||
|
||||
app.get('/', (c) => c.redirect('/web/'))
|
||||
|
||||
// 手动处理前端静态文件
|
||||
const MIME: Record<string, string> = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.js': 'application/javascript; charset=utf-8',
|
||||
'.json': 'application/json',
|
||||
'.png': 'image/png',
|
||||
'.svg': 'image/svg+xml',
|
||||
}
|
||||
|
||||
app.get('/web/*', (c) => {
|
||||
const reqPath = c.req.path.replace(/^\/web\//, '') || 'index.html'
|
||||
const filePath = path.resolve(WEB_ROOT, reqPath)
|
||||
|
||||
if (!filePath.startsWith(WEB_ROOT)) {
|
||||
return c.text('Forbidden', 403)
|
||||
}
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return c.text('404 Not Found', 404)
|
||||
}
|
||||
|
||||
const ext = path.extname(filePath)
|
||||
const content = fs.readFileSync(filePath)
|
||||
return c.newResponse(content, 200, { 'Content-Type': MIME[ext] || 'application/octet-stream' })
|
||||
})
|
||||
|
||||
const PORT = parseInt(process.env.PORT || '3000', 10)
|
||||
|
||||
serve({
|
||||
fetch: app.fetch,
|
||||
port: PORT,
|
||||
}, (info) => {
|
||||
logger.info('Server', `http://localhost:${PORT}`)
|
||||
})
|
||||
|
||||
try {
|
||||
ensureDatabase()
|
||||
|
||||
const skipPlc = process.env.SKIP_PLC === '1'
|
||||
if (!skipPlc) {
|
||||
scanner.start()
|
||||
} else {
|
||||
logger.info('Server', 'SKIP_PLC=1,跳过 PLC 连接')
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('Server', '初始化失败', err)
|
||||
}
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
scanner.stop()
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
scanner.stop()
|
||||
process.exit(0)
|
||||
})
|
||||
Reference in new issue
Block a user