30 lines
1006 B
TypeScript
30 lines
1006 B
TypeScript
import express from 'express'
|
|
import './database'
|
|
import { rpcModules } from './rpcModules'
|
|
|
|
const app = express()
|
|
app.use(express.json({ limit: '50mb' })) // Backup-Import kann alle Tabellen als ein JSON-Body enthalten
|
|
|
|
app.post('/api/rpc', (req, res) => {
|
|
const { module, fn, args } = req.body as { module?: string; fn?: string; args?: unknown[] }
|
|
|
|
const mod = module ? rpcModules[module] : undefined
|
|
const handler = fn ? mod?.[fn] : undefined
|
|
if (!mod || typeof handler !== 'function') {
|
|
res.json({ ok: false, error: `Unbekannte RPC-Zielfunktion: ${module}.${fn}` })
|
|
return
|
|
}
|
|
|
|
try {
|
|
const result = handler(...(args ?? []))
|
|
res.json({ ok: true, result: result ?? null })
|
|
} catch (err) {
|
|
res.json({ ok: false, error: err instanceof Error ? err.message : 'Unbekannter Server-Fehler' })
|
|
}
|
|
})
|
|
|
|
const port = process.env.API_PORT ? Number(process.env.API_PORT) : 4000
|
|
app.listen(port, () => {
|
|
console.log(`[server] RPC-API läuft auf http://localhost:${port}/api/rpc`)
|
|
})
|