-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
144 lines (120 loc) · 3.62 KB
/
server.js
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
'use strict';
//var itemsapi = require('../itemsapi');
var itemsapi = require('itemsapi');
var ItemsAPI = require('itemsapi-node');
var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var _ = require('lodash');
var bodyParser = require('body-parser');
var express = require('express');
var config = require('./config/index').get()
var configService = require('./src/services/config')
var colors = require('colors')
var figlet = require('figlet')
var mongoose = require('./config/mongoose')
console.log(figlet.textSync('itemsapi'))
console.log('Ideas or issues - https://github.com/itemsapi/starter/issues');
console.log();
var listeners = require('./src/listeners/index')
var storage = require('./config/storage')
itemsapi.init({
server: config.server,
elasticsearch: config.elasticsearch,
redis: config.redis,
mongodb: {
uri: config.mongodb.uri
},
collections: {
db: 'mongodb'
},
allowed_ips: config.allowed_ips
})
// standard app syntax
var app = itemsapi.get('express');
var urlHelper = require('./src/helpers/url');
var statusHelper = require('./src/helpers/status');
var nunenv = require('./src/nunenv')(app, 'views', {
autoescape: true,
watch: true,
noCache: true
})
app.use('/bootstrap', express.static('node_modules/bootstrap'));
app.use('/assets', express.static('assets'));
//app.use('/libs', express.static('node_modules/@bower_components'));
app.use('/libs', express.static('bower_components'));
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: false, limit: '5mb' }));
app.set('view engine', 'html.twig');
app.set('view cache', false);
app.engine('html.twig', nunenv.render);
/**
* middleware route
*/
app.all('*', function(req, res, next) {
req.is_installation = true
req.step = 2
req.base_url = req.protocol + '://' + req.get('host')
return configService.getConfig()
.timeout(2000)
.then(function(dynamic_config) {
req.dynamic_config = dynamic_config
if (dynamic_config.name) {
req.name = dynamic_config.name
req.step = dynamic_config.step
req.is_installation = false
req.settings = dynamic_config
res.locals.settings = dynamic_config
}
var client = new ItemsAPI('http://localhost:' + config.server.port + '/api/v1', req.name)
req.client = client;
//nunenv.addGlobal('step', req.step)
//nunenv.addGlobal('name', req.name)
res.locals.step = req.step
res.locals.name = req.name
return next()
})
.catch(function(err) {
return res.status(500).json({
message: 'unexpected error - probably mongodb is required'
});
})
})
var session = require('express-session');
var cookieParser = require('cookie-parser');
app.use(cookieParser())
//app.use(i18n.init);
var RedisStore = require('connect-redis')(session);
//var passport = require('passport');
//var LocalStrategy = require('passport-local').Strategy;
app.use(session({
secret: 'itemsapi-starter',
saveUninitialized: false,
store: new RedisStore({
prefix: 'front_sess:',
host: config.redis.host,
port: config.redis.port,
pass: config.redis.auth_pass
}),
cookie: config.cookie,
resave: false
}));
app.use(require('flash')());
var admin = require('./admin')
app.use('/admin', admin)
require('./config/passport')(app)
app.get('/*', function(req, res, next) {
req.session.flash = []
next()
})
/**
* middleware route
*/
app.all('*', function(req, res, next) {
//res.locals = _.assignIn(res.locals, config.template_variables)
res.locals.settings = req.settings
res.locals.user = req.user
//console.log(req.user);
next();
})
require('./src/routes')(app)
module.exports = itemsapi