diff --git a/packages/services/src/Client.ts b/packages/services/src/Client.ts index 2c450aa2..e61ac0bf 100644 --- a/packages/services/src/Client.ts +++ b/packages/services/src/Client.ts @@ -1,6 +1,6 @@ -import { Request, Response, ExecutionManager, Runner } from '@jitar/execution'; -import { MiddlewareManager, ProcedureRunner } from '@jitar/middleware'; +import type { Request, Response, ExecutionManager, Runner } from '@jitar/execution'; +import { type MiddlewareManager, ProcedureRunner } from '@jitar/middleware'; import RemoteGateway from './gateway/RemoteGateway'; @@ -27,8 +27,6 @@ export default class Client implements Runner // TODO: Should be done when constructing the middleware manager this.#middlewareManager.addMiddleware(new ProcedureRunner(this.#executionManager)); - - // TODO: Move runtime registration to the starter script } run(request: Request): Promise diff --git a/packages/services/src/Server.ts b/packages/services/src/Server.ts new file mode 100644 index 00000000..3bacae0e --- /dev/null +++ b/packages/services/src/Server.ts @@ -0,0 +1,76 @@ + +import type { Request, Response, Runner } from '@jitar/execution'; +import type { SourcingManager } from '@jitar/sourcing'; + +import Service from './Service'; +import RunnerService from './RunnerService'; + +type Configuration = +{ + service: Service; + sourcingManager: SourcingManager; + setUpScripts?: string[]; + tearDownScripts?: string[]; +}; + +export default class Server implements Runner +{ + #service: Service; + #sourcingManager: SourcingManager; + #setUpScripts: string[]; + #tearDownScripts: string[]; + + constructor(configuration: Configuration) + { + this.#service = configuration.service; + this.#sourcingManager = configuration.sourcingManager; + this.#setUpScripts = configuration.setUpScripts ?? []; + this.#tearDownScripts = configuration.tearDownScripts ?? []; + } + + get service() { return this.#service; } + + async start(): Promise + { + await this.#setUp(); + + return this.#service.start(); + } + + async stop(): Promise + { + await this.#service.stop();; + + return this.#tearDown(); + } + + run(request: Request): Promise + { + if (this.#canNotRun()) + { + throw new Error('Cannot run'); + } + + return (this.#service as RunnerService).run(request); + } + + #canNotRun(): boolean + { + return (this.#service as RunnerService).run === undefined; + } + + #setUp(): Promise + { + return this.#runScripts(this.#setUpScripts); + } + + #tearDown(): Promise + { + return this.#runScripts(this.#tearDownScripts); + } + + async #runScripts(scripts: string[]): Promise + { + await Promise.all(scripts.map(script => this.#sourcingManager.import(script))); + } +} diff --git a/packages/services/src/index.ts b/packages/services/src/index.ts index e865bcba..c073df24 100644 --- a/packages/services/src/index.ts +++ b/packages/services/src/index.ts @@ -14,6 +14,7 @@ export { default as LocalWorker } from './worker/LocalWorker'; export { default as RemoteWorker } from './worker/RemoteWorker'; export { default as Client } from './Client'; +export { default as Server } from './Server'; export { default as Remote } from './Remote'; export { default as Service } from './Service'; diff --git a/packages/sourcing/src/SourcingManager.ts b/packages/sourcing/src/SourcingManager.ts index 35eb5c4c..7373434b 100644 --- a/packages/sourcing/src/SourcingManager.ts +++ b/packages/sourcing/src/SourcingManager.ts @@ -4,13 +4,18 @@ import { Module, ImportFunction, ModuleNotLoaded } from './modules'; export default class SourceManager { - #import: ImportFunction; #fileManager: FileManager; + #import: ImportFunction; - constructor(importFunction: ImportFunction, fileManager: FileManager) + constructor(fileManager: FileManager, importFunction: ImportFunction) { - this.#import = importFunction; this.#fileManager = fileManager; + this.#import = importFunction; + } + + async read(filename: string): Promise + { + return this.#fileManager.read(filename); } async import(specifier: string): Promise @@ -33,9 +38,4 @@ export default class SourceManager throw new ModuleNotLoaded(specifier, message); } } - - async read(filename: string): Promise - { - return this.#fileManager.read(filename); - } }