peertube-plugin-livechat/server/lib/routers/api.ts

64 lines
2.3 KiB
TypeScript
Raw Normal View History

import type { RegisterServerOptions } from '@peertube/peertube-types'
2021-04-29 16:31:48 +00:00
import type { Router, Request, Response, NextFunction } from 'express'
2021-05-03 18:06:36 +00:00
import { asyncMiddleware } from '../middlewares/async'
2021-05-05 15:06:19 +00:00
import { getCheckAPIKeyMiddleware } from '../middlewares/apikey'
import { ensureProsodyRunning } from '../prosody/ctl'
import { isDebugMode } from '../debug'
import { initRoomApiRouter } from './api/room'
import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth'
import { initFederationServerInfosApiRouter } from './api/federation-server-infos'
2023-08-08 16:26:40 +00:00
import { initModerationApiRouter } from './api/moderation'
/**
* Initiate API routes
* @param options server register options
* @returns the router
*/
2021-04-29 16:31:48 +00:00
async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
const { peertubeHelpers, getRouter } = options
const router = getRouter()
const logger = peertubeHelpers.logger
// /test endpoint: used by the prosody module http_peertubelivechat_test to test Peertube API.
router.get('/test', asyncMiddleware([
getCheckAPIKeyMiddleware(options),
async (req: Request, res: Response, _next: NextFunction) => {
logger.info('Test api call')
res.json({ ok: true })
}
]))
await initRoomApiRouter(options, router)
2021-04-30 16:03:12 +00:00
await initAuthApiRouter(options, router)
await initUserAuthApiRouter(options, router)
2021-04-29 16:31:48 +00:00
await initFederationServerInfosApiRouter(options, router)
if (isDebugMode(options)) {
// Only add this route if the debug mode is enabled at time of the server launch.
// Note: the isDebugMode will be tested again when the API is called.
// Note: we dont authenticate the user. We want this API to be callable from debug tools.
// This should not be an issue, as debug_mode should only be available on dev environments.
router.get('/restart_prosody', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
if (!isDebugMode(options)) {
res.json({ ok: false })
return
}
const restartProsodyInDebugMode = req.query.debugger === 'true'
await ensureProsodyRunning(options, true, restartProsodyInDebugMode)
res.json({ ok: true })
}
))
}
2023-08-08 16:26:40 +00:00
router.use('/moderation', await initModerationApiRouter(options))
2021-04-29 16:31:48 +00:00
return router
}
export {
initApiRouter
}