typedserver/ts/utilityservers/classes.serviceserver.ts
2024-05-23 14:54:25 +02:00

52 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TypedServer } from '../classes.typedserver.js';
import * as servertools from '../servertools/index.js';
import * as plugins from '../plugins.js';
export interface ILoleServiceServerConstructorOptions {
addCustomRoutes?: (serverArg: servertools.Server) => Promise<any>;
serviceName: string;
serviceVersion: string;
serviceDomain: string;
port?: number;
}
// the main service server
export class UtilityServiceServer {
public options: ILoleServiceServerConstructorOptions;
public typedServer: TypedServer;
constructor(optionsArg: ILoleServiceServerConstructorOptions) {
this.options = optionsArg;
}
public async start() {
console.log('starting lole-serviceserver...')
this.typedServer = new TypedServer({
cors: true,
domain: this.options.serviceDomain,
forceSsl: false,
port: this.options.port || 3000,
robots: true,
defaultAnswer: async () => {
const InfoHtml = (await import('../infohtml/index.js')).InfoHtml;
return (
await InfoHtml.fromSimpleText(
`${this.options.serviceName} (version ${this.options.serviceVersion})`
)
).htmlString;
},
});
// lets add any custom routes
if (this.options.addCustomRoutes) {
await this.options.addCustomRoutes(this.typedServer.server);
}
await this.typedServer.start();
}
public async stop() {
await this.typedServer.stop();
}
}