start.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const Backend = require('./api/structures/Server');
  2. const express = require('express');
  3. const compression = require('compression');
  4. // const ream = require('ream');
  5. const config = require('../config');
  6. const path = require('path');
  7. const log = require('./api/utils/Log');
  8. const dev = process.env.NODE_ENV !== 'production';
  9. const oneliner = require('one-liner');
  10. const jetpack = require('fs-jetpack');
  11. // const { Nuxt, Builder } = require('nuxt-edge');
  12. // const nuxtConfig = require('./nuxt/nuxt.config.js');
  13. function startProduction() {
  14. startAPI();
  15. // startSite();
  16. // startNuxt();
  17. }
  18. function startAPI() {
  19. writeFrontendConfig();
  20. new Backend().start();
  21. }
  22. async function startNuxt() {
  23. /*
  24. Make sure the frontend has enough data to prepare the service
  25. */
  26. writeFrontendConfig();
  27. /*
  28. Starting Nuxt's custom server powered by express
  29. */
  30. const app = express();
  31. /*
  32. Instantiate Nuxt.js
  33. */
  34. nuxtConfig.dev = true;
  35. const nuxt = new Nuxt(nuxtConfig);
  36. /*
  37. Start the server or build it if we're on dev mode
  38. */
  39. if (nuxtConfig.dev) {
  40. try {
  41. await new Builder(nuxt).build();
  42. } catch (error) {
  43. log.error(error);
  44. process.exit(1);
  45. }
  46. }
  47. /*
  48. Render every route with Nuxt.js
  49. */
  50. app.use(nuxt.render);
  51. /*
  52. Start the server and listen to the configured port
  53. */
  54. app.listen(config.server.ports.frontend, '127.0.0.1');
  55. log.info(`> Frontend ready and listening on port ${config.server.ports.frontend}`);
  56. /*
  57. Starting Nuxt's custom server powered by express
  58. */
  59. /*
  60. const app = express();
  61. app.set('port', config.server.ports.frontend);
  62. // Configure dev enviroment
  63. nuxtConfig.dev = dev;
  64. // Init Nuxt.js
  65. const nuxt = new Nuxt(nuxtConfig);
  66. // Build only in dev mode
  67. if (nuxtConfig.dev) {
  68. const builder = new Builder(nuxt);
  69. await builder.build();
  70. }
  71. // Give nuxt middleware to express
  72. app.use(nuxt.render);
  73. if (config.serveFilesWithNode) {
  74. app.use('/', express.static(`./${config.uploads.uploadFolder}`));
  75. }
  76. // Listen the server
  77. app.listen(config.server.ports.frontend, '127.0.0.1');
  78. app.on('renderer-ready', () => log.info(`> Frontend ready and listening on port ${config.server.ports.frontend}`));
  79. // log.success(`> Frontend ready and listening on port ${config.server.ports.frontend}`);
  80. // console.log(`Server listening on http://${host}:${port}`); // eslint-disable-line no-console
  81. */
  82. }
  83. function startSite() {
  84. /*
  85. Make sure the frontend has enough data to prepare the service
  86. */
  87. writeFrontendConfig();
  88. /*
  89. Starting ream's custom server
  90. */
  91. const server = express();
  92. const app = ream({
  93. dev,
  94. entry: path.join(__dirname, 'site', 'index.js')
  95. });
  96. app.getRequestHandler().then(handler => {
  97. server.use(compression());
  98. /*
  99. This option is mostly for development, since serving the files with nginx is better.
  100. */
  101. if (config.serveFilesWithNode) {
  102. server.use('/', express.static(`./${config.uploads.uploadFolder}`));
  103. }
  104. server.get('*', handler);
  105. server.listen(config.server.ports.frontend, error => {
  106. if (error) log.error(error);
  107. });
  108. });
  109. app.on('renderer-ready', () => log.info(`> Frontend ready and listening on port ${config.server.ports.frontend}`));
  110. }
  111. function writeFrontendConfig() {
  112. /*
  113. Since ream can't execute getInitialData on non-routes we write a config file for it.
  114. */
  115. const template = oneliner`
  116. module.exports = {
  117. version: '${process.env.npm_package_version}',
  118. URL: '${config.filesServeLocation}',
  119. baseURL: '${config.backendLocation}',
  120. serviceName: '${config.serviceName}',
  121. maxFileSize: '${config.uploads.uploadMaxSize}',
  122. chunkSize: '${config.uploads.chunkSize}',
  123. maxLinksPerAlbum: '${config.albums.maxLinksPerAlbum}'
  124. }`;
  125. jetpack.write(path.join(__dirname, 'site', 'config.js'), template);
  126. log.success('Frontend config file generated successfully');
  127. }
  128. /*
  129. Having multiple files for different scripts was mendokusai.
  130. */
  131. const args = process.argv[2];
  132. if (!args) startProduction();
  133. else if (args === 'api') startAPI();
  134. else if (args === 'site') startNuxt();
  135. else process.exit(0);