115 lines
4.7 KiB
TypeScript
115 lines
4.7 KiB
TypeScript
import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod"
|
|
import {
|
|
certificateIssueRequestSchema,
|
|
certificatesStateSchema,
|
|
putAcmeCloudflareSettingsSchema,
|
|
putCertificateRenewSettingsSchema,
|
|
testAcmeCloudflareSettingsSchema,
|
|
} from "@mmapp/contracts/certificates"
|
|
import { acceptCertificateIssueRequest, IssueRequestError } from "../services/certificate-issue-request.js"
|
|
import { refreshScheduler } from "../services/scheduler.js"
|
|
import { testCloudflareToken } from "../services/acme-cloudflare.js"
|
|
import { queueCertificateIssueJob } from "../services/certificate-issue-runner.js"
|
|
import {
|
|
createIssueJobRecordIfAbsent,
|
|
getAcmeCloudflareToken,
|
|
getAcmeSettingsPublic,
|
|
getCertificateRenewSettings,
|
|
getIssueJobRecord,
|
|
getServerRowByIdString,
|
|
listCertificatesFromServers,
|
|
listCertificatesState,
|
|
toIssueJobDto,
|
|
updateAcmeSettings,
|
|
updateCertificateRenewSettings,
|
|
} from "../services/certificates-service.js"
|
|
|
|
export function createCertificatesRoutes(deps: { getState?: typeof listCertificatesState; acceptIssue?: (input: import("@mmapp/contracts/certificates").CertificateIssueRequest) => Promise<{ jobId: string }> } = {}): FastifyPluginAsyncZod {
|
|
return async (app) => {
|
|
app.get("/certificates/state", async (_req, reply) => {
|
|
const parsed = certificatesStateSchema.safeParse(await (deps.getState ?? listCertificatesState)())
|
|
if (!parsed.success) return reply.status(502).send({ error: "Некорректный каталог сертификатов" })
|
|
return reply.send(parsed.data)
|
|
})
|
|
app.get("/certificates", async (_req, reply) => {
|
|
return reply.send(await listCertificatesFromServers())
|
|
})
|
|
|
|
app.post("/certificates/refresh", async (_req, reply) => {
|
|
return reply.send(await listCertificatesFromServers())
|
|
})
|
|
|
|
app.get("/certificates/acme-settings", async (_req, reply) => {
|
|
return reply.send(await getAcmeSettingsPublic())
|
|
})
|
|
|
|
app.put("/certificates/acme-settings", async (req, reply) => {
|
|
const parsed = putAcmeCloudflareSettingsSchema.safeParse(req.body ?? {})
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() })
|
|
}
|
|
return reply.send(await updateAcmeSettings(parsed.data))
|
|
})
|
|
|
|
app.get("/certificates/renew-settings", async (_req, reply) => {
|
|
return reply.send(await getCertificateRenewSettings())
|
|
})
|
|
|
|
app.put("/certificates/renew-settings", async (req, reply) => {
|
|
const parsed = putCertificateRenewSettingsSchema.safeParse(req.body ?? {})
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() })
|
|
}
|
|
const result = await updateCertificateRenewSettings(parsed.data)
|
|
await refreshScheduler()
|
|
return reply.send(result)
|
|
})
|
|
|
|
app.post("/certificates/acme-settings/test", async (req, reply) => {
|
|
const parsed = testAcmeCloudflareSettingsSchema.safeParse(req.body ?? {})
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() })
|
|
}
|
|
const effective = parsed.data.cloudflareApiToken?.trim() || await getAcmeCloudflareToken()
|
|
if (!effective) {
|
|
return reply.status(400).send({ error: "Не задан Cloudflare API token" })
|
|
}
|
|
try {
|
|
await testCloudflareToken(effective)
|
|
return reply.send({ ok: true, message: "Cloudflare API доступен" })
|
|
} catch (e) {
|
|
return reply.status(400).send({
|
|
ok: false,
|
|
message: e instanceof Error ? e.message : String(e),
|
|
})
|
|
}
|
|
})
|
|
|
|
app.post("/certificates/issue", async (req, reply) => {
|
|
const parsed = certificateIssueRequestSchema.safeParse(req.body ?? {})
|
|
if (!parsed.success) {
|
|
return reply.status(400).send({ error: "Некорректное тело запроса", details: parsed.error.flatten() })
|
|
}
|
|
try {
|
|
const result = await (deps.acceptIssue ? deps.acceptIssue(parsed.data) : acceptCertificateIssueRequest(parsed.data, {
|
|
getServer: getServerRowByIdString, getJob: getIssueJobRecord, createJob: createIssueJobRecordIfAbsent, queue: queueCertificateIssueJob,
|
|
}))
|
|
return reply.send(result)
|
|
} catch (error) {
|
|
if (error instanceof IssueRequestError) return reply.status(error.status).send({ error: error.message })
|
|
throw error
|
|
}
|
|
})
|
|
|
|
app.get("/certificates/issue/:jobId", async (req, reply) => {
|
|
const jobId = String((req.params as { jobId: string }).jobId)
|
|
const row = await getIssueJobRecord(jobId)
|
|
if (!row) return reply.status(404).send({ error: "Задача не найдена" })
|
|
return reply.send(await toIssueJobDto(row))
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
export default createCertificatesRoutes()
|