#!/usr/bin/env node

const http = require('http');
const https = require('https');
const debug = require('debug')('ultrong:server');
const opn = require('opn');
const path = require('path');
const fs = require('fs');
const app = require('../app');
const net = require('net');
const httpserver = require('./httpserver');



const httpport = 7333;



httpserver.httpServerStart(httpport, app).then(result => {
  debug('http Listening on ' + httpport);
  // debug('http NODE_ENV=' + process.env.NODE_ENV);
}).catch(err => {
  onError(err);
});



function normalizehttpport(val) {
  let httpport = parseInt(val, 10);

  if (isNaN(httpport)) {
    // named pipe
    return val;
  }

  if (httpport >= 0) {
    // httpport number
    return httpport;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  let bind = typeof httpport === 'string' ?
    'Pipe ' + httpport :
    'httpport ' + httpport;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}