|
1 | | -(function () { |
2 | | - "use strict"; |
3 | | - |
4 | | - require('dotenv').config(); |
5 | | - |
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'); |
15 | | - |
16 | | - let app, |
17 | | - expressWs, |
18 | | - hbs, |
19 | | - server; |
20 | | - |
21 | | - require('console-stamp')(console, 'HH:MM:ss.l'); |
| 1 | +require('dotenv').config(); |
| 2 | + |
| 3 | +const config = require('./config'), |
| 4 | + cors = require('cors'), |
| 5 | + express = require('express'), |
| 6 | + exphbs = require('express-handlebars'), |
| 7 | + getDayjs = require('./services/dayjs-wrapper'), |
| 8 | + mongodb = require('./services/mongodb'), |
| 9 | + morgan = require('morgan'), |
| 10 | + { setupLogRetention } = require('./services/log-cleanup'), |
| 11 | + removeExpiredSubscriptions = require('./services/remove-expired-subscriptions'); |
| 12 | + |
| 13 | +let app, hbs, server, dayjs; |
| 14 | + |
| 15 | +console.log(`${config.appName} ${config.appVersion}`); |
| 16 | + |
| 17 | +// Schedule cleanup tasks |
| 18 | +function scheduleCleanupTasks() { |
| 19 | + // Run subscription cleanup every 24 hours |
| 20 | + setInterval(async() => { |
| 21 | + try { |
| 22 | + console.log('Running scheduled subscription cleanup...'); |
| 23 | + await removeExpiredSubscriptions(); |
| 24 | + } catch (error) { |
| 25 | + console.error('Error in scheduled subscription cleanup:', error); |
| 26 | + } |
| 27 | + }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds |
| 28 | +} |
22 | 29 |
|
23 | | - console.log(`${config.appName} ${config.appVersion}`); |
| 30 | +morgan.format('mydate', () => { |
| 31 | + return new Date().toLocaleTimeString('en-US', { hour12: false, fractionalSecondDigits: 3 }).replace(/:/g, ':'); |
| 32 | +}); |
24 | 33 |
|
25 | | - // TODO: Every 24 hours run removeExpiredSubscriptions(data); |
| 34 | +// Initialize dayjs at startup |
| 35 | +async function initializeDayjs() { |
| 36 | + dayjs = await getDayjs(); |
| 37 | +} |
26 | 38 |
|
27 | | - morgan.format('mydate', function() { |
28 | | - var df = require('dateformat'); |
29 | | - return df(new Date(), 'HH:MM:ss.l'); |
30 | | - }); |
| 39 | +app = express(); |
31 | 40 |
|
32 | | - app = express(); |
33 | | - expressWs = require('express-ws')(app); |
| 41 | +app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms')); |
34 | 42 |
|
35 | | - app.use(morgan('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms')); |
| 43 | +app.use(cors()); |
36 | 44 |
|
37 | | - app.use(cors()); |
| 45 | +// Configure handlebars template engine to work with dayjs |
| 46 | +hbs = exphbs.create({ |
| 47 | + helpers: { |
| 48 | + formatDate: (datetime, format) => { |
| 49 | + return dayjs(datetime).format(format); |
| 50 | + } |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +// Configure express to use handlebars |
| 55 | +app.engine('handlebars', hbs.engine); |
| 56 | +app.set('view engine', 'handlebars'); |
| 57 | + |
| 58 | +// Handle static files in public directory |
| 59 | +app.use(express.static('public', { |
| 60 | + dotfiles: 'ignore', |
| 61 | + maxAge: '1d' |
| 62 | +})); |
| 63 | + |
| 64 | +// Load controllers |
| 65 | +app.use(require('./controllers')); |
| 66 | + |
| 67 | +// Start server |
| 68 | +async function startServer() { |
| 69 | + await initializeDayjs(); |
| 70 | + await mongodb.connect('rsscloud', config.mongodbUri); |
| 71 | + |
| 72 | + // Setup log retention TTL index |
| 73 | + try { |
| 74 | + await setupLogRetention(); |
| 75 | + } catch (error) { |
| 76 | + console.error('Failed to setup log retention, continuing without it:', error); |
| 77 | + } |
| 78 | + |
| 79 | + // Start cleanup scheduling |
| 80 | + scheduleCleanupTasks(); |
| 81 | + |
| 82 | + server = app.listen(config.port, () => { |
| 83 | + app.locals.host = config.domain; |
| 84 | + app.locals.port = server.address().port; |
| 85 | + |
| 86 | + if (app.locals.host.indexOf(':') > -1) { |
| 87 | + app.locals.host = '[' + app.locals.host + ']'; |
| 88 | + } |
38 | 89 |
|
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); |
| 90 | + console.log(`Listening at http://${app.locals.host}:${app.locals.port}`); |
| 91 | + }) |
| 92 | + .on('error', (error) => { |
| 93 | + switch (error.code) { |
| 94 | + case 'EADDRINUSE': |
| 95 | + console.log(`Error: Port ${config.port} is already in use.`); |
| 96 | + break; |
| 97 | + default: |
| 98 | + console.log(error.code); |
44 | 99 | } |
45 | | - } |
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; |
67 | | - |
68 | | - if (app.locals.host.indexOf(':') > -1) { |
69 | | - app.locals.host = '[' + app.locals.host + ']'; |
70 | | - } |
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 | 100 | }); |
84 | | -}()); |
| 101 | +} |
| 102 | + |
| 103 | +startServer().catch(console.error); |
0 commit comments