Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let fs = require('fs'),
- os = require('os'),
- path = require('path'),
- winston = require('winston'),
- express = require('express');
- /**
- * Returns an Application.
- * @param {object} options the parent directory
- * @returns {Application} the Application function.
- */
- let Application = function Application ( options ) {
- let opts = options || {},
- directoryInformation = opts['directoryInformation'],
- //sys argv options :> .-. ;-;
- // verbosity , v , default:info,
- argv = opts['argv'],
- verbosity = opts['argv']['verbosity'], /* ;-; 26:me.config */
- me = this;
- // me.viewDirectories = [];
- // or put it in me._init();
- me._init(opts);
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.config = require(path.join(directoryInformation['Etc']['EtcDirectory'],
- 'config.json'));
- me.logger = me.initLogManager({'directoryInformation': directoryInformation,
- 'config':me.config});
- me.logger.log('info',argv);
- me.logger.log('info', 'Application CTOR( options ) { ... }');
- me.app = me.initExpress({'directoryInformation': directoryInformation,
- 'config':me.config});
- me.viewDirectories.push(directoryInformation['Application']['TemplatesDirectory']);
- me.db = me.initDB({"application": me.app,
- 'directoryInformation': directoryInformation,
- 'config':me.config,
- 'logger':me.logger});
- me.assets = me.initAssets({"application": me.app,
- 'directoryInformation': directoryInformation,
- 'config':me.config,
- 'logger':me.logger,
- "datastore": me.db });
- me.extensions = me.initExtensions({"application": me.app,
- 'directoryInformation': directoryInformation,
- 'config':me.config,
- 'logger':me.logger,
- "datastore": me.db,
- "viewDirectories": me.viewDirectories});
- me.db.initialize({'application': me.app,
- 'directoryInformation': directoryInformation,
- 'config': me.config,
- 'logger':me.logger});
- me.initRoutes({"application": me.app,
- "directoryInformation": directoryInformation,
- "config": me.config,
- 'logger':me.logger,
- "datastore": me.db});
- };
- /**
- * Initializes the Application object's member variables.
- * @param {object} options the parent directory
- * @returns {Application} the Application function.
- */
- Application.prototype._init = function _init ( options ) {
- let opts = options || {},
- directoryInformation = opts['directoryInformation'],
- //sys argv options :> .-. ;-;
- // verbosity , v , default:info,
- argv = opts['argv'],
- verbosity = opts['argv']['verbosity'], /* ;-; 26:me.config */
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.viewDirectories = [];
- }
- /**
- * Returns an Winston Logger Object.
- * @param {object} options the parent directory
- * @return {winston} the Logger Object.
- */
- Application.prototype.initLogManager = function initLogManager ( options ) {
- let opts = options || {},
- directoryInformation = opts['directoryInformation'],
- config = opts['config'],
- me = this;
- let winston = require('winston'); // +++ check goto win
- return new winston.Logger({
- level: config['logger']['verbosity'],
- transports: [
- new (winston.transports.Console)({colorize: true}),
- new (winston.transports.File)({ filename: path.join(directoryInformation['Var']['Log']['LogDirectory'],'log.json') })
- ]
- });
- };
- /**
- * Returns an Express Application Object.
- * @param {object} options the parent directory
- * @return {express} the Application function.
- */
- Application.prototype.initExpress = function initExpress ( options ) {
- let opts = options || {},
- directoryInformation = opts['directoryInformation'],
- config = opts['config'],
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.logger.log('info','Application.initDB(' + options +')');
- let app = express();
- let bodyParser =require('body-parser'),
- cookieParser =require('cookie-parser'),
- expressSession =require('express-session');
- app.locals.pretty = true;
- app.use(express.static(directoryInformation['Application']['StaticDirectory']));
- app.use(bodyParser.urlencoded({extended:false}));
- app.use(bodyParser.json({inflate:true}));
- app.use(cookieParser());
- app.use(expressSession({
- secret: 'keyboard cat',
- resave: false,
- saveUninitialized: false
- }));
- app.use(require('connect-flash')());
- app.use(function (req, res, next) {
- res.locals.messages = require('express-messages')(req, res);
- next();
- });
- app.use(function(req,res,next) {
- //request.headers['user-agent']
- console.log(req.get('user-agent'));
- next();
- });
- app.set('views', me.viewDirectories );
- app.set('view engine', 'pug');
- return app;
- };
- /**
- * Returns the Datastore Object.
- * @param {object} options the parent directory
- * @return {express} the instantiated Datastore object.
- */
- Application.prototype.initDB = function initDB ( options ) {
- let opts = options || {},
- application = opts['application'],
- directoryInformation = opts['directoryInformation'],
- config = opts['config'],
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.logger.log('info','ApplicationFactory.initDB(' + options +')');
- let DataStore = require(
- path.join ( opts ['directoryInformation'].baseDirectory , path.join ( 'Datastore' , 'index.js' ) )
- );
- let datastore = new DataStore(opts);
- // Here's the chance to alter the datastore before sending it to the main Application Object.
- return datastore;
- };
- /**
- * Returns the Datastore Object.
- * @param {object} options the parent directory
- * @return {express} the instantiated Datastore object.
- */
- Application.prototype.initAssets = function initAssets ( options ) {
- let opts = options || {},
- application = opts['application'],
- directoryInformation = opts['directoryInformation'],
- config = opts['config'],
- logger = opts['logger'],
- datastore = opts['datastore']['db'],
- models = datastore['sequelize']['models'],
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.logger.log('info', 'Application.initAssets(' + options +')');
- let AssetManager = require(
- path.join ( directoryInformation['assetsDirectory'],
- 'main.js' )
- );
- let assetManager = new AssetManager(opts);
- // Here's the chance to alter the datastore before sending it to the main Application Object.
- return assetManager;
- };
- /**
- * Returns the Datastore Object.
- * @param {object} options the parent directory
- * @return {express} the instantiated Datastore object.
- */
- Application.prototype.initExtensions = function initExtensions ( options ) {
- let opts = options || {},
- application = opts['application'],
- directoryInformation = opts['directoryInformation'],
- config = opts['config'],
- logger = opts['logger'],
- datastore = opts['datastore']['db'],
- models = datastore['sequelize']['models'],
- viewDirectories = opts['viewDirectories'],
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.logger.log('info', 'Application.initExtensions(' + options +')');
- let ExtensionManager = require(
- path.join ( directoryInformation['Application']['ApplicationDirectory'],
- 'extensionmanager.js' )
- );
- let extensionManager = new ExtensionManager(opts);
- // Here's the chance to alter the datastore before sending it to the main Application Object.
- return extensionManager;
- };
- /**
- * Returns the Datastore Object.
- * @param {object} options the parent directory
- * @return {express} the instantiated Datastore object.
- */
- Application.prototype.initRoutes = function initRoutes ( options ) {
- let opts = options || {},
- application = opts.application,
- app = application,
- directoryInformation = opts.directoryInformation,
- config = opts.config,
- logger = opts['logger'],
- datastore = opts['datastore'],
- db = datastore.db,
- models = db.sequelize.models,
- passport =require('passport'),
- passportLocal =require('passport-local'),
- LocalStrategy =passportLocal.Strategy,
- bCrypt =require('bcrypt'),
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.logger.log('info', 'Application.initRoutes(' + options +')');
- app.use(passport.initialize());
- app.use(passport.session());
- passport.serializeUser(function(user, done) {
- done(null, user.id);
- });
- passport.deserializeUser(function(id, done) {
- datastore.models['Member'].model.find({where: {id: id}}).then(function(user){
- done(null, user);
- }).error(function(err){
- done(err, null);
- });
- });
- passport.use('local-login',
- new LocalStrategy({
- usernameField: 'email',
- passwordField: 'password',
- passReqToCallback: true // allows us to pass back the entire request to the callback
- }, function(req, email, password, done) {
- var compareHash = function(password,hash) {
- var bcrypt = require('bcrypt');
- return bcrypt.compareSync(password, hash);
- };
- datastore.models['Member'].model.find({ where: { email: email }}).then(function(user) {
- if (!user) {
- done(null, false, { message: 'Unknown user' });
- }
- if (!compareHash(password,user.password)) {
- return done(null, false, {
- message: 'Incorrect password.'
- });
- }
- var userinfo = user.get();
- return done(null, userinfo);
- }).error(function(err){
- done(err);
- });
- }));
- passport.use('local-register',
- new LocalStrategy({
- usernameField: 'email',
- passwordField: 'password',
- passReqToCallback: true // allows us to pass back the entire request to the callback
- },function(req, email, password, done) {
- console.dir(datastore.models);
- //datastore.models['Member'].
- var generateHash = function(password) {
- return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);
- };
- datastore.models['Member']['model'].findOne({where: {email: email}}).then(function(user) {
- if (user)
- {
- return done(null, false, {
- message: 'That email is already taken'
- });
- }
- else
- {
- var userPassword = generateHash(password);
- var data =
- {
- email: email,
- password: userPassword,
- firstname: req.body.firstname,
- lastname: req.body.lastname
- };
- datastore.models['Member']['model'].create(data).then(function(newUser, created) {
- if (!newUser) {
- return done(null, false);
- }
- if (newUser) {
- return done(null, newUser);
- }
- });
- }
- });
- }));
- me.app.get('/', function( req , res ) {
- res.render('basic/index', { ds:datastore, lg:logger, user:req.user,expressFlash:req.flash('success') });
- });
- me.app.get('/login', function( req , res ) {
- res.render('basic/login', { ds:datastore, lg:logger,expressFlash:req.flash('success') });
- });
- me.app.post('/login',
- passport.authenticate('local-login', {
- successRedirect: '/',
- failureRedirect: '/login'
- }));
- me.app.get('/register', function( req , res ) {
- res.render('basic/register', { ds:datastore, lg:logger, expressFlash:req.flash('success') });
- });
- me.app.post('/register',
- passport.authenticate('local-register', {
- successRedirect: '/',
- failureRedirect: '/register'
- }));
- me.app.get('/logout', function(req,res) {
- req.logout();
- res.redirect('/');
- });
- };
- /**
- * Returns the Datastore Object.
- * @param {object} options the parent directory
- * @return {express} the instantiated Datastore object.
- */
- Application.prototype.run = function run ( options ) {
- let opts = options || {},
- application = opts['application'],
- directoryInformation = opts['directoryInformation'],
- config = opts['config'],
- logger = opts['logger'],
- me = this;
- /*<-Class related
- Function Related
- Log Related
- Tab delimited beautify standard */
- me.logger.log('info', 'Application.run(' + options +')');
- me.app.listen(3000, function (err) {
- me.logger.log('info', '[http] listening on port 3000!');
- });
- };
- module.exports = Application;
Add Comment
Please, Sign In to add comment