|
1 | | -/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |
2 | | -App/Filename : BASE/app.js |
3 | | -Description : Initializes nodejs |
4 | | -Author : RAk3rman |
5 | | -\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/ |
6 | | - |
7 | | -//===================================================// |
8 | | -// --- Initialize Packages and Routers --- // |
9 | | -//===================================================// |
10 | | - |
11 | | -//Declare Packages |
12 | | -let express = require('express'); |
13 | | -let session = require('express-session'); |
14 | | -let morgan = require('morgan'); |
15 | | -let createError = require('http-errors'); |
16 | | -let cookieParser = require('cookie-parser'); |
17 | | -let bodyParser = require('body-parser'); |
18 | | -let ip = require('ip'); |
19 | | -let uuidv4 = require('uuid/v4'); |
20 | | - |
21 | | -//Setup Local Database |
22 | | -let dataStore = require('data-store'); |
23 | | -let storage = new dataStore({path: './config/sysConfig.json'}); |
24 | | - |
25 | | -//System Config Checks - - - - - - - - - - - - - - - - - |
26 | | -//Session Secret Check |
27 | | -let session_secret = storage.get('session_secret'); |
28 | | -if (session_secret === undefined) { |
29 | | - let newSecret = uuidv4(); |
30 | | - storage.set('session_secret', newSecret); |
31 | | - console.log('Config Manager: Session Secret Set - ' + newSecret); |
32 | | -} |
33 | | -//Console Port Check |
34 | | -let console_port = storage.get('console_port'); |
35 | | -if (console_port === undefined) { |
36 | | - storage.set('console_port', 3000); |
37 | | - console.log('Config Manager: Port Set to DEFAULT: 3000'); |
38 | | -} |
39 | | -//End of System Config Checks - - - - - - - - - - - - - - |
40 | | - |
41 | | -//Declare App |
42 | | -const app = express(); |
43 | | -app.set('view engine', 'ejs'); |
44 | | - |
45 | | -//Initialize Exit Options (for Testing Environments) |
46 | | -let exitOpt = require('./config/exitOpt.js'); |
47 | | -setTimeout(exitOpt.testCheck, 3000); |
48 | | - |
49 | | -//Routers |
50 | | -let mainRoutes = require('./routes/mainRoutes.js'); |
51 | | - |
52 | | -//Express Processes/Packages Setup |
53 | | -app.use(session({ |
54 | | - secret: storage.get('session_secret'), |
55 | | - resave: true, |
56 | | - saveUninitialized: true |
57 | | -})); |
58 | | -app.use(cookieParser()); |
59 | | -app.use(bodyParser.urlencoded({extended: true})); |
60 | | -app.use(bodyParser.json()); |
61 | | -app.use(morgan('dev')); |
62 | | -app.use(express.json()); |
63 | | -app.use(express.urlencoded({extended: false})); |
64 | | - |
65 | | -//Import Static Files to Webpages |
66 | | -app.use('/static', express.static(process.cwd() + '/static')); |
67 | | - |
68 | | -//End of Initialize Packages and Routers - - - - - - - - |
69 | | - |
70 | | - |
71 | | -//===================================================// |
72 | | -// --- LEMAgent Config Routes/Logic --- // |
73 | | -//===================================================// |
74 | | - |
75 | | -//Create Routes |
76 | | -app.get('/', mainRoutes.homeRoute); |
77 | | - |
78 | | -//End of LEMAgent Config Routes/Logic - - - - - - - - - |
79 | | - |
80 | | - |
81 | | -//===================================================// |
82 | | -// --- Error Handlers --- // |
83 | | -//===================================================// |
84 | | - |
85 | | -//404 - Send to Error Handler |
86 | | -app.use(function (req, res, next) { |
87 | | - next(createError(404)); |
88 | | -}); |
89 | | - |
90 | | -// Error Handler Logic |
91 | | -app.use(function (err, req, res, next) { |
92 | | - //Determine Message |
93 | | - res.locals.message = err.message; |
94 | | - res.locals.error = req.app.get('env') === 'development' ? err : {}; |
95 | | - //Render Error Page |
96 | | - res.status(err.status || 500); |
97 | | - res.render('pages/error.ejs', {title: 'Error'}); |
98 | | -}); |
99 | | - |
100 | | -//End of Error Handler - - - - - - - - - - - - - - - - - |
101 | | - |
102 | | - |
103 | | -//===================================================// |
104 | | -// --- External Connections Setup --- // |
105 | | -//===================================================// |
106 | | - |
107 | | -//Port Listen |
108 | | -let http = require('http'); |
109 | | -let server = http.createServer(app); |
110 | | -server.listen(storage.get('console_port'), function () { |
111 | | - console.log(' '); |
112 | | - console.log('============================================'); |
113 | | - console.log(' Base-WebFramework-NodeJS | RAk3rman 2019 '); |
114 | | - console.log('============================================'); |
115 | | - console.log('Web Page Accessable at: ' + ip.address() + ":" + storage.get('console_port')); |
116 | | - console.log(' '); |
117 | | -}); |
118 | | - |
119 | | -//End of External Connections Setup - - - - - - - - - - |
120 | | - |
121 | | -//Export Express |
122 | | -module.exports = app; |
| 1 | +/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |
| 2 | +Filename : base/app.js |
| 3 | +Desc : main application file |
| 4 | +Author(s): RAk3rman |
| 5 | +\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/ |
| 6 | + |
| 7 | +// Packages and configuration - - - - - - - - - - - - - - - - - - - - - - - - - |
| 8 | + |
| 9 | +// Declare packages |
| 10 | +const path = require('path'); |
| 11 | +const dataStore = require('data-store'); |
| 12 | +const config_storage = new dataStore({path: './config/config.json'}); |
| 13 | +const chalk = require('chalk'); |
| 14 | +const pkg = require('./package.json'); |
| 15 | +const moment = require('moment'); |
| 16 | +const wipe = chalk.white; |
| 17 | + |
| 18 | +// Print header to console |
| 19 | +console.clear(); |
| 20 | +console.log(chalk.blue.bold('\nBase Webframework v' + pkg.version + ((process.argv[2] !== undefined) ? ' | ' + process.argv[2].toUpperCase() : "" ))); |
| 21 | +console.log(chalk.white('--> Contributors: ' + pkg.author)); |
| 22 | +console.log(chalk.white('--> Description: ' + pkg.description)); |
| 23 | +console.log(chalk.white('--> Github: ' + pkg.homepage + '\n')); |
| 24 | + |
| 25 | +// Check configuration values |
| 26 | +let setup = require('./config/setup.js'); |
| 27 | +setup.check_values(config_storage); |
| 28 | + |
| 29 | +// End of Packages and configuration - - - - - - - - - - - - - - - - - - - - - - |
| 30 | + |
| 31 | +// Fastify and main functions - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 32 | + |
| 33 | +// Declare fastify |
| 34 | +const fastify = require('fastify')({logger: false}); |
| 35 | + |
| 36 | +// Prepare rendering template |
| 37 | +fastify.register(require('point-of-view'), { |
| 38 | + engine: { |
| 39 | + handlebars: require('handlebars') |
| 40 | + }, |
| 41 | +}) |
| 42 | +fastify.register(require('fastify-static'), { |
| 43 | + root: path.join(__dirname, 'public'), |
| 44 | + prefix: '/public/', |
| 45 | +}) |
| 46 | +// fastify.register(require('fastify-socket.io'), {}) |
| 47 | +// fastify.register(require('fastify-formbody')) |
| 48 | +// fastify.register(require('fastify-rate-limit'), { |
| 49 | +// global: false, |
| 50 | +// max: 250, |
| 51 | +// timeWindow: '1 minute' |
| 52 | +// }) |
| 53 | + |
| 54 | +// Routers |
| 55 | +let error_routes = require('./routes/error-routes.js'); |
| 56 | + |
| 57 | +// Import routes |
| 58 | +error_routes(fastify); |
| 59 | + |
| 60 | +// Home page |
| 61 | +fastify.get('/', (req, reply) => { |
| 62 | + reply.view('/templates/home.hbs', { |
| 63 | + title: "Home" |
| 64 | + }) |
| 65 | + console.log(wipe(`${chalk.bold.magenta('Fastify')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] GET /`)); |
| 66 | +}) |
| 67 | + |
| 68 | +// End of Fastify and main functions - - - - - - - - - - - - - - - - - - - - - - |
| 69 | + |
| 70 | + |
| 71 | +// Setup external connections - - - - - - - - - - - - - - - - - - - - - - - - - |
| 72 | + |
| 73 | +// Start webserver using config values |
| 74 | +console.log(wipe(`${chalk.bold.magenta('Fastify')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Attempting to start http webserver on port ` + config_storage.get('webserver_port'))); |
| 75 | +fastify.listen(config_storage.get('webserver_port'), function (err) { |
| 76 | + if (err) { |
| 77 | + fastify.log.error(err) |
| 78 | + process.exit(1) |
| 79 | + } |
| 80 | + console.log(wipe(`${chalk.bold.magenta('Fastify')}: [` + moment().format('MM/DD/YY-HH:mm:ss') + `] Running http webserver on port ` + config_storage.get('webserver_port'))); |
| 81 | + // Check if we are testing |
| 82 | + const testing = require('./config/testing.js'); |
| 83 | + testing.testCheck(); |
| 84 | +}) |
| 85 | + |
| 86 | +// End of Setup external connections - - - - - - - - - - - - - - - - - - - - - - |
0 commit comments