forked from mt-k/storyfinder-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
196 lines (171 loc) · 5.52 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var express = require('express')
, app = express()
, http = require('http').Server(app)
, io = require('socket.io')(http)
, bodyParser = require('body-parser')
, cookieSession = require('cookie-session')
, methodOverride = require('method-override')
, serveStatic = require('serve-static')
, port = 3055
, mysql = require('mysql')
, connection = mysql.createConnection({
host : process.env.MYSQL_HOST || "mysql",
port : process.env.MYSQL_PORT || 3306,
user : process.env.MYSQL_USER || 'storyfinder',
password : process.env.MYSQL_PASSWORD || 'storyfinder',
database : process.env.MYSQL_DATABASE || 'storyfinder',
charset: "utf8_general_ci"
})
, fs = require('fs')
, handleVisit = require('./libs/handle_visit.js')
, async = require('async')
, _ = require('lodash')
, passport = require('passport')
, BasicStrategy = require('passport-http').BasicStrategy
, LocalStrategy = require('passport-local').Strategy
, User = new (require('./models/User.js'))(connection)
, ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn
, exphbs = require('express-handlebars')
, tables = {'articles': true,'articles_entities': true,'articles_tokens': true,'changelogs': true,'changelogs_updates': true,'collections': true,'entities': true,'entities_sentences': true,'log_entities': true,'ngrams': true,'relations': true,'relations_sentences': true,'relationtypes': true,'sentences': true,'sites': true,'tokens': true,'users': true,'visits': true}
;
function startServer(){
http.listen(port, function () {
console.log('Storyfinder listening on port ' + port);
});
}
/*
Initialise database.
*/
connection.query('SHOW TABLES', (err, result, fields) => {
if(err){
throw err;
return;
}
var fieldName = fields[0].name;
for(var row of result) {
var tbl = row[fieldName];
if(typeof tables[tbl] !== 'undefined')
delete tables[tbl];
}
if(!_.isEmpty(tables)){
console.log('Some database tables are missing. Creating tables ' + _.keys(tables).join("\n"));
fs.readFile('./data/sql/schema.sql', (err, content) => {
if(err){
throw err;
return;
}
var queries = content.toString().split("\n\n");
async.each(queries, (query, nextQuery) => {
connection.query(query, nextQuery);
}, startServer);
});
}else{
setImmediate(startServer);
}
});
/*
Authentication
*/
passport.use(new BasicStrategy(
function(username, password, done){
User.verify(username, password, function(err, isValid, user){
if (err) { return done(err); }
if (!isValid) {
return done(null, false, { message: 'Incorrect username or password.' });
}
return done(null, user);
});
}
));
passport.use(new LocalStrategy(
function(username, password, done){
User.verify(username, password, function(err, isValid, user){
if (err) { return done(err); }
if (!isValid) {
return done(null, false, { message: 'Incorrect username or password.' });
}
return done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
if(user == null)
done(null, null);
else
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log('User deserialized');
done(err, user);
});
});
function loggedInHTTP(req, res, next){
if (!req.isAuthenticated || !req.isAuthenticated()) {
passport.authenticate('basic', {session: true})(req, res, next);
}else{
next();
}
}
// parse application/json
app.use(bodyParser.json({limit: '50mb'}))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieSession({secret: process.env.COOKIE_SECRET}));
//MethodOverride
app.use(methodOverride('X-HTTP-Method-Override'))
app.use(methodOverride('_method'))
//Serve static files
app.use(serveStatic(__dirname + '/public'))
app.use(passport.initialize());
app.use(passport.session());
var hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
style: function () { return fs.readFileSync(__dirname + '/public/css/bootstrap.css').toString(); }
},
defaultLayout: 'main'
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
var UsersController = new (require('./controllers/UsersController.js'))(connection, app, passport)
, GraphsController = new (require('./controllers/GraphsController.js'))(connection, app, passport)
, SitesController = new (require('./controllers/SitesController.js'))(connection, app, passport, io)
, EntitiesController = new (require('./controllers/EntitiesController.js'))(connection, app, passport, io)
, ArticlesController = new (require('./controllers/ArticlesController.js'))(connection, app, passport)
, RelationsController = new (require('./controllers/RelationsController.js'))(connection, app, passport)
;
app.get('/', ensureLoggedIn((process.env.PATH_PREFIX || '/') + 'login'), function (req, res) {
fs.readFile(__dirname + '/public/css/bootstrap.css', function(err, style){
if(err){
console.log(err);
return;
}
fs.readFile(__dirname + '/public/_index.html', function(err, html){
if(err){
console.log(err);
return;
}
res.send(html.toString().replace(/\{\{style\}\}/, style.toString()));
});
});
});
app.get('/reseteval', function (req, res) {
var t = ['articles',
'articles_entities',
'articles_tokens',
'changelogs',
'changelogs_updates',
'entities',
'entities_sentences',
'log_entities',
'ngrams',
'relations',
'relations_sentences',
'relationtypes',
'sentences',
'sites',
'tokens'];
for(var i = 0;i < t.length; i++)
connection.query('TRUNCATE ' + t[i]);
res.send('ok');
});