const express = require('express');
const {json} = require("body-parser");
const cors = require('cors');

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(json());

app.locals.apiKey = 'uc_api'

const checkJson = require('./middlewares/checkJson');
const apiKeyValidation = require('./middlewares/apiKeyValidation');
const setAdminValues = require('./middlewares/setAdminValues');

app.use(apiKeyValidation)
app.use(checkJson)
app.use(setAdminValues);

const usersRouter = require('./routes/users');
const departmentsRouter = require('./routes/departments');
const adminRouter = require('./routes/admin');

app.use('/users', usersRouter);
app.use('/departments', departmentsRouter);
app.use('/admin', adminRouter);

app.listen(port, () => {
    console.log(`Server running on http://localhost:${port}`);
});

// Graceful shutdown function
function gracefulShutdown() {
    console.log('\nGracefully shutting down from SIGINT (Ctrl-C)');

    process.exit(0);
}

process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT', gracefulShutdown);