80 lines
3.6 KiB
TypeScript
80 lines
3.6 KiB
TypeScript
import { containersStateSchema } from "@mmapp/contracts/containers"
|
|
import { z } from "zod"
|
|
import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod"
|
|
import {
|
|
getEnabledServerById,
|
|
listContainers,
|
|
listContainersState,
|
|
listContainersForServer,
|
|
removeContainer,
|
|
restartContainer,
|
|
startContainer,
|
|
stopContainer,
|
|
} from "../services/containers-live.js"
|
|
import { ServerIdParamSchema, type ServerIdParams } from "../types/server.js"
|
|
import { ContainerRemovalConflict } from "../services/container-removal.js"
|
|
|
|
const RosIdBodySchema = z.object({
|
|
rosId: z.string().min(1),
|
|
})
|
|
|
|
type RosIdBody = z.infer<typeof RosIdBodySchema>
|
|
type MutateFn = typeof startContainer
|
|
|
|
export function createContainersRoutes(deps: { getState?: typeof listContainersState; getServer?: typeof getEnabledServerById; getLegacyCatalog?: typeof listContainers; getServerContainers?: typeof listContainersForServer; remove?: MutateFn } = {}): FastifyPluginAsyncZod {
|
|
return async (app) => {
|
|
app.get("/containers/state", async (_req, reply) => {
|
|
const state = containersStateSchema.safeParse(await (deps.getState ?? listContainersState)())
|
|
if (!state.success) return reply.status(502).send({ error: "Некорректное состояние каталога контейнеров" })
|
|
return reply.send(state.data)
|
|
})
|
|
app.get("/containers", async (_req, reply) => {
|
|
try {
|
|
const containers = await (deps.getLegacyCatalog ?? listContainers)()
|
|
return reply.send({ containers })
|
|
} catch (error) {
|
|
return reply.status(502).send({ error: error instanceof Error ? error.message : "Не удалось получить контейнеры" })
|
|
}
|
|
})
|
|
|
|
app.get("/servers/:id/containers", { schema: { params: ServerIdParamSchema } }, async (req, reply) => {
|
|
const params = req.params as ServerIdParams
|
|
const server = await (deps.getServer ?? getEnabledServerById)(params.id)
|
|
if (!server) return reply.status(404).send({ error: "Server not found" })
|
|
if (!server.enabled) return reply.status(409).send({ error: "Сервер выключен. Опрос контейнеров недоступен." })
|
|
try {
|
|
const containers = await (deps.getServerContainers ?? listContainersForServer)(server)
|
|
return reply.send({ containers })
|
|
} catch (error) {
|
|
return reply.status(502).send({ error: error instanceof Error ? error.message : "Не удалось получить контейнеры" })
|
|
}
|
|
})
|
|
|
|
function registerMutate(path: string, fn: MutateFn) {
|
|
app.post(path, { schema: { params: ServerIdParamSchema, body: RosIdBodySchema } }, async (req, reply) => {
|
|
const params = req.params as ServerIdParams
|
|
const body = req.body as RosIdBody
|
|
const server = await (deps.getServer ?? getEnabledServerById)(params.id)
|
|
if (!server) return reply.status(404).send({ error: "Server not found" })
|
|
if (!server.enabled) return reply.status(409).send({ error: "Сервер выключен. Операции с контейнерами недоступны." })
|
|
try {
|
|
await fn(server, body.rosId)
|
|
return reply.send({ ok: true })
|
|
} catch (err) {
|
|
return reply.status(err instanceof ContainerRemovalConflict ? 409 : 502).send({
|
|
error: err instanceof Error ? err.message : "Ошибка RouterOS",
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
registerMutate("/servers/:id/containers/start", startContainer)
|
|
registerMutate("/servers/:id/containers/stop", stopContainer)
|
|
registerMutate("/servers/:id/containers/restart", restartContainer)
|
|
registerMutate("/servers/:id/containers/remove", deps.remove ?? removeContainer)
|
|
}
|
|
|
|
}
|
|
|
|
export default createContainersRoutes()
|