/*
    ! /servers/dashboard/server.js – Central Dashboard 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
app.use(express.static(path.resolve(dirs.interfaceDir, 'resources'))); // set resources directory (e.g. scripts, style sheets, images ...)
app.set('view engine', 'ejs'); // set "embedded javascript" as view engine
app.set('views', path.resolve(dirs.interfaceDir, 'views')); // set the folder where the .ejs files are located

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

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

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