improve gateway

This commit is contained in:
SrGooglo 2025-02-26 20:39:33 +00:00
parent 539539db34
commit 192e0e9bd8
2 changed files with 460 additions and 415 deletions

View File

@ -0,0 +1,22 @@
# Default services allocation ports
3000 -> main
3001 -> posts
3002 -> files
3003 -> music
3004 -> chats
3005 -> marketplace
3006 -> sync
3007 -> ems (External Messaging Service)
3008 -> users
3009 -> notifications
3010 -> search
3011 -> unallocated
3012 -> unallocated
3013 -> unallocated
3014 -> unallocated
3015 -> unallocated
3016 -> unallocated
3017 -> unallocated
3018 -> unallocated
3019 -> unallocated
3020 -> auth

View File

@ -22,6 +22,7 @@ import comtyAscii from "./ascii"
import pkg from "../package.json"
const useLoadSpinner = process.argv.includes("--load-spinner")
const isProduction = process.env.NODE_ENV === "production"
export default class Gateway {
spinnies = new Spinnies()
@ -30,7 +31,7 @@ export default class Gateway {
state = {
proxyPort: 9000,
internalIp: "0.0.0.0",
allReady: false
allReady: false,
}
selectedProcessInstance = null
@ -48,7 +49,9 @@ export default class Gateway {
const instanceFile = path.basename(service)
const instanceBasePath = path.dirname(service)
const servicePkg = require(path.resolve(instanceBasePath, "package.json"))
const servicePkg = require(
path.resolve(instanceBasePath, "package.json"),
)
this.serviceFileReference[instanceFile] = servicePkg.name
@ -66,7 +69,10 @@ export default class Gateway {
async createServicesProcess() {
for await (let service of this.services) {
const { id, version, cwd } = this.serviceRegistry[this.serviceFileReference[path.basename(service)]]
const { id, version, cwd } =
this.serviceRegistry[
this.serviceFileReference[path.basename(service)]
]
this.serviceHandlers.onStarting(id)
@ -87,16 +93,16 @@ export default class Gateway {
const serviceInstance = {
id,
version,
instance
instance,
}
// push to pool
this.instancePool.push(serviceInstance)
// if is NODE_ENV to development, start a file watcher for hot-reload
if (process.env.NODE_ENV === "development") {
// if is development, start a file watcher for hot-reload
if (!isProduction) {
const ignored = [
...await getIgnoredFiles(cwd),
...(await getIgnoredFiles(cwd)),
"**/.cache/**",
"**/node_modules/**",
"**/dist/**",
@ -111,7 +117,9 @@ export default class Gateway {
watcher.on("all", (event, path) => {
// find instance from pool
const instanceIndex = this.instancePool.findIndex((instance) => instance.id === id)
const instanceIndex = this.instancePool.findIndex(
(instance) => instance.id === id,
)
console.log(event, path, instanceIndex)
@ -128,7 +136,7 @@ export default class Gateway {
if (useLoadSpinner) {
this.spinnies.add(id, {
text: `📦 [${id}] Loading service...`,
spinner: DefaultSpinner
spinner: DefaultSpinner,
})
}
}
@ -139,7 +147,9 @@ export default class Gateway {
if (this.serviceRegistry[id].ready === false) {
if (useLoadSpinner) {
if (this.spinnies.pick(id)) {
this.spinnies.succeed(id, { text: `[${id}][${this.serviceRegistry[id].index}] Ready` })
this.spinnies.succeed(id, {
text: `[${id}][${this.serviceRegistry[id].index}] Ready`,
})
}
}
}
@ -188,10 +198,12 @@ export default class Gateway {
})
}
},
onReload: async ({ id, service, cwd, }) => {
onReload: async ({ id, service, cwd }) => {
console.log(`[onReload] ${id} ${service}`)
let instance = this.instancePool.find((instance) => instance.id === id)
let instance = this.instancePool.find(
(instance) => instance.id === id,
)
if (!instance) {
console.error(`❌ Service ${id} not found`)
@ -223,7 +235,9 @@ export default class Gateway {
onIPCData: this.serviceHandlers.onIPCData,
})
const instanceIndex = this.instancePool.findIndex((_instance) => _instance.id === id)
const instanceIndex = this.instancePool.findIndex(
(_instance) => _instance.id === id,
)
if (instanceIndex !== -1) {
this.instancePool[instanceIndex] = instance
@ -242,7 +256,9 @@ export default class Gateway {
if (this.serviceRegistry[id].ready === false) {
if (this.spinnies.pick(id)) {
this.spinnies.fail(id, { text: `[${id}][${this.serviceRegistry[id].index}] Failed with code ${code}` })
this.spinnies.fail(id, {
text: `[${id}][${this.serviceRegistry[id].index}] Failed with code ${code}`,
})
}
}
@ -352,12 +368,16 @@ export default class Gateway {
return false
}
this.selectedProcessInstance = this.instancePool.find((instance) => instance.id === service.id)
this.selectedProcessInstance = this.instancePool.find(
(instance) => instance.id === service.id,
)
if (!this.selectedProcessInstance) {
this.selectedProcessInstance = null
console.error(`Cannot find service [${service.id}] in the instances pool`)
console.error(
`Cannot find service [${service.id}] in the instances pool`,
)
return false
}
@ -368,10 +388,7 @@ export default class Gateway {
return true
},
dettachServiceSTD: (id) => {
},
dettachServiceSTD: (id) => {},
attachAllServicesSTD: () => {
this.std.detachAllServicesSTD()
@ -402,7 +419,9 @@ export default class Gateway {
console.clear()
console.log(comtyAscii)
console.log(`\nRunning ${chalk.bgBlue(`${pkg.name}`)} | ${chalk.bgMagenta(`[v${pkg.version}]`)} | ${this.state.internalIp} \n\n\n`)
console.log(
`\nRunning ${chalk.bgBlue(`${pkg.name}`)} | ${chalk.bgMagenta(`[v${pkg.version}]`)} | ${this.state.internalIp} | ${isProduction ? "production" : "development"} \n\n\n`,
)
if (this.services.length === 0) {
console.error("❌ No services found")
@ -416,7 +435,11 @@ export default class Gateway {
switch (type) {
case "update": {
if (Object.values(this.serviceRegistry).every((service) => service.initialized)) {
if (
Object.values(this.serviceRegistry).every(
(service) => service.initialized,
)
) {
this.onAllServicesReady()
}