index.mjs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // ===========================================
  2. // Wiki.js Server
  3. // Licensed under AGPLv3
  4. // ===========================================
  5. import path from 'node:path'
  6. import { DateTime } from 'luxon'
  7. import semver from 'semver'
  8. import { customAlphabet } from 'nanoid'
  9. import fse from 'fs-extra'
  10. import configSvc from './core/config.mjs'
  11. import kernel from './core/kernel.mjs'
  12. import logger from './core/logger.mjs'
  13. const nanoid = customAlphabet('1234567890abcdef', 10)
  14. if (!semver.satisfies(process.version, '>=20')) {
  15. console.error('ERROR: Node.js 20.x or later required!')
  16. process.exit(1)
  17. }
  18. if (fse.pathExistsSync('./package.json')) {
  19. console.error('ERROR: Must run server from the parent directory!')
  20. process.exit(1)
  21. }
  22. const WIKI = {
  23. IS_DEBUG: process.env.NODE_ENV === 'development',
  24. ROOTPATH: process.cwd(),
  25. INSTANCE_ID: nanoid(10),
  26. SERVERPATH: path.join(process.cwd(), 'server'),
  27. configSvc,
  28. kernel,
  29. sites: {},
  30. sitesMappings: {},
  31. startedAt: DateTime.utc(),
  32. storage: {
  33. defs: [],
  34. modules: []
  35. }
  36. }
  37. global.WIKI = WIKI
  38. if (WIKI.IS_DEBUG) {
  39. process.on('warning', (warning) => {
  40. console.log(warning.stack)
  41. })
  42. }
  43. await WIKI.configSvc.init()
  44. // ----------------------------------------
  45. // Init Logger
  46. // ----------------------------------------
  47. WIKI.logger = logger.init()
  48. // ----------------------------------------
  49. // Start Kernel
  50. // ----------------------------------------
  51. WIKI.kernel.init()
  52. // ----------------------------------------
  53. // Register exit handler
  54. // ----------------------------------------
  55. process.on('SIGINT', () => {
  56. WIKI.kernel.shutdown()
  57. })
  58. process.on('message', (msg) => {
  59. if (msg === 'shutdown') {
  60. WIKI.kernel.shutdown()
  61. }
  62. })