75 lines
2.3 KiB
JavaScript
75 lines
2.3 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) {
|
|
var date = new Date(req.query.date);
|
|
const kind = parseInt(req.query.kind);
|
|
if (date instanceof Date && Number.isInteger(kind)) {
|
|
//Mock Data:
|
|
if (kind === -1) return res.send(mockDataDay);
|
|
|
|
if (!isNaN(date.getTime()) && kind >= 1 && kind <= 113) {
|
|
const daybegin = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
|
|
date.setDate(date.getDate() + 1);
|
|
const dayend = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
|
|
const querystring = `SELECT time, price FROM market WHERE kind = ${kind} AND time > "${daybegin}" AND time < "${dayend}"`
|
|
connection.query(querystring, function (error, results, fields) {
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
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); |