Skip to content

Commit

Permalink
split api start and index
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarlosn committed Mar 9, 2025
1 parent 0357970 commit e6ad773
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 72 deletions.
74 changes: 2 additions & 72 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,3 @@
import dotenv from 'dotenv'
dotenv.config({ path: '../../.env' });
import { getServiceToken } from 'protonode'
import { setConfig, getLogger } from 'protobase';
import { getBaseConfig } from '@my/config'
setConfig(getBaseConfig('api', process, getServiceToken()))
require('events').EventEmitter.defaultMaxListeners = 100;
const logger = getLogger()
import http from 'http';
global.defaultRoute = '/api/v1'
import app from './api'
//@ts-ignore
import { generateEvent } from 'app/bundles/library'
import chokidar from 'chokidar';
import { start } from "./start"

const isProduction = process.env.NODE_ENV === 'production';
const serviceName = isProduction?'api':'api-dev'
const server = http.createServer(app);
const PORT = 3001

server.listen(PORT, () => {
logger.debug({ service: { protocol: "http", port: PORT } }, "Service started: HTTP")
});


generateEvent({
path: 'services/'+serviceName+'/start', //event type: / separated event category: files/create/file, files/create/dir, devices/device/online
from: serviceName, // system entity where the event was generated (next, api, cmd...)
user: 'system', // the original user that generates the action, 'system' if the event originated in the system itself
payload: {}, // event payload, event-specific data
}, getServiceToken())

if (process.env.NODE_ENV != 'production') {
const pathsToWatch = [
'src/**',
'../../packages/app/bundles/**',
'../../packages/app/apis/**',
'../../packages/app/chatbots/**',
'../../packages/protolib/dist/**',
'../../system.js',
'../../packages/app/conf.ts',
'../../packages/protonode/dist/**',
'../../packages/protobase/dist/**',
];

const watcher = chokidar.watch(pathsToWatch, {
ignored: /^([.][^.\/\\])|([\/\\]+[.][^.])/,
persistent: true
});

var restarting = false
var restartTimer = null
watcher.on('change', async (path) => {

if (restarting) {
clearTimeout(restartTimer)
} else {
console.log(`File ${path} has been changed, restarting...`);
restarting = true
}


restartTimer = setTimeout(async () => {
await generateEvent({
path: 'services/' + serviceName + '/stop', //event type: / separated event category: files/create/file, files/create/dir, devices/device/online
from: serviceName, // system entity where the event was generated (next, api, cmd...)
user: 'system', // the original user that generates the action, 'system' if the event originated in the system itself
payload: {}, // event payload, event-specific data
}, getServiceToken())
process.exit(0)
}, 1000);
})
}
start()
76 changes: 76 additions & 0 deletions apps/api/src/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import dotenv from 'dotenv'
dotenv.config({ path: '../../.env' });
import { getServiceToken } from 'protonode'
import { setConfig, getLogger } from 'protobase';
import { getBaseConfig } from '@my/config'
setConfig(getBaseConfig('api', process, getServiceToken()))
require('events').EventEmitter.defaultMaxListeners = 100;
const logger = getLogger()
import http from 'http';
global.defaultRoute = '/api/v1'
import app from './api'
//@ts-ignore
import { generateEvent } from 'app/bundles/library'
import chokidar from 'chokidar';

export const start = async () => {
const isProduction = process.env.NODE_ENV === 'production';
const serviceName = isProduction?'api':'api-dev'
const server = http.createServer(app);
const PORT = 3001

server.listen(PORT, () => {
logger.debug({ service: { protocol: "http", port: PORT } }, "Service started: HTTP")
});


generateEvent({
path: 'services/'+serviceName+'/start', //event type: / separated event category: files/create/file, files/create/dir, devices/device/online
from: serviceName, // system entity where the event was generated (next, api, cmd...)
user: 'system', // the original user that generates the action, 'system' if the event originated in the system itself
payload: {}, // event payload, event-specific data
}, getServiceToken())

if (process.env.NODE_ENV != 'production') {
const pathsToWatch = [
'src/**',
'../../packages/app/bundles/**',
'../../packages/app/apis/**',
'../../packages/app/chatbots/**',
'../../packages/protolib/dist/**',
'../../system.js',
'../../packages/app/conf.ts',
'../../packages/protonode/dist/**',
'../../packages/protobase/dist/**',
];

const watcher = chokidar.watch(pathsToWatch, {
ignored: /^([.][^.\/\\])|([\/\\]+[.][^.])/,
persistent: true
});

var restarting = false
var restartTimer = null
watcher.on('change', async (path) => {

if (restarting) {
clearTimeout(restartTimer)
} else {
console.log(`File ${path} has been changed, restarting...`);
restarting = true
}


restartTimer = setTimeout(async () => {
await generateEvent({
path: 'services/' + serviceName + '/stop', //event type: / separated event category: files/create/file, files/create/dir, devices/device/online
from: serviceName, // system entity where the event was generated (next, api, cmd...)
user: 'system', // the original user that generates the action, 'system' if the event originated in the system itself
payload: {}, // event payload, event-specific data
}, getServiceToken())
process.exit(0)
}, 1000);
})
}
}

0 comments on commit e6ad773

Please sign in to comment.