main.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env node
  2. // Gestion du signal d'arret (SIGINT = Ctrl+C)
  3. process.on('SIGINT', function () {
  4. console.info('\nStopping the server');
  5. process.exit();
  6. });
  7. // Chargement fichier config
  8. const config = require('./src/config');
  9. const protocol = (config.https) ? 'https' : 'http';
  10. // Gestion des commandes et des options de l'application
  11. const argv = require('yargs')
  12. .command('serve [port]', 'start the Loquicompta server', (yargs) => {
  13. yargs.positional('port', {describe: 'port to bind', default: 80});
  14. }, (argv) => {
  15. })
  16. .command('dev', 'start the Loquicompta server on 8080 port with verbose', (yargs) => {
  17. }, (argv) => {
  18. argv.port = 8080;
  19. argv.verbose = 1;
  20. })
  21. .option('port', {
  22. alias: 'p',
  23. default: (config.https) ? 443 : 80
  24. })
  25. .option('auth', {
  26. default: true
  27. })
  28. .count('verbose')
  29. .alias('v', 'verbose')
  30. .count('sql')
  31. .alias('s', 'sql')
  32. .describe('p', 'port to bind')
  33. .describe('auth', 'disables authentication')
  34. .describe('v', 'show server informations')
  35. .describe('s', 'show sql informations')
  36. .argv;
  37. // Creation variable globale
  38. if (!config.auth) {
  39. global.auth = false;
  40. } else {
  41. global.auth = argv.auth;
  42. }
  43. global.storage = config.storage;
  44. global.verbose = argv.verbose >= 1;
  45. global.sqlVerbose = argv.sql >= 1;
  46. // Validation stockage fichier
  47. const validator = require('./src/validate').getValidator(config);
  48. // Lancement du serveur
  49. const server = require('./src/server');
  50. server.https = config.https;
  51. server.route(require('./src/router'));
  52. server.start(argv.port).then((port) => {
  53. console.info(`Server starting on port ${port} (${protocol}://localhost:${port})`);
  54. }).catch((err) => {
  55. // Si erreur port deja utilisé et option recherche de port ouvert activée
  56. if (err.toString().includes('Error: No open ports') && config.findPort) {
  57. console.info(`Port ${argv.port} not available, search for a new available port`);
  58. server.start(config.basePort, config.highestPort).then((port) => {
  59. console.info(`New available port found: ${port}`);
  60. console.info(`Server starting on port ${port} (${protocol}://localhost:${port})`);
  61. }).catch((err) => {
  62. console.error(err.toString());
  63. console.info('Unable to start the server, end of execution');
  64. process.exit();
  65. });
  66. }
  67. // Sinon erreur
  68. else {
  69. console.error(err.toString());
  70. console.info('Unable to start the server, end of execution');
  71. process.exit();
  72. }
  73. });