This repository has been archived on 2023-08-24. You can view files and clone it, but cannot push or open issues or pull requests.
OwlBoard/app.js

97 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-11-30 22:08:27 +00:00
// OwlBoard - © Fred Boniface 2022 - Licensed under GPLv3 (or later)
// Please see the included LICENSE file statically served fonts are
// licensed separately, each folder contains a license file
2022-11-30 00:21:59 +00:00
// Version Number:
2022-11-30 14:32:36 +00:00
var version = "0.0.1"
2022-11-30 00:21:59 +00:00
console.log(`Starting OwlBoard version ${version}`)
2022-11-30 22:08:27 +00:00
// Load & prepare modules:
2022-11-30 14:32:36 +00:00
const fs = require('fs');
const ldb = require('ldbs-json');
const express = require('express');
const app = express();
2022-11-30 00:21:59 +00:00
// 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:
2022-11-30 14:32:36 +00:00
app.use(express.json()); //JSON Parsing for POST Requests
app.use(express.static('static')); //Serve static content from static
2022-11-30 00:21:59 +00:00
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);
}
);
2022-11-30 22:08:27 +00:00
// Create/Connect to database:
2022-11-30 00:21:59 +00:00
// Routes:
2022-11-30 14:32:36 +00:00
app.get('/api', (req, res)=>{
2022-11-30 00:21:59 +00:00
res.status(200);
res.set('Content-Type', 'application/json');
res.send(JSON.stringify({"OwlBoard_Status":"ready","OwlBoard_Version":version}));
});
2022-11-30 22:08:27 +00:00
app.post('/api/test', (req, res)=>{ //Check auth and return status
2022-11-30 00:21:59 +00:00
const {test} = req.body;
res.status(200);
res.set('Content-Type', 'application/json');
res.send(JSON.stringify({"test_outcome":"success","post_value":test}))
})
2022-11-30 14:32:36 +00:00
app.post('/api/arrdep', (req, res)=>{
2022-11-30 00:21:59 +00:00
//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"}));
}
})
2022-11-30 22:08:27 +00:00
app.post('/api/stations', (req, res)=>{
2022-11-30 00:21:59 +00:00
//Return JSON of all available stations - No Auth Required
})
2022-11-30 22:08:27 +00:00
app.post('/api/send-issue', (req, res)=>{
//Submit issue to Gitea (based on existing PHP Athena function)
})
2022-11-30 00:21:59 +00:00
// 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
}