diff --git a/ami-pipeline.js b/ami-pipeline.js new file mode 100755 index 0000000..61decc2 --- /dev/null +++ b/ami-pipeline.js @@ -0,0 +1,137 @@ +#!/usr/bin/env node +/*--------------------------------------------------------------------------------------------------------------------*/ + +import fs from 'fs'; +import url from 'url'; +import path from 'path'; + +/*--------------------------------------------------------------------------------------------------------------------*/ + +import http from 'http'; +import https from 'https'; +import express from 'express'; +import nodeRed from 'node-red'; + +/*--------------------------------------------------------------------------------------------------------------------*/ +/* CONSTANTS */ +/*--------------------------------------------------------------------------------------------------------------------*/ + +const DIR_PATH = path.dirname(url.fileURLToPath(import.meta.url)); + +/*--------------------------------------------------------------------------------------------------------------------*/ + +const NODE_RED_SETTINGS = { + uiPort: 1880, + uiHost: '0.0.0.0', + httpNodeRoot: '/api', + httpAdminRoot: '/', + userDir: path.join(DIR_PATH, 'data', 'node-red'), + editorTheme: { + page: { + css: path.join(DIR_PATH, 'node_modules', 'node-red-contrib-ami', 'style', 'ami.css'), + }, + header: { + title: 'pipeline', + image: path.join(DIR_PATH, 'node_modules', 'node-red-contrib-ami', 'style', 'logo.png'), + }, + login: { + image: path.join(DIR_PATH, 'node_modules', 'node-red-contrib-ami', 'style', 'login.png'), + }, + palette: { + categories: ['AMI', 'subflows', 'common', 'function', 'network', 'sequence', 'parser', 'storage'], + }, + }, +}; + +/*--------------------------------------------------------------------------------------------------------------------*/ +/* APPLICATION */ +/*--------------------------------------------------------------------------------------------------------------------*/ + +const app = express(); + +const isSecured = process.argv.slice(2).includes('--secured'); + +/*--------------------------------------------------------------------------------------------------------------------*/ + +let httpsServer; + +if(isSecured) +{ + /*----------------------------------------------------------------------------------------------------------------*/ + + const HTTPS_SETTINGS = {}; + + const KEY_PATH = path.join(DIR_PATH, 'data', 'key.pem'); + const CERT_PATH = path.join(DIR_PATH, 'data', 'cert.pem'); + + /*----------------------------------------------------------------------------------------------------------------*/ + + try + { + HTTPS_SETTINGS['key'] = fs.readFileSync(KEY_PATH, 'utf8'); + } + catch(e) + { + console.error(`Error opening "${KEY_PATH}"`); + + process.exit(1); + } + + /*----------------------------------------------------------------------------------------------------------------*/ + + try + { + HTTPS_SETTINGS['cert'] = fs.readFileSync(CERT_PATH, 'utf8'); + } + catch(e) + { + console.error(`Error opening "${CERT_PATH}"`); + + process.exit(1); + } + + /*----------------------------------------------------------------------------------------------------------------*/ + + httpsServer = https.createServer(HTTPS_SETTINGS, app); + + /*----------------------------------------------------------------------------------------------------------------*/ +} +else +{ + /*----------------------------------------------------------------------------------------------------------------*/ + + httpsServer = http.createServer(app); + + /*----------------------------------------------------------------------------------------------------------------*/ +} + +/*--------------------------------------------------------------------------------------------------------------------*/ +/* NOD-RED */ +/*--------------------------------------------------------------------------------------------------------------------*/ + +nodeRed.init(httpsServer, NODE_RED_SETTINGS); + +/*--------------------------------------------------------------------------------------------------------------------*/ +/* ROUTES */ +/*--------------------------------------------------------------------------------------------------------------------*/ + +app.use(NODE_RED_SETTINGS.httpAdminRoot, nodeRed.httpAdmin); + +app.use(NODE_RED_SETTINGS.httpNodeRoot, nodeRed.httpNode); + +/*--------------------------------------------------------------------------------------------------------------------*/ +/* LOOP */ +/*--------------------------------------------------------------------------------------------------------------------*/ + +httpsServer.listen(1880); + +/*--------------------------------------------------------------------------------------------------------------------*/ + +nodeRed.start().catch((e) => { + + console.log(`Error starting frontend: ${e}`); + + process.exit(1); +}); + +/*--------------------------------------------------------------------------------------------------------------------*/ diff --git a/package.json b/package.json new file mode 100644 index 0000000..bc2d3b9 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "ami-pipeline", + "version": "1.0.0", + "description": "Node-RED-based pipeline for AMI", + "main": "ami-pipeline.js", + "type": "module", + "scripts": { + "ami-pipeline-http": "node ami-pipeline.js", + "ami-pipeline-https": "node ami-pipeline.js --secured" + }, + "dependencies": { + "express": "^4.18.2", + "node-red": "^3.1.3", + "node-red-contrib-ami": "github:ami-team/node-red-contrib-ami" + } +}