Organised

This commit is contained in:
Fred Boniface 2022-12-01 13:40:24 +00:00
parent 1cf9c8cd66
commit c76a3c561b
17 changed files with 159 additions and 153 deletions

102
app.js
View File

@ -3,95 +3,35 @@
// Please see the included LICENSE file statically served fonts are // Please see the included LICENSE file statically served fonts are
// licensed separately, each folder contains a license file // licensed separately, each folder contains a license file
// Version Number:
var version = "0.0.1"
console.log(`Starting OwlBoard version ${version}`)
// Load & prepare modules: // Load & prepare modules:
const fs = require('fs');
const ldb = require('ldbs-json');
const express = require('express'); const express = require('express');
const app = express(); const app = express();
const config = require('./src/configs/server.configs');
const version = require('./src/configs/version.configs');
const testRtr = require('./src/routes/test.routes');
const listRtr = require('./src/routes/list.routes');
// Get API Keys: // Print version number:
try { console.log(`Starting OwlBoard, API version ${version.api}`);
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: // Middleware:
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()); //JSON Parsing for POST Requests app.use(express.json()); //JSON Parsing for POST Requests
app.use(express.static('static')); //Serve static content from static app.use(express.static('static')); //Serve static content from static
app.use((err, req, res, next) => {
app.listen(settings.webPort, (error) =>{ const statusCode = err.statuscode || 500;
if(!error) console.error(err.message, err.stack);
console.log(`Server is running on http://${settings.listen}:${settings.webPort}`) res.status(statusCode).json({'message': err.message});
else
console.log("Error occurred, server can't start", error);
}
);
// Create/Connect to database:
// Routes:
app.get('/api', (req, res)=>{
res.status(200);
res.set('Content-Type', 'application/json');
res.send(JSON.stringify({"OwlBoard_Status":"ready","OwlBoard_Version":version}));
}); });
app.post('/api/test', (req, res)=>{ //Check auth and return status // Point to Routes
const {test} = req.body; app.use('/api/test', testRtr);
res.status(200); app.use('/api/list', listRtr);
res.set('Content-Type', 'application/json');
res.send(JSON.stringify({"test_outcome":"success","post_value":test}))
})
app.post('/api/arrdep', (req, res)=>{ // Start server
//Check Validity and Auth then return ArrDep JSON app.listen(config.port, config.listen, (error) =>{
const {reqCode} = req.body; if(!error) {
const {auth} = req.body; console.log(`Started server on http://${config.listen}:${config.port}`);
const permit = checkAuth(auth); } else {
const valid = checkValid(reqCode); console.log("Error occurred, server can't start", error);
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('/api/stations', (req, res)=>{
//Return JSON of all available stations - No Auth Required
})
app.post('/api/send-issue', (req, res)=>{
//Submit issue to Gitea (based on existing PHP Athena function)
})
// 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
}

View File

@ -1,68 +0,0 @@
// OwlBoard - © Fred Boniface 2022
// Licensed under GPLv3 (or later)
// Please see the included LICENSE file
// Version Number:
var version = 0.1
console.log(`Starting OwlBoard version ${version}`)
// Load Modules:
const fs = require('fs');
const ldb = require('ldbs-json');
const http = require('http');
const url = require('url');
// 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:
//let 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');
console.log(err);
var settings = {"webPort":8460,"listen":"localhost"};
};
// Create Web Service:
try {
http.createServer((req, res) => {
const queryOpt = url.parse(req.url, true).query;
const query = url.parse(req.url, true).pathname.replace(/[/]/g, '');
switch (req.url){
case "":
res.writeHead(200, { 'Content-Type':'application/json' });
res.end(JSON.stringify({"OwlBoardStatus":"ready","OwnBoardVersion":version}));
break;
}
switch (req.url){
case "/crs":
res.writeHead(200, { 'Content-Type':'application/json'});
res.end(crs);
break;
}
}).listen(settings.webPort);
console.log(`Web server started on http://${settings.listen}:${settings.webPort}`);
} catch (err) {
console.error('Unable to start webserver');
console.error(err);
};
// Check Athena API Key:
// Routes:
var crs = JSON.stringify({"resource":"crs_lookup"});

3
package-lock.json generated
View File

@ -12,8 +12,7 @@
"express": "^4.18.2", "express": "^4.18.2",
"ldbs-json": "^1.2.1", "ldbs-json": "^1.2.1",
"sqlite3": "^5.1.2" "sqlite3": "^5.1.2"
}, }
"devDependencies": {}
}, },
"node_modules/@gar/promisify": { "node_modules/@gar/promisify": {
"version": "1.1.3", "version": "1.1.3",

View File

@ -1 +0,0 @@
{"webPort":8460,"listen":"localhost"}

View File

@ -0,0 +1,6 @@
const server = {
port: 8460,
listen: "localhost"
};
module.exports = server;

View File

@ -0,0 +1,5 @@
const version = {
api: "0.0.1"
};
module.exports = version;

View File

@ -0,0 +1,15 @@
const list = require('../services/list.services');
async function get(req, res, next){
try {
res.json(await list.get(req.body))
} catch (err) {
console.error(`Unknown Error`, err.message);
next(err);
}
}
module.exports = {
get,
post
}

View File

@ -0,0 +1,24 @@
const test = require('../services/test.services');
async function get(req, res, next){
try {
res.json(await test.get(req.body))
} catch (err) {
console.error(`Unknown Error`, err.message);
next(err);
}
}
async function post(req, res){
try {
res.json(await test.post(req.body))
} catch (err) {
console.error(`Unknown Error`, err.message);
next(err);
}
};
module.exports = {
get,
post
}

19
src/routes/list.routes.js Normal file
View File

@ -0,0 +1,19 @@
const express = require('express');
const router = express.Router();
const listController = require('../controllers/list.controllers');
/* GET programming languages. */
//router.get('/', programmingLanguagesController.get);
/* POST programming language */
//router.post('/', programmingLanguagesController.create);
/* PUT programming language */
//router.put('/:id', programmingLanguagesController.update);
/* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove);
router.get('/', listController.get);
module.exports = router;

20
src/routes/test.routes.js Normal file
View File

@ -0,0 +1,20 @@
const express = require('express');
const router = express.Router();
const testController = require('../controllers/test.controllers');
/* GET programming languages. */
//router.get('/', programmingLanguagesController.get);
/* POST programming language */
//router.post('/', programmingLanguagesController.create);
/* PUT programming language */
//router.put('/:id', programmingLanguagesController.update);
/* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove);
router.get('/', testController.get);
router.post('/', testController.post);
module.exports = router;

View File

@ -0,0 +1 @@
const ldb = require('ldbs-json');

View File

@ -0,0 +1,10 @@
const version = require('../configs/version.configs');
async function get(){
return {"OwlBoard_Status":"ready","OwlBoard_Version":version.api};
}
module.exports = {
get,
post
}

View File

@ -0,0 +1,14 @@
const version = require('../configs/version.configs');
async function get(){
return {"OwlBoard_Status":"ready","OwlBoard_Version":version.api};
}
async function post(){
return {"type":"post"};
}
module.exports = {
get,
post
}

22
src/utils/ldbKey.utils.js Normal file
View File

@ -0,0 +1,22 @@
const fs = require('fs');
async function returnKey(type){ //Reads API key from file and returns either ldb or ldbsv key
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);
};
if (staff = true){
return keys.ldb;
} else {
return keys.ldvsv;
};
};
module.exports = {
returnKey
};