feat: каркас Express + pg, health check
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
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 (для nubes_nodejs)
|
||||
app.get('/healthz', async (req, res) => {
|
||||
try {
|
||||
await checkConnection();
|
||||
res.status(200).send('OK');
|
||||
} catch (e) {
|
||||
res.status(500).send('DB error');
|
||||
}
|
||||
});
|
||||
|
||||
// Главная страница
|
||||
app.get('/', (req, res) => {
|
||||
res.render('index', { title: 'IP WhiteList' });
|
||||
});
|
||||
|
||||
async function start() {
|
||||
try {
|
||||
const ok = await checkConnection();
|
||||
console.log('DB connected:', ok);
|
||||
app.listen(PORT, () => console.log(`Server on port ${PORT}`));
|
||||
} catch (e) {
|
||||
console.error('DB connection failed:', e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
start();
|
||||
Reference in New Issue
Block a user