@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
117 lines • 4.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Server = void 0;
const error_1 = require("../../../error/index.js");
const adapters_1 = require("./api/adapters");
const api_server_1 = require("./api/api-server");
const database_server_1 = require("./database/database-server");
const adapters_2 = require("./page/adapters");
const page_server_1 = require("./page/page-server");
const server_config_1 = require("./types/server-config");
class Server {
config;
apiAdapter;
pageAdapter;
apiServer;
pageServer;
databaseServer;
constructor(config) {
this.config = config;
const adapters = this.createAdapters(config.server);
this.apiAdapter = adapters.api;
this.pageAdapter = adapters.page;
this.initializeServers();
}
createAdapters(config) {
if (config.vendor === server_config_1.ServerVendor.FASTIFY) {
const instance = config.instance;
return {
api: new adapters_1.FastifyApiAdapter(instance),
page: new adapters_2.FastifyPageAdapter(instance),
};
}
else if (config.vendor === server_config_1.ServerVendor.EXPRESS) {
const instance = config.instance;
return {
api: new adapters_1.ExpressApiAdapter(instance),
page: new adapters_2.ExpressPageAdapter(instance),
};
}
else {
throw new error_1.InternalServerError({
message: `Unsupported server vendor: ${config.vendor}`,
reason: 'Server vendor must be Fastify or Express',
component: 'Server',
operation: 'createAdapters',
metadata: { vendor: config.vendor },
});
}
}
initializeServers() {
this.apiServer = new api_server_1.ApiServer(this.config.server, this.apiAdapter);
this.pageServer = new page_server_1.PageServer(this.config, this.pageAdapter);
if (this.config.databases.length > 0) {
this.databaseServer = new database_server_1.DatabaseServer(this.config.name, this.config.databases);
}
}
async configureDatabases() {
if (this.databaseServer) {
await this.databaseServer.configure();
}
}
async registerEntrypoints(entryPoints) {
if (this.apiServer) {
await this.apiServer.registerEntrypoints(entryPoints);
}
}
getApiAdapter() {
return this.apiAdapter;
}
getPageAdapter() {
return this.pageAdapter;
}
getApiServer() {
return this.apiServer;
}
getPageServer() {
return this.pageServer;
}
getDatabaseServer() {
return this.databaseServer;
}
async start(options) {
try {
await this.configureDatabases();
if (this.pageServer) {
await this.pageServer.configure();
}
if (!this.apiServer) {
throw new error_1.InternalServerError({
message: 'ApiServer not initialized',
reason: 'ApiServer failed to initialize',
component: 'Server',
operation: 'start',
});
}
const port = options?.port ?? this.config.port;
await this.apiServer.listen(port);
}
catch (error) {
throw new error_1.CustomError({
message: `failed to start service "${this.config.name}"`,
code: 500,
reason: 'service start failed',
error: error,
metadata: {
service: {
name: this.config.name,
vendor: this.config.server.vendor,
type: this.config.server.type,
},
},
});
}
}
}
exports.Server = Server;
//# sourceMappingURL=server.js.map