Spaces:
Running
Running
File size: 13,242 Bytes
b39afbe 0526d9d b39afbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
/**
*
* This file composes the runnable server from services and integrations
*
**/
import { OmniLogLevels, registerOmnilogGlobal, type IServiceConfig } from 'omni-shared';
import Server from './core/Server.js';
import { loadServerConfig, type IServerConfig } from './loadConfig.js';
import { exec } from 'child_process';
import os from 'os';
import fs from 'node:fs';
// Services
import { APIServerService, type IAPIServerServiceConfig } from './services/APIService.js';
import { AmqpService } from './services/AmqpService.js';
import { ChatService } from './services/ChatService.js';
import { CredentialService } from './services/CredentialsService/CredentialService.js';
import { LocalFileCredentialStore } from './services/CredentialsService/Store/BaseCredentialStore.js';
import { VaultWardenCredentialStore } from './services/CredentialsService/Store/VaultWardenCredentialStore.js';
import { DBService } from './services/DBService.js';
import { FastifyServerService, type FastifyServerServiceConfig } from './services/FastifyServerService.js';
import { HttpClientService } from './services/HttpClientService.js';
import {
JobControllerService,
type IJobControllerServiceConfig
} from './services/JobController/JobControllerService.js';
import { MessagingServerService, type IMessagingServerServiceConfig } from './services/MessagingService.js';
import {
RESTConsumerService,
type RESTConsumerServiceConfig
} from './services/RestConsumerService/RESTConsumerService.js';
// Integrations
import { AuthIntegration, type IAuthIntegrationConfig } from './integrations/Authentication/AuthIntegration.js';
import {
LocalCdnIntegration,
type ILocalCdnIntegrationConfig
} from './integrations/CdnIntegrations/LocalCdnIntegration.js';
import { ChatIntegration, type IChatIntegrationConfig } from './integrations/Chat/ChatIntegration.js';
import {
MercsDefaultIntegration,
type MercsDefaultIntegrationConfig
} from './integrations/Mercenaries/MercsDefaultIntegration.js';
import {
WorkflowIntegration,
type IWorkflowIntegrationConfig
} from './integrations/WorkflowIntegration/WorkflowIntegration.js';
import { Command, type OptionValues } from 'commander';
import path from 'path';
import { ServerExtensionManager } from './core/ServerExtensionsManager.js';
// ----------------------------------------- Globals ----------------------------------------
registerOmnilogGlobal();
omnilog.wrapConsoleLogger();
// ----------------------------------------- Server -----------------------------------------
const config: IServerConfig = loadServerConfig('../../.mercs.yaml') as IServerConfig;
const packagejson = JSON.parse(
fs.readFileSync('package.json', { encoding: 'utf-8' }));
const serverConfig = config.server;
serverConfig.version = packagejson.version;
const server_config = serverConfig;
process.on('unhandledRejection', (reason, promise) => {
omnilog.trace();
omnilog.error('Uncaught error in', promise, reason);
process.exit(1);
});
// ----------------------------------------- CLI -----------------------------------------
const bootstrap = async (): Promise<void> => {
const program: Command = new Command();
// common options
program
.option('-u, --updateExtensions', 'Update all extensions')
.option('-rb, --refreshBlocks', 'Refresh block definitions')
.option('-px, --pruneExtensions', 'Prune deprecated extensions')
.option('-R, --resetDB <scope>', 'Reset the database on startup. Valid scopes: blocks,settings')
.option('--chown <user>', 'Reparent all unowned files in CDN storage to this user')
.option('-ll, --loglevel <level>', 'Set logging level', serverConfig.logger.level.toString())
.option('--emittery', 'Enable emittery debug logs. Always disabled on log level silent(0).')
.option('--verbose', 'Max logging level')
.option(
'-purl, --publicUrl <url>',
'Set the external address for services that requires it',
server_config.network.public_url
)
.option(
'--fastifyopt <fastifyopt>',
'Advanced Fastify options - JSON Object',
JSON.stringify({ bodyLimit: 32 * 1024 * 1024 })
)
.option('-p, --port <port>', 'Overwrite the listening port', '1688')
.option('--openBrowser')
.option('-nx, --noExtensions', 'Disable all (non core) extensions')
.option('-s, --secure <secure>', 'Enforce secure connection', false)
.option('--dburl <url>', 'Connection URL to the DB')
.option('--dbuser <user>', 'DB admin user', '[email protected]')
.option('--viteProxy <url>', 'Specify vite debugger URL')
.option('--autologin', 'Autologin user')
.option('--uncensored', 'Disable NSFW protections')
.option('--flushLogs', 'Flush logs to DB')
.requiredOption('-l, --listen <addr>', 'Sets the interface the host listens on');
program.action((options) => {
// apply option overwrites
omnilog.setCustomLevel('emittery', options.emittery ? OmniLogLevels.verbose : OmniLogLevels.silent);
omnilog.level = options.verbose ? OmniLogLevels.verbose : Number.parseInt(options.loglevel);
const isLocalStack = options.listen === '127.0.0.1';
// set defaults for Autologin if not present
if (options.autologin === undefined) {
options.autologin = isLocalStack;
}
// Default to true: set --flushLogs false to disable
if (options.flushLogs === undefined) {
options.flushLogs = true;
}
// set defaults for DB if not present, rewrites
if (!options.dburl) {
server_config.services.db.pocketbaseDbUrl = isLocalStack
? serverConfig.services.db.pocketbase.local.dbUrl
: serverConfig.services.db.pocketbase.development.dbUrl;
} else {
server_config.services.db.pocketbaseDbUrl = options.dburl;
}
server_config.services.db.pocketbaseDbAdmin = options.dbuser;
server_config.services.db.flushLogs = options.flushLogs;
// public URL
const publicURL = new URL(options.publicUrl);
server_config.network.public_url = options.publicUrl;
// Cookie security
server_config.session.cookie.secure = options.secure;
// CDN overwrites
const currentCDNLocalRoute = new URL(server_config.integrations.cdn.localRoute);
server_config.integrations.cdn.local.url = publicURL.host;
currentCDNLocalRoute.protocol = publicURL.protocol;
currentCDNLocalRoute.hostname = publicURL.hostname;
currentCDNLocalRoute.port = publicURL.port;
server_config.integrations.cdn.localRoute = currentCDNLocalRoute.href;
// finally boot
void boot(options);
});
program.parse();
};
// ----------------------------------------- Boot -----------------------------------------
const boot = async (options: OptionValues) => {
const server = new Server('mercs', serverConfig, options);
// Initialize global settings before everything starts
await server.initGlobalSettings();
const extensionPath = path.join(process.cwd(), 'extensions');
omnilog.status_start('--- Ensuring core extensions -----');
await ServerExtensionManager.ensureCoreExtensions(extensionPath);
omnilog.status_success('OK');
omnilog.status_start('--- Updating extensions -----');
await ServerExtensionManager.updateExtensions(extensionPath, options);
omnilog.status_success('OK');
if (options.pruneExtensions) {
omnilog.status_start('--- Pruning extensions -----');
await ServerExtensionManager.pruneExtensions(extensionPath);
omnilog.status_success('OK');
}
omnilog.status_start('Booting Server');
// ----------------------------------------- Services -----------------------------------------
const dbConfig = Object.assign({ id: 'db' }, server_config.services?.db);
server.use(DBService, dbConfig, 'service');
const messagingConfig: IMessagingServerServiceConfig = Object.assign(
{ id: 'messaging' },
serverConfig.services?.messaging
);
server.use(MessagingServerService, messagingConfig, 'service');
// Amqp Service
const amqpConfig = Object.assign({ id: 'amqp' }, serverConfig.services?.amqp);
server.use(AmqpService, amqpConfig);
if (!serverConfig.services?.credentials?.disabled) {
if (serverConfig.services?.credentials?.type === 'local') {
const store = new LocalFileCredentialStore(serverConfig.services?.credentials?.storeConfig);
server.use(
CredentialService,
Object.assign({ id: 'credentials' }, serverConfig.services?.credentials, { store })
);
} else if (serverConfig.services?.credentials?.type === 'vaultWarden') {
const store = new VaultWardenCredentialStore(serverConfig.services?.credentials?.storeConfig);
server.use(
CredentialService,
Object.assign({ id: 'credentials' }, serverConfig.services?.credentials, { store })
);
} else {
server.debug('⚠️Default to KV storage');
server.use(CredentialService, Object.assign({ id: 'credentials' }, serverConfig.services?.credentials));
}
} else {
server.warn('⚠️CredentialService is disabled in config.');
}
// RestConsumerService
if (!serverConfig.services?.rest_consumer?.disabled) {
const consumerConfig: RESTConsumerServiceConfig = Object.assign(
{ id: 'rest_consumer' },
serverConfig.services?.rest_consumer
);
server.use(RESTConsumerService, consumerConfig);
} else {
server.warn('⚠️RestConsumerService is disabled in config.');
}
// Axios wrapper
server.use(HttpClientService, { id: 'http_client' });
// TODO: Currently this is using the client path rather than directly invoking the functions on the server. That all
// needs to be reworked. But for now this allows us to run code on both client and server.
const apiConfig: IAPIServerServiceConfig = {
id: 'api',
host: 'http://127.0.0.1:1688', // remote API is disabled?
integrationsUrl: '/api/v1/mercenaries/integrations'
};
server.use(APIServerService, apiConfig);
// if (config.server.integrations.cdn.type == 'local') {
const cdnConfig: ILocalCdnIntegrationConfig = Object.assign({ id: 'cdn' }, serverConfig.integrations?.cdn);
server.use(LocalCdnIntegration, cdnConfig, 'integration');
// JobControllerService
const jobControllerServiceConfig: IJobControllerServiceConfig = Object.assign(
{ id: 'jobs' },
serverConfig.services?.jobs
);
server.use(JobControllerService, jobControllerServiceConfig);
// Chat Service
const chatServiceConfig: IServiceConfig = Object.assign({ id: 'chat' });
server.use(ChatService, chatServiceConfig);
const listenOn = new URL('http://0.0.0.0:1688');
listenOn.hostname = options.listen;
listenOn.protocol = options.secure ? 'https' : 'http';
listenOn.port = options.port;
const fastifyOptions = JSON.parse(options.fastifyopt);
const corsOrigin = [listenOn.origin];
if (options.viteProxy !== undefined) {
corsOrigin.push(options.viteProxy);
}
const fastifyConfig: FastifyServerServiceConfig = {
id: 'httpd',
listen: { host: listenOn.hostname, port: Number.parseInt(listenOn.port) },
cors: { origin: corsOrigin, credentials: true },
autologin: options.autologin,
proxy: {
enabled: options.viteProxy !== undefined,
viteDebugger: options.viteProxy
},
plugins: {},
opts: fastifyOptions,
session: {
secret: serverConfig.session.secret,
cookie: serverConfig.session.cookie,
kvStorage: serverConfig.kvStorage
},
rateLimit: {
global: serverConfig.network.rateLimit.global,
max: serverConfig.network.rateLimit.max,
timeWindow: serverConfig.network.rateLimit.timeWindow
}
};
server.use(FastifyServerService, fastifyConfig);
// ----------------------------------------- Integrations -----------------------------------------
const mercsIntegrationConfig: MercsDefaultIntegrationConfig = Object.assign(
{ id: 'mercenaries' },
serverConfig.integrations?.mercenaries
);
server.use(MercsDefaultIntegration, mercsIntegrationConfig);
const workflowConfig: IWorkflowIntegrationConfig = Object.assign(
{ id: 'workflow' },
serverConfig.integrations?.workflow
);
server.use(WorkflowIntegration, workflowConfig);
const authConfig: IAuthIntegrationConfig = Object.assign({ id: 'auth' }, serverConfig.integrations?.auth);
server.use(AuthIntegration, authConfig);
const chatConfig: IChatIntegrationConfig = Object.assign({ id: 'chat' }, serverConfig.integrations?.chat);
server.use(ChatIntegration, chatConfig);
// ----------------------------------------- Extensions -----------------------------------------
await server.init();
await server.load();
await server.start();
omnilog.status_success(`Server has started and is ready to accept connections on ${listenOn.origin}`);
omnilog.status_success('Ctrl-C to quit.');
// open default browser
if (options.openBrowser) {
switch (os.platform()) {
case 'win32':
exec(`start ${options.publicUrl}`);
break;
case 'darwin':
exec(`open ${options.publicUrl}`);
break;
}
}
};
bootstrap().catch((err) => {
omnilog.trace();
omnilog.error('Caught unhandled exception during bootstrap: ', err);
process.exit(1);
});
|