ok
This commit is contained in:
1 parent
6082a0e610
commit
b3b1edec17
10 files changed
+373
-231
No files matched your search
@@ -2,10 +2,13 @@ import type { HttpContext } from '@adonisjs/core/http'
|
||||
import logger from '@adonisjs/core/services/logger'
|
||||
import DeviceModel from '#models/device'
|
||||
import { Tr069TraceService } from '#services/tr069_trace_service'
|
||||
import { readFileSync, existsSync } from 'node:fs'
|
||||
import { readFileSync, existsSync, mkdirSync } from 'node:fs'
|
||||
import { writeFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
|
||||
const LOG_DIR = join(process.cwd(), 'logs', 'tr069')
|
||||
const APP_ROOT = process.cwd()
|
||||
const CONFIG_PATH = join(APP_ROOT, 'data', 'tr069-trace.json')
|
||||
const LOG_DIR = join(APP_ROOT, 'logs', 'tr069')
|
||||
const MAX_TAIL_LINES = 1000
|
||||
|
||||
export default class Tr069TraceController {
|
||||
@@ -77,4 +80,45 @@ export default class Tr069TraceController {
|
||||
|
||||
return response.status(200).json({ deviceId, content: tail, lines: allLines.length })
|
||||
}
|
||||
|
||||
async rawConfig({ response }: HttpContext) {
|
||||
try {
|
||||
if (!existsSync(CONFIG_PATH)) {
|
||||
return response.status(200).json({ raw: JSON.stringify({ entries: [] }, null, 2) })
|
||||
}
|
||||
const raw = readFileSync(CONFIG_PATH, 'utf-8')
|
||||
return response.status(200).json({ raw })
|
||||
} catch (error) {
|
||||
return response.status(500).json({ error: 'Failed to read config file' })
|
||||
}
|
||||
}
|
||||
|
||||
async saveRawConfig({ request, response }: HttpContext) {
|
||||
const raw = request.input('raw') as string | undefined
|
||||
|
||||
if (!raw) {
|
||||
return response.status(422).json({ error: 'raw JSON content is required' })
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw)
|
||||
if (!parsed.entries || !Array.isArray(parsed.entries)) {
|
||||
return response.status(422).json({ error: 'JSON must contain an "entries" array' })
|
||||
}
|
||||
|
||||
mkdirSync(join(APP_ROOT, 'data'), { recursive: true })
|
||||
await writeFile(CONFIG_PATH, JSON.stringify(parsed, null, 2), 'utf-8')
|
||||
|
||||
// Reload the service in-memory state
|
||||
this.service.reload()
|
||||
|
||||
logger.info('TR-069 trace config updated via raw editor')
|
||||
return response.status(200).json({ entries: this.service.list() })
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
return response.status(422).json({ error: `Invalid JSON: ${error.message}` })
|
||||
}
|
||||
return response.status(500).json({ error: 'Failed to save config file' })
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user