-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} | ||
} | ||
|