Browse Source

Await migration before running server

pull/1/head
rozetko 6 years ago
parent
commit
e894272f40
  1. 122
      server/src/index.ts

122
server/src/index.ts

@ -14,73 +14,75 @@ import * as Koa from 'koa';
import * as Router from 'koa-router'; import * as Router from 'koa-router';
import * as bodyParser from 'koa-bodyparser'; import * as bodyParser from 'koa-bodyparser';
init();
AnalyticsController.init(); async function init() {
ProcessService.registerExitHandler(AnalyticsController.terminate); await convertPanelUrlToPanelId();
convertPanelUrlToPanelId(); AnalyticsController.init();
ProcessService.registerExitHandler(AnalyticsController.terminate);
var app = new Koa(); const app = new Koa();
app.on('error', (err, ctx) => { app.on('error', (err, ctx) => {
console.log('got server error:'); console.log('got server error:');
console.log(err); console.log(err);
}); });
app.use(bodyParser()) app.use(bodyParser())
app.use(async function(ctx, next) { app.use(async function(ctx, next) {
ctx.set('Access-Control-Allow-Origin', '*'); ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
await next();
});
app.use(async function(ctx, next) {
try {
await next(); await next();
} catch (e) { });
console.error(e); app.use(async function(ctx, next) {
ctx.response.status = 500; try {
await next();
} catch (e) {
console.error(e);
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `${ctx.method} ${ctx.url} error: ${e.message}`
};
}
});
const rootRouter = new Router();
rootRouter.use('/analyticUnits', analyticUnitsRouter.routes(), analyticUnitsRouter.allowedMethods());
rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods());
rootRouter.use('/threshold', thresholdRouter.routes(), thresholdRouter.allowedMethods());
rootRouter.get('/', async (ctx) => {
const activeWebhooks = await AnalyticsController.getActiveWebhooks();
ctx.response.body = { ctx.response.body = {
code: 500, server: 'OK',
message: `${ctx.method} ${ctx.url} error: ${e.message}` analytics: {
ready: AnalyticsController.isAnalyticReady(),
lastAlive: AnalyticsController.analyticsLastAlive(),
tasksQueueLength: AnalyticsController.getQueueLength()
},
nodeVersion: process.version,
packageVersion: PACKAGE_VERSION,
npmUserAgent: process.env.npm_config_user_agent,
docker: process.env.INSIDE_DOCKER !== undefined,
zmqConectionString: ZMQ_CONNECTION_STRING,
serverPort: HASTIC_PORT,
git: GIT_INFO,
activeWebhooks: activeWebhooks.length,
timestamp: new Date(Date.now())
}; };
} });
});
app
.use(rootRouter.routes())
var rootRouter = new Router(); .use(rootRouter.allowedMethods());
rootRouter.use('/analyticUnits', analyticUnitsRouter.routes(), analyticUnitsRouter.allowedMethods());
rootRouter.use('/segments', segmentsRouter.routes(), segmentsRouter.allowedMethods());
rootRouter.use('/threshold', thresholdRouter.routes(), thresholdRouter.allowedMethods());
rootRouter.get('/', async (ctx) => {
const activeWebhooks = await AnalyticsController.getActiveWebhooks();
ctx.response.body = {
server: 'OK',
analytics: {
ready: AnalyticsController.isAnalyticReady(),
lastAlive: AnalyticsController.analyticsLastAlive(),
tasksQueueLength: AnalyticsController.getQueueLength()
},
nodeVersion: process.version,
packageVersion: PACKAGE_VERSION,
npmUserAgent: process.env.npm_config_user_agent,
docker: process.env.INSIDE_DOCKER !== undefined,
zmqConectionString: ZMQ_CONNECTION_STRING,
serverPort: HASTIC_PORT,
git: GIT_INFO,
activeWebhooks: activeWebhooks.length,
timestamp: new Date(Date.now())
};
});
app
.use(rootRouter.routes())
.use(rootRouter.allowedMethods());
app.listen(HASTIC_PORT, () => {
console.log(`Server is running on :${HASTIC_PORT}`);
});
app.listen(HASTIC_PORT, () => {
console.log(`Server is running on :${HASTIC_PORT}`);
});
}

Loading…
Cancel
Save