/*
    ! /servers/gateway/server.js – Central Gateway Server Manager
    ? This module orchestrates the express lifecycle, handles initialization, middleware configs and router integrations
*/

// % import nodejs packages
const express = require('express');
const http = require('http');
const path = require('path');

// % import external dependencies
const rootDir = path.resolve(__dirname, '..', '..');
const { dirs } = require(path.resolve(rootDir, 'configuration', 'mapping.js'));

// % set up express server
const app = express();
const server = http.createServer(app);

// $ server setup
app.use(express.json()); // add json support for express

// $ router setup
const router = require(path.resolve(dirs.gatewayDir, 'routers'));
app.use(router);

// $ server functions
const {
    boot, shutdown, restart, status
} = require(path.resolve(dirs.gatewayDir, 'functions.js'));

// $ export server module
module.exports = { boot, shutdown, restart, status, server };