2025-01-03 19:46:10 +00:00
|
|
|
|
require('dotenv').config();
|
|
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const cors = require('cors');
|
|
|
|
|
|
const swaggerJsdoc = require('swagger-jsdoc');
|
|
|
|
|
|
const path = require('path');
|
|
|
|
|
|
const TMDBClient = require('./config/tmdb');
|
|
|
|
|
|
const healthCheck = require('./utils/health');
|
|
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
|
|
|
|
// Определяем базовый URL для документации
|
2025-01-03 20:08:13 +00:00
|
|
|
|
const BASE_URL = process.env.NODE_ENV === 'production'
|
|
|
|
|
|
? 'https://neomovies-api.vercel.app'
|
2025-01-03 20:14:34 +00:00
|
|
|
|
: 'http://localhost:3000';
|
2025-01-03 19:46:10 +00:00
|
|
|
|
|
|
|
|
|
|
// Swagger configuration
|
|
|
|
|
|
const swaggerOptions = {
|
|
|
|
|
|
definition: {
|
|
|
|
|
|
openapi: '3.0.0',
|
|
|
|
|
|
info: {
|
|
|
|
|
|
title: 'Neo Movies API',
|
|
|
|
|
|
version: '1.0.0',
|
|
|
|
|
|
description: 'API для поиска и получения информации о фильмах с поддержкой русского языка',
|
|
|
|
|
|
contact: {
|
|
|
|
|
|
name: 'API Support',
|
|
|
|
|
|
url: 'https://gitlab.com/foxixus/neomovies-api'
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
servers: [
|
|
|
|
|
|
{
|
|
|
|
|
|
url: BASE_URL,
|
2025-01-03 20:14:34 +00:00
|
|
|
|
description: process.env.NODE_ENV === 'production' ? 'Production server' : 'Development server'
|
|
|
|
|
|
}
|
2025-01-03 19:46:10 +00:00
|
|
|
|
],
|
|
|
|
|
|
tags: [
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'movies',
|
|
|
|
|
|
description: 'Операции с фильмами'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'health',
|
|
|
|
|
|
description: 'Проверка работоспособности API'
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
components: {
|
|
|
|
|
|
schemas: {
|
|
|
|
|
|
Movie: {
|
|
|
|
|
|
type: 'object',
|
|
|
|
|
|
properties: {
|
|
|
|
|
|
id: {
|
|
|
|
|
|
type: 'integer',
|
|
|
|
|
|
description: 'ID фильма'
|
|
|
|
|
|
},
|
|
|
|
|
|
title: {
|
|
|
|
|
|
type: 'string',
|
|
|
|
|
|
description: 'Название фильма'
|
|
|
|
|
|
},
|
|
|
|
|
|
overview: {
|
|
|
|
|
|
type: 'string',
|
|
|
|
|
|
description: 'Описание фильма'
|
|
|
|
|
|
},
|
|
|
|
|
|
release_date: {
|
|
|
|
|
|
type: 'string',
|
|
|
|
|
|
format: 'date',
|
|
|
|
|
|
description: 'Дата выхода'
|
|
|
|
|
|
},
|
|
|
|
|
|
vote_average: {
|
|
|
|
|
|
type: 'number',
|
|
|
|
|
|
description: 'Средняя оценка'
|
|
|
|
|
|
},
|
|
|
|
|
|
poster_path: {
|
|
|
|
|
|
type: 'string',
|
|
|
|
|
|
description: 'URL постера'
|
|
|
|
|
|
},
|
|
|
|
|
|
backdrop_path: {
|
|
|
|
|
|
type: 'string',
|
|
|
|
|
|
description: 'URL фонового изображения'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
Error: {
|
|
|
|
|
|
type: 'object',
|
|
|
|
|
|
properties: {
|
|
|
|
|
|
error: {
|
|
|
|
|
|
type: 'string',
|
|
|
|
|
|
description: 'Сообщение об ошибке'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2025-01-03 20:14:34 +00:00
|
|
|
|
apis: [path.join(__dirname, 'routes', '*.js'), __filename]
|
2025-01-03 19:46:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const swaggerDocs = swaggerJsdoc(swaggerOptions);
|
|
|
|
|
|
|
2025-01-03 19:58:01 +00:00
|
|
|
|
// CORS configuration
|
|
|
|
|
|
app.use(cors({
|
2025-01-03 20:14:34 +00:00
|
|
|
|
origin: true,
|
2025-01-03 20:08:13 +00:00
|
|
|
|
credentials: true,
|
2025-01-03 19:58:01 +00:00
|
|
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
2025-01-03 20:08:13 +00:00
|
|
|
|
allowedHeaders: ['X-Requested-With', 'Content-Type', 'Authorization', 'Accept']
|
2025-01-03 19:58:01 +00:00
|
|
|
|
}));
|
|
|
|
|
|
|
2025-01-03 20:08:13 +00:00
|
|
|
|
// Handle preflight requests
|
|
|
|
|
|
app.options('*', cors());
|
|
|
|
|
|
|
2025-01-03 19:46:10 +00:00
|
|
|
|
// Middleware
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
|
|
|
|
|
|
|
// TMDB client middleware
|
|
|
|
|
|
app.use((req, res, next) => {
|
2025-01-03 20:08:13 +00:00
|
|
|
|
const token = process.env.TMDB_ACCESS_TOKEN || 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJkOWRlZTY5ZjYzNzYzOGU2MjY5OGZhZGY0ZjhhYTNkYyIsInN1YiI6IjY1OTVkNmM5ODY5ZTc1NzJmOTY1MjZiZiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.Wd_tBYGkAoGPVHq3A5DwV1iLs_eGvH3RRz86ghJTmU8';
|
|
|
|
|
|
if (!token) {
|
2025-01-03 19:46:10 +00:00
|
|
|
|
return res.status(500).json({ error: 'TMDB_ACCESS_TOKEN is not set' });
|
|
|
|
|
|
}
|
2025-01-03 20:08:13 +00:00
|
|
|
|
req.tmdb = new TMDBClient(token);
|
2025-01-03 19:46:10 +00:00
|
|
|
|
next();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-03 19:58:01 +00:00
|
|
|
|
// API Documentation routes
|
|
|
|
|
|
app.get('/api-docs', (req, res) => {
|
|
|
|
|
|
res.sendFile(path.join(__dirname, 'public', 'api-docs', 'index.html'));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-03 19:46:10 +00:00
|
|
|
|
app.get('/api-docs/swagger.json', (req, res) => {
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
|
|
res.send(swaggerDocs);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-03 19:58:01 +00:00
|
|
|
|
// API routes
|
2025-01-03 19:46:10 +00:00
|
|
|
|
app.use('/movies', require('./routes/movies'));
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /health:
|
|
|
|
|
|
* get:
|
|
|
|
|
|
* tags: [health]
|
|
|
|
|
|
* summary: Проверка работоспособности API
|
|
|
|
|
|
* description: Возвращает подробную информацию о состоянии API, включая статус TMDB, использование памяти и системную информацию
|
|
|
|
|
|
* responses:
|
|
|
|
|
|
* 200:
|
|
|
|
|
|
* description: API работает нормально
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
2025-01-03 20:14:34 +00:00
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* status:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [ok, error]
|
|
|
|
|
|
* tmdb:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* status:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [ok, error]
|
2025-01-03 19:46:10 +00:00
|
|
|
|
*/
|
|
|
|
|
|
app.get('/health', async (req, res) => {
|
2025-01-03 20:14:34 +00:00
|
|
|
|
try {
|
|
|
|
|
|
const health = await healthCheck.getFullHealth(req.tmdb);
|
|
|
|
|
|
res.json(health);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
status: 'error',
|
|
|
|
|
|
error: error.message
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-01-03 19:46:10 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Error handling
|
|
|
|
|
|
app.use((err, req, res, next) => {
|
2025-01-03 20:14:34 +00:00
|
|
|
|
console.error('Error:', err);
|
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
error: 'Internal Server Error',
|
|
|
|
|
|
message: process.env.NODE_ENV === 'development' ? err.message : undefined
|
|
|
|
|
|
});
|
2025-01-03 19:46:10 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-01-03 20:14:34 +00:00
|
|
|
|
// Handle 404
|
|
|
|
|
|
app.use((req, res) => {
|
|
|
|
|
|
res.status(404).json({ error: 'Not Found' });
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Export the Express API
|
|
|
|
|
|
module.exports = app;
|
|
|
|
|
|
|
|
|
|
|
|
// Start server only in development
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
|
const port = process.env.PORT || 3000;
|
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
|
|
console.log(`Server is running on port ${port}`);
|
|
|
|
|
|
console.log(`Documentation available at http://localhost:${port}/api-docs`);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|