test-sdk/ts/test-sdk.classes.testserver.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-03-20 13:23:33 +00:00
import * as plugins from './test-sdk.plugins.js';
2020-07-05 17:53:49 +00:00
export class AgTestServer {
public handlers: Array<plugins.agSdk.AAgHandler<any>> = [];
public server: plugins.smartexpress.Server;
constructor(handlerArg?: plugins.agSdk.AAgHandler<any>) {
if (handlerArg) {
2020-07-26 14:01:17 +00:00
this.addAgHandler(handlerArg);
2020-07-05 17:53:49 +00:00
}
}
public async addAgHandler(handlerArg: plugins.agSdk.AAgHandler<any>) {
this.handlers.push(handlerArg);
2020-07-26 14:01:17 +00:00
console.log(`added handler with slug ${handlerArg.slug}`);
if (this.server && this.server.serverStatus === 'running') {
await this.stop();
await this.start();
}
2020-07-05 17:53:49 +00:00
}
public async start() {
this.server = new plugins.smartexpress.Server({
cors: true,
2020-07-26 13:54:46 +00:00
forceSsl: false,
2020-07-05 17:53:49 +00:00
defaultAnswer: async () => 'apiglobal testserver',
domain: 'localhost',
2020-07-25 15:47:34 +00:00
port: 3000,
2020-07-05 17:53:49 +00:00
});
for (const handlerArg of this.handlers) {
2020-07-26 13:54:46 +00:00
console.log(`found handler with slug ${handlerArg.slug}`);
2020-07-25 16:34:10 +00:00
await handlerArg.start();
2020-07-26 13:54:46 +00:00
console.log(`started handler with slug ${handlerArg.slug}`);
const slugroute = `/${handlerArg.slug}`;
2020-07-05 17:53:49 +00:00
this.server.addRoute(
2020-07-26 13:54:46 +00:00
slugroute,
2020-07-05 17:53:49 +00:00
new plugins.smartexpress.HandlerTypedRouter(handlerArg.typedrouter)
);
2020-07-26 13:54:46 +00:00
console.log(`added slugroute ${slugroute}`);
2020-07-05 17:53:49 +00:00
}
2020-07-05 23:31:26 +00:00
await this.server.start();
2020-07-05 17:53:49 +00:00
}
public async stop() {
if (this.server) {
const previousServer = this.server;
this.server = null;
2020-07-05 23:35:55 +00:00
await previousServer.startedPromise;
await previousServer.stop();
2020-07-05 17:53:49 +00:00
}
}
}