2022-01-11 00:29:33 +00:00
|
|
|
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'
|
2023-05-19 10:52:52 +00:00
|
|
|
import { ensureProsodyRunning } from '../prosody/ctl'
|
|
|
|
import { isDebugMode } from '../debug'
|
2023-09-07 15:20:01 +00:00
|
|
|
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'
|
2023-09-07 15:20:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
// /test endpoint: used by the prosody module http_peertubelivechat_test to test Peertube API.
|
2021-06-22 10:57:24 +00:00
|
|
|
router.get('/test', asyncMiddleware([
|
|
|
|
getCheckAPIKeyMiddleware(options),
|
|
|
|
async (req: Request, res: Response, _next: NextFunction) => {
|
|
|
|
logger.info('Test api call')
|
|
|
|
res.json({ ok: true })
|
|
|
|
}
|
|
|
|
]))
|
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
await initRoomApiRouter(options, router)
|
2021-04-30 16:03:12 +00:00
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
await initAuthApiRouter(options, router)
|
|
|
|
await initUserAuthApiRouter(options, router)
|
2021-04-29 16:31:48 +00:00
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
await initFederationServerInfosApiRouter(options, router)
|
2023-05-19 10:52:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|