-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (62 loc) · 2.06 KB
/
index.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
const express = require('express');
const morgan = require('morgan');
const http = require('http');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const cron = require('node-cron');
const { getSales, saveSales } = require('./services/util');
const routes = require('./routes');
const { initiateDB } = require('./services');
const { createLogger, transports, format } = require('winston');
const PORT = 5000;
/**
* express application
*/
const app = express();
initiateDB();
const server = http.Server(app);
const logDate = new Date().toISOString().substring(0, 10);
if (!fs.existsSync('logs')) {
fs.mkdirSync('logs');
}
// Create log file
const accessLogStream = fs.createWriteStream(
path.join(__dirname, `./logs/${logDate}.log`),
{ flags: 'a+' }
);
// Create cron log file
const cronLogs = path.join(__dirname, `./logs/cron/${logDate}.log`);
const logger = createLogger({
level: 'info',
format: format.combine(format.json(), format.timestamp()),
transports: [new transports.File({ filename: cronLogs, level: 'info' })],
});
// Send specific information to morgan for logging
app.use(
morgan(
':remote-addr\t\t:remote-user\t\t:date\t\t":method :url HTTP/:http-version"\t\t:status\t\t:response-time ms\t\t:res[content-length]\t\t":referrer"\t\t":user-agent"',
{ stream: accessLogStream }
)
);
app.use(cors({ origin: true, credentials: true }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/v1', routes);
// Catch none-existing routes and other errors
app.use((req, res, next) => {
return res.status(404).json({
status: 'error',
message: 'Route not found.',
});
});
// Schedule tasks to run daily on the server.
cron.schedule('0 0 * * *', async () => {
// cron.schedule('* * * * *', async () => {
const { ps5Sales, xboxSales, date, responseTime } = await getSales();
const salesLog = await saveSales(ps5Sales, xboxSales, date, responseTime);
logger.log('info', salesLog);
});
server.listen(PORT, () => {
console.error(`Server running on http://localhost:${PORT}.`);
});