2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
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 express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
2019-08-26 13:21:52 -03:00
|
|
|
import * as fs from 'fs';
|
2020-12-31 15:36:19 -03:00
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const Path = require('path');
|
2019-03-09 16:59:31 -03:00
|
|
|
|
2019-10-17 22:32:32 -03:00
|
|
|
import { GBLog, GBMinInstance, IGBCoreService, IGBInstance, IGBPackage } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { GBAdminService } from '../packages/admin.gbapp/services/GBAdminService';
|
|
|
|
import { AzureDeployerService } from '../packages/azuredeployer.gbapp/services/AzureDeployerService';
|
|
|
|
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';
|
|
|
|
|
2019-05-14 23:02:21 -03:00
|
|
|
/**
|
|
|
|
* Global shared server data;
|
|
|
|
*/
|
|
|
|
export class RootData {
|
2019-10-17 22:32:32 -03:00
|
|
|
public publicAddress: string; // URI for BotServer
|
|
|
|
public server: any; // Express reference
|
|
|
|
public sysPackages: any[]; // Loaded system package list
|
|
|
|
public appPackages: any[]; // Loaded .gbapp package list
|
2020-10-23 09:55:44 -03:00
|
|
|
public minService: GBMinService; // Minimalist service core
|
2019-10-17 22:32:32 -03:00
|
|
|
public bootInstance: IGBInstance; // General Bot Interface Instance
|
2021-01-20 18:23:42 -03:00
|
|
|
public minInstances: any[]; // List of bot instances.
|
|
|
|
public minBoot: GBMinInstance; // Reference to boot bot.
|
2020-02-25 10:13:38 -03:00
|
|
|
public wwwroot: string; // .gbui or a static webapp.
|
2020-02-25 12:37:10 -03:00
|
|
|
public entryPointDialog: string; // To replace default welcome dialog.
|
2019-05-14 23:02:21 -03:00
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
/**
|
|
|
|
* General Bots open-core entry point.
|
|
|
|
*/
|
|
|
|
export class GBServer {
|
2019-05-14 23:02:21 -03:00
|
|
|
public static globals: RootData;
|
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
/**
|
|
|
|
* Program entry-point.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
public static run() {
|
2021-01-05 07:47:48 -03:00
|
|
|
|
2021-11-16 14:53:52 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`The Bot Server is in STARTING mode...`);
|
2019-05-14 23:02:21 -03:00
|
|
|
GBServer.globals = new RootData();
|
2019-06-28 11:17:41 -03:00
|
|
|
GBConfigService.init();
|
2021-11-16 14:53:52 -03:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
const port = GBConfigService.getServerPort();
|
2018-11-12 12:20:44 -02:00
|
|
|
const server = express();
|
2019-06-05 18:23:31 -03:00
|
|
|
GBServer.globals.server = server;
|
2019-08-22 17:28:11 -03:00
|
|
|
GBServer.globals.appPackages = [];
|
|
|
|
GBServer.globals.sysPackages = [];
|
|
|
|
GBServer.globals.minInstances = [];
|
2020-02-25 10:13:38 -03:00
|
|
|
GBServer.globals.wwwroot = null;
|
2020-02-26 15:20:47 -03:00
|
|
|
GBServer.globals.entryPointDialog = null;
|
2019-08-22 17:28:11 -03:00
|
|
|
|
2019-03-11 19:32:47 -03:00
|
|
|
server.use(bodyParser.json());
|
2019-05-27 08:46:17 -03:00
|
|
|
server.use(bodyParser.urlencoded({ extended: true }));
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2019-08-26 13:21:52 -03:00
|
|
|
// Creates working directory.
|
|
|
|
|
2019-10-17 22:32:32 -03:00
|
|
|
const workDir = Path.join(process.env.PWD, 'work');
|
|
|
|
if (!fs.existsSync(workDir)) {
|
2019-08-26 13:21:52 -03:00
|
|
|
mkdirp.sync(workDir);
|
2019-10-17 22:32:32 -03:00
|
|
|
}
|
2019-08-26 13:21:52 -03:00
|
|
|
|
2018-09-09 16:40:04 -03:00
|
|
|
server.listen(port, () => {
|
|
|
|
(async () => {
|
2021-12-19 16:39:50 -03:00
|
|
|
|
2018-09-09 16:40:04 -03:00
|
|
|
try {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.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
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
const core: IGBCoreService = new GBCoreService();
|
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);
|
2020-04-02 22:02:50 -03:00
|
|
|
|
2019-05-27 09:14:49 -03:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
2019-06-26 13:18:15 -03:00
|
|
|
const proxy = GBConfigService.get('REVERSE_PROXY');
|
|
|
|
if (proxy !== undefined) {
|
|
|
|
GBServer.globals.publicAddress = proxy;
|
2019-10-17 22:32:32 -03:00
|
|
|
} else {
|
2019-06-26 13:18:15 -03:00
|
|
|
// Ensure that local development proxy is setup.
|
|
|
|
|
|
|
|
GBLog.info(`Establishing a development local proxy (ngrok)...`);
|
|
|
|
GBServer.globals.publicAddress = await core.ensureProxy(port);
|
|
|
|
}
|
2020-04-28 20:54:04 -03:00
|
|
|
process.env.BOT_URL = GBServer.globals.publicAddress;
|
2019-05-27 09:14:49 -03:00
|
|
|
} else {
|
2020-04-02 23:52:59 -03:00
|
|
|
const serverAddress = process.env.BOT_URL;
|
2019-05-27 09:14:49 -03:00
|
|
|
GBLog.info(`Defining server address at ${serverAddress}...`);
|
|
|
|
GBServer.globals.publicAddress = serverAddress;
|
|
|
|
}
|
2020-07-26 16:46:37 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
// Creates a boot instance or load it from storage.
|
2019-02-25 08:36:43 -03:00
|
|
|
|
2018-11-28 17:08:06 -02:00
|
|
|
try {
|
|
|
|
await core.initStorage();
|
|
|
|
} catch (error) {
|
2019-03-11 19:32:47 -03:00
|
|
|
GBLog.verbose(`Error initializing storage: ${error}`);
|
2020-12-31 15:36:19 -03:00
|
|
|
GBServer.globals.bootInstance =
|
2020-10-26 20:18:38 -03:00
|
|
|
await core.createBootInstance(core, azureDeployer, GBServer.globals.publicAddress);
|
2021-04-17 17:20:44 -03:00
|
|
|
|
2018-11-28 17:08:06 -02:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:44:39 -03:00
|
|
|
core.ensureAdminIsSecured();
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Deploys system and user packages.
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Deploying packages...`);
|
2020-04-13 19:14:55 -03:00
|
|
|
GBServer.globals.sysPackages = await core.loadSysPackages(core);
|
2018-11-28 17:08:06 -02:00
|
|
|
await core.checkStorage(azureDeployer);
|
2019-08-22 17:28:11 -03:00
|
|
|
await deployer.deployPackages(core, server, GBServer.globals.appPackages);
|
2021-05-18 10:56:04 -03:00
|
|
|
await core.syncDatabaseStructure();
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2021-11-29 18:39:42 -03:00
|
|
|
|
2020-10-26 20:18:38 -03:00
|
|
|
GBLog.info(`Publishing instances...`);
|
2020-12-31 15:36:19 -03:00
|
|
|
const instances: IGBInstance[] = await core.loadAllInstances(
|
2020-10-23 09:55:44 -03:00
|
|
|
core,
|
|
|
|
azureDeployer,
|
|
|
|
GBServer.globals.publicAddress
|
2018-11-28 17:08:06 -02:00
|
|
|
);
|
2020-10-26 20:18:38 -03:00
|
|
|
|
|
|
|
if (instances.length === 0) {
|
2021-04-17 17:20:44 -03:00
|
|
|
|
2020-10-26 20:18:38 -03:00
|
|
|
const instance = await importer.importIfNotExistsBotPackage(
|
|
|
|
GBConfigService.get('BOT_ID'),
|
|
|
|
'boot.gbot',
|
|
|
|
'packages/boot.gbot',
|
|
|
|
GBServer.globals.bootInstance
|
|
|
|
);
|
|
|
|
await deployer.deployBotFull(instance, GBServer.globals.publicAddress);
|
|
|
|
instances.push(instance);
|
2021-04-17 17:20:44 -03:00
|
|
|
|
2021-11-29 18:39:42 -03:00
|
|
|
// Runs the search even with empty content to create structure.
|
|
|
|
|
2021-04-17 17:20:44 -03:00
|
|
|
await azureDeployer['runSearch'](instance);
|
2020-10-26 20:18:38 -03:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:55:44 -03:00
|
|
|
GBServer.globals.bootInstance = instances[0];
|
2018-11-27 22:56:11 -02:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Builds minimal service infrastructure.
|
2020-04-02 22:02:50 -03:00
|
|
|
|
2020-03-31 09:11:04 -03:00
|
|
|
const conversationalService: GBConversationalService = new GBConversationalService(core);
|
2018-11-27 22:56:11 -02:00
|
|
|
const minService: GBMinService = new GBMinService(core, conversationalService, adminService, deployer);
|
2019-08-22 01:54:30 +00:00
|
|
|
GBServer.globals.minService = minService;
|
2019-08-22 17:28:11 -03:00
|
|
|
await minService.buildMin(instances);
|
2018-11-30 11:55:44 -02:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Deployment of local applications for the first time.
|
|
|
|
|
2020-10-23 09:55:44 -03:00
|
|
|
if (GBConfigService.get('DISABLE_WEB') !== 'true') {
|
2020-04-02 22:02:50 -03:00
|
|
|
deployer.setupDefaultGBUI();
|
|
|
|
}
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`The Bot Server is in RUNNING mode...`);
|
2021-04-17 17:20:44 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Opens Navigator.
|
|
|
|
|
2019-06-28 11:17:41 -03:00
|
|
|
// TODO: Config: core.openBrowserInDevelopment();
|
2018-09-09 16:40:04 -03:00
|
|
|
} catch (err) {
|
2020-07-26 16:46:37 -03:00
|
|
|
GBLog.error(`STOP: ${err.message ? err.message : 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
|
|
|
}
|
|
|
|
}
|