|
| 1 | +import type { FastifyInstance /* FastifyRegisterOptions */ } from "fastify"; |
| 2 | +import { type Static, Type } from "@sinclair/typebox"; |
| 3 | +import { ApiErrorType } from "../plugins/errors.js"; |
| 4 | +import { isFailure } from "../../result/index.js"; |
| 5 | +import { Container } from "typedi"; |
| 6 | +import { AppConfigService } from "../../services/AppConfigService.js"; |
| 7 | + |
| 8 | +const InitializeSchema = Type.Object({ |
| 9 | + xmlTvUrl: Type.String(), |
| 10 | + language: Type.String(), |
| 11 | + outputFolder: Type.String(), |
| 12 | + backgroundContentFolder: Type.String(), |
| 13 | +}); |
| 14 | + |
| 15 | +type InitializeSchema = Static<typeof InitializeSchema>; |
| 16 | + |
| 17 | +const routes = async ( |
| 18 | + fastify: FastifyInstance, |
| 19 | + // options: FastifyRegisterOptions<never>, |
| 20 | +) => { |
| 21 | + fastify.get("/", () => { |
| 22 | + const appConfigService = Container.get(AppConfigService); |
| 23 | + return { |
| 24 | + isInitialized: appConfigService.isInitialized, |
| 25 | + }; |
| 26 | + }); |
| 27 | + |
| 28 | + fastify.post<{ Body: InitializeSchema }>( |
| 29 | + "/", |
| 30 | + { schema: { body: InitializeSchema } }, |
| 31 | + async (request, reply) => { |
| 32 | + const appConfigService = Container.get(AppConfigService); |
| 33 | + if (appConfigService.isInitialized) { |
| 34 | + reply.sendError(ApiErrorType.APP_ALREADY_INITIALIZED); |
| 35 | + } else { |
| 36 | + const result = await appConfigService.initializeFromDefaults( |
| 37 | + request.body.xmlTvUrl, |
| 38 | + request.body.language, |
| 39 | + request.body.outputFolder, |
| 40 | + request.body.backgroundContentFolder, |
| 41 | + ); |
| 42 | + |
| 43 | + if (isFailure(result)) { |
| 44 | + reply.sendError( |
| 45 | + ApiErrorType.UNABLE_TO_INITIALIZE, |
| 46 | + result.error.message, |
| 47 | + ); |
| 48 | + } else { |
| 49 | + reply.status(200).send(appConfigService.config); |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +export default routes; |
0 commit comments