-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94bb7ab
commit 49335b4
Showing
4 changed files
with
87 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> | ||
{ | ||
await this.#setUp(); | ||
|
||
return this.#service.start(); | ||
} | ||
|
||
async stop(): Promise<void> | ||
{ | ||
await this.#service.stop();; | ||
|
||
return this.#tearDown(); | ||
} | ||
|
||
run(request: Request): Promise<Response> | ||
{ | ||
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<void> | ||
{ | ||
return this.#runScripts(this.#setUpScripts); | ||
} | ||
|
||
#tearDown(): Promise<void> | ||
{ | ||
return this.#runScripts(this.#tearDownScripts); | ||
} | ||
|
||
async #runScripts(scripts: string[]): Promise<void> | ||
{ | ||
await Promise.all(scripts.map(script => this.#sourcingManager.import(script))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters