You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
"use strict";
|
|
|
|
let { PythonShell } = require("python-shell");
|
|
|
|
/**
|
|
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
|
|
* to customize this controller
|
|
*/
|
|
|
|
module.exports = {
|
|
async findOne(ctx) {
|
|
const { name } = ctx.params;
|
|
|
|
const entity = await strapi.services.function.findOne({ name });
|
|
|
|
if (entity) {
|
|
let args = [];
|
|
|
|
entity.input.forEach((input) => {
|
|
args.push(ctx.request.query[input.name] || input.defaultValue || "");
|
|
});
|
|
|
|
let script = () => {
|
|
return new Promise((resolve, reject) => {
|
|
PythonShell.run(
|
|
`./public${entity.script.url}`,
|
|
{ args: [...args] },
|
|
function (err, results) {
|
|
if (err) throw err;
|
|
resolve(results);
|
|
}
|
|
);
|
|
});
|
|
};
|
|
|
|
let result = await script();
|
|
let message = {};
|
|
entity.output.forEach((out, index) => {
|
|
if (out.type.name == "list")
|
|
message[out.name] = arrayFromLiteral(result[index]);
|
|
else message[out.name] = result[index];
|
|
});
|
|
|
|
return message;
|
|
}
|
|
|
|
// TODO: default error message
|
|
return entity;
|
|
},
|
|
};
|
|
|
|
// happy with it? totally not. but for now oke
|
|
// https://stackoverflow.com/questions/48676751/convert-javascript-string-to-literal-array
|
|
function arrayFromLiteral(literal) {
|
|
return literal
|
|
.replace(/^\[|\]$/g, "") // Remove leading and ending square brackets ([]).
|
|
.split(",") // Split by comma.
|
|
.map(
|
|
(
|
|
phrase // Iterate over each phrase.
|
|
) =>
|
|
phrase
|
|
.trim() // Remove leading and ending whitespace.
|
|
.replace(/"/g, "") // Remove all double quotes (").
|
|
.replace(/^\'|\'$/g, "") // Remove leading and ending single quotes (').
|
|
);
|
|
}
|