"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerInitializer = void 0;
const url_1 = require("url");
const util_1 = require("util");
const LogUtil_1 = require("../logging/LogUtil");
const HttpServerFactory_1 = require("../server/HttpServerFactory");
const Initializer_1 = require("./Initializer");
/**
 * Creates and starts an HTTP server.
 */
class ServerInitializer extends Initializer_1.Initializer {
    constructor(serverFactory, port, socketPath) {
        super();
        this.logger = (0, LogUtil_1.getLoggerFor)(this);
        this.serverFactory = serverFactory;
        this.port = port;
        this.socketPath = socketPath;
        if (!port && !socketPath) {
            throw new Error('Either Port or Socket arguments must be set');
        }
    }
    async handle() {
        this.server = await this.serverFactory.createServer();
        if (this.socketPath) {
            this.logger.info(`Listening to server at ${this.server.address()}`);
            this.server.listen(this.socketPath);
        }
        else {
            const url = new url_1.URL(`http${(0, HttpServerFactory_1.isHttpsServer)(this.server) ? 's' : ''}://localhost:${this.port}/`).href;
            this.logger.info(`Listening to server at ${url}`);
            this.server.listen(this.port);
        }
    }
    async finalize() {
        if (this.server) {
            return (0, util_1.promisify)(this.server.close.bind(this.server))();
        }
    }
}
exports.ServerInitializer = ServerInitializer;
//# sourceMappingURL=ServerInitializer.js.map