forked from dzt/shopify-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
104 lines (88 loc) · 2.36 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
require('console-stamp')(console, {
colors: {
stamp: 'yellow',
label: 'cyan',
label: true,
metadata: 'green'
}
});
/* Moduless */
const mongoose = require('mongoose');
const express = require('express');
const nunjucks = require('nunjucks');
const bodyParser = require('body-parser');
const http = require('http');
const moment = require('moment');
/* Classes and Models */
const Task = require('./src/classes/Task.js');
const AppRouter = require('./src/classes/AppRouter.js');
const Seller = require('./src/models/Seller');
const Product = require('./src/models/Product');
/* Express Server Setup */
const app = express();
app.server = http.createServer(app);
global.status = 'Stopped';
global.tasks = [];
global.logs = '';
global.config = require('./config')
const PORT = global.config.port;
/* MongoDB Connection */
mongoose.set('debug', false);
mongoose.Promise = Promise;
mongoose.connect(global.config.mongodb_uri, (err) => {
if (!err) {
Product.remove({}, function(err) {
if (!err) {
console.log('All items in the Product collection have been removed.')
}
});
nunjucks.configure('views', {
autoescape: true,
watch: true,
express: app
});
app.use('/resources', express.static(__dirname + '/resources'));
app.use(bodyParser.json({
limit: '50mb'
}));
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb'
}));
app.set('view engine', 'html');
new AppRouter(app);
app.server.listen(process.env.PORT || PORT, () => {
console.log(`App is running on at http://127.0.0.1:${app.server.address().port}`);
});
return;
}
console.error('MongoDB connection error. Please make sure MongoDB is running.\n', err);
process.exit();
});
global.startTasks = function() {
Product.remove({}, function(err) {
if (!err) {
Seller.find({}, (err, tasksQuery) => {
for (let i = 0; i < tasksQuery.length; i++) {
global.tasks.push(new Task(tasksQuery[i]));
global.tasks[i].start();
}
global.status = 'Active';
global.startTime = moment().format('x');
global.needsRestart = false;
global.stoppedTime = null
});
}
});
}
global.stopTasks = function() {
Seller.find({}, (err, tasksQuery) => {
for (let i = 0; i < global.tasks.length; i++) {
global.tasks[i].stop();
}
global.tasks = [];
global.status = 'Stopped';
global.needsRestart = false;
global.stoppedTime = moment().format('x')
});
}