const https = require('https');
const debug = require('debug')('ultrong:httpsserver');
const path = require('path');
const config = require('../config/server');
const fs = require('fs');

/**
 * SSlServer
 * @param {*} port 
 * @param {*} app 
 */
function httpSslServerStart(port, app) {
    /*证书配置*/
    const options = {
        key: fs.readFileSync(config.https.certificate.key),
        cert: fs.readFileSync(config.https.certificate.cert),
        crt:fs.readFileSync(config.https.certificate.crt)
    }
    /*启动sslserver*/
    return new Promise((resolve, reject) => {
        app.set('sslPort', port);
        const sslServer = https.createServer(options,app);
        sslServer.listen(port);
        sslServer.on('error', err => {
            reject(err);
        });
        sslServer.on('listening', () => {
            debug('https server ready now');
            resolve(sslServer);
        });
    })
}

module.exports.httpSslServerStart = httpSslServerStart;