This repository was archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (103 loc) · 2.98 KB
/
index.js
File metadata and controls
127 lines (103 loc) · 2.98 KB
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
/*
* @author koko-ng <koko.fr.mu@gmail.com>
* JSON api beer, only sessionID needs to be given for login -> see webServer
*/
const mongo = require('mongodb').MongoClient,
app = require('express')(),
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
fs = require('fs'),
util = require('util')
const beer = require('./lib/beer/index.js');
const account = require('./lib/account/index.js');
exports = module.exports = main;
const regEmail = /\S+@\S+\.\S+/;
var db;
var beers,
users,
secret,
options;
/**
* Server base
*
* @namespace Server
*
* @param {param} param Parms to be passed to the server
*/
function main(param) {
util.log('starting server...')
if(param.database)
db=param.database;
else {
console.log("You must provide a mongoDB database object, exiting..."); process.exit(1);
}
if(param.secret)
secret=param.secret
else {
secret = 'secret'; console.log("You must provide a secret in production environment");
}
param.port = param.port || 8081
param.hostname = param.hostname || 'localhost'
if(!param.url) {
param.url=param.hostname+':'+param.port; console.log("WARN: Using hostname+':'port for the url")
}
if(!Number.isInteger(param.token_rand_byte)) {
console.log('WARN: param.token_rand_byte has to be an integer');
param.token_rand_byte=32;
}
if(!param.mailValidation) {
console.log("You must provide a mail validation function, exiting..."); process.exit(1);
}
options=param;
param=null;
beers = db.collection('beers');
users = db.collection('users');
checkDirectorySync('uploads');
checkDirectorySync('uploads/144');
checkDirectorySync('uploads/360');
checkDirectorySync('uploads/480');
checkDirectorySync('uploads/720');
checkDirectorySync('uploads/1080');
if('development' == app.get('env')) {
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
})
.use('/sass', require('express').static('sass'));
}
app.use(session({
store: new MongoStore({
db: db,
ttl: 864000 //1 day
}),
saveUninitialized: false,
resave: false,
secret: secret
}))
.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
beer(app, beers, options)
account(app, users, options)
app.get('/', function (req, res) {
res.send('Hello ' + req.session.hello)
})
.get('/acceptCookies', function (req, res) {
req.session.save();
res.send()
})
.use('/uploads', require('express').static(__dirname + '/uploads'))
.listen(options.port, options.hostname)
util.log('Api server listening on ' + options.hostname + ':' + options.port)
};
//function definitions in parallel of mongo connect
function checkDirectorySync(directory) {
try {
fs.statSync(directory);
} catch(e) {
fs.mkdirSync(directory);
}
}