ntp&modercontent_front
This commit is contained in:
+34
-3
@@ -6,7 +6,13 @@ const { connectDB } = require('./config/db');
|
||||
const { logger } = require('./config/logger');
|
||||
const handbookRoutes = require('./routes/handbook');
|
||||
const employeeRoutes = require('./routes/employee');
|
||||
const helmet = require('helmet');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
require('dotenv').config({ path: path.join(__dirname, '../config.env') });
|
||||
const limiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000,
|
||||
max: 100
|
||||
});
|
||||
|
||||
async function readJson(filePath) {
|
||||
const data = await fs.readFile(filePath, 'utf8');
|
||||
@@ -58,11 +64,20 @@ async function connectToNetworkShare(config) {
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use((req, res, next) => {
|
||||
if (req.method === 'POST' || req.method === 'PUT') {
|
||||
if (!req.is('application/json')) {
|
||||
return res.status(415).json({ message: 'Only JSON allowed' });
|
||||
}
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// 📍 Маршруты API
|
||||
// ============================================
|
||||
|
||||
app.use(helmet());
|
||||
app.use('/api/', limiter);
|
||||
app.use('/api/handbook', handbookRoutes);
|
||||
app.use('/api/employee', employeeRoutes);
|
||||
|
||||
@@ -320,7 +335,12 @@ app.get('/api/db-status', async (req, res) => {
|
||||
res.status(500).json({ status: 'error', message: 'DB connection failed' });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/time', (req, res) => {
|
||||
res.json({
|
||||
time: new Date().toISOString(),
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
||||
});
|
||||
});
|
||||
async function startServer() {
|
||||
try {
|
||||
const configPath = path.join(__dirname, '../config.json');
|
||||
@@ -328,6 +348,14 @@ async function startServer() {
|
||||
const { host, port } = config.server;
|
||||
|
||||
await connectDB();
|
||||
app.get('/api/time', (req, res) => {
|
||||
const now = new Date();
|
||||
logger.info(`[TIME] Запрос времени от ${req.ip || req.connection.remoteAddress}`);
|
||||
res.json({
|
||||
time: now.toISOString(),
|
||||
timestamp: now.getTime()
|
||||
});
|
||||
});
|
||||
app.listen(port, host, () => {
|
||||
logger.info(`Server is running on http://${host}:${port}`);
|
||||
});
|
||||
@@ -339,5 +367,8 @@ async function startServer() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
logger.error('Unhandled error', { error: err.message, stack: err.stack });
|
||||
res.status(500).json({ message: 'Внутренняя ошибка сервера' });
|
||||
});
|
||||
startServer();
|
||||
Reference in New Issue
Block a user