43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
|
import * as plugins from './test-sdk.plugins';
|
||
|
|
||
|
export class AgTestServer {
|
||
|
public handlers: Array<plugins.agSdk.AAgHandler<any>> = [];
|
||
|
public server: plugins.smartexpress.Server;
|
||
|
|
||
|
constructor(handlerArg?: plugins.agSdk.AAgHandler<any>) {
|
||
|
if (handlerArg) {
|
||
|
this.handlers.push(handlerArg);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public async addAgHandler(handlerArg: plugins.agSdk.AAgHandler<any>) {
|
||
|
this.handlers.push(handlerArg);
|
||
|
await this.stop();
|
||
|
await this.start();
|
||
|
}
|
||
|
|
||
|
public async start() {
|
||
|
this.server = new plugins.smartexpress.Server({
|
||
|
cors: true,
|
||
|
defaultAnswer: async () => 'apiglobal testserver',
|
||
|
domain: 'localhost',
|
||
|
forceSsl: false,
|
||
|
});
|
||
|
for (const handlerArg of this.handlers) {
|
||
|
this.server.addRoute(
|
||
|
`/${handlerArg.slug}`,
|
||
|
new plugins.smartexpress.HandlerTypedRouter(handlerArg.typedrouter)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public async stop() {
|
||
|
if (this.server) {
|
||
|
const previousServer = this.server;
|
||
|
this.server = null;
|
||
|
await this.server.startedPromise;
|
||
|
await this.server.stop();
|
||
|
}
|
||
|
}
|
||
|
}
|