2018-05-11 22:18:38 -03:00
|
|
|
#! /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. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| 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, |
|
2018-09-11 19:40:53 -03:00
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| 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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const logger = require('./logger');
|
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
import { IGBInstance, IGBPackage } from 'botlib';
|
|
|
|
import { GBAdminService } from '../packages/admin.gbapp/services/GBAdminService';
|
|
|
|
import { AzureDeployerService } from '../packages/azuredeployer.gbapp/services/AzureDeployerService';
|
|
|
|
import { GuaribasInstance } from '../packages/core.gbapp/models/GBModel';
|
|
|
|
import { GBConfigService } from '../packages/core.gbapp/services/GBConfigService';
|
|
|
|
import { GBConversationalService } from '../packages/core.gbapp/services/GBConversationalService';
|
|
|
|
import { GBCoreService } from '../packages/core.gbapp/services/GBCoreService';
|
|
|
|
import { GBDeployer } from '../packages/core.gbapp/services/GBDeployer';
|
2018-11-26 14:09:09 -02:00
|
|
|
import { GBImporter } from '../packages/core.gbapp/services/GBImporterService';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { GBMinService } from '../packages/core.gbapp/services/GBMinService';
|
2018-12-01 20:48:08 -02:00
|
|
|
import { GBVMService } from '../packages/core.gbapp/services/GBVMService';
|
2019-02-23 13:17:21 -03:00
|
|
|
import { load } from 'dotenv';
|
2018-11-12 12:20:44 -02:00
|
|
|
|
|
|
|
const appPackages = new Array<IGBPackage>();
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
/**
|
|
|
|
* General Bots open-core entry point.
|
|
|
|
*/
|
|
|
|
export class GBServer {
|
2018-09-24 15:27:26 -03:00
|
|
|
/**
|
|
|
|
* Program entry-point.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
public static run() {
|
2018-11-04 09:19:03 -02:00
|
|
|
logger.info(`The Bot Server is in STARTING mode...`);
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
// 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.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const port = process.env.port || process.env.PORT || 4242;
|
|
|
|
const server = express();
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
server.use(bodyParser.json()); // to support JSON-encoded bodies
|
|
|
|
server.use(
|
|
|
|
bodyParser.urlencoded({
|
|
|
|
// to support URL-encoded bodies
|
2018-11-27 22:56:11 -02:00
|
|
|
extended: true
|
|
|
|
})
|
2018-09-24 11:04:36 -03:00
|
|
|
);
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2018-10-25 18:13:51 -03:00
|
|
|
let bootInstance: IGBInstance;
|
2018-09-09 16:40:04 -03:00
|
|
|
server.listen(port, () => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
2018-11-04 09:19:03 -02:00
|
|
|
logger.info(`Now accepting connections on ${port}...`);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-09 16:40:04 -03:00
|
|
|
// Reads basic configuration, initialize minimal services.
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
GBConfigService.init();
|
2018-11-12 12:20:44 -02:00
|
|
|
const core = new GBCoreService();
|
2018-12-18 13:50:35 -02:00
|
|
|
core.ensureAdminIsSecured();
|
2018-10-25 18:13:51 -03:00
|
|
|
|
2018-11-27 22:56:11 -02:00
|
|
|
const importer: GBImporter = new GBImporter(core);
|
|
|
|
const deployer: GBDeployer = new GBDeployer(core, importer);
|
|
|
|
const azureDeployer: AzureDeployerService = new AzureDeployerService(deployer);
|
|
|
|
const adminService: GBAdminService = new GBAdminService(core);
|
|
|
|
const conversationalService: GBConversationalService = new GBConversationalService(core);
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Ensure that local development proxy is setup.
|
|
|
|
|
|
|
|
logger.info(`Establishing a development local proxy (ngrok)...`);
|
|
|
|
const proxyAddress: string = await core.ensureProxy(port);
|
2019-02-25 08:36:43 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Creates a boot instance or load it frmo storage.
|
2019-02-25 08:36:43 -03:00
|
|
|
|
2018-11-28 17:08:06 -02:00
|
|
|
let bootInstance: IGBInstance = null;
|
|
|
|
try {
|
|
|
|
await core.initStorage();
|
|
|
|
} catch (error) {
|
|
|
|
bootInstance = await core.createBootInstance(core, azureDeployer, proxyAddress);
|
|
|
|
await core.initStorage();
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Deploys system and user packages.
|
|
|
|
|
|
|
|
logger.info(`Deploying packages...`);
|
2019-02-25 08:36:43 -03:00
|
|
|
core.loadSysPackages(core);
|
2018-11-28 17:08:06 -02:00
|
|
|
await core.checkStorage(azureDeployer);
|
2018-09-24 11:04:36 -03:00
|
|
|
await deployer.deployPackages(core, server, appPackages);
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Loads all bot instances.
|
|
|
|
|
2018-11-05 14:23:36 -02:00
|
|
|
logger.info(`Publishing instances...`);
|
2018-11-28 17:08:06 -02:00
|
|
|
const packageInstance = await importer.importIfNotExistsBotPackage(
|
|
|
|
GBConfigService.get('CLOUD_GROUP'),
|
|
|
|
'boot.gbot',
|
|
|
|
'packages/boot.gbot'
|
|
|
|
);
|
2018-11-27 22:56:11 -02:00
|
|
|
const fullInstance = Object.assign(packageInstance, bootInstance);
|
|
|
|
await core.saveInstance(fullInstance);
|
|
|
|
let instances: GuaribasInstance[] = await core.loadAllInstances(core, azureDeployer, proxyAddress);
|
|
|
|
instances = await core.ensureInstances(instances, bootInstance, core);
|
2018-12-18 13:50:35 -02:00
|
|
|
if (!bootInstance) {
|
2018-11-30 11:55:44 -02:00
|
|
|
bootInstance = instances[0];
|
|
|
|
}
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Builds minimal service infrastructure.
|
|
|
|
|
2018-11-27 22:56:11 -02:00
|
|
|
const minService: GBMinService = new GBMinService(core, conversationalService, adminService, deployer);
|
2018-11-30 11:55:44 -02:00
|
|
|
await minService.buildMin(bootInstance, server, appPackages, instances, deployer);
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Deployment of local applications for the first time.
|
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
deployer.installDefaultGBUI();
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2018-11-26 15:54:34 -02:00
|
|
|
logger.info(`The Bot Server is in RUNNING mode...`);
|
2018-12-18 13:50:35 -02:00
|
|
|
|
|
|
|
// Opens Navigator.
|
|
|
|
|
2018-11-26 15:54:34 -02:00
|
|
|
core.openBrowserInDevelopment();
|
2018-11-04 17:26:29 -02:00
|
|
|
|
2018-09-09 16:40:04 -03:00
|
|
|
} catch (err) {
|
2018-11-12 12:20:44 -02:00
|
|
|
logger.error(`STOP: ${err} ${err.stack ? err.stack : ''}`);
|
2018-09-24 15:27:26 -03:00
|
|
|
process.exit(1);
|
2018-09-09 16:40:04 -03:00
|
|
|
}
|
2018-09-24 11:04:36 -03:00
|
|
|
})();
|
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 19:59:27 -02:00
|
|
|
// First line to run.
|
|
|
|
|
2019-02-25 08:36:43 -03:00
|
|
|
GBServer.run();
|