135 lines
3.5 KiB
JavaScript
135 lines
3.5 KiB
JavaScript
const path = require('path');
|
|
const mysql = require('mysql');
|
|
const express = require('express');
|
|
const moment = require('moment-timezone');
|
|
const fs = require('fs');
|
|
const bodyParser = require('body-parser');
|
|
|
|
/* Database setup */
|
|
|
|
const dbPool = mysql.createPool({
|
|
host: process.env.MYSQL_HOST || 'localhost',
|
|
user: process.env.MYSQL_USER || 'root',
|
|
password: process.env.MYSQL_PASSWORD || undefined,
|
|
database: process.env.MYSQL_DATABASE || 'vizon',
|
|
socketPath: process.env.MYSQL_SOCKET_PATH || undefined,
|
|
timezone: process.env.TZ || 'local',
|
|
|
|
debug: process.env.NODE_ENV === 'development',
|
|
|
|
});
|
|
|
|
const tableNames = {
|
|
drawings: process.env.MYSQL_TABLE_DRAWINGS || 'vizon_drawings',
|
|
users: process.env.MYSQL_TABLE_USERS || 'vizon_users',
|
|
rankings: 'vizon_web_rankings',
|
|
};
|
|
|
|
/* Frontend server setup */
|
|
|
|
const frontendDir = path.resolve(__dirname, 'dist');
|
|
|
|
const app = express();
|
|
|
|
app.set('port', (process.env.PORT || 3000));
|
|
|
|
if (process.NODE_ENV !== 'production') {
|
|
console.log('Serving development build with webpack dev middleware');
|
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
require('babel-register');
|
|
const webpackConfig = require('./webpack.config.babel.js').default({
|
|
server: true,
|
|
});
|
|
const compiler = require('webpack')(webpackConfig);
|
|
app.use(require('webpack-dev-middleware')(compiler, webpackConfig.devServer));
|
|
app.use(require('webpack-hot-middleware')(compiler, {
|
|
log: false,
|
|
}));
|
|
|
|
app.get('/api/dev/webpackConfig', (req, res) => {
|
|
res.setHeader('content-type', 'text/plain');
|
|
res.send(require('util').inspect(webpackConfig, { depth: null, colors: false }));
|
|
});
|
|
} else {
|
|
console.log(`Serving static build from ${frontendDir}`);
|
|
app.use('/', express.static(frontendDir));
|
|
}
|
|
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
// Additional middleware which will set headers that we need on each request.
|
|
app.use((req, res, next) => {
|
|
// Disable caching so we'll always get the latest comments.
|
|
res.setHeader('Cache-Control', 'no-cache');
|
|
next();
|
|
});
|
|
|
|
function calculateRankings(id) {
|
|
db.query('SELECT * FROM `vizon_web_rankings` WHERE `vizon_drawings_id`=?', [id], (err, results, fields) => {
|
|
if (err) {
|
|
console.error('Failed to query rankings from database:', err);
|
|
res.status(500).json({
|
|
error: 'Database query failed',
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
app.get('/api/status', (req, res) => {
|
|
dbPool.getConnection((err, db) => {
|
|
if (err) {
|
|
console.error('Failed to connect to database:', err);
|
|
res.status(500).json({
|
|
error: 'Database connection failed',
|
|
});
|
|
}
|
|
|
|
db.query('SELECT * FROM ?? ORDER BY ?? DESC LIMIT 0, 1', [
|
|
tableNames.drawings,
|
|
'id',
|
|
], (qerr, results, fields) => {
|
|
if (qerr) {
|
|
console.error('Failed to request drawings:', err);
|
|
res.status(500).json({
|
|
error: 'Database query failed',
|
|
});
|
|
return;
|
|
}
|
|
|
|
const row = results[0];
|
|
|
|
const id = row.id;
|
|
|
|
const date = moment(row.drawing_date);
|
|
|
|
const numbers = [
|
|
row.first,
|
|
row.second,
|
|
row.third,
|
|
row.fourth,
|
|
row.fifth,
|
|
row.sixth,
|
|
].map(i => parseInt(i, 10));
|
|
|
|
const ranking = [
|
|
// @TODO - calculate ranking from mysql tables {drawings} and {bets}
|
|
];
|
|
|
|
res.json({
|
|
lastDrawing: {
|
|
id,
|
|
date,
|
|
numbers,
|
|
ranking,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
app.listen(app.get('port'), () => {
|
|
console.log(`Server started: http://localhost:${app.get('port')}/`);
|
|
});
|