Initial commit

This commit is contained in:
D. Berge
2020-08-08 23:59:13 +02:00
commit 4c5d29494c
113 changed files with 16479 additions and 0 deletions

161
lib/www/server/api/index.js Normal file
View File

@@ -0,0 +1,161 @@
const http = require('http');
const express = require('express');
const cookieParser = require('cookie-parser')
const mw = require('./middleware');
const app = express();
const verbose = process.env.NODE_ENV != 'test';
app.map = function(a, route){
route = route || '';
for (var key in a) {
switch (typeof a[key]) {
// { '/path': { ... }}
case 'object':
if (!Array.isArray(a[key])) {
app.map(a[key], route + key);
break;
} // else drop through
// get: function(){ ... }
case 'function':
if (verbose) console.log('%s %s', key, route);
app[key](route, a[key]);
break;
}
}
};
app.use(express.json({type: "application/json", strict: false}));
app.use(express.urlencoded({ type: "application/x-www-form-urlencoded", extended: true }));
app.use(express.text({type: "text/*"}));
app.use((req, res, next) => {
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE");
res.set("Access-Control-Allow-Headers", "Content-Type");
next();
});
app.use(cookieParser());
app.use(mw.auth.jwt);
// app.use(mw.auth.access({path: {allow:["^/login", "^/user$"]}}));
app.map({
'/project': {
get: [ mw.project.list ], // Get list of projects
},
'/project/:project': {
get: [ mw.project.get ], // Get project data
},
'/project/:project/summary': {
get: [ mw.project.get ],
},
'/project/:project/gis': {
get: [ mw.gis.project.bbox ]
},
'/project/:project/gis/preplot': {
get: [ mw.gis.project.preplot ]
},
'/project/:project/gis/preplot/:featuretype(line|point)': {
get: [ mw.gis.project.preplot ]
},
'/project/:project/gis/raw/:featuretype(line|point)': {
get: [ mw.gis.project.raw ]
},
'/project/:project/gis/final/:featuretype(line|point)': {
get: [ mw.gis.project.final ]
},
'/project/:project/line/': {
get: [ mw.line.list ],
},
// '/project/:project/line/:line': {
// get: [ mw.line.get ],
// patch: [ mw.line.patch ],
// },
//
'/project/:project/sequence/': {
get: [ mw.sequence.list ],
},
// '/project/:project/sequence/:line': {
// get: [ mw.sequence.get ],
// patch: [ mw.sequence.patch ],
// },
//
// '/project/:project/event/': {
// get: [ mw.event.list ],
// post: [ mw.event.post ],
// },
// '/project/:project/event/:event': {
// get: [ mw.event.get ],
// put: [ mw.event.put ],
// },
//
// '/project/:id/permissions/:mode(read|write)?': {
// get: [ mw.permissions.get ],
// put: [ mw.permissions.put ],
// // post: [ mw.permissions.post ],
// // delete: [ mw.permissions.delete ]
// },
//
// '/user': {
// get: [ mw.user.get ],
// post: [ mw.user.put ]
// },
// '/user/:user': {
// get: [ mw.user.get ],
// put: [ mw.user.put ],
// // delete: [ mw.user.delete ]
// },
//
// '/login': {
// post: [ mw.user.login ]
// },
// '/logout': {
// post: [ mw.user.logout ]
// }
});
// Generic error handler. Stops stack dumps
// being sent to clients.
app.use(function (err, req, res, next) {
console.log("Error:", err);
if (err instanceof Error && err.name != "UnauthorizedError") {
console.error(err.stack);
res.status(500).send('General internal error');
} else if (typeof err === 'string') {
res.status(500).send({message: err});
} else {
res.status(err.status || 500).send({message: err.message || (err.inner && err.inner.message) || "Internal error"});
}
});
app.disable('x-powered-by');
app.enable('trust proxy');
console.log('trust proxy is ' + (app.get('trust proxy')? 'on' : 'off'));
const addr = "127.0.0.1";
if (!module.parent) {
var port = process.env.HTTP_PORT || 3000;
var server = http.createServer(app).listen(port, addr);
console.log('API started on port ' + port);
} else {
app.start = function (port = 3000, path) {
var root = app;
if (path) {
root = express();
['x-powered-by', 'trust proxy'].forEach(k => root.set(k, app.get(k)));
root.use(path, app);
}
const server = http.createServer(root).listen(port, addr);
if (server) {
console.log(`API started on port ${port}, prefix: ${path || "/"}`);
}
return server;
}
module.exports = app;
}

View File

@@ -0,0 +1,3 @@
exports.jwt = require('./jwt');
// exports.access = require('./access');

View File

@@ -0,0 +1,26 @@
const expressJWT = require('express-jwt');
const cfg = require("../../../lib/config").jwt;
const getToken = function (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] == 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.cookies.JWT) {
return req.cookies.JWT;
}
return null;
}
const options = {
secret: cfg.secret,
credentialsRequired: false,
algorithms: ['HS256'],
getToken
};
const allow = {
path: [/\/login$/, /\/logout$/],
useOriginalUrl: false
};
module.exports = expressJWT(options).unless(allow);

View File

@@ -0,0 +1,3 @@
module.exports = {
project: require('./project')
};

View File

@@ -0,0 +1,9 @@
const { gis } = require('../../../../lib/db');
module.exports = async function (req, res, next) {
res.status(200).send(await gis.project.bbox(req.params.project));
next();
};

View File

@@ -0,0 +1,37 @@
const { gis } = require('../../../../lib/db');
function makeOptions (query) {
const options = {};
if (query.hasOwnProperty("limit")) {
options.limit = Math.abs(Number(query.limit));
}
if (query.hasOwnProperty("bbox")) {
options.bbox = query.bbox
.split(",")
.slice(0, 4)
.map( (n, i) => (i % 2) ? Number(n) % 90 : Number(n) % 180 );
if (options.bbox.some(n => isNaN(n))) {
delete options.bbox;
}
}
return options;
}
module.exports = async function (req, res, next) {
const handler = req.params.featuretype == "point"
? gis.project.final.points
: gis.project.final.lines;
const options = makeOptions(req.query);
res.set("Cache-Control", "public, max-age=30");
res.status(200).send(await handler(req.params.project, options));
next();
};

View File

@@ -0,0 +1,6 @@
module.exports = {
bbox: require('./bbox'),
preplot: require('./preplot'),
raw: require('./raw'),
final: require('./final')
};

View File

@@ -0,0 +1,41 @@
const { gis } = require('../../../../lib/db');
function makeOptions (query) {
const options = {};
if (query.hasOwnProperty("limit")) {
options.limit = Math.abs(Number(query.limit));
}
if (query.hasOwnProperty("bbox")) {
options.bbox = query.bbox
.split(",")
.slice(0, 4)
.map( (n, i) => (i % 2) ? Number(n) % 90 : Number(n) % 180 );
if (options.bbox.some(n => isNaN(n))) {
delete options.bbox;
}
}
if (query.hasOwnProperty("class")) {
options.class = query.class;
}
return options;
}
module.exports = async function (req, res, next) {
const handler = req.params.featuretype == "point"
? gis.project.preplot.points
: gis.project.preplot.lines;
const options = makeOptions(req.query);
res.set("Cache-Control", "public, max-age=30");
res.status(200).send(await handler(req.params.project, options));
next();
};

View File

@@ -0,0 +1,36 @@
const { gis } = require('../../../../lib/db');
function makeOptions (query) {
const options = {};
if (query.hasOwnProperty("limit")) {
options.limit = Math.abs(Number(query.limit));
}
if (query.hasOwnProperty("bbox")) {
options.bbox = query.bbox
.split(",")
.slice(0, 4)
.map( (n, i) => (i % 2) ? Number(n) % 90 : Number(n) % 180 );
if (options.bbox.some(n => isNaN(n))) {
delete options.bbox;
}
}
return options;
}
module.exports = async function (req, res, next) {
const handler = req.params.featuretype == "point"
? gis.project.raw.points
: gis.project.raw.lines;
const options = makeOptions(req.query);
res.status(200).send(await handler(req.params.project, options));
next();
};

View File

@@ -0,0 +1,9 @@
module.exports = {
event: require('./event'),
line: require('./line'),
project: require('./project'),
sequence: require('./sequence'),
user: require('./user'),
auth: require('./auth'),
gis: require('./gis')
};

View File

@@ -0,0 +1,4 @@
module.exports = {
list: require('./list'),
// get: require('./get')
};

View File

@@ -0,0 +1,10 @@
const { line } = require('../../../lib/db');
module.exports = async function (req, res, next) {
console.log(req.query);
res.status(200).send(await line.list(req.params.project, req.query));
next();
};

View File

@@ -0,0 +1,9 @@
const { project} = require('../../../lib/db');
module.exports = async function (req, res, next) {
res.status(200).send(await project.summary(req.params.project));
next();
};

View File

@@ -0,0 +1,4 @@
module.exports = {
list: require('./list'),
get: require('./get')
};

View File

@@ -0,0 +1,9 @@
const { project} = require('../../../lib/db');
module.exports = async function (req, res, next) {
res.status(200).send(await project.list());
next();
};

View File

@@ -0,0 +1,4 @@
module.exports = {
list: require('./list'),
get: require('./get')
};

View File

@@ -0,0 +1,10 @@
const { sequence } = require('../../../lib/db');
module.exports = async function (req, res, next) {
console.log(req.query);
res.status(200).send(await sequence.list(req.params.project, req.query));
next();
};