Files
ipwhitelist-app/server.js
T

44 lines
1.3 KiB
JavaScript

const express = require('express');
const path = require('path');
require('dotenv').config();
const { checkConnection } = require('./src/db');
const app = express();
const PORT = process.env.PORT || 3000;
// Шаблоны
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Статика
app.use(express.static(path.join(__dirname, 'public')));
// Парсинг форм
app.use(express.urlencoded({ extended: true }));
// Health check — всегда 200 если сервер жив.
// Статус БД проверяется отдельно.
app.get('/healthz', (req, res) => {
res.status(200).send('OK');
});
// Главная — показывает реальный статус БД
app.get('/', async (req, res) => {
let dbStatus = 'неизвестно';
try {
await checkConnection();
dbStatus = 'подключена';
} catch (e) {
dbStatus = 'ошибка: ' + e.message;
}
res.render('index', { title: 'IP WhiteList', dbStatus });
});
// Не падаем если БД недоступна — healthcheck покажет статус
let dbOk = false;
checkConnection()
.then(() => { dbOk = true; console.log('DB connected'); })
.catch((e) => console.error('DB not ready:', e.message));
app.listen(PORT, () => console.log(`Server on port ${PORT}`));