DTP Base provides a scalable and secure Node.js application development harness ready for production service.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

132 lines
3.6 KiB

// dtp-chat.js
// Copyright (C) 2024 DTP Technologies, LLC
// All Rights Reserved
'use strict';
import 'dotenv/config';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url)); // jshint ignore:line
import { createRequire } from 'module';
const require = createRequire(import.meta.url); // jshint ignore:line
import * as glob from 'glob';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import WEBPACK_CONFIG from './webpack.config.js';
import express from 'express';
const APP_CONFIG = {
pkg: require('./package.json'),
};
class Harness {
constructor ( ) {
this.config = {
root: __dirname,
};
}
async start ( ) {
this.app = express();
this.app.locals.config = APP_CONFIG;
this.app.locals.pkg = APP_CONFIG.pkg; // convenience
this.app.locals.site = (await import(path.join(this.config.root, 'config', 'site.js'))).default;
this.app.set('view engine', 'pug');
this.app.set('views', path.join(__dirname, 'app', 'views'));
this.app.use('/static', express.static(path.join(__dirname, 'client', 'static')));
this.app.use('/fontawesome', express.static(path.join(__dirname, 'node_modules', '@fortawesome', 'fontawesome-free')));
this.app.use('/uikit', express.static(path.join(__dirname, 'node_modules', 'uikit')));
this.app.use('/pretty-checkbox', express.static(path.join(__dirname, 'node_modules', 'pretty-checkbox', 'dist')));
this.app.use('/dist', express.static(path.join(__dirname, 'dist')));
/*
* Webpack integration
*/
if (process.env.NODE_ENV !== 'production') {
this.compiler = webpack(WEBPACK_CONFIG);
this.webpackDevMiddleware = webpackDevMiddleware(this.compiler, {
publicPath: WEBPACK_CONFIG.output.publicPath,
writeToDisk: true,
});
this.app.use(this.webpackDevMiddleware);
}
this.app.use((req, res, next) => {
res.locals.dtp = {
request: req,
};
return next();
});
await this.loadControllers();
/*
* Start the ExpressJS server
*/
const host = process.env.DTP_HTTP_HOST || '127.0.0.1';
const port = parseInt(process.env.DTP_HTTP_PORT || '3000', 10);
console.log('Starting application server', { host, port });
this.app.listen(port, host, ( ) => {
console.log(`${APP_CONFIG.pkg.name} online.`);
});
}
async loadControllers ( ) {
const scripts = glob.sync(path.join(this.config.root, 'app', 'controllers', '*.js'));
const inits = [ ];
this.controllers = { };
for await (const script of scripts) {
try {
const file = path.parse(script);
console.log('loading controller', { name: file.base });
let controller = await import(script);
controller = controller.default;
controller.instance = controller.create(this);
this.controllers[controller.slug] = controller;
inits.push(controller);
} catch (error) {
console.error('failed to load controller', { error });
throw new Error('failed to load controller', { cause: error });
}
}
for await (const controller of inits) {
if (controller.isHome) {
continue;
}
await controller.instance.start();
}
/*
* Start the Home controller
*/
await this.controllers.home.instance.start();
}
}
(async ( ) => {
try {
const harness = new Harness();
await harness.start();
} catch (error) {
console.log('failed to start application harness', error);
}
})();