-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (117 loc) · 4.45 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
145
146
147
148
149
150
var swig = require('swig');
var React = require('react');
import { StaticRouter } from "react-router";
import { renderToString } from "react-dom/server";
var alt = require('./app/alt');
var Q = require('q');
var EventEmitter = require('events').EventEmitter
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
mongoose.Promise = Q.Promise;
var routes = require('./app/routes');
var config = require('./config');
var app = express();
mongoose.connect(config.mongodbPath, {
useMongoClient: true
})
app.set('port', config.port);
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));
if (config.trustProxy) {
app.set('trust proxy', config.trustProxy);
}
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser(config.sessionSecret));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
resave: true,
saveUninitialized: true,
secret: config.sessionSecret,
store: new MongoStore({ url: config.mongodbPath }),
cookie: {
secure: config.site.search('https') === 0
}
}));
var services = new EventEmitter;
app.use(function(req, res, next) {
res.messages = [];
next();
})
var authRoute = require("./src/routes/auth")(app, '/api/auth', config, services);
app.use('/api/auth', authRoute)
var adminRoute = require("./src/routes/admin")(app, '/api/admin', config, services);
app.use('/api/admin', adminRoute)
var ssoRoute = require("./src/routes/sso")(app, '/api/sso', config, services);
app.use('/api/sso', ssoRoute)
app.use('/api', function (req, res) {
res.status(404).end('api not found')
})
var mongo_express = require('mongo-express/lib/middleware');
var mongo_express_config = require('./mongo_express_config');
(function (mongo_express_config) {
var URL = require('url');
var temp = URL.parse(config.mongodbPath);
if ((/\/\/[^\/,]+,[^\/,]+[^\/]+\//).test(config.mongodbPath)) {
var pathes = (/\/\/([^\/,]+,[^\/,]+[^\/]+)\//).exec(config.mongodbPath)[1].split(/,/g);
mongo_express_config.mongodb.server = pathes.map(function (str) {
var temp = str.split(':');
mongo_express_config.mongodb.port = parseInt(temp[1]) || mongo_express_config.mongodb.port
return temp[0]
})
mongo_express_config.mongodb.port = mongo_express_config.mongodb.port || 27017;
} else {
mongo_express_config.mongodb.server = temp.hostname;
mongo_express_config.mongodb.port = parseInt(temp.port) || 27017;
}
console.log('current mongo_express db setting')
console.log(JSON.stringify(mongo_express_config.mongodb.server, 0, 4))
console.log(JSON.stringify(mongo_express_config.mongodb.port, 0, 4))
if (temp.auth) {
mongo_express_config.mongodb.auth.push({
database: temp.pathname.replace(/^\//, ''),
username: temp.auth.split(':')[0],
password: decodeURIComponent(temp.auth.split(':').slice(1).join(':'))
})
}
mongo_express_config.basicAuth.username = config.dbManagerAccount
mongo_express_config.basicAuth.password = config.dbManagerPassword
} (mongo_express_config));
if (config.enableDbManager) {
app.use(config.dbManagerPath, mongo_express(mongo_express_config))
}
app.use(function(req, res) {
const context = {};
// alt.getActions('AppActions').loginStatusSuccess({username: 'mmis1000'})
if (req.user) {
alt.getActions('AppActions').loginStatusSuccess(req.user.toSafeObject())
} else {
alt.getActions('AppActions').loginStatusFail(null);
}
alt.getActions('AppActions').pathChange(req.path);
alt.getActions('AppActions').searchChange(req.query);
alt.getActions('AppActions').setDomain(config.site);
alt.getActions('AppActions').addMessages(res.messages);
// console.log(res.messages)
var snapshot = JSON.stringify(alt.takeSnapshot()).replace(/</g, '\\u003c');;
// console.log(snapshot)
const html = renderToString(
<StaticRouter location={req.url} context={context}>
{routes}
</StaticRouter>
);
alt.flush();
const page = swig.renderFile('views/index.html', { html, snapshot });
res.status(200).send(page);
});
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});