From 792e89b7ab29fe627fbec78a5b5971a5acd3a958 Mon Sep 17 00:00:00 2001 From: Oliver Boehlk Date: Fri, 17 Apr 2020 21:39:31 +0200 Subject: [PATCH] load resource list in backend for API --- backend/index.js | 23 ++++++++++++++++++++++- backend/package-lock.json | 5 +++++ backend/package.json | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/index.js b/backend/index.js index 86be953..0fbb3f1 100644 --- a/backend/index.js +++ b/backend/index.js @@ -1,6 +1,7 @@ const express = require('express') const app = express(); const status = require('http-status'); +const fetch = require('node-fetch'); var mysql = require('mysql'); var connection = mysql.createConnection({ host: 'localhost', @@ -9,7 +10,23 @@ var connection = mysql.createConnection({ database: 'simcompanies' }); -const mockDataDay = require('./mockdata-test/day.json') +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.all("*", function (req, res, next) { + if (!serverStartupComplete) + return res.send("Server is starting..."); + else return next(); +}) app.get('/API/day', function (req, res) { var date = new Date(req.query.date); @@ -38,4 +55,8 @@ app.get('/API/day', function (req, res) { res.status(status.BAD_REQUEST).send("invalid data provided"); }); +app.get('/API/resourcelist', function (req, res) { + res.send(resourceList); +}); + app.listen(3001); \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index d57432b..eebf721 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -690,6 +690,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, "nodemon": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.2.tgz", diff --git a/backend/package.json b/backend/package.json index 7eef899..1c95193 100644 --- a/backend/package.json +++ b/backend/package.json @@ -13,6 +13,7 @@ "express": "^4.17.1", "http-status": "^1.4.2", "mysql": "^2.18.1", + "node-fetch": "^2.6.0", "nodemon": "^2.0.2" } }