From e8d7cc04fd9e5e83de2b190f2f52633eb4324c8b Mon Sep 17 00:00:00 2001 From: Foxix Date: Fri, 3 Jan 2025 19:26:29 +0000 Subject: [PATCH] Update 3 files - /package-lock.json - /vercel.json - /src/index.js --- package-lock.json | 3 +++ src/index.js | 26 ++++++++++++++++++++++---- vercel.json | 9 ++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 97d61a2..c17d700 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,9 @@ }, "devDependencies": { "nodemon": "^3.0.2" + }, + "engines": { + "node": ">=18.0.0" } }, "node_modules/@apidevtools/json-schema-ref-parser": { diff --git a/src/index.js b/src/index.js index 8b25f71..d2fa077 100644 --- a/src/index.js +++ b/src/index.js @@ -3,12 +3,18 @@ const express = require('express'); const cors = require('cors'); const swaggerJsdoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express'); +const path = require('path'); const TMDBClient = require('./config/tmdb'); const healthCheck = require('./utils/health'); const app = express(); const port = process.env.PORT || 3000; +// Определяем базовый URL для документации +const BASE_URL = process.env.VERCEL_URL + ? `https://${process.env.VERCEL_URL}` + : `http://localhost:${port}`; + // Swagger configuration const swaggerOptions = { definition: { @@ -24,8 +30,8 @@ const swaggerOptions = { }, servers: [ { - url: `http://localhost:${port}`, - description: 'Development server', + url: BASE_URL, + description: process.env.VERCEL_URL ? 'Production server' : 'Development server', }, ], tags: [ @@ -204,12 +210,17 @@ const swaggerDocs = swaggerJsdoc(swaggerOptions); const swaggerCustomOptions = { customCss: '.swagger-ui .topbar { display: none }', customSiteTitle: "Neo Movies API Documentation", - customfavIcon: "https://www.themoviedb.org/favicon.ico" + customfavIcon: "https://www.themoviedb.org/favicon.ico", + swaggerOptions: { + url: `${BASE_URL}/api-docs/swagger.json`, + persistAuthorization: true + } }; // Middleware app.use(cors()); app.use(express.json()); +app.use(express.static(path.join(__dirname, 'public'))); // TMDB client middleware app.use((req, res, next) => { @@ -220,8 +231,15 @@ app.use((req, res, next) => { next(); }); +// Serve Swagger JSON +app.get('/api-docs/swagger.json', (req, res) => { + res.setHeader('Content-Type', 'application/json'); + res.send(swaggerDocs); +}); + // Routes -app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs, swaggerCustomOptions)); +app.use('/api-docs', swaggerUi.serve); +app.get('/api-docs', swaggerUi.setup(null, swaggerCustomOptions)); app.use('/movies', require('./routes/movies')); /** diff --git a/vercel.json b/vercel.json index 3f0e090..e6e16d3 100644 --- a/vercel.json +++ b/vercel.json @@ -7,9 +7,16 @@ } ], "routes": [ + { + "src": "/api-docs/(.*)", + "dest": "src/index.js" + }, { "src": "/(.*)", "dest": "src/index.js" } - ] + ], + "env": { + "NODE_ENV": "production" + } }