2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
const http = require('http');
|
|
|
|
|
const express = require('express');
|
2023-08-23 19:11:20 +02:00
|
|
|
express.yaml ??= require('body-parser').yaml; // NOTE: Use own customised body-parser
|
2020-08-08 23:59:13 +02:00
|
|
|
const cookieParser = require('cookie-parser')
|
|
|
|
|
|
2020-08-25 11:32:13 +02:00
|
|
|
const maybeSendAlert = require("../lib/alerts");
|
2020-08-08 23:59:13 +02:00
|
|
|
const mw = require('./middleware');
|
|
|
|
|
|
2022-05-12 22:09:08 +02:00
|
|
|
const { ERROR, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
2020-08-08 23:59:13 +02:00
|
|
|
const verbose = process.env.NODE_ENV != 'test';
|
2022-02-27 18:33:42 +01:00
|
|
|
const app = express();
|
2022-05-04 18:11:05 +02:00
|
|
|
app.locals.version = "0.3.1"; // API version
|
2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
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':
|
2022-05-12 22:09:08 +02:00
|
|
|
if (verbose) INFO('%s %s', key, route);
|
2020-08-08 23:59:13 +02:00
|
|
|
app[key](route, a[key]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-02 15:28:43 +02:00
|
|
|
app.use(express.json({type: "application/json", strict: false, limit: '10mb'}));
|
2023-08-23 19:11:20 +02:00
|
|
|
app.use(express.yaml({type: "application/yaml", limit: '10mb'}));
|
2020-08-08 23:59:13 +02:00
|
|
|
app.use(express.urlencoded({ type: "application/x-www-form-urlencoded", extended: true }));
|
2020-10-02 15:28:43 +02:00
|
|
|
app.use(express.text({type: "text/*", limit: '10mb'}));
|
2020-08-08 23:59:13 +02:00
|
|
|
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$"]}}));
|
|
|
|
|
|
2020-08-22 20:30:15 +02:00
|
|
|
// Adds arbitrary information to the request object
|
|
|
|
|
const meta = (key, value) => {
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
|
if (!req.meta) {
|
|
|
|
|
req.meta = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
|
req.meta[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Short for adding meta to all methods
|
|
|
|
|
const allMeta = (key, value) => {
|
|
|
|
|
return { all: [ meta(key, value) ] };
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-11 22:11:36 +02:00
|
|
|
// These routes do not require authentication
|
2020-08-08 23:59:13 +02:00
|
|
|
app.map({
|
2020-08-22 20:30:15 +02:00
|
|
|
'*': { all: [ meta() ] }, // Create the req.meta object
|
2020-10-11 22:11:36 +02:00
|
|
|
'/login': {
|
|
|
|
|
post: [ mw.user.login ]
|
|
|
|
|
},
|
|
|
|
|
'/logout': {
|
|
|
|
|
get: [ mw.user.logout ],
|
|
|
|
|
post: [ mw.user.logout ]
|
2020-12-29 16:20:57 +01:00
|
|
|
},
|
|
|
|
|
'/': {
|
|
|
|
|
get: [ mw.openapi.get ]
|
2020-10-11 22:11:36 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use(mw.auth.authentify);
|
|
|
|
|
|
|
|
|
|
// We must be authenticated before we can access these
|
|
|
|
|
app.map({
|
2020-08-08 23:59:13 +02:00
|
|
|
'/project': {
|
2023-08-23 19:17:20 +02:00
|
|
|
get: [ mw.project.get ], // Get list of projects
|
2020-08-08 23:59:13 +02:00
|
|
|
},
|
|
|
|
|
'/project/:project': {
|
2023-08-23 19:17:20 +02:00
|
|
|
get: [ mw.project.summary.get ], // Get project data
|
2020-08-08 23:59:13 +02:00
|
|
|
},
|
|
|
|
|
'/project/:project/summary': {
|
2023-08-23 19:17:20 +02:00
|
|
|
get: [ mw.project.summary.get ],
|
2020-08-08 23:59:13 +02:00
|
|
|
},
|
2022-03-09 17:43:51 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* GIS endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
'/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 ]
|
|
|
|
|
},
|
2022-03-09 17:43:51 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Line endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
'/project/:project/line/': {
|
|
|
|
|
get: [ mw.line.list ],
|
|
|
|
|
},
|
2020-10-01 18:25:32 +02:00
|
|
|
'/project/:project/line/:line': {
|
2020-08-08 23:59:13 +02:00
|
|
|
// get: [ mw.line.get ],
|
2020-10-12 19:43:07 +02:00
|
|
|
patch: [ mw.auth.access.write, mw.line.patch ],
|
2020-10-01 18:25:32 +02:00
|
|
|
},
|
|
|
|
|
|
2022-03-09 17:43:51 +01:00
|
|
|
/*
|
|
|
|
|
* Sequence endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
'/project/:project/sequence/': {
|
|
|
|
|
get: [ mw.sequence.list ],
|
|
|
|
|
},
|
2020-09-27 19:21:59 +02:00
|
|
|
'/project/:project/sequence/:sequence': {
|
2021-09-04 18:04:52 +02:00
|
|
|
get: [ mw.sequence.get ],
|
2020-10-12 19:43:07 +02:00
|
|
|
patch: [ mw.auth.access.write, mw.sequence.patch ],
|
2022-02-27 17:31:08 +01:00
|
|
|
'/:point': {
|
|
|
|
|
get: [ mw.sequence.point.get ]
|
|
|
|
|
}
|
2020-09-27 19:21:59 +02:00
|
|
|
},
|
2020-10-08 16:36:15 +02:00
|
|
|
|
2022-03-09 17:43:51 +01:00
|
|
|
/*
|
|
|
|
|
* Planner endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-08 16:36:15 +02:00
|
|
|
'/project/:project/plan/': {
|
|
|
|
|
get: [ mw.plan.list ],
|
2020-10-12 19:43:07 +02:00
|
|
|
put: [ mw.auth.access.write, mw.plan.put ],
|
|
|
|
|
post: [ mw.auth.access.write, mw.plan.post ]
|
2020-10-08 16:36:15 +02:00
|
|
|
},
|
|
|
|
|
'/project/:project/plan/:sequence': {
|
|
|
|
|
// get: [ mw.plan.get ],
|
2020-10-12 19:43:07 +02:00
|
|
|
patch: [ mw.auth.access.write, mw.plan.patch ],
|
|
|
|
|
delete: [ mw.auth.access.write, mw.plan.delete ]
|
2020-10-08 16:36:15 +02:00
|
|
|
},
|
2022-03-09 17:43:51 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Event log endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-12 11:35:57 +02:00
|
|
|
'/project/:project/event/': {
|
2022-02-27 18:16:20 +01:00
|
|
|
get: [ mw.event.list ],
|
2020-10-12 19:43:07 +02:00
|
|
|
post: [ mw.auth.access.write, mw.event.post ],
|
|
|
|
|
put: [ mw.auth.access.write, mw.event.put ],
|
|
|
|
|
delete: [ mw.auth.access.write, mw.event.delete ],
|
2022-02-27 17:42:28 +01:00
|
|
|
// TODO Rename -/:sequence → sequence/:sequence
|
2021-05-11 23:37:55 +02:00
|
|
|
'-/:sequence/': { // NOTE: We need to avoid conflict with the next endpoint ☹
|
2022-02-27 17:42:28 +01:00
|
|
|
get: [ mw.event.sequence.get ],
|
2021-05-11 23:37:55 +02:00
|
|
|
},
|
2022-02-27 18:13:25 +01:00
|
|
|
':id/': {
|
|
|
|
|
get: [ mw.event.get ],
|
|
|
|
|
put: [ mw.auth.access.write, mw.event.put ],
|
|
|
|
|
patch: [ mw.auth.access.write, mw.event.patch ],
|
|
|
|
|
delete: [mw.auth.access.write, mw.event.delete ]
|
2020-08-22 20:33:43 +02:00
|
|
|
},
|
2020-08-12 11:35:57 +02:00
|
|
|
},
|
2022-03-09 17:43:10 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* QC endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
'/project/:project/qc': {
|
|
|
|
|
'/results': {
|
|
|
|
|
// Get all QC results for :project
|
|
|
|
|
get: [ mw.qc.results.get ],
|
|
|
|
|
|
|
|
|
|
// Delete all QC results for :project
|
|
|
|
|
delete: [ mw.auth.access.write, mw.qc.results.delete ],
|
|
|
|
|
|
2022-05-04 18:11:05 +02:00
|
|
|
'/accept': {
|
|
|
|
|
post: [ mw.auth.access.write, mw.qc.results.accept ]
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'/unaccept': {
|
|
|
|
|
post: [ mw.auth.access.write, mw.qc.results.unaccept ]
|
|
|
|
|
},
|
|
|
|
|
|
2022-03-09 17:43:10 +01:00
|
|
|
'/sequence/:sequence': {
|
|
|
|
|
// Get QC results for :project, :sequence
|
|
|
|
|
get: [ mw.qc.results.get ],
|
|
|
|
|
|
|
|
|
|
// Delete QC results for :project, :sequence
|
|
|
|
|
delete: [ mw.auth.access.write, mw.qc.results.delete ]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2022-03-09 17:43:51 +01:00
|
|
|
/*
|
|
|
|
|
* Other miscellaneous endpoints
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-12 11:41:28 +02:00
|
|
|
'/project/:project/label/': {
|
|
|
|
|
get: [ mw.label.list ],
|
2020-08-16 10:06:43 +02:00
|
|
|
// post: [ mw.label.post ],
|
|
|
|
|
},
|
|
|
|
|
'/project/:project/configuration/:path(*)?': {
|
|
|
|
|
get: [ mw.configuration.get ],
|
2020-10-12 19:43:07 +02:00
|
|
|
// post: [ mw.auth.access.admin, mw.label.post ],
|
2020-08-12 11:41:28 +02:00
|
|
|
},
|
2020-09-09 15:55:04 +02:00
|
|
|
'/project/:project/info/:path(*)': {
|
|
|
|
|
get: [ mw.info.get ],
|
2021-05-31 02:23:08 +02:00
|
|
|
post: [ mw.auth.access.write, mw.info.post ],
|
|
|
|
|
put: [ mw.auth.access.write, mw.info.put ],
|
|
|
|
|
delete: [ mw.auth.access.write, mw.info.delete ]
|
2020-09-09 15:55:04 +02:00
|
|
|
},
|
2020-09-20 18:11:33 +02:00
|
|
|
'/project/:project/meta/': {
|
2021-05-16 19:48:41 +02:00
|
|
|
put: [ mw.auth.access.write, mw.meta.put ],
|
2020-09-20 18:11:33 +02:00
|
|
|
},
|
|
|
|
|
'/project/:project/meta/:path(*)': {
|
|
|
|
|
// Path examples:
|
|
|
|
|
// GET:
|
|
|
|
|
// `/raw/sequences/qc/missing_shots`,
|
|
|
|
|
// `/final/points/qc/sync_warn/results
|
|
|
|
|
get: [ mw.meta.get ],
|
|
|
|
|
// // PUT:
|
|
|
|
|
// // `/raw/qc/missing_shots` ← { sequence: …, value: … }
|
|
|
|
|
// put: [ mw.meta.put ]
|
|
|
|
|
},
|
2020-08-08 23:59:13 +02:00
|
|
|
//
|
|
|
|
|
// '/project/:id/permissions/:mode(read|write)?': {
|
|
|
|
|
// get: [ mw.permissions.get ],
|
|
|
|
|
// put: [ mw.permissions.put ],
|
|
|
|
|
// // post: [ mw.permissions.post ],
|
|
|
|
|
// // delete: [ mw.permissions.delete ]
|
|
|
|
|
// },
|
2020-09-01 10:56:25 +02:00
|
|
|
'/navdata/': {
|
|
|
|
|
get: [ mw.navdata.get ],
|
2020-09-01 10:58:37 +02:00
|
|
|
'gis/:featuretype(line|point)': {
|
|
|
|
|
get: [ mw.gis.navdata.get ]
|
|
|
|
|
}
|
2020-10-11 17:51:31 +02:00
|
|
|
},
|
2021-05-20 18:19:29 +02:00
|
|
|
'/info/': {
|
|
|
|
|
':path(*)': {
|
|
|
|
|
get: [ mw.info.get ],
|
|
|
|
|
put: [ mw.auth.access.write, mw.info.put ],
|
|
|
|
|
post: [ mw.auth.access.write, mw.info.post ],
|
|
|
|
|
delete: [ mw.auth.access.write, mw.info.delete ]
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-10-03 22:01:21 +02:00
|
|
|
'/queue/outgoing/': {
|
|
|
|
|
'asaqc': {
|
|
|
|
|
get: [ mw.queue.asaqc.get ],
|
|
|
|
|
post: [ mw.auth.access.write, mw.queue.asaqc.post ],
|
|
|
|
|
'/project/:project': {
|
|
|
|
|
get: [ mw.queue.asaqc.get ],
|
|
|
|
|
'/sequence/:sequence': {
|
|
|
|
|
get: [ mw.queue.asaqc.get ],
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'/:id': {
|
|
|
|
|
delete: [ mw.auth.access.write, mw.queue.asaqc.delete ]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-05-08 00:46:31 +02:00
|
|
|
'/rss/': {
|
|
|
|
|
get: [ mw.rss.get ]
|
|
|
|
|
}
|
2020-08-08 23:59:13 +02:00
|
|
|
//
|
|
|
|
|
// '/user': {
|
|
|
|
|
// get: [ mw.user.get ],
|
|
|
|
|
// post: [ mw.user.put ]
|
|
|
|
|
// },
|
|
|
|
|
// '/user/:user': {
|
|
|
|
|
// get: [ mw.user.get ],
|
|
|
|
|
// put: [ mw.user.put ],
|
|
|
|
|
// // delete: [ mw.user.delete ]
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Generic error handler. Stops stack dumps
|
|
|
|
|
// being sent to clients.
|
|
|
|
|
app.use(function (err, req, res, next) {
|
2020-08-25 11:32:13 +02:00
|
|
|
const title = `HTTP backend error at ${req.method} ${req.originalUrl}`;
|
|
|
|
|
const description = err.message;
|
|
|
|
|
const message = err.message;
|
|
|
|
|
const alert = {title, message, description, error: err};
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
console.log("Error:", err);
|
2022-05-12 22:09:08 +02:00
|
|
|
ERROR("%O", err)
|
2020-08-25 11:32:13 +02:00
|
|
|
|
2021-05-13 21:48:46 +02:00
|
|
|
res.set("Content-Type", "application/json");
|
2020-08-08 23:59:13 +02:00
|
|
|
if (err instanceof Error && err.name != "UnauthorizedError") {
|
2022-05-12 22:09:08 +02:00
|
|
|
// console.error(err.stack);
|
|
|
|
|
ERROR(err.stack);
|
2021-05-13 21:48:46 +02:00
|
|
|
res.set("Content-Type", "text/plain");
|
2020-08-08 23:59:13 +02:00
|
|
|
res.status(500).send('General internal error');
|
2020-08-25 11:32:13 +02:00
|
|
|
maybeSendAlert(alert);
|
2020-08-08 23:59:13 +02:00
|
|
|
} else if (typeof err === 'string') {
|
|
|
|
|
res.status(500).send({message: err});
|
2020-08-25 11:32:13 +02:00
|
|
|
maybeSendAlert(alert);
|
2020-08-08 23:59:13 +02:00
|
|
|
} else {
|
|
|
|
|
res.status(err.status || 500).send({message: err.message || (err.inner && err.inner.message) || "Internal error"});
|
2020-08-25 11:32:13 +02:00
|
|
|
if (!res.status) {
|
|
|
|
|
maybeSendAlert(alert);
|
|
|
|
|
}
|
2020-08-08 23:59:13 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
app.enable('trust proxy');
|
2022-05-12 22:09:08 +02:00
|
|
|
INFO('trust proxy is ' + (app.get('trust proxy')? 'on' : 'off'));
|
2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
if (!module.parent) {
|
2023-04-07 09:04:51 +02:00
|
|
|
const port = process.env.HTTP_PORT || 3000;
|
|
|
|
|
const host = process.env.HTTP_HOST || "127.0.0.1";
|
|
|
|
|
var server = http.createServer(app).listen(port, host);
|
2020-08-08 23:59:13 +02:00
|
|
|
|
2022-05-12 22:09:08 +02:00
|
|
|
INFO('API started on port ' + port);
|
2020-08-08 23:59:13 +02:00
|
|
|
} else {
|
2023-04-07 09:04:51 +02:00
|
|
|
app.start = function (port = 3000, host = "127.0.0.1", path) {
|
2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
var root = app;
|
|
|
|
|
if (path) {
|
|
|
|
|
root = express();
|
|
|
|
|
['x-powered-by', 'trust proxy'].forEach(k => root.set(k, app.get(k)));
|
|
|
|
|
root.use(path, app);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 09:04:51 +02:00
|
|
|
const server = http.createServer(root).listen(port, host);
|
2020-08-08 23:59:13 +02:00
|
|
|
if (server) {
|
2023-05-18 18:30:48 +02:00
|
|
|
console.log(`API started on port ${port}, prefix: ${path || "/"}`);
|
2022-05-12 22:09:08 +02:00
|
|
|
INFO(`API started on port ${port}, prefix: ${path || "/"}`);
|
2020-08-08 23:59:13 +02:00
|
|
|
}
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
module.exports = app;
|
|
|
|
|
}
|