feat: каркас Express + pg, health check
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# База данных
|
||||
DB_HOST=your-db-host.svc.cluster.local
|
||||
DB_PORT=5432
|
||||
DB_NAME=postgres
|
||||
DB_USER=your-user
|
||||
DB_PASS=your-password
|
||||
DB_SSLMODE=require
|
||||
|
||||
# Сервер
|
||||
PORT=3000
|
||||
|
||||
# Лимиты
|
||||
DEFAULT_LIMIT=15
|
||||
|
||||
# Режим разработки (true = без Keycloak, читать X-Dev-User из заголовка)
|
||||
DEV_MODE=true
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "ipwhitelist",
|
||||
"version": "0.1.0",
|
||||
"description": "IP WhiteList microservice for cloud provider",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"start": "node server.js",
|
||||
"dev": "node --watch server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"dotenv": "^16.4.7",
|
||||
"ejs": "^3.1.10",
|
||||
"express": "^4.21.1",
|
||||
"pg": "^8.13.1"
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
@@ -0,0 +1,24 @@
|
||||
const { Pool } = require('pg');
|
||||
require('dotenv').config();
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT || 5432,
|
||||
database: process.env.DB_NAME || 'postgres',
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
ssl: process.env.DB_SSLMODE === 'require' ? { rejectUnauthorized: false } : false,
|
||||
max: 10,
|
||||
idleTimeoutMillis: 30000,
|
||||
});
|
||||
|
||||
pool.on('error', (err) => {
|
||||
console.error('DB pool error:', err.message);
|
||||
});
|
||||
|
||||
async function checkConnection() {
|
||||
const { rows } = await pool.query('SELECT 1 AS ok');
|
||||
return rows[0].ok === 1;
|
||||
}
|
||||
|
||||
module.exports = { pool, checkConnection };
|
||||
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title><%= title %></title>
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1><%= title %></h1>
|
||||
<p>Сервис работает. БД подключена.</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user