main.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Creation variable globale
  37. if (!config.auth) {
  38. global.auth = false;
  39. } else {
  40. global.auth = argv.auth;
  41. }
  42. global.storage = config.storage;
  43. global.verbose = argv.verbose >= 1;
  44. global.sqlVerbose = argv.sql >= 1;
  45. // Lancement du serveur
  46. const server = require('./src/server');
  47. server.route(require('./src/router'));
  48. server.start(argv.port).then((port) => {
  49. console.info(`Server starting on port ${port} (http://localhost:${port})`);
  50. }).catch((err) => {
  51. // Si erreur port deja utilisé et option recherche de port ouvert activée
  52. if (err.toString().includes('Error: No open ports') && config.findPort) {
  53. console.info(`Port ${argv.port} not available, search for a new available port`);
  54. server.start(config.basePort, config.highestPort).then((port) => {
  55. console.info(`New available port found: ${port}`);
  56. console.info(`Server starting on port ${port} (http://localhost:${port})`);
  57. }).catch((err) => {
  58. console.error(err.toString());
  59. console.info('Unable to start the server, end of execution');
  60. process.exit();
  61. });
  62. }
  63. // Sinon erreur
  64. else {
  65. console.error(err.toString());
  66. console.info('Unable to start the server, end of execution');
  67. process.exit();
  68. }
  69. });