From ab2452250e72ac5c49ced42a9de27e5c513b6580 Mon Sep 17 00:00:00 2001 From: Maxhy Date: Fri, 1 Mar 2024 13:20:04 +0100 Subject: [PATCH] Allow to run without a repository folder --- src/app.js | 9 ++++++--- src/services/RepositoryService.js | 3 ++- tests/render.test.js | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index fa5dcac..5e64c14 100644 --- a/src/app.js +++ b/src/app.js @@ -5,7 +5,6 @@ const morgan = require('morgan'); const swaggerJsdoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express'); const winston = require('winston'); -const path = require('path'); const canvas = require('canvas'); const app = express(); @@ -47,7 +46,7 @@ app.use(bodyParser.json({ limit: '5mb' })); global.ImageData = canvas.ImageData; // Required since PIXI 7.2.x and Canvas update, temporary workaround (?). Not required if PIXI peer dependency <= 7.1. if (!process.env.TEMPLATE_REPOSITORY) { - process.env.TEMPLATE_REPOSITORY = path.join(__dirname, '../repository'); + logger.warn("The environment variable `TEMPLATE_REPOSITORY` is not defined. Templates caching/storage will not be persistent."); } const swaggerOptions = { @@ -68,6 +67,10 @@ const swaggerOptions = { }, }, servers: [ + { + url: "/", + description: "Current Server" + }, { url: "http://localhost:4000/", description: "Development Server" @@ -88,7 +91,7 @@ const swaggerOptions = { const specs = swaggerJsdoc(swaggerOptions); app.use( - "/api-docs", + "/swagger", swaggerUi.serve, swaggerUi.setup(specs, { explorer: true }) ); diff --git a/src/services/RepositoryService.js b/src/services/RepositoryService.js index 44fadd5..e7bff90 100644 --- a/src/services/RepositoryService.js +++ b/src/services/RepositoryService.js @@ -43,6 +43,7 @@ class RepositoryService { load() { try { if (this.folder !== undefined) { + this.logger.info("Loading template from repository folder `"+ this.folder +"`..."); this.templates = RepositoryService.getTemplates(this.folder); } else { this.templates = []; @@ -58,7 +59,7 @@ class RepositoryService { } else { templateId = sanitize(templateId); } - if (this.folder !== null) { + if (this.folder !== undefined) { if (fs.existsSync(this.folder)) { const fullfile = path.join(this.folder, templateId + '.json'); fs.writeFileSync(fullfile, JSON.stringify(template)); diff --git a/tests/render.test.js b/tests/render.test.js index 0bf8fe9..50017ee 100644 --- a/tests/render.test.js +++ b/tests/render.test.js @@ -1,8 +1,12 @@ const supertest = require('supertest'); const app = require('../src/app'); +const path = require('path'); const RepositoryService = require('../src/services/RepositoryService'); describe('Render Endpoint', () => { + if (!process.env.TEMPLATE_REPOSITORY) { + process.env.TEMPLATE_REPOSITORY = path.join(__dirname, '../repository'); + } const templates = RepositoryService.getTemplates(process.env.TEMPLATE_REPOSITORY); templates.forEach(tpl => { it('POST /render/image should generate a png | ' + tpl.id, async () => {