main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // 1er Lancement
  8. const fs = require('fs');
  9. const first = !fs.existsSync(require('./src/db').getPath());
  10. // Chargement fichier config
  11. const config = require('./src/config');
  12. const protocol = (config.https) ? 'https' : 'http';
  13. // Gestion des commandes et des options de l'application
  14. const argv = require('yargs')
  15. .command('serve [port]', 'start the Loquicompta server', (yargs) => {
  16. yargs.positional('port', {describe: 'port to bind', default: 80});
  17. }, (argv) => {
  18. })
  19. .command('dev', 'start the Loquicompta server on 8080 port with verbose', (yargs) => {
  20. }, (argv) => {
  21. argv.port = 8080;
  22. argv.verbose = 1;
  23. })
  24. .option('port', {
  25. alias: 'p',
  26. default: (config.https) ? 443 : 80
  27. })
  28. .option('auth', {
  29. default: true
  30. })
  31. .count('verbose')
  32. .alias('v', 'verbose')
  33. .count('sql')
  34. .alias('s', 'sql')
  35. .describe('p', 'port to bind')
  36. .describe('auth', 'disables authentication')
  37. .describe('v', 'show server informations')
  38. .describe('s', 'show sql informations')
  39. .argv;
  40. // Creation variable globale
  41. if (!config.auth) {
  42. global.auth = false;
  43. } else {
  44. global.auth = argv.auth;
  45. }
  46. global.storage = config.storage;
  47. global.verbose = argv.verbose >= 1;
  48. global.sqlVerbose = argv.sql >= 1;
  49. // Validation des fichiers si active
  50. if (config.validate && !first) {
  51. validate(startServer);
  52. }
  53. // Sinon demarrage du serveur
  54. else {
  55. startServer();
  56. }
  57. /**
  58. * Validate the storage method
  59. */
  60. function validate(callback) {
  61. const validator = require('./src/validate').getValidator(config);
  62. validator.check().then((result) => {
  63. // Pas de probleme
  64. if (result) {
  65. callback();
  66. }
  67. // Tentative de resolution des problemes
  68. else {
  69. validator.rectify().then((success) => {
  70. if (success) {
  71. console.info();
  72. callback();
  73. } else {
  74. console.info('Unable to start the server in this state, end of execution');
  75. process.exit();
  76. }
  77. });
  78. }
  79. });
  80. }
  81. /**
  82. * Start the server
  83. */
  84. function startServer() {
  85. const server = require('./src/server');
  86. server.https = config.https;
  87. server.route(require('./src/router'));
  88. server.start(argv.port).then((port) => {
  89. console.info(`Server starting on port ${port} (${protocol}://localhost:${port})`);
  90. }).catch((err) => {
  91. // Si erreur port deja utilisé et option recherche de port ouvert activée
  92. if (err.toString().includes('Error: No open ports') && config.findPort) {
  93. console.info(`Port ${argv.port} not available, search for a new available port`);
  94. server.start(config.basePort, config.highestPort).then((port) => {
  95. console.info(`New available port found: ${port}`);
  96. console.info(`Server starting on port ${port} (${protocol}://localhost:${port})`);
  97. }).catch((err) => {
  98. console.error(err.toString());
  99. console.info('Unable to start the server, end of execution');
  100. process.exit();
  101. });
  102. }
  103. // Sinon erreur
  104. else {
  105. console.error(err.toString());
  106. console.info('Unable to start the server, end of execution');
  107. process.exit();
  108. }
  109. });
  110. }