Files
Module-uo/server/router/admin/uoLink.controller.js
wtclaude 740a677f92 feat(server): register the routes, the slot, the leg and the boot hooks
The entry point becomes real: five mount prefixes, the admin.users.detail
extension slot, the shard push catalog, the town-crier announce leg and both
lifecycle hooks. module.json declares all of it and the loader checks the
declaration against what register() actually registers, in both directions.

The URLs are byte-identical to the ones core served before the extraction. That
is the whole point of moving the code and not the paths: the shipped Android app
calls POST /api/v1/admin/shard/kick and the Discord bot reads
/api/v1/public/shard/*, and neither knows a module answers now.

Require order is load-bearing and the requires are inside register() because of
it. Every ported file reaches core through ./core, whose members resolve ctx
when called -- but a router does `const express = core.express` at ITS file
scope, which runs the moment it is required. Hoisting these to the top of the
file breaks the module with an error about ctx being missing, from a file that
never mentions it.

boot.js takes the eight UO call sites out of core's server.js. One behavioural
change, deliberate: uoLinkSocket.start() and the sidecar health probe used to
run AFTER the listener bound and now run before it, because onBoot does. start()
returns as soon as the reconnecting client is armed, but the probe is a real
HTTP call, so it is fired and NOT awaited -- an unreachable sidecar must not
hold the site closed. Reporting that the bridge is down is diagnostics; being up
is not a precondition for serving a page.

router/rateLimits.js builds the market limiter through ctx.middleware.rateLimit,
core's factory. The policy is the module's -- only the module knows what its
endpoints cost -- and the plumbing is core's, so there is one express-rate-limit
in the process and one place a breach is logged.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-11 12:06:46 -05:00

124 lines
5.0 KiB
JavaScript

// ── Admin: uo-link sidecar control ─────────────────────────────────────────
//
// Configure the connection to the uo-link sidecar (base/ws URL, shared-secret
// token, protocol pin, enabled) and drive the town crier. SECURITY: the token
// is write-only over this API — stored encrypted, NEVER returned; responses
// expose only `hasToken` (same convention as the Discord bot token). Saving
// (re)starts the WS ingest client so a change takes effect with no redeploy.
const uoLinkConfig = require('../../model/uoLinkConfig/uoLinkConfig.model')
const uoLinkClient = require('../../utils/uoLinkClient')
const uoLinkSocket = require('../../utils/uoLinkSocket')
const shardBroadcast = require('../../utils/shardBroadcast')
const { activity } = require('../../core')
const log = require('../../core').logger('admin-uolink')
// Assemble the masked config + live health + ingestion stats for the panel.
async function buildStatus() {
const config = await uoLinkConfig.getSafe()
const health = await uoLinkClient.health()
return {
...config,
health: health.ok ? health.data : { ok: false, error: health.error || `status ${health.status}` },
ingest: uoLinkSocket.getState(),
sse: shardBroadcast.stats(),
}
}
// GET /admin/uo-link/config — masked config + live status + ingestion stats.
async function getConfig(req, res) {
try {
return res.json(await buildStatus())
} catch (err) {
log.error('uoLink.getConfig', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// PUT /admin/uo-link/config — save connection settings + (re)start the socket.
async function saveConfig(req, res) {
const { baseUrl, wsUrl, token, protocol, enabled } = req.body
try {
const current = await uoLinkConfig.getSafe()
const willHaveToken = Boolean(token) || current.hasToken
if (enabled && !willHaveToken) {
return res.status(400).json({ message: 'An auth token is required before enabling.' })
}
await uoLinkConfig.save({
baseUrl,
wsUrl,
token,
protocol: protocol !== undefined ? Number(protocol) : undefined,
enabled,
updatedBy: req.user.id,
})
// Drop the client's cached config so the health check below uses the new values.
uoLinkClient.invalidateConfig()
// (Re)start or stop the ingest socket to match the new enabled/URL/token.
const saved = await uoLinkConfig.getSafe()
if (saved.enabled && saved.hasToken) {
await uoLinkSocket.start()
} else {
uoLinkSocket.stop()
await uoLinkConfig.recordStatus({ status: 'disconnected', pluginConnected: false })
}
await activity.log({ req, action: 'uoLink.config.update', detail: { baseUrl: saved.baseUrl, enabled: saved.enabled } })
log.info('uo-link config updated', { by: req.user.username, enabled: saved.enabled })
return res.json(await buildStatus())
} catch (err) {
log.error('uoLink.saveConfig', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// POST /admin/uo-link/towncrier — publish/replace a town-crier message.
async function postTownCrier(req, res) {
const { id, lines, durationSec } = req.body
try {
const result = await uoLinkClient.postTownCrier({ id, lines, durationSec })
if (result.ok) {
await activity.log({ req, action: 'uoLink.towncrier.post', detail: { id } })
return res.json(result.data || { ok: true, id })
}
if (result.status === 400) return res.status(400).json({ message: 'The shard rejected that message (over the line/duration caps?).' })
if (result.status === 503 || result.status === 0) {
return res.status(503).json({ message: 'The shard is unavailable right now.' })
}
return res.status(502).json({ message: 'Could not reach the shard.' })
} catch (err) {
log.error('uoLink.postTownCrier', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// DELETE /admin/uo-link/towncrier/:id — remove a town-crier message.
async function deleteTownCrier(req, res) {
const { id } = req.params
try {
const result = await uoLinkClient.deleteTownCrier(id)
if (result.ok) {
await activity.log({ req, action: 'uoLink.towncrier.delete', detail: { id } })
return res.json(result.data || { ok: true, id })
}
if (result.status === 404) return res.status(404).json({ message: 'No town-crier message with that id.' })
if (result.status === 503 || result.status === 0) {
return res.status(503).json({ message: 'The shard is unavailable right now.' })
}
return res.status(502).json({ message: 'Could not reach the shard.' })
} catch (err) {
log.error('uoLink.deleteTownCrier', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// GET /admin/uo-link/stream — the full live feed (incl. audit/cheat), staff only.
function stream(req, res) {
shardBroadcast.subscribe(req, res, 'admin')
}
module.exports = { getConfig, saveConfig, postTownCrier, deleteTownCrier, stream }