diff --git a/ecosystem.config.js b/ecosystem.config.js index 435cd173..dcd7e851 100644 --- a/ecosystem.config.js +++ b/ecosystem.config.js @@ -2,7 +2,7 @@ module.exports = { apps: [ { name: "main_api", - script: "./infisical-run.sh", + script: "./dist/index.js", instances: "max", exec_mode: "cluster", env: { @@ -12,7 +12,7 @@ module.exports = { }, { name: "music_api", - script: "./infisical-run.sh", + script: "./dist/index.js", instances: "max", exec_mode: "cluster", env: { @@ -22,7 +22,7 @@ module.exports = { }, { name: "file_api", - script: "./infisical-run.sh", + script: "./dist/index.js", instances: "max", exec_mode: "cluster", env: { @@ -32,7 +32,7 @@ module.exports = { }, { name: "marketplace_api", - script: "./infisical-run.sh", + script: "./dist/index.js", instances: "max", exec_mode: "cluster", env: { @@ -42,7 +42,7 @@ module.exports = { }, { name: "chat_api", - script: "./infisical-run.sh", + script: "./dist/index.js", instances: "max", exec_mode: "cluster", env: { diff --git a/packages/chat_server/infisical-run.sh b/packages/chat_server/infisical-run.sh deleted file mode 100644 index d55a4ca3..00000000 --- a/packages/chat_server/infisical-run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -/usr/bin/infisical run -- npm run:prod \ No newline at end of file diff --git a/packages/chat_server/src/index.js b/packages/chat_server/src/index.js index d1b83a65..57895a24 100755 --- a/packages/chat_server/src/index.js +++ b/packages/chat_server/src/index.js @@ -1,13 +1,12 @@ -require("dotenv").config() - -if (typeof process.env.NODE_ENV === "undefined") { - process.env.NODE_ENV = "development" -} - -global.isProduction = process.env.NODE_ENV === "production" +import { webcrypto as crypto } from "crypto" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +import infisical from "infisical-node" + +require("dotenv").config() + +global.isProduction = process.env.NODE_ENV === "production" globalThis["__root"] = path.resolve(__dirname) @@ -34,6 +33,8 @@ global.b64Encode = (data) => { return Buffer.from(data, "utf-8").toString("base64") } +global.nanoid = (t = 21) => crypto.getRandomValues(new Uint8Array(t)).reduce(((t, e) => t += (e &= 63) < 36 ? e.toString(36) : e < 62 ? (e - 26).toString(36).toUpperCase() : e > 62 ? "-" : "_"), ""); + Array.prototype.updateFromObjectKeys = function (obj) { this.forEach((value, index) => { if (obj[value] !== undefined) { @@ -59,9 +60,60 @@ global.toBoolean = (value) => { import API from "./api" async function main() { - const api = new API() + if (process.env.INFISICAL_TOKEN) { + const client = new infisical({ + token: process.env.INFISICAL_TOKEN, + }) - await api.initialize() + const secrets = await client.getAllSecrets() + + // inject to process.env + secrets.forEach((secret) => { + process.env[secret.secretName] = secret.secretValue + }) + } + + const instance = new API() + + await instance.initialize() + + // kill on process exit + process.on("exit", () => { + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(0) + }) + + // kill on ctrl+c + process.on("SIGINT", () => { + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(0) + }) + + // kill on uncaught exceptions + process.on("uncaughtException", (error) => { + console.error(`🆘 [FATAL ERROR] >`, error) + + if (typeof instance.server.close === "function") { + instance.server.close() + } + + process.exit(1) + }) + + // kill on unhandled rejections + process.on("unhandledRejection", (error) => { + console.error(`🆘 [FATAL ERROR] >`, error) + + if (typeof instance.server.close === "function") { + instance.server.close() + } + + process.exit(1) + }) } main().catch((error) => { diff --git a/packages/file_server/infisical-run.sh b/packages/file_server/infisical-run.sh deleted file mode 100644 index d55a4ca3..00000000 --- a/packages/file_server/infisical-run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -/usr/bin/infisical run -- npm run:prod \ No newline at end of file diff --git a/packages/file_server/src/index.js b/packages/file_server/src/index.js index 1176d8d4..57895a24 100644 --- a/packages/file_server/src/index.js +++ b/packages/file_server/src/index.js @@ -1,9 +1,12 @@ -require("dotenv").config() -global.isProduction = process.env.NODE_ENV === "production" import { webcrypto as crypto } from "crypto" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +import infisical from "infisical-node" + +require("dotenv").config() + +global.isProduction = process.env.NODE_ENV === "production" globalThis["__root"] = path.resolve(__dirname) @@ -57,35 +60,60 @@ global.toBoolean = (value) => { import API from "./api" async function main() { + if (process.env.INFISICAL_TOKEN) { + const client = new infisical({ + token: process.env.INFISICAL_TOKEN, + }) + + const secrets = await client.getAllSecrets() + + // inject to process.env + secrets.forEach((secret) => { + process.env[secret.secretName] = secret.secretValue + }) + } + const instance = new API() await instance.initialize() - // // kill on process exit - // process.on("exit", () => { - // instance.server.close() - // process.exit(0) - // }) + // kill on process exit + process.on("exit", () => { + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(0) + }) - // // kill on ctrl+c - // process.on("SIGINT", () => { - // instance.server.close() - // process.exit(0) - // }) + // kill on ctrl+c + process.on("SIGINT", () => { + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(0) + }) - // // kill on uncaught exceptions - // process.on("uncaughtException", (error) => { - // console.error(`🆘 [FATAL ERROR] >`, error) - // instance.server.close() - // process.exit(1) - // }) + // kill on uncaught exceptions + process.on("uncaughtException", (error) => { + console.error(`🆘 [FATAL ERROR] >`, error) - // // kill on unhandled rejections - // process.on("unhandledRejection", (error) => { - // console.error(`🆘 [FATAL ERROR] >`, error) - // instance.server.close() - // process.exit(1) - // }) + if (typeof instance.server.close === "function") { + instance.server.close() + } + + process.exit(1) + }) + + // kill on unhandled rejections + process.on("unhandledRejection", (error) => { + console.error(`🆘 [FATAL ERROR] >`, error) + + if (typeof instance.server.close === "function") { + instance.server.close() + } + + process.exit(1) + }) } main().catch((error) => { diff --git a/packages/marketplace_server/infisical-run.sh b/packages/marketplace_server/infisical-run.sh deleted file mode 100644 index d55a4ca3..00000000 --- a/packages/marketplace_server/infisical-run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -/usr/bin/infisical run -- npm run:prod \ No newline at end of file diff --git a/packages/marketplace_server/src/index.js b/packages/marketplace_server/src/index.js index eae1eb45..57895a24 100644 --- a/packages/marketplace_server/src/index.js +++ b/packages/marketplace_server/src/index.js @@ -1,9 +1,12 @@ -require("dotenv").config() -global.isProduction = process.env.NODE_ENV === "production" import { webcrypto as crypto } from "crypto" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +import infisical from "infisical-node" + +require("dotenv").config() + +global.isProduction = process.env.NODE_ENV === "production" globalThis["__root"] = path.resolve(__dirname) @@ -57,33 +60,58 @@ global.toBoolean = (value) => { import API from "./api" async function main() { + if (process.env.INFISICAL_TOKEN) { + const client = new infisical({ + token: process.env.INFISICAL_TOKEN, + }) + + const secrets = await client.getAllSecrets() + + // inject to process.env + secrets.forEach((secret) => { + process.env[secret.secretName] = secret.secretValue + }) + } + const instance = new API() await instance.initialize() // kill on process exit process.on("exit", () => { - instance.server.close() + if (typeof instance.server.close === "function") { + instance.server.close() + } process.exit(0) }) // kill on ctrl+c process.on("SIGINT", () => { - instance.server.close() + if (typeof instance.server.close === "function") { + instance.server.close() + } process.exit(0) }) // kill on uncaught exceptions process.on("uncaughtException", (error) => { console.error(`🆘 [FATAL ERROR] >`, error) - instance.server.close() + + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(1) }) // kill on unhandled rejections process.on("unhandledRejection", (error) => { console.error(`🆘 [FATAL ERROR] >`, error) - instance.server.close() + + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(1) }) } diff --git a/packages/music_server/infisical-run.sh b/packages/music_server/infisical-run.sh deleted file mode 100644 index d55a4ca3..00000000 --- a/packages/music_server/infisical-run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -/usr/bin/infisical run -- npm run:prod \ No newline at end of file diff --git a/packages/music_server/src/index.js b/packages/music_server/src/index.js index cf65ad47..57895a24 100755 --- a/packages/music_server/src/index.js +++ b/packages/music_server/src/index.js @@ -1,13 +1,12 @@ -require("dotenv").config() - -if (typeof process.env.NODE_ENV === "undefined") { - process.env.NODE_ENV = "development" -} - -global.isProduction = process.env.NODE_ENV === "production" +import { webcrypto as crypto } from "crypto" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +import infisical from "infisical-node" + +require("dotenv").config() + +global.isProduction = process.env.NODE_ENV === "production" globalThis["__root"] = path.resolve(__dirname) @@ -34,6 +33,8 @@ global.b64Encode = (data) => { return Buffer.from(data, "utf-8").toString("base64") } +global.nanoid = (t = 21) => crypto.getRandomValues(new Uint8Array(t)).reduce(((t, e) => t += (e &= 63) < 36 ? e.toString(36) : e < 62 ? (e - 26).toString(36).toUpperCase() : e > 62 ? "-" : "_"), ""); + Array.prototype.updateFromObjectKeys = function (obj) { this.forEach((value, index) => { if (obj[value] !== undefined) { @@ -59,33 +60,58 @@ global.toBoolean = (value) => { import API from "./api" async function main() { - const api = new API() + if (process.env.INFISICAL_TOKEN) { + const client = new infisical({ + token: process.env.INFISICAL_TOKEN, + }) - await api.initialize() + const secrets = await client.getAllSecrets() + + // inject to process.env + secrets.forEach((secret) => { + process.env[secret.secretName] = secret.secretValue + }) + } + + const instance = new API() + + await instance.initialize() // kill on process exit process.on("exit", () => { - api.server.close() + if (typeof instance.server.close === "function") { + instance.server.close() + } process.exit(0) }) // kill on ctrl+c process.on("SIGINT", () => { - api.server.close() + if (typeof instance.server.close === "function") { + instance.server.close() + } process.exit(0) }) // kill on uncaught exceptions process.on("uncaughtException", (error) => { console.error(`🆘 [FATAL ERROR] >`, error) - api.server.close() + + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(1) }) // kill on unhandled rejections process.on("unhandledRejection", (error) => { console.error(`🆘 [FATAL ERROR] >`, error) - api.server.close() + + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(1) }) } diff --git a/packages/server/infisical-run.sh b/packages/server/infisical-run.sh deleted file mode 100755 index d55a4ca3..00000000 --- a/packages/server/infisical-run.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -/usr/bin/infisical run -- npm run:prod \ No newline at end of file diff --git a/packages/server/package.json b/packages/server/package.json index 05240318..41240147 100755 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -5,8 +5,7 @@ "scripts": { "build": "corenode-cli build", "dev": "cross-env NODE_ENV=development nodemon --ignore dist/ --exec corenode-node ./src/index.js", - "run:prod": "cross-env NODE_ENV=production node ./dist/index.js", - "run:infisical:prod": "infisical run -- npm run run:prod" + "run:prod": "cross-env NODE_ENV=production node ./dist/index.js" }, "license": "MIT", "dependencies": { @@ -26,6 +25,7 @@ "dotenv": "^16.0.3", "fluent-ffmpeg": "^2.1.2", "formidable": "^2.1.1", + "infisical-node": "^1.2.1", "jimp": "^0.16.2", "jsonwebtoken": "^9.0.0", "linebridge": "0.15.13", @@ -54,4 +54,4 @@ "mocha": "^10.2.0", "nodemon": "^2.0.15" } -} \ No newline at end of file +} diff --git a/packages/server/src/index.js b/packages/server/src/index.js index 51908a39..57895a24 100755 --- a/packages/server/src/index.js +++ b/packages/server/src/index.js @@ -1,16 +1,24 @@ -require("dotenv").config() -global.isProduction = process.env.NODE_ENV === "production" import { webcrypto as crypto } from "crypto" import path from "path" import { registerBaseAliases } from "linebridge/dist/server" +import infisical from "infisical-node" + +require("dotenv").config() + +global.isProduction = process.env.NODE_ENV === "production" + +globalThis["__root"] = path.resolve(__dirname) const customAliases = { + "root": globalThis["__root"], + "@shared-classes": path.resolve(__dirname, "_shared/classes"), "@services": path.resolve(__dirname, "services"), } if (!global.isProduction) { customAliases["comty.js"] = path.resolve(__dirname, "../../comty.js/src") + customAliases["@shared-classes"] = path.resolve(__dirname, "shared-classes") } registerBaseAliases(undefined, customAliases) @@ -52,9 +60,60 @@ global.toBoolean = (value) => { import API from "./api" async function main() { - const mainAPI = new API() + if (process.env.INFISICAL_TOKEN) { + const client = new infisical({ + token: process.env.INFISICAL_TOKEN, + }) - await mainAPI.initialize() + const secrets = await client.getAllSecrets() + + // inject to process.env + secrets.forEach((secret) => { + process.env[secret.secretName] = secret.secretValue + }) + } + + const instance = new API() + + await instance.initialize() + + // kill on process exit + process.on("exit", () => { + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(0) + }) + + // kill on ctrl+c + process.on("SIGINT", () => { + if (typeof instance.server.close === "function") { + instance.server.close() + } + process.exit(0) + }) + + // kill on uncaught exceptions + process.on("uncaughtException", (error) => { + console.error(`🆘 [FATAL ERROR] >`, error) + + if (typeof instance.server.close === "function") { + instance.server.close() + } + + process.exit(1) + }) + + // kill on unhandled rejections + process.on("unhandledRejection", (error) => { + console.error(`🆘 [FATAL ERROR] >`, error) + + if (typeof instance.server.close === "function") { + instance.server.close() + } + + process.exit(1) + }) } main().catch((error) => { diff --git a/scripts/post-deploy.js b/scripts/post-deploy.js index 046c268d..151628be 100644 --- a/scripts/post-deploy.js +++ b/scripts/post-deploy.js @@ -1,3 +1,5 @@ +require("dotenv").config() + const path = require("path") const fs = require("fs") const child_process = require("child_process") @@ -26,7 +28,6 @@ async function main() { } // copy entire shared folder - // shared/* => /_shared/* fs.mkdirSync(sharedPath, { recursive: true }) await child_process.execSync(`cp -r ${sharedRootPath}/* ${sharedPath} && echo Shared lib copied`, { @@ -39,6 +40,19 @@ async function main() { cwd: packagePath, stdio: "inherit" }) + + if (process.env.INFISICAL_TOKEN) { + // write env + const envPath = path.resolve(packagePath, ".env") + + if (fs.existsSync(envPath)) { + fs.unlinkSync(envPath) + } + + const envData = `INFISICAL_TOKEN="${process.env.INFISICAL_TOKEN}"` + + await fs.writeFileSync(envPath, envData) + } } }