Initial import
This commit is contained in:
22
backend/src/app.controller.spec.ts
Normal file
22
backend/src/app.controller.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
||||
12
backend/src/app.controller.ts
Normal file
12
backend/src/app.controller.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
||||
12
backend/src/app.module.ts
Normal file
12
backend/src/app.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { GlobalWordsModule } from './global-words/global-words.module';
|
||||
import { ImageEngineModule } from './image-engine/image-engine.module';
|
||||
|
||||
@Module({
|
||||
imports: [ImageEngineModule, GlobalWordsModule],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
8
backend/src/app.service.ts
Normal file
8
backend/src/app.service.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
12
backend/src/global-words/global-word.interface.ts
Normal file
12
backend/src/global-words/global-word.interface.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export interface GlobalWord {
|
||||
palabra: string;
|
||||
categoria: string;
|
||||
tema: string;
|
||||
descripcion: string;
|
||||
}
|
||||
|
||||
export interface WordWithAsset extends GlobalWord {
|
||||
slug: string;
|
||||
imageUrl: string | null;
|
||||
imageStatus: 'ready' | 'pending';
|
||||
}
|
||||
44
backend/src/global-words/global-words.controller.ts
Normal file
44
backend/src/global-words/global-words.controller.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Res,
|
||||
StreamableFile,
|
||||
HttpStatus,
|
||||
HttpCode,
|
||||
} from '@nestjs/common';
|
||||
import type { Response } from 'express';
|
||||
import { createReadStream } from 'node:fs';
|
||||
import { GlobalWordsService } from './global-words.service';
|
||||
|
||||
@Controller('global-words')
|
||||
export class GlobalWordsController {
|
||||
constructor(private readonly globalWordsService: GlobalWordsService) {}
|
||||
|
||||
@Get()
|
||||
async getWords() {
|
||||
return this.globalWordsService.listWords();
|
||||
}
|
||||
|
||||
@Post('prefetch')
|
||||
@HttpCode(HttpStatus.ACCEPTED)
|
||||
async prefetchAll() {
|
||||
this.globalWordsService.prefetchAll();
|
||||
return { status: 'scheduled' };
|
||||
}
|
||||
|
||||
@Post(':slug/generate')
|
||||
async generateForWord(@Param('slug') slug: string) {
|
||||
await this.globalWordsService.ensureImage(slug);
|
||||
return { slug, status: 'ready' };
|
||||
}
|
||||
|
||||
@Get(':slug/image')
|
||||
async getImage(@Param('slug') slug: string, @Res({ passthrough: true }) res: Response) {
|
||||
const asset = await this.globalWordsService.ensureImage(slug);
|
||||
res.setHeader('Content-Type', asset.mimeType);
|
||||
const file = createReadStream(asset.path);
|
||||
return new StreamableFile(file);
|
||||
}
|
||||
}
|
||||
12
backend/src/global-words/global-words.module.ts
Normal file
12
backend/src/global-words/global-words.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GlobalWordsController } from './global-words.controller';
|
||||
import { GlobalWordsService } from './global-words.service';
|
||||
import { ImageEngineModule } from '../image-engine/image-engine.module';
|
||||
|
||||
@Module({
|
||||
imports: [ImageEngineModule],
|
||||
controllers: [GlobalWordsController],
|
||||
providers: [GlobalWordsService],
|
||||
exports: [GlobalWordsService],
|
||||
})
|
||||
export class GlobalWordsModule {}
|
||||
150
backend/src/global-words/global-words.service.ts
Normal file
150
backend/src/global-words/global-words.service.ts
Normal file
@@ -0,0 +1,150 @@
|
||||
import { Injectable, Logger, NotFoundException, OnModuleInit } from '@nestjs/common';
|
||||
import { promises as fs } from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import type { GlobalWord, WordWithAsset } from './global-word.interface';
|
||||
import { ImageEngineService } from '../image-engine/image-engine.service';
|
||||
|
||||
@Injectable()
|
||||
export class GlobalWordsService implements OnModuleInit {
|
||||
private readonly logger = new Logger(GlobalWordsService.name);
|
||||
private readonly repoRoot = path.resolve(process.cwd(), '..');
|
||||
private readonly contentRoot = path.join(this.repoRoot, 'content');
|
||||
private readonly wordsPath = path.join(this.contentRoot, 'palabras-globales.json');
|
||||
private readonly assetsDir = path.join(this.contentRoot, 'generated', 'global-words');
|
||||
private wordsCache: GlobalWord[] | null = null;
|
||||
|
||||
constructor(private readonly imageEngine: ImageEngineService) {}
|
||||
|
||||
async onModuleInit() {
|
||||
await fs.mkdir(this.assetsDir, { recursive: true });
|
||||
// Lanzamos prefetch en background.
|
||||
this.prefetchAll().catch((error) =>
|
||||
this.logger.error('No se pudieron precargar todas las imágenes', error as Error),
|
||||
);
|
||||
}
|
||||
|
||||
private async loadWords(): Promise<GlobalWord[]> {
|
||||
if (!this.wordsCache) {
|
||||
const raw = await fs.readFile(this.wordsPath, 'utf8');
|
||||
this.wordsCache = JSON.parse(raw) as GlobalWord[];
|
||||
}
|
||||
return this.wordsCache;
|
||||
}
|
||||
|
||||
private slugify(text: string) {
|
||||
return text
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-zA-Z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
private readonly mimeByExtension: Record<string, string> = {
|
||||
png: 'image/png',
|
||||
webp: 'image/webp',
|
||||
jpg: 'image/jpeg',
|
||||
jpeg: 'image/jpeg',
|
||||
svg: 'image/svg+xml',
|
||||
};
|
||||
|
||||
private async resolveExistingAsset(slug: string) {
|
||||
for (const ext of Object.keys(this.mimeByExtension)) {
|
||||
const candidate = path.join(this.assetsDir, `${slug}.${ext}`);
|
||||
try {
|
||||
await fs.access(candidate);
|
||||
return { path: candidate, mimeType: this.mimeByExtension[ext] };
|
||||
} catch {
|
||||
// keep iterating
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private buildPrompt(word: GlobalWord) {
|
||||
return `Ilustración infantil estilo acuarela, vibrante y amigable para niños pequeños. Palabra clave: "${word.palabra}". Tema: ${word.tema}. Categoría: ${word.categoria}. Descripción: ${word.descripcion}. Usa los colores azul y oro si es un elemento de fútbol o autos. Fondo suave, detalles simples.`;
|
||||
}
|
||||
|
||||
async listWords(): Promise<WordWithAsset[]> {
|
||||
const words = await this.loadWords();
|
||||
return Promise.all(
|
||||
words.map(async (word) => {
|
||||
const slug = this.slugify(word.palabra);
|
||||
const existing = await this.resolveExistingAsset(slug);
|
||||
return {
|
||||
...word,
|
||||
slug,
|
||||
imageUrl: existing ? `/global-words/${slug}/image` : null,
|
||||
imageStatus: existing ? 'ready' : 'pending',
|
||||
};
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
private async findWordBySlug(slug: string): Promise<GlobalWord & { slug: string }> {
|
||||
const words = await this.loadWords();
|
||||
const found = words
|
||||
.map((word) => ({ ...word, slug: this.slugify(word.palabra) }))
|
||||
.find((word) => word.slug === slug);
|
||||
if (!found) {
|
||||
throw new NotFoundException(`No existe la palabra con slug ${slug}`);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
async ensureImage(slug: string) {
|
||||
const word = await this.findWordBySlug(slug);
|
||||
const existing = await this.resolveExistingAsset(slug);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
this.logger.log(`Generando imagen para ${word.palabra}`);
|
||||
const prompt = this.buildPrompt(word);
|
||||
try {
|
||||
const asset = await this.imageEngine.generate(word, slug, prompt);
|
||||
return asset;
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`No se pudo generar ${word.palabra} con el motor local, usando placeholder. Motivo: ${
|
||||
(error as Error).message
|
||||
}`,
|
||||
);
|
||||
const svgBuffer = this.buildPlaceholder(word);
|
||||
const filePath = path.join(this.assetsDir, `${slug}.svg`);
|
||||
await fs.writeFile(filePath, svgBuffer);
|
||||
return { path: filePath, mimeType: this.mimeByExtension.svg };
|
||||
}
|
||||
}
|
||||
|
||||
async prefetchAll() {
|
||||
const words = await this.loadWords();
|
||||
for (const word of words) {
|
||||
const slug = this.slugify(word.palabra);
|
||||
if (await this.resolveExistingAsset(slug)) continue;
|
||||
await this.ensureImage(slug);
|
||||
await new Promise((resolve) => setTimeout(resolve, 400)); // evita saturar API
|
||||
}
|
||||
}
|
||||
|
||||
private buildPlaceholder(word: GlobalWord) {
|
||||
const background = word.categoria === 'fútbol' ? '#13347a' : '#ffe6b3';
|
||||
const accent = word.categoria === 'fútbol' ? '#f7c948' : '#1967d2';
|
||||
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512">
|
||||
<defs>
|
||||
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="100%">
|
||||
<stop offset="0%" stop-color="${background}" stop-opacity="0.9"/>
|
||||
<stop offset="100%" stop-color="${accent}" stop-opacity="0.9"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<rect width="512" height="512" fill="url(#grad)" rx="42" ry="42"/>
|
||||
<text x="50%" y="45%" font-size="120" font-family="Nunito, Arial" font-weight="800" text-anchor="middle" fill="#ffffff">${word.palabra}</text>
|
||||
<text x="50%" y="62%" font-size="36" font-family="Nunito, Arial" text-anchor="middle" fill="#ffffff" opacity="0.85">${word.categoria}</text>
|
||||
<foreignObject x="56" y="320" width="400" height="160">
|
||||
<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:28px;font-family:Nunito,Arial;text-align:center;color:#fff;">
|
||||
${word.descripcion}
|
||||
</div>
|
||||
</foreignObject>
|
||||
</svg>`;
|
||||
return Buffer.from(svg);
|
||||
}
|
||||
}
|
||||
8
backend/src/image-engine/image-engine.module.ts
Normal file
8
backend/src/image-engine/image-engine.module.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ImageEngineService } from './image-engine.service';
|
||||
|
||||
@Module({
|
||||
providers: [ImageEngineService],
|
||||
exports: [ImageEngineService],
|
||||
})
|
||||
export class ImageEngineModule {}
|
||||
49
backend/src/image-engine/image-engine.service.ts
Normal file
49
backend/src/image-engine/image-engine.service.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { fetch } from 'undici';
|
||||
import type { GlobalWord } from '../global-words/global-word.interface';
|
||||
|
||||
interface GenerateResponse {
|
||||
path: string;
|
||||
mimeType?: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ImageEngineService {
|
||||
private readonly logger = new Logger(ImageEngineService.name);
|
||||
private readonly baseUrl = process.env.IMAGE_SERVICE_URL ?? 'http://localhost:8001';
|
||||
|
||||
async generate(word: GlobalWord, slug: string, prompt: string) {
|
||||
const response = await fetch(`${this.baseUrl}/generate`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
prompt,
|
||||
slug,
|
||||
negative_prompt: 'text, watermark, logo, disfigured, deformed',
|
||||
width: 640,
|
||||
height: 512,
|
||||
steps: 4,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
this.logger.error(
|
||||
`Image service error ${response.status}: ${errorText}`,
|
||||
);
|
||||
throw new Error(
|
||||
`Image service responded with ${response.status}: ${errorText}`,
|
||||
);
|
||||
}
|
||||
|
||||
const data = (await response.json()) as GenerateResponse;
|
||||
if (!data?.path) {
|
||||
throw new Error('Image service did not return a file path');
|
||||
}
|
||||
|
||||
return {
|
||||
path: data.path,
|
||||
mimeType: data.mimeType ?? 'image/png',
|
||||
};
|
||||
}
|
||||
}
|
||||
15
backend/src/main.ts
Normal file
15
backend/src/main.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { config } from 'dotenv';
|
||||
import * as path from 'node:path';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
config({ path: path.resolve(process.cwd(), '..', '.env') });
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.enableCors({
|
||||
origin: process.env.CLIENT_ORIGIN ?? true,
|
||||
});
|
||||
await app.listen(process.env.PORT ?? 3000);
|
||||
}
|
||||
bootstrap();
|
||||
Reference in New Issue
Block a user