mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-29 02:18:50 +05:00
Update 25 files
- /docs/docs.go - /docs/swagger.json - /docs/swagger.yaml - /internal/api/handlers.go - /internal/api/init.go - /internal/api/models.go - /internal/api/utils.go - /internal/tmdb/client.go - /internal/tmdb/models.go - /src/config/tmdb.js - /src/routes/movies.js - /src/utils/date.js - /src/utils/health.js - /src/index.js - /build.sh - /clean.sh - /go.mod - /go.sum - /main.go - /package-lock.json - /package.json - /README.md - /render.yaml - /run.sh - /vercel.json
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
const axios = require('axios');
|
||||
|
||||
class TMDBClient {
|
||||
constructor(accessToken) {
|
||||
this.client = axios.create({
|
||||
baseURL: 'https://api.themoviedb.org/3',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async makeRequest(method, endpoint, params = {}) {
|
||||
try {
|
||||
const response = await this.client.request({
|
||||
method,
|
||||
url: endpoint,
|
||||
params: {
|
||||
...params,
|
||||
language: 'ru-RU',
|
||||
region: 'RU'
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`TMDB API Error: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
getImageURL(path, size = 'original') {
|
||||
if (!path) return null;
|
||||
return `https://image.tmdb.org/t/p/${size}${path}`;
|
||||
}
|
||||
|
||||
async searchMovies(query, page = 1) {
|
||||
const data = await this.makeRequest('GET', '/search/movie', {
|
||||
query,
|
||||
page,
|
||||
include_adult: false
|
||||
});
|
||||
|
||||
// Фильтруем результаты
|
||||
data.results = data.results.filter(movie =>
|
||||
movie.poster_path &&
|
||||
movie.overview &&
|
||||
movie.vote_average > 0
|
||||
);
|
||||
|
||||
// Добавляем полные URL для изображений
|
||||
data.results = data.results.map(movie => ({
|
||||
...movie,
|
||||
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
||||
backdrop_path: this.getImageURL(movie.backdrop_path, 'w1280')
|
||||
}));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
async getMovie(id) {
|
||||
const movie = await this.makeRequest('GET', `/movie/${id}`);
|
||||
return {
|
||||
...movie,
|
||||
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
||||
backdrop_path: this.getImageURL(movie.backdrop_path, 'w1280')
|
||||
};
|
||||
}
|
||||
|
||||
async getPopularMovies(page = 1) {
|
||||
const data = await this.makeRequest('GET', '/movie/popular', { page });
|
||||
data.results = data.results.map(movie => ({
|
||||
...movie,
|
||||
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
||||
backdrop_path: this.getImageURL(movie.backdrop_path, 'w1280')
|
||||
}));
|
||||
return data;
|
||||
}
|
||||
|
||||
async getTopRatedMovies(page = 1) {
|
||||
const data = await this.makeRequest('GET', '/movie/top_rated', { page });
|
||||
data.results = data.results.map(movie => ({
|
||||
...movie,
|
||||
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
||||
backdrop_path: this.getImageURL(movie.backdrop_path, 'w1280')
|
||||
}));
|
||||
return data;
|
||||
}
|
||||
|
||||
async getUpcomingMovies(page = 1) {
|
||||
const data = await this.makeRequest('GET', '/movie/upcoming', { page });
|
||||
data.results = data.results.map(movie => ({
|
||||
...movie,
|
||||
poster_path: this.getImageURL(movie.poster_path, 'w500'),
|
||||
backdrop_path: this.getImageURL(movie.backdrop_path, 'w1280')
|
||||
}));
|
||||
return data;
|
||||
}
|
||||
|
||||
async getMovieExternalIDs(id) {
|
||||
return await this.makeRequest('GET', `/movie/${id}/external_ids`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TMDBClient;
|
||||
275
src/index.js
275
src/index.js
@@ -1,275 +0,0 @@
|
||||
require('dotenv').config();
|
||||
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: {
|
||||
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,
|
||||
description: process.env.VERCEL_URL ? 'Production server' : 'Development server',
|
||||
},
|
||||
],
|
||||
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: 'Сообщение об ошибке'
|
||||
}
|
||||
}
|
||||
},
|
||||
Health: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
status: {
|
||||
type: 'string',
|
||||
enum: ['healthy', 'unhealthy'],
|
||||
description: 'Общий статус API'
|
||||
},
|
||||
version: {
|
||||
type: 'string',
|
||||
description: 'Версия API'
|
||||
},
|
||||
uptime: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
seconds: {
|
||||
type: 'integer',
|
||||
description: 'Время работы в секундах'
|
||||
},
|
||||
formatted: {
|
||||
type: 'string',
|
||||
description: 'Отформатированное время работы'
|
||||
}
|
||||
}
|
||||
},
|
||||
tmdb: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
status: {
|
||||
type: 'string',
|
||||
enum: ['ok', 'error'],
|
||||
description: 'Статус подключения к TMDB'
|
||||
},
|
||||
responseTime: {
|
||||
type: 'integer',
|
||||
description: 'Время ответа TMDB в мс'
|
||||
},
|
||||
error: {
|
||||
type: 'string',
|
||||
description: 'Сообщение об ошибке, если есть'
|
||||
}
|
||||
}
|
||||
},
|
||||
memory: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
heapTotal: {
|
||||
type: 'integer',
|
||||
description: 'Общий размер кучи (MB)'
|
||||
},
|
||||
heapUsed: {
|
||||
type: 'integer',
|
||||
description: 'Использованный размер кучи (MB)'
|
||||
},
|
||||
rss: {
|
||||
type: 'integer',
|
||||
description: 'Resident Set Size (MB)'
|
||||
},
|
||||
memoryUsage: {
|
||||
type: 'integer',
|
||||
description: 'Процент использования памяти'
|
||||
},
|
||||
system: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
total: {
|
||||
type: 'integer',
|
||||
description: 'Общая память системы (MB)'
|
||||
},
|
||||
free: {
|
||||
type: 'integer',
|
||||
description: 'Свободная память системы (MB)'
|
||||
},
|
||||
usage: {
|
||||
type: 'integer',
|
||||
description: 'Процент использования системной памяти'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
system: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
platform: {
|
||||
type: 'string',
|
||||
description: 'Операционная система'
|
||||
},
|
||||
arch: {
|
||||
type: 'string',
|
||||
description: 'Архитектура процессора'
|
||||
},
|
||||
nodeVersion: {
|
||||
type: 'string',
|
||||
description: 'Версия Node.js'
|
||||
},
|
||||
cpuUsage: {
|
||||
type: 'number',
|
||||
description: 'Загрузка CPU'
|
||||
}
|
||||
}
|
||||
},
|
||||
timestamp: {
|
||||
type: 'string',
|
||||
format: 'date-time',
|
||||
description: 'Время проверки'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
apis: ['./src/routes/*.js', './src/index.js'],
|
||||
};
|
||||
|
||||
const swaggerDocs = swaggerJsdoc(swaggerOptions);
|
||||
|
||||
// Custom CSS для Swagger UI
|
||||
const swaggerCustomOptions = {
|
||||
customCss: '.swagger-ui .topbar { display: none }',
|
||||
customSiteTitle: "Neo Movies API Documentation",
|
||||
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) => {
|
||||
if (!process.env.TMDB_ACCESS_TOKEN) {
|
||||
return res.status(500).json({ error: 'TMDB_ACCESS_TOKEN is not set' });
|
||||
}
|
||||
req.tmdb = new TMDBClient(process.env.TMDB_ACCESS_TOKEN);
|
||||
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);
|
||||
app.get('/api-docs', swaggerUi.setup(null, swaggerCustomOptions));
|
||||
app.use('/movies', require('./routes/movies'));
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /health:
|
||||
* get:
|
||||
* tags: [health]
|
||||
* summary: Проверка работоспособности API
|
||||
* description: Возвращает подробную информацию о состоянии API, включая статус TMDB, использование памяти и системную информацию
|
||||
* responses:
|
||||
* 200:
|
||||
* description: API работает нормально
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Health'
|
||||
*/
|
||||
app.get('/health', async (req, res) => {
|
||||
const health = await healthCheck.getFullHealth(req.tmdb);
|
||||
res.json(health);
|
||||
});
|
||||
|
||||
// Error handling
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err.stack);
|
||||
res.status(500).json({ error: 'Something went wrong!' });
|
||||
});
|
||||
|
||||
// Start server
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
console.log(`Documentation available at http://localhost:${port}/api-docs`);
|
||||
});
|
||||
@@ -1,325 +0,0 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { formatDate } = require('../utils/date');
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /movies/search:
|
||||
* get:
|
||||
* summary: Поиск фильмов
|
||||
* description: Поиск фильмов по запросу с поддержкой русского языка
|
||||
* tags: [movies]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: query
|
||||
* required: true
|
||||
* description: Поисковый запрос
|
||||
* schema:
|
||||
* type: string
|
||||
* example: Матрица
|
||||
* - in: query
|
||||
* name: page
|
||||
* description: Номер страницы (по умолчанию 1)
|
||||
* schema:
|
||||
* type: integer
|
||||
* minimum: 1
|
||||
* default: 1
|
||||
* example: 1
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Успешный поиск
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* page:
|
||||
* type: integer
|
||||
* description: Текущая страница
|
||||
* total_pages:
|
||||
* type: integer
|
||||
* description: Всего страниц
|
||||
* total_results:
|
||||
* type: integer
|
||||
* description: Всего результатов
|
||||
* results:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Movie'
|
||||
* 400:
|
||||
* description: Неверный запрос
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
* 500:
|
||||
* description: Ошибка сервера
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
router.get('/search', async (req, res) => {
|
||||
try {
|
||||
const { query, page = 1 } = req.query;
|
||||
if (!query) {
|
||||
return res.status(400).json({ error: "query parameter is required" });
|
||||
}
|
||||
|
||||
const results = await req.tmdb.searchMovies(query, page);
|
||||
|
||||
const response = {
|
||||
page: results.page,
|
||||
total_pages: results.total_pages,
|
||||
total_results: results.total_results,
|
||||
results: results.results.map(movie => ({
|
||||
id: movie.id,
|
||||
title: movie.title,
|
||||
overview: movie.overview,
|
||||
release_date: formatDate(movie.release_date),
|
||||
vote_average: movie.vote_average,
|
||||
poster_path: movie.poster_path,
|
||||
backdrop_path: movie.backdrop_path
|
||||
}))
|
||||
};
|
||||
|
||||
res.json(response);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /movies/{id}:
|
||||
* get:
|
||||
* summary: Получить информацию о фильме
|
||||
* description: Получает детальную информацию о фильме по ID
|
||||
* tags: [movies]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* description: ID фильма
|
||||
* schema:
|
||||
* type: integer
|
||||
* example: 603
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Информация о фильме
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Movie'
|
||||
* 500:
|
||||
* description: Ошибка сервера
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const movie = await req.tmdb.getMovie(req.params.id);
|
||||
res.json({
|
||||
...movie,
|
||||
release_date: formatDate(movie.release_date)
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /movies/popular:
|
||||
* get:
|
||||
* summary: Популярные фильмы
|
||||
* description: Получает список популярных фильмов с русскими названиями и описаниями
|
||||
* tags: [movies]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: page
|
||||
* description: Номер страницы
|
||||
* schema:
|
||||
* type: integer
|
||||
* minimum: 1
|
||||
* default: 1
|
||||
* example: 1
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Список популярных фильмов
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* page:
|
||||
* type: integer
|
||||
* results:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Movie'
|
||||
* 500:
|
||||
* description: Ошибка сервера
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
router.get('/popular', async (req, res) => {
|
||||
try {
|
||||
const { page = 1 } = req.query;
|
||||
const movies = await req.tmdb.getPopularMovies(page);
|
||||
res.json(movies);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /movies/top-rated:
|
||||
* get:
|
||||
* summary: Лучшие фильмы
|
||||
* description: Получает список лучших фильмов с русскими названиями и описаниями
|
||||
* tags: [movies]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: page
|
||||
* description: Номер страницы
|
||||
* schema:
|
||||
* type: integer
|
||||
* minimum: 1
|
||||
* default: 1
|
||||
* example: 1
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Список лучших фильмов
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* page:
|
||||
* type: integer
|
||||
* results:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Movie'
|
||||
* 500:
|
||||
* description: Ошибка сервера
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
router.get('/top-rated', async (req, res) => {
|
||||
try {
|
||||
const { page = 1 } = req.query;
|
||||
const movies = await req.tmdb.getTopRatedMovies(page);
|
||||
res.json(movies);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /movies/upcoming:
|
||||
* get:
|
||||
* summary: Предстоящие фильмы
|
||||
* description: Получает список предстоящих фильмов с русскими названиями и описаниями
|
||||
* tags: [movies]
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: page
|
||||
* description: Номер страницы
|
||||
* schema:
|
||||
* type: integer
|
||||
* minimum: 1
|
||||
* default: 1
|
||||
* example: 1
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Список предстоящих фильмов
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* page:
|
||||
* type: integer
|
||||
* results:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Movie'
|
||||
* 500:
|
||||
* description: Ошибка сервера
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
router.get('/upcoming', async (req, res) => {
|
||||
try {
|
||||
const { page = 1 } = req.query;
|
||||
const movies = await req.tmdb.getUpcomingMovies(page);
|
||||
res.json(movies);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /movies/{id}/external-ids:
|
||||
* get:
|
||||
* summary: Внешние ID фильма
|
||||
* description: Получает внешние идентификаторы фильма (IMDb и др.)
|
||||
* tags: [movies]
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* description: ID фильма
|
||||
* schema:
|
||||
* type: integer
|
||||
* example: 603
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Внешние ID фильма
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* imdb_id:
|
||||
* type: string
|
||||
* description: ID на IMDb
|
||||
* facebook_id:
|
||||
* type: string
|
||||
* description: ID на Facebook
|
||||
* instagram_id:
|
||||
* type: string
|
||||
* description: ID на Instagram
|
||||
* twitter_id:
|
||||
* type: string
|
||||
* description: ID на Twitter
|
||||
* 500:
|
||||
* description: Ошибка сервера
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* $ref: '#/components/schemas/Error'
|
||||
*/
|
||||
router.get('/:id/external-ids', async (req, res) => {
|
||||
try {
|
||||
const externalIds = await req.tmdb.getMovieExternalIDs(req.params.id);
|
||||
res.json(externalIds);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,13 +0,0 @@
|
||||
function formatDate(dateString) {
|
||||
if (!dateString) return null;
|
||||
const date = new Date(dateString);
|
||||
return date.toLocaleDateString('ru-RU', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric'
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatDate
|
||||
};
|
||||
@@ -1,103 +0,0 @@
|
||||
const os = require('os');
|
||||
const process = require('process');
|
||||
|
||||
class HealthCheck {
|
||||
constructor() {
|
||||
this.startTime = Date.now();
|
||||
}
|
||||
|
||||
getUptime() {
|
||||
return Math.floor((Date.now() - this.startTime) / 1000);
|
||||
}
|
||||
|
||||
getMemoryUsage() {
|
||||
const used = process.memoryUsage();
|
||||
return {
|
||||
heapTotal: Math.round(used.heapTotal / 1024 / 1024), // MB
|
||||
heapUsed: Math.round(used.heapUsed / 1024 / 1024), // MB
|
||||
rss: Math.round(used.rss / 1024 / 1024), // MB
|
||||
memoryUsage: Math.round((used.heapUsed / used.heapTotal) * 100) // %
|
||||
};
|
||||
}
|
||||
|
||||
getSystemInfo() {
|
||||
return {
|
||||
platform: process.platform,
|
||||
arch: process.arch,
|
||||
nodeVersion: process.version,
|
||||
cpuUsage: Math.round(os.loadavg()[0] * 100) / 100,
|
||||
totalMemory: Math.round(os.totalmem() / 1024 / 1024), // MB
|
||||
freeMemory: Math.round(os.freemem() / 1024 / 1024) // MB
|
||||
};
|
||||
}
|
||||
|
||||
async checkTMDBConnection(tmdbClient) {
|
||||
try {
|
||||
const startTime = Date.now();
|
||||
await tmdbClient.makeRequest('GET', '/configuration');
|
||||
const endTime = Date.now();
|
||||
return {
|
||||
status: 'ok',
|
||||
responseTime: endTime - startTime
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'error',
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
formatUptime(seconds) {
|
||||
const days = Math.floor(seconds / (24 * 60 * 60));
|
||||
const hours = Math.floor((seconds % (24 * 60 * 60)) / (60 * 60));
|
||||
const minutes = Math.floor((seconds % (60 * 60)) / 60);
|
||||
const remainingSeconds = seconds % 60;
|
||||
|
||||
const parts = [];
|
||||
if (days > 0) parts.push(`${days}d`);
|
||||
if (hours > 0) parts.push(`${hours}h`);
|
||||
if (minutes > 0) parts.push(`${minutes}m`);
|
||||
if (remainingSeconds > 0 || parts.length === 0) parts.push(`${remainingSeconds}s`);
|
||||
|
||||
return parts.join(' ');
|
||||
}
|
||||
|
||||
async getFullHealth(tmdbClient) {
|
||||
const uptime = this.getUptime();
|
||||
const tmdbStatus = await this.checkTMDBConnection(tmdbClient);
|
||||
const memory = this.getMemoryUsage();
|
||||
const system = this.getSystemInfo();
|
||||
|
||||
return {
|
||||
status: tmdbStatus.status === 'ok' ? 'healthy' : 'unhealthy',
|
||||
version: process.env.npm_package_version || '1.0.0',
|
||||
uptime: {
|
||||
seconds: uptime,
|
||||
formatted: this.formatUptime(uptime)
|
||||
},
|
||||
tmdb: {
|
||||
status: tmdbStatus.status,
|
||||
responseTime: tmdbStatus.responseTime,
|
||||
error: tmdbStatus.error
|
||||
},
|
||||
memory: {
|
||||
...memory,
|
||||
system: {
|
||||
total: system.totalMemory,
|
||||
free: system.freeMemory,
|
||||
usage: Math.round(((system.totalMemory - system.freeMemory) / system.totalMemory) * 100)
|
||||
}
|
||||
},
|
||||
system: {
|
||||
platform: system.platform,
|
||||
arch: system.arch,
|
||||
nodeVersion: system.nodeVersion,
|
||||
cpuUsage: system.cpuUsage
|
||||
},
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new HealthCheck();
|
||||
Reference in New Issue
Block a user