75 lines
2.4 KiB
JavaScript
75 lines
2.4 KiB
JavaScript
const express = require('express')
|
|
const app = express();
|
|
const status = require('http-status');
|
|
const fetch = require('node-fetch');
|
|
var mysql = require('mysql');
|
|
const path = require('path');
|
|
var connection = mysql.createConnection({
|
|
host: 'localhost',
|
|
user: 'simcompanies',
|
|
password: '',
|
|
database: 'simcompanies'
|
|
});
|
|
|
|
const mockDataDay = require('./mockdata-test/day.json');
|
|
var resourceList;
|
|
|
|
var serverStartupComplete = false;
|
|
|
|
async function loadData() {
|
|
var rL = await fetch("https://www.simcompanies.com/api/v3/en/encyclopedia/resources/");
|
|
resourceList = await rL.json();
|
|
serverStartupComplete = true;
|
|
}
|
|
loadData();
|
|
|
|
app.get("*", function (req, res, next) {
|
|
if (!serverStartupComplete)
|
|
return res.send("Server is starting...");
|
|
else return next();
|
|
});
|
|
|
|
app.use(express.static(path.join(__dirname, 'frontend')));
|
|
|
|
|
|
app.use(express.static(`${__dirname}/./frontend`));
|
|
app.all("/*", function (req, res, next) {
|
|
if (/^\/simcompanies\/API\/.*/.test(req.url))
|
|
req.url = req.url.substring(13);
|
|
if (!/^\/API\/.*/.test(req.url)) {
|
|
return res.sendFile(path.join(__dirname, 'frontend', 'index.html'));
|
|
} else return next();
|
|
})
|
|
|
|
app.get('/API/day', function (req, res) {
|
|
const kind = parseInt(req.query.kind);
|
|
if (Number.isInteger(kind)) {
|
|
//Mock Data:
|
|
if (kind === -1) return res.send(mockDataDay);
|
|
|
|
if (kind >= 1 && kind <= 113) {
|
|
var dayend = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
|
var daybegin = new Date();
|
|
daybegin.setDate(daybegin.getDate() - 1);
|
|
daybegin = daybegin.toISOString().slice(0, 19).replace('T', ' ');
|
|
const querystring = `SELECT time, price, quality FROM marketv2 WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}" ORDER BY quality, time`;
|
|
connection.query(querystring, function (error, results, fields) {
|
|
if (error) {
|
|
res.status(status.INTERNAL_SERVER_ERROR).send("database connection failed");
|
|
}
|
|
res.send(results);
|
|
});
|
|
}
|
|
else
|
|
res.status(status.BAD_REQUEST).send("invalid data provided");
|
|
|
|
}
|
|
else
|
|
res.status(status.BAD_REQUEST).send("invalid data provided");
|
|
});
|
|
|
|
app.get('/API/resourcelist', function (req, res) {
|
|
res.send(resourceList);
|
|
});
|
|
|
|
app.listen(3001); |