-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (141 loc) · 4.85 KB
/
app.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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var cors = require('cors');
var compression = require('compression')
var mongoose = require('mongoose');
var User = require('./mongoSchemes/user');
var Role = require('./mongoSchemes/role');
var Drink = require('./mongoSchemes/drink');
var routes = require('./routes/routes');
var bcrypt = require('bcrypt-nodejs');
var webpush = require('web-push');
global.APPROOTPATH = path.resolve(__dirname);
var app = express();
app.use(cors());
// view engine setup
// to delete?
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/api', routes);
app.use('/public', express.static(path.join(__dirname, 'public')));
//app.use('/attachments', express.static(path.join(__dirname, 'attachments')));
app.get('/ngsw-worker.js', function(req, res) {
res.sendFile(__dirname + '/public/ngsw-worker.js');
});
//default route:
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
var vapidKeys = {
publicKey: 'BKTnbc-Ix1NCqty80oTIeLcGTTwmneo6Y9LwHSoxp7lgtmBDYfHUY0MQIvg2wfyzJZ0hdKZ_tfo7FtiEq3lGKxw',
privateKey: 'XofyPUHu8KSJCOmfH7PJN-cw3vNWmXHSJDX1ohqrApc'
}
webpush.setGCMAPIKey('AIzaSyCuFlNg01aXLvGkI1ValqTFLS7SlDcttrE');
//push stuff
webpush.setVapidDetails(
'mailto:[email protected]',
vapidKeys.publicKey,
vapidKeys.privateKey
);
//mongostuff
// Connection URL
//local
var url = 'mongodb://kjgAdmin:kjg2017@localhost:27017/kjgapp';
// uberspace
//var url = 'mongodb://kjgAdmin:kjg2017@localhost:21197/kjgapp';
// Use connect method to connect to the server
// mongo user is kjgAdmin, pw: kjg2017
mongoose.connect(url, { useMongoClient: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
checkRoles(checkUsers);
checkBlitzKolben();
console.log("Connected successfully to server");
});
function checkRoles(callback) {
Role.find((err, roles) => {
if (err) {
console.log(err);
} else {
if (roles.length <= 0) {
var adminRole = new Role({ name: "Admin" });
var userRole = new Role({ name: "Knecht" });
var superAdminRole = new Role({ name: "SuperAdmin" });
adminRole.save(() => {
if (err) {
console.log(err);
} else {
console.log("AdminRole created successfully");
}
userRole.save(() => {
if (err) {
console.log(err);
} else {
console.log("UserRole created successfully");
superAdminRole.save(() => {
if (err) {
console.log(err);
} else {
console.log("SuperAdminRole created successfully");
callback();
}
});
}
})
});
} else {
callback();
}
}
});
}
function checkUsers() {
User.find((err, users) => {
if (users.length === 0) {
Role.findOne({ name: "SuperAdmin" }, (err, role) => {
if (err) {
console.log(err);
}
bcrypt.hash("admin", null, null, function(err, hash) {
var user = new User({ name: "Admin", password: hash, role: role._id });
user.save((err, user) => {
if (err) {
console.log(err);
} else {
console.log("Superadmin created successfully")
}
});
});
})
}
});
}
function checkBlitzKolben() {
console.log("checkBlitzKolben");
Drink.findOne({ name: "Blitzkolben" }, (err, drink) => {
if (err) {
console.log(err);
}
if (!drink) {
var blitzi = new Drink({ name: "Blitzkolben" });
blitzi.save((err, drink) => {
if (err) {
console.log(err);
} else {
console.log("Blitzkolben created successfully")
}
});
}
})
}
module.exports = app;