92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
|
// OwlBoard - © Fred Boniface 2022
|
||
|
// Licensed under GPLv3 (or later)
|
||
|
// Please see the included LICENSE file
|
||
|
|
||
|
// Version Number:
|
||
|
var version = "0.0.1"
|
||
|
console.log(`Starting OwlBoard version ${version}`)
|
||
|
|
||
|
// Load Modules:
|
||
|
const fs = require('fs');
|
||
|
const ldb = require('ldbs-json');
|
||
|
const express = require('express');
|
||
|
const app = express();
|
||
|
|
||
|
// Get API Keys:
|
||
|
try {
|
||
|
var keys = JSON.parse(fs.readFileSync('/srv/keys/owlboard/keys.json', 'utf8'));
|
||
|
console.log('API Keys loaded from /srv/keys/ownboard/keys.json');
|
||
|
} catch (err) {
|
||
|
var keys = 'ERR';
|
||
|
console.error('Unable to obtain API Keys from file');
|
||
|
console.error(err);
|
||
|
};
|
||
|
|
||
|
// Get Settings:
|
||
|
try {
|
||
|
var settings = JSON.parse(fs.readFileSync('./settings.json', 'utf8'));
|
||
|
console.log("Settings loaded from .settings.json");
|
||
|
} catch (err) {
|
||
|
console.warn('Unable to load settings, using defaults', err);
|
||
|
var settings = {"webPort":8460,"listen":"localhost"};
|
||
|
};
|
||
|
|
||
|
// Create Web Service:
|
||
|
app.use(express.json());
|
||
|
|
||
|
app.listen(settings.webPort, (error) =>{
|
||
|
if(!error)
|
||
|
console.log(`Server is running on http://${settings.listen}:${settings.webPort}`)
|
||
|
else
|
||
|
console.log("Error occurred, server can't start", error);
|
||
|
}
|
||
|
);
|
||
|
|
||
|
|
||
|
// Check Athena API Key:
|
||
|
|
||
|
// Routes:
|
||
|
|
||
|
app.get('/', (req, res)=>{
|
||
|
res.status(200);
|
||
|
res.set('Content-Type', 'application/json');
|
||
|
res.send(JSON.stringify({"OwlBoard_Status":"ready","OwlBoard_Version":version}));
|
||
|
});
|
||
|
|
||
|
app.post('/test', (req, res)=>{
|
||
|
const {test} = req.body;
|
||
|
res.status(200);
|
||
|
res.set('Content-Type', 'application/json');
|
||
|
res.send(JSON.stringify({"test_outcome":"success","post_value":test}))
|
||
|
})
|
||
|
|
||
|
app.post('/arrdep', (req, res)=>{
|
||
|
//Check Validity and Auth then return ArrDep JSON
|
||
|
const {reqCode} = req.body;
|
||
|
const {auth} = req.body;
|
||
|
const permit = checkAuth(auth);
|
||
|
const valid = checkValid(reqCode);
|
||
|
res.set('Content-Type', 'application/json');
|
||
|
if (permit == false) { //This should be the other way round - If Valid&Auth then return, elif not auth, elif invalid elif UnknownError
|
||
|
res.status(401);
|
||
|
res.send(JSON.stringify({"status":"fail","error":"Not Authorised"}));
|
||
|
} else if (valid == false) {
|
||
|
res.status(404);
|
||
|
res.send(JSON.stringify({"status":"fail","error":"Location not Recognized"}));
|
||
|
}
|
||
|
|
||
|
})
|
||
|
|
||
|
app.post('/stations', (req, res)=>{
|
||
|
//Return JSON of all available stations - No Auth Required
|
||
|
})
|
||
|
|
||
|
|
||
|
// General Functions
|
||
|
function checkValid(input){
|
||
|
// Check validity of the entered CRS/Tiploc against DB
|
||
|
}
|
||
|
|
||
|
function checkAuth(downstreamKey){
|
||
|
// Check the downstream requester has authority to get depBoards
|
||
|
}
|