|
1 | | -(function () { |
2 | | - "use strict"; |
| 1 | +require('dotenv').config(); |
3 | 2 |
|
4 | | - require('dotenv').config(); |
| 3 | +const config = require('./config'), |
| 4 | + cors = require('cors'), |
| 5 | + express = require('express'), |
| 6 | + exphbs = require('express-handlebars'), |
| 7 | + fs = require('fs'), |
| 8 | + moment = require('moment'), |
| 9 | + mongodb = require('./services/mongodb'), |
| 10 | + morgan = require('morgan'), |
| 11 | + removeExpiredSubscriptions = require('./services/remove-expired-subscriptions'); |
5 | 12 |
|
6 | | - const config = require('./config'), |
7 | | - cors = require('cors'), |
8 | | - express = require('express'), |
9 | | - exphbs = require('express-handlebars'), |
10 | | - fs = require('fs'), |
11 | | - moment = require('moment'), |
12 | | - mongodb = require('./services/mongodb'), |
13 | | - morgan = require('morgan'), |
14 | | - removeExpiredSubscriptions = require('./services/remove-expired-subscriptions'); |
| 13 | +let app, |
| 14 | + expressWs, |
| 15 | + hbs, |
| 16 | + server; |
15 | 17 |
|
16 | | - let app, |
17 | | - expressWs, |
18 | | - hbs, |
19 | | - server; |
| 18 | +require('console-stamp')(console, 'HH:MM:ss.l'); |
20 | 19 |
|
21 | | - require('console-stamp')(console, 'HH:MM:ss.l'); |
| 20 | +console.log(`${config.appName} ${config.appVersion}`); |
22 | 21 |
|
23 | | - console.log(`${config.appName} ${config.appVersion}`); |
| 22 | +// TODO: Every 24 hours run removeExpiredSubscriptions(data); |
24 | 23 |
|
25 | | - // TODO: Every 24 hours run removeExpiredSubscriptions(data); |
| 24 | +morgan.format('mydate', function() { |
| 25 | + var df = require('dateformat'); |
| 26 | + return df(new Date(), 'HH:MM:ss.l'); |
| 27 | +}); |
26 | 28 |
|
27 | | - morgan.format('mydate', function() { |
28 | | - var df = require('dateformat'); |
29 | | - return df(new Date(), 'HH:MM:ss.l'); |
30 | | - }); |
31 | | - |
32 | | - app = express(); |
33 | | - expressWs = require('express-ws')(app); |
| 29 | +app = express(); |
| 30 | +expressWs = require('express-ws')(app); |
34 | 31 |
|
35 | | - app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms')); |
| 32 | +app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms')); |
36 | 33 |
|
37 | | - app.use(cors()); |
| 34 | +app.use(cors()); |
38 | 35 |
|
39 | | - // Configure handlebars template engine to work with moment |
40 | | - hbs = exphbs.create({ |
41 | | - helpers: { |
42 | | - formatDate: function (datetime, format) { |
43 | | - return moment(datetime).format(format); |
44 | | - } |
| 36 | +// Configure handlebars template engine to work with moment |
| 37 | +hbs = exphbs.create({ |
| 38 | + helpers: { |
| 39 | + formatDate: function (datetime, format) { |
| 40 | + return moment(datetime).format(format); |
45 | 41 | } |
46 | | - }); |
47 | | - |
48 | | - // Configure express to use handlebars |
49 | | - app.engine('handlebars', hbs.engine); |
50 | | - app.set('view engine', 'handlebars'); |
51 | | - |
52 | | - // Handle static files in public directory |
53 | | - app.use(express.static('public', { |
54 | | - dotfiles: 'ignore', |
55 | | - maxAge: '1d' |
56 | | - })); |
57 | | - |
58 | | - // Load controllers |
59 | | - app.use(require('./controllers')); |
60 | | - |
61 | | - // Start server |
62 | | - mongodb.connect('rsscloud', config.mongodbUri) |
63 | | - .then(() => { |
64 | | - server = app.listen(config.port, function () { |
65 | | - app.locals.host = config.domain; |
66 | | - app.locals.port = server.address().port; |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +// Configure express to use handlebars |
| 46 | +app.engine('handlebars', hbs.engine); |
| 47 | +app.set('view engine', 'handlebars'); |
| 48 | + |
| 49 | +// Handle static files in public directory |
| 50 | +app.use(express.static('public', { |
| 51 | + dotfiles: 'ignore', |
| 52 | + maxAge: '1d' |
| 53 | +})); |
| 54 | + |
| 55 | +// Load controllers |
| 56 | +app.use(require('./controllers')); |
| 57 | + |
| 58 | +// Start server |
| 59 | +mongodb.connect('rsscloud', config.mongodbUri) |
| 60 | + .then(() => { |
| 61 | + server = app.listen(config.port, function () { |
| 62 | + app.locals.host = config.domain; |
| 63 | + app.locals.port = server.address().port; |
| 64 | + |
| 65 | + if (app.locals.host.indexOf(':') > -1) { |
| 66 | + app.locals.host = '[' + app.locals.host + ']'; |
| 67 | + } |
67 | 68 |
|
68 | | - if (app.locals.host.indexOf(':') > -1) { |
69 | | - app.locals.host = '[' + app.locals.host + ']'; |
| 69 | + console.log('Listening at http://%s:%s', app.locals.host, app.locals.port); |
| 70 | + }) |
| 71 | + .on('error', function (error) { |
| 72 | + switch (error.code) { |
| 73 | + case 'EADDRINUSE': |
| 74 | + console.log(`Error: Port ${config.port} is already in use.`); |
| 75 | + break; |
| 76 | + default: |
| 77 | + console.log(error.code); |
70 | 78 | } |
71 | | - |
72 | | - console.log('Listening at http://%s:%s', app.locals.host, app.locals.port); |
73 | | - }) |
74 | | - .on('error', function (error) { |
75 | | - switch (error.code) { |
76 | | - case 'EADDRINUSE': |
77 | | - console.log(`Error: Port ${config.port} is already in use.`); |
78 | | - break; |
79 | | - default: |
80 | | - console.log(error.code); |
81 | | - } |
82 | | - }); |
83 | | - }); |
84 | | -}()); |
| 79 | + }); |
| 80 | + }); |
0 commit comments