-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
62 lines (49 loc) · 1.43 KB
/
index.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
var express = require('express');
var compress = require('compression');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var auth = require('http-auth');
var Joi = require('joi');
var ConfigSchema = require('./lib/configSchema');
var app = function (config) {
var app = null;
var validate = function (callback) {
Joi.validate(config, ConfigSchema, callback);
};
var prepare = function (config) {
var basic = auth.basic({
realm: config.realm
}, function (username, password, callback) {
callback(username === config.username && password === config.password);
}
);
app = express();
app.use(morgan('dev'));
app.use(compress());
app.use(bodyParser());
app.use(cookieParser());
if (config.auth) app.use(auth.connect(basic));
app.use('/api', require('./lib/api')(config));
app.use('/bower_components', express.static(__dirname + "/bower_components"));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.use(express.static(__dirname + '/public'));
};
validate(function (err, config) {
if (!err) {
prepare(config);
} else {
console.log(err);
}
});
var start = function () {
if (app) app.listen(config.port);
};
return {
start: start
};
};
module.exports = app;