|
3 | 3 |
|
4 | 4 | require('dotenv').config(); |
5 | 5 |
|
6 | | - var app, |
| 6 | + const config = require('./config'), |
7 | 7 | cors = require('cors'), |
8 | 8 | express = require('express'), |
9 | | - expressWs, |
10 | 9 | exphbs = require('express-handlebars'), |
11 | | - hbs, |
| 10 | + fs = require('fs'), |
12 | 11 | moment = require('moment'), |
| 12 | + mongodb = require('./services/mongodb'), |
13 | 13 | morgan = require('morgan'), |
14 | | - nconf = require('nconf'), |
15 | | - packageJson = require('./package.json'), |
16 | | - removeExpiredSubscriptions = require('./services/remove-expired-subscriptions'), |
17 | | - syncStruct = require('./services/sync-struct'), |
| 14 | + Promise = require('bluebird'), |
| 15 | + removeExpiredSubscriptions = require('./services/remove-expired-subscriptions'); |
| 16 | + |
| 17 | + let app, |
| 18 | + expressWs, |
| 19 | + hbs, |
18 | 20 | server; |
19 | 21 |
|
20 | 22 | require('console-stamp')(console, 'HH:MM:ss.l'); |
21 | 23 |
|
22 | | - // Setup nconf to use (in-order): |
23 | | - // 1. Overrides |
24 | | - // 2. Command-line arguments |
25 | | - // 3. Environment variables |
26 | | - // 4. Default values |
27 | | - nconf |
28 | | - .overrides({ |
29 | | - 'APP_NAME': 'rssCloudServer', |
30 | | - 'APP_VERSION': packageJson.version, |
31 | | - }) |
32 | | - .argv() |
33 | | - .env() |
34 | | - .defaults({ |
35 | | - "DOMAIN": "localhost", |
36 | | - "PORT": 5337 |
37 | | - }); |
| 24 | + console.log(`${config.appName} ${config.appVersion}`); |
38 | 25 |
|
39 | | - console.log(nconf.get('APP_NAME') + ' ' + nconf.get('APP_VERSION')); |
40 | | - |
41 | | - // Assign where data struct is saved |
42 | | - syncStruct.nameStruct('data/data.json', 'data'); |
43 | | - syncStruct.watchStruct('data', function (err, data) { |
44 | | - if (err) { |
45 | | - console.error(err); |
46 | | - return; |
47 | | - } |
48 | | - setInterval( |
49 | | - function () { |
50 | | - removeExpiredSubscriptions(data); |
51 | | - }, |
52 | | - 1000 * 60 * 24 |
53 | | - ); |
54 | | - }); |
| 26 | + // TODO: Every 24 hours run removeExpiredSubscriptions(data); |
55 | 27 |
|
56 | 28 | morgan.format('mydate', function() { |
57 | 29 | var df = require('dateformat'); |
|
88 | 60 | app.use(require('./controllers')); |
89 | 61 |
|
90 | 62 | // Start server |
91 | | - server = app.listen(nconf.get('PORT'), function () { |
92 | | - app.locals.host = nconf.get('DOMAIN'); |
93 | | - app.locals.port = server.address().port; |
94 | | - |
95 | | - if (app.locals.host.indexOf(':') > -1) { |
96 | | - app.locals.host = '[' + app.locals.host + ']'; |
97 | | - } |
98 | | - |
99 | | - console.log('Listening at http://%s:%s', app.locals.host, app.locals.port); |
100 | | - }) |
101 | | - .on('error', function (error) { |
102 | | - switch (error.code) { |
103 | | - case 'EADDRINUSE': |
104 | | - console.log('Error: Port ' + nconf.get('PORT') + ' is already in use.'); |
105 | | - break; |
106 | | - default: |
107 | | - console.log(error.code); |
108 | | - } |
| 63 | + mongodb.connect(config.mongodbUri) |
| 64 | + .then(() => { |
| 65 | + server = app.listen(config.port, function () { |
| 66 | + app.locals.host = config.domain; |
| 67 | + app.locals.port = server.address().port; |
| 68 | + |
| 69 | + if (app.locals.host.indexOf(':') > -1) { |
| 70 | + app.locals.host = '[' + app.locals.host + ']'; |
| 71 | + } |
| 72 | + |
| 73 | + console.log('Listening at http://%s:%s', app.locals.host, app.locals.port); |
| 74 | + }) |
| 75 | + .on('error', function (error) { |
| 76 | + switch (error.code) { |
| 77 | + case 'EADDRINUSE': |
| 78 | + console.log(`Error: Port ${config.port} is already in use.`); |
| 79 | + break; |
| 80 | + default: |
| 81 | + console.log(error.code); |
| 82 | + } |
| 83 | + }); |
109 | 84 | }); |
110 | 85 | }()); |
0 commit comments