mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Transitioned server to typescript
This commit is contained in:
83
frontend/server/src/routes/api/airbases.ts
Normal file
83
frontend/server/src/routes/api/airbases.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import express = require("express");
|
||||
import fs = require("fs");
|
||||
|
||||
const app = express();
|
||||
|
||||
const allowedTheatres = [
|
||||
"caucasus",
|
||||
"falklands",
|
||||
"marianas",
|
||||
"nevada",
|
||||
"normandy",
|
||||
"persiangulf",
|
||||
"sinaimap",
|
||||
"syria",
|
||||
"thechannel",
|
||||
"kola",
|
||||
];
|
||||
|
||||
function getAirbasesData(theatreName) {
|
||||
if (!isValidTheatre(theatreName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return JSON.parse(
|
||||
fs.readFileSync(`public/databases/airbases/${theatreName}.json`, {
|
||||
encoding: "utf-8",
|
||||
})
|
||||
).airfields;
|
||||
}
|
||||
|
||||
function isValidTheatre(theatre) {
|
||||
return allowedTheatres.indexOf(theatre) > -1;
|
||||
}
|
||||
|
||||
function sendInvalidTheatre(res) {
|
||||
res
|
||||
.status(400)
|
||||
.send(
|
||||
"Missing/invalid theatre name; must be one of:\n\t" +
|
||||
allowedTheatres.join("\n\t")
|
||||
);
|
||||
}
|
||||
|
||||
/**************************************************************************************************************/
|
||||
// Endpoints
|
||||
/**************************************************************************************************************/
|
||||
app.get("/", (req, res) => {
|
||||
sendInvalidTheatre(res);
|
||||
});
|
||||
|
||||
app.get("/:theatreName/:airbaseName", (req, res) => {
|
||||
const airbases = getAirbasesData(req.params.theatreName);
|
||||
if (!airbases) {
|
||||
sendInvalidTheatre(res);
|
||||
return;
|
||||
}
|
||||
|
||||
const airbaseName = req.params.airbaseName;
|
||||
if (!airbases.hasOwnProperty(airbaseName)) {
|
||||
res
|
||||
.status(404)
|
||||
.send(
|
||||
`Unknown airbase name "${airbaseName}". Available options are:\n\t` +
|
||||
Object.keys(airbases).join("\n\t")
|
||||
);
|
||||
} else {
|
||||
res.status(200).json(airbases[airbaseName]);
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/:theatreName", (req, res) => {
|
||||
const theatreName = req.params.theatreName.toLowerCase().replace(/\s*/g, "");
|
||||
const airbases = getAirbasesData(theatreName);
|
||||
|
||||
if (!airbases) {
|
||||
sendInvalidTheatre(res);
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json(airbases);
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
62
frontend/server/src/routes/api/databases.ts
Normal file
62
frontend/server/src/routes/api/databases.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import express = require('express');
|
||||
import fs = require("fs");
|
||||
import path = require("path");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
module.exports = function (databasesLocation) {
|
||||
router.get('/:type/:name', function (req, res) {
|
||||
var contents = fs.readFileSync(path.join(databasesLocation, req.params.type, req.params.name + ".json"));
|
||||
res.status(200).send(contents);
|
||||
});
|
||||
|
||||
router.put('/save/:type/:name', function (req, res) {
|
||||
var dir = path.join(databasesLocation, req.params.type, "old");
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir);
|
||||
}
|
||||
|
||||
var filepath = path.join(databasesLocation, req.params.type, req.params.name + ".json");
|
||||
if (fs.existsSync(filepath)) {
|
||||
var newFilepath = path.join(databasesLocation, req.params.type, "old", req.params.name + ".json");
|
||||
fs.copyFileSync(filepath, newFilepath);
|
||||
if (fs.existsSync(newFilepath)) {
|
||||
try {
|
||||
var json = JSON.stringify(req.body.blueprints, null, "\t");
|
||||
fs.writeFileSync(filepath, json, 'utf8');
|
||||
res.send("OK");
|
||||
} catch {
|
||||
res.status(422).send("Error");
|
||||
}
|
||||
} else {
|
||||
res.status(422).send("Error");
|
||||
}
|
||||
} else {
|
||||
res.status(404).send('Not found');
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/reset/:type/:name', function (req, res) {
|
||||
var filepath = path.join(databasesLocation, req.params.type, "default", req.params.name + ".json");
|
||||
if (fs.existsSync(filepath)) {
|
||||
var newFilepath = path.join(databasesLocation, req.params.type, req.params.name + ".json");
|
||||
fs.copyFileSync(filepath, newFilepath);
|
||||
res.send("OK");
|
||||
} else {
|
||||
res.status(404).send('Not found');
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/restore/:type/:name', function (req, res) {
|
||||
var filepath = path.join(databasesLocation, req.params.type, "old", req.params.name + ".json");
|
||||
if (fs.existsSync(filepath)) {
|
||||
var newFilepath = path.join(databasesLocation, req.params.type, req.params.name + ".json");
|
||||
fs.copyFileSync(filepath, newFilepath);
|
||||
res.send("OK");
|
||||
} else {
|
||||
res.status(404).send('Not found');
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
32
frontend/server/src/routes/api/elevation.ts
Normal file
32
frontend/server/src/routes/api/elevation.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import express = require('express');
|
||||
import fs = require('fs');
|
||||
import srtmElevation = require('srtm-elevation');
|
||||
|
||||
const TileSet = srtmElevation.TileSet;
|
||||
const SRTMElevationDownloader = srtmElevation.SRTMElevationDownloader;
|
||||
const router = express.Router();
|
||||
|
||||
module.exports = function (configLocation) {
|
||||
let rawdata = fs.readFileSync(configLocation, "utf-8");
|
||||
let config = JSON.parse(rawdata);
|
||||
var tileset = null;
|
||||
|
||||
if (config["frontend"] === undefined || config["frontend"]["elevationProvider"] === undefined)
|
||||
tileset = new TileSet('./hgt');
|
||||
else
|
||||
tileset = new TileSet('./hgt', {downloader: new SRTMElevationDownloader('./hgt', config["frontend"]["elevationProvider"])});
|
||||
|
||||
router.get( "/:lat/:lng", ( req, res ) => {
|
||||
tileset.getElevation([req.params.lat, req.params.lng], function(err, elevation) {
|
||||
if (err) {
|
||||
console.log('getElevation failed: ' + err.message);
|
||||
res.send("n/a");
|
||||
} else {
|
||||
res.send(String(elevation));
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
9
frontend/server/src/routes/index.ts
Normal file
9
frontend/server/src/routes/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
/* GET home page. */
|
||||
router.get('/', function (req, res, next) {
|
||||
res.render('index', { title: 'Express' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
21
frontend/server/src/routes/plugins.ts
Normal file
21
frontend/server/src/routes/plugins.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import express = require('express');
|
||||
import fs = require('fs');
|
||||
import path = require('path');
|
||||
|
||||
const pluginsDirectory = "./public/plugins"
|
||||
const router = express.Router();
|
||||
|
||||
function listDirectories(source) {
|
||||
const directories = fs.readdirSync(source, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
return directories;
|
||||
}
|
||||
|
||||
router.get('/list', function (req, res) {
|
||||
var directories = listDirectories(pluginsDirectory);
|
||||
res.send(directories.filter(directory => fs.existsSync(path.join(pluginsDirectory, directory))));
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
18
frontend/server/src/routes/resources.ts
Normal file
18
frontend/server/src/routes/resources.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import express = require('express');
|
||||
import fs = require('fs');
|
||||
const router = express.Router();
|
||||
|
||||
module.exports = function (configLocation) {
|
||||
router.get('/config', function (req, res, next) {
|
||||
if (fs.existsSync(configLocation)) {
|
||||
let rawdata = fs.readFileSync(configLocation, "utf-8");
|
||||
const config = JSON.parse(rawdata);
|
||||
res.send(JSON.stringify(config.frontend));
|
||||
res.end()
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in New Issue
Block a user