Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (117 loc) · 6.11 KB
/
Copy pathindex.js
File metadata and controls
147 lines (117 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* File : index.js
Author : Daniel S. A. Khan
Copywrite : Daniel S. A. Khan (c) 2025
Description : Main file for Sanddy - the Personal Assistant Application
Notes :
*/
const mongoose = require( 'mongoose' );
const express = require( 'express' );
const bodyParser = require( 'body-parser' );
const fileUpload = require( 'express-fileupload' );
const favicon = require( 'serve-favicon' );
const path = require( 'path' );
const cors = require( 'cors' );
const {logger} = require( './services/generic' );
const {ApplicationPort} = require( './services/generic' );
const {applicationName} = require( './services/generic' );
const {dbName} = require( './services/generic' );
const {version} = require( './services/generic' );
const {lastFix} = require( './services/generic' );
const db = mongoose.connection;
const app = express();
// eslint-disable-next-line no-undef
const directoryName = __dirname;
app.set( 'view engine','ejs' );
mongoose.connect( dbName );
app.use( bodyParser.json( { limit: '10mb' } ) );
app.use( bodyParser.urlencoded( { extended: true, limit: '10mb' } ) );
app.use( fileUpload( {
limits: { fileSize: 50 * 1024 * 1024 }, // 50MB max file size
useTempFiles: true,
tempFileDir: '/tmp/'
} ) );
app.use( express.static( 'public' ) );
app.use( favicon( path.join( directoryName, 'public', 'img', 'favicon.ico' ) ) );
app.use( cors() );
// Bootstrap CSS
app.use( '/css', express.static( path.join( directoryName, 'node_modules/bootstrap/dist/css' ) ) );
app.use( '/js', express.static( path.join( directoryName, 'node_modules/bootstrap/dist/js' ) ) );
app.use( '/icons', express.static( path.join( directoryName, 'node_modules/bootstrap-icons' ) ) );
app.use( function ( req, res, next )
{ res.header( 'Access-Control-Allow-Origin', '*' );
res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept' );
res.header( 'Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS' );
next();
} );
app.use( function ( req, res, next )
{ logger.http( `${applicationName}:${req.method} ${req.originalUrl} - ${req.ip}` );
next();
} );
const genCntrl = require( './controllers/generic' );
function setRouting ()
{ try
{ logger.trace( applicationName + ':index:setRouting:Started' );
app.use( '*', genCntrl.main );
logger.trace( applicationName + ':index:setRouting:Done' );
}
catch ( ex )
{ logger.exception( applicationName + ':index:setRouting:An exception Occurred:[' + ex + ']' );
}
}
async function initializeServices ()
{ try
{ logger.trace( applicationName + ':index:initializeServices: Starting' );
const timeStamp = new Date();
const dbNameArray = dbName.split( '/' );
const appNameString = `Starting ${applicationName}`;
const timeStampString = `Time: ${timeStamp.toLocaleTimeString( 'de-DE' )}`;
const dateString = `Date: ${timeStamp.toLocaleDateString( 'de-DE' )}`;
const portString = `App listening on port [${ApplicationPort}]`;
const dbString = `DB Name: [${dbNameArray[dbNameArray.length - 1]}]`;
const versionString = `Version: [${version}]`;
const lastFixString = `Last Fix: [${lastFix}]`;
const nodeString = `Node.js: [${process.version}]`;
const platformString = `Platform: [${process.platform} ${process.arch}]`;
logger.info( '********************************************************************************' );
logger.info( '*'.padEnd( 21 ,' ' ) + appNameString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21 ,' ' ) + timeStampString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21 ,' ' ) + dateString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21 ,' ' ) + portString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21 ,' ' ) + dbString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21 ,' ' ) + versionString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21 ,' ' ) + lastFixString.padEnd( '58',' ' ) + '*' );
logger.info( '*'.padEnd( 21, ' ' ) + nodeString.padEnd( 58, ' ' ) + '*' );
logger.info( '*'.padEnd( 21, ' ' ) + platformString.padEnd( 58, ' ' ) + '*' );
logger.info( '********************************************************************************' );
db.on( 'error', function ( error )
{ logger.error( `${applicationName}:index:Database connection error: ${error}` );
} );
db.once( 'open', function ()
{ logger.info( `${applicationName}:index:Connected to MongoDB successfully` );
} );
db.on( 'disconnected', function ()
{ logger.warn( `${applicationName}:index:Database disconnected` );
} );
db.on( 'reconnected', function ()
{ logger.info( `${applicationName}:index:Database reconnected` );
} );
logger.trace( applicationName + ':index:initializeServices: Done' );
}
catch ( ex )
{ logger.exception( applicationName + ':index:initializeServices:An exception occured:[' + ex + ']' );
}
}
function main ()
{ try
{ logger.trace( applicationName + ':index:main:Starting' );
setRouting();
initializeServices();
logger.trace( applicationName + ':index:main:Done' );
}
catch ( ex )
{ logger.exception( applicationName + 'index:main:An exception Occurred:[' + ex + ']' );
}
}
app.listen( ApplicationPort, () => { logger.info( `${applicationName} server started successfully on port ${ApplicationPort}` ); } );
module.exports = app;
main();