server.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // 数据库启动器
  2. const path = require("path")
  3. const fs = require("fs")
  4. // 全局配置加载
  5. // IMPORTANT:通过apps/<appId>/加载应用独立配置
  6. var appConfig
  7. try{
  8. appConfig = require(`./config.js`) // 默认加载包内config.js文件
  9. }catch{}
  10. if(!appConfig) appConfig = {}
  11. var userConfig
  12. /*********************
  13. * windows: process.env.PORTABLE_EXECUTABLE_DIR
  14. * linux: process.env.OWD
  15. */
  16. var OWN_DIR = process.env.PORTABLE_EXECUTABLE_DIR || process.env.OWD || process.cwd() || __dirname
  17. if (OWN_DIR) {
  18. let configPath = path.join(OWN_DIR, `config.js`)
  19. if (fs.existsSync(configPath)) {
  20. userConfig = require(configPath) // 用户独立配置config.js覆盖
  21. if (userConfig) {
  22. Object.keys(userConfig).forEach(key => {
  23. appConfig[key] = userConfig[key]
  24. })
  25. }
  26. }
  27. }
  28. global.config = global.config || {}
  29. global.config['DATABASE_LOCAL'] = process.env["DATABASE_LOCAL"] || appConfig["DATABASE_LOCAL"] || false
  30. global.config['MCENTER_ENABLED'] = process.env["MCENTER_ENABLED"] || appConfig["MCENTER_ENABLED"] || false
  31. global.config['PARSE_SERVERURL'] = process.env["PARSE_SERVERURL"] || appConfig["PARSE_SERVERURL"] || false
  32. global.config['PARSE_APPID'] = process.env["PARSE_APPID"] || appConfig["PARSE_APPID"] || false
  33. global.config['PARSE_MASTERKEY'] = process.env["PARSE_MASTERKEY"] || appConfig["PARSE_MASTERKEY"] || false
  34. global.config['SEGMENT_COUNT'] = process.env["SEGMENT_COUNT"] || appConfig["SEGMENT_COUNT"] || false
  35. // 全局共享属性
  36. global.coolMap = {}
  37. global.minerMap = {}
  38. global.monitorMap = {}
  39. // Web服务启动器
  40. const express = require('express');
  41. const parse = require('parse-server')
  42. const ParseServer = parse.ParseServer;
  43. const ParseDashboard = require('parse-dashboard');
  44. // 同端口多进程集群
  45. let processCluster = require('cluster')
  46. let cpus = require('os').cpus()
  47. console.log(cpus.length)
  48. const app = express();
  49. const cors = require('cors');
  50. const { importAllSchemas } = require("./db/func/import-schemas")
  51. const { PostgreSQLKeep } = require("./lib/pg/index.js")
  52. app.use(cors({
  53. origin: '*'
  54. }));
  55. global.parseConfig = {
  56. port : 61337,
  57. allowClientClassCreation: true,
  58. allowExpiredAuthDataToken: false,
  59. encodeParseObjectInCloudFunction: false,
  60. }
  61. global.parseConfig.serverURL = `http://localhost:${global.parseConfig?.port}/parse`
  62. global.parseConfig.databaseURI = "postgresql://postgres@127.0.0.1:25432/"
  63. global.parseConfig.appId = 'edu-parse'
  64. global.parseConfig.appName = 'EduParse'
  65. global.parseConfig.masterKey = 'EDU2024'
  66. async function startParse(){
  67. if(global.config['DATABASE_LOCAL']) {
  68. await PostgreSQLKeep({
  69. username:"postgres",
  70. password:"postgres",
  71. port:"25432",
  72. dbpath:"database/edudata"
  73. })
  74. }
  75. // 挂载微服务
  76. const api = new ParseServer(global.parseConfig);
  77. await api.start();
  78. // Serve the Parse API at /parse URL prefix
  79. app.use('/parse', api.app);
  80. // 挂载管理看板
  81. const dashboard = new ParseDashboard({
  82. "apps": [
  83. global.parseConfig
  84. ]
  85. }, { allowInsecureHTTP: true });
  86. app.use('/dashboard', dashboard);
  87. }
  88. process.on('exit', async () => {
  89. // await stopDB()
  90. });
  91. process.on('SIGINT', async () => {
  92. /* DO SOME STUFF HERE */
  93. // await stopDB()
  94. process.exit()
  95. })
  96. // 在程序中捕获异常并进行处理
  97. process.on('uncaughtException', async (err) => {
  98. console.error('Uncaught Exception:', err);
  99. process.exit(1);
  100. });
  101. // 启动服务
  102. async function initParseAndDatabase(){
  103. try{
  104. console.log("正在启动微服务...")
  105. await startParse();
  106. // console.log("parse:",parseConfig)
  107. console.log("正在启动api接口")
  108. // 加载textbook专属路由 通过代理操控局域网设备
  109. let textbookRouter = require("./api/textbook/routes")
  110. app.use("/api/textbook",textbookRouter)
  111. app.get("/",(req,res)=>{
  112. res.json({
  113. code:200,
  114. data:{message:"ok"}
  115. })
  116. })
  117. /**
  118. * Listen on provided port, on all network interfaces.
  119. */
  120. app.listen(global.parseConfig?.port, function() {
  121. console.log('微服务已运行,端口 ' + global.parseConfig?.port + '.');
  122. // 迁移数据范式 Schemas
  123. importAllSchemas();
  124. });
  125. console.log("正在启动管理看板...")
  126. console.log("浏览器管理看板:","http://localhost:61337/dashboard")
  127. }catch(err){
  128. console.error(err)
  129. }
  130. }
  131. // 启动服务端及数据库
  132. initParseAndDatabase()