botserver/src/app.ts

127 lines
5.6 KiB
TypeScript
Raw Normal View History

#! /usr/bin/env node
2018-04-21 02:59:30 -03:00
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
| Licensed under the AGPL-3.0. |
| |
| According to our dual licensing model, this program can be used either |
| under the terms of the GNU Affero General Public License, version 3, |
| or under a proprietary license. |
| |
| The texts of the GNU Affero General Public License with an additional |
| permission and of our proprietary license can be found at and |
| in the LICENSE file you have received along with this program. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU Affero General Public License for more details. |
| |
| "General Bots" is a registered trademark of Pragmatismo.io. |
| The licensing of the program under the AGPLv3 does not imply a |
| trademark license. Therefore any rights, title and interest in |
| our trademarks remain entirely with us. |
| |
\*****************************************************************************/
"use strict";
const UrlJoin = require("url-join");
const logger = require("./logger");
const express = require("express");
import { UniversalBot } from "botbuilder";
import { Sequelize } from "sequelize-typescript";
import { GBConfigService } from "../deploy/core.gbapp/services/GBConfigService";
import { GBConversationalService } from "../deploy/core.gbapp/services/GBConversationalService";
import { GBMinService } from "../deploy/core.gbapp/services/GBMinService";
import { GBDeployer } from "../deploy/core.gbapp/services/GBDeployer";
import { GBCoreService } from "../deploy/core.gbapp/services/GBCoreService";
import { GBImporter } from "../deploy/core.gbapp/services/GBImporter";
import { GBAnalyticsPackage } from "../deploy/analytics.gblib";
import { GBCorePackage } from "../deploy/core.gbapp";
import { GBKBPackage } from '../deploy/kb.gbapp';
import { GBSecurityPackage } from '../deploy/security.gblib';
import { GBAdminPackage } from '../deploy/admin.gbapp/index';
import { GBCustomerSatisfactionPackage } from "../deploy/customer-satisfaction.gbapp";
import { IGBPackage } from 'botlib';
/**
* General Bots open-core entry point.
*/
export class GBServer {
/** Program entry-point. */
static run() {
logger.info("Starting General Bots Open Core (Guaribas)...");
// Creates a basic HTTP server that will serve several URL, one for each
// bot instance. This allows the same server to attend multiple Bot on
// the Marketplace until GB get serverless.
let port = process.env.port || process.env.PORT || 4242;
logger.info(`Starting HTTP BotServer...`);
2018-04-21 02:59:30 -03:00
let server = express();
server.listen(port, () => {
logger.info(`General Bot - RUNNING on ${port}...`);
2018-04-21 02:59:30 -03:00
logger.info(`Starting instances...`);
// Reads basic configuration, initialize minimal services.
GBConfigService.init();
let core = new GBCoreService();
core.initDatabase(() => {
// Boot a bot package if any.
let deployer = new GBDeployer(core, new GBImporter(core));
// Build a minimal bot instance for each .gbot deployment.
let conversationalService = new GBConversationalService(core);
let minService = new GBMinService(core, conversationalService, deployer);
let sysPackages = new Array<IGBPackage>();
[GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage,
GBKBPackage, GBCustomerSatisfactionPackage].forEach(e => {
2018-04-21 02:59:30 -03:00
logger.trace(`Loading sys package: ${e.name}...`);
let p = Object.create(e.prototype) as IGBPackage;
p.loadPackage(core, core.sequelize);
sysPackages.push(p);
});
(async () => {
try {
let appPackages = new Array<IGBPackage>();
await minService.deployPackages(core, server, appPackages, sysPackages);
minService.buildMin(instance => {
logger.info(`Instance loaded: ${instance.botId}...`);
}, server, appPackages);
} catch (err) {
logger.log(err)
}
})()
2018-04-21 02:59:30 -03:00
});
return core;
});
}
}
// First line to run.
GBServer.run();