main.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Gestion des commandes et des options de l'application
  8. const argv = require('yargs')
  9. .command('serve [port]', 'start the Loquicompta server', (yargs) => {
  10. yargs.positional('port', {describe: 'port to bind', default: 80});
  11. }, (argv) => {
  12. })
  13. .command('dev', 'start the Loquicompta server on 8080 port with verbose', (yargs) => {
  14. }, (argv) => {
  15. argv.port = 8080;
  16. argv.verbose = 1;
  17. })
  18. .option('port', {
  19. alias: 'p',
  20. default: 80
  21. })
  22. .option('auth', {
  23. default: true
  24. })
  25. .count('verbose')
  26. .alias('v', 'verbose')
  27. .count('sql')
  28. .alias('s', 'sql')
  29. .describe('p', 'port to bind')
  30. .describe('auth', 'disables authentication')
  31. .describe('v', 'show server informations')
  32. .describe('s', 'show sql informations')
  33. .argv;
  34. // Chargement fichier config
  35. const config = require('./src/config');
  36. const protocol = (config.https) ? 'https' : 'http';
  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. // Lancement du serveur
  47. const server = require('./src/server');
  48. server.https = config.https;
  49. server.route(require('./src/router'));
  50. server.start(argv.port).then((port) => {
  51. console.info(`Server starting on port ${port} (${protocol}://localhost:${port})`);
  52. }).catch((err) => {
  53. // Si erreur port deja utilisé et option recherche de port ouvert activée
  54. if (err.toString().includes('Error: No open ports') && config.findPort) {
  55. console.info(`Port ${argv.port} not available, search for a new available port`);
  56. server.start(config.basePort, config.highestPort).then((port) => {
  57. console.info(`New available port found: ${port}`);
  58. console.info(`Server starting on port ${port} (${protocol}://localhost:${port})`);
  59. }).catch((err) => {
  60. console.error(err.toString());
  61. console.info('Unable to start the server, end of execution');
  62. process.exit();
  63. });
  64. }
  65. // Sinon erreur
  66. else {
  67. console.error(err.toString());
  68. console.info('Unable to start the server, end of execution');
  69. process.exit();
  70. }
  71. });