index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. const debug = require('debug')('growi:crowi');
  2. const logger = require('@alias/logger')('growi:crowi');
  3. const pkg = require('@root/package.json');
  4. const InterceptorManager = require('@commons/service/interceptor-manager');
  5. const CdnResourcesService = require('@commons/service/cdn-resources-service');
  6. const Xss = require('@commons/service/xss');
  7. const { getMongoUri, mongoOptions } = require('@commons/util/mongoose-utils');
  8. const path = require('path');
  9. const sep = path.sep;
  10. const mongoose = require('mongoose');
  11. const models = require('../models');
  12. const PluginService = require('../plugins/plugin.service');
  13. function Crowi(rootdir) {
  14. const self = this;
  15. this.version = pkg.version;
  16. this.runtimeVersions = undefined; // initialized by scanRuntimeVersions()
  17. this.rootDir = rootdir;
  18. this.pluginDir = path.join(this.rootDir, 'node_modules') + sep;
  19. this.publicDir = path.join(this.rootDir, 'public') + sep;
  20. this.libDir = path.join(this.rootDir, 'src/server') + sep;
  21. this.eventsDir = path.join(this.libDir, 'events') + sep;
  22. this.viewsDir = path.join(this.libDir, 'views') + sep;
  23. this.resourceDir = path.join(this.rootDir, 'resource') + sep;
  24. this.localeDir = path.join(this.resourceDir, 'locales') + sep;
  25. this.tmpDir = path.join(this.rootDir, 'tmp') + sep;
  26. this.cacheDir = path.join(this.tmpDir, 'cache');
  27. this.config = {};
  28. this.configManager = null;
  29. this.mailService = null;
  30. this.passportService = null;
  31. this.globalNotificationService = null;
  32. this.slackNotificationService = null;
  33. this.xssService = null;
  34. this.aclService = null;
  35. this.appService = null;
  36. this.fileUploadService = null;
  37. this.restQiitaAPIService = null;
  38. this.growiBridgeService = null;
  39. this.exportService = null;
  40. this.importService = null;
  41. this.searchService = null;
  42. this.cdnResourcesService = new CdnResourcesService();
  43. this.interceptorManager = new InterceptorManager();
  44. this.xss = new Xss();
  45. this.tokens = null;
  46. this.models = {};
  47. this.env = process.env;
  48. this.node_env = this.env.NODE_ENV || 'development';
  49. this.port = this.env.PORT || 3000;
  50. this.events = {
  51. user: new (require(`${self.eventsDir}user`))(this),
  52. page: new (require(`${self.eventsDir}page`))(this),
  53. search: new (require(`${self.eventsDir}search`))(this),
  54. bookmark: new (require(`${self.eventsDir}bookmark`))(this),
  55. tag: new (require(`${self.eventsDir}tag`))(this),
  56. admin: new (require(`${self.eventsDir}admin`))(this),
  57. };
  58. }
  59. Crowi.prototype.init = async function() {
  60. await this.setupDatabase();
  61. await this.setupModels();
  62. await this.setupSessionConfig();
  63. await this.setupConfigManager();
  64. // customizeService depends on AppService and XssService
  65. // passportService depends on appService
  66. // slack depends on setUpSlacklNotification
  67. // export and import depends on setUpGrowiBridge
  68. await Promise.all([
  69. this.setUpApp(),
  70. this.setUpXss(),
  71. this.setUpSlacklNotification(),
  72. this.setUpGrowiBridge(),
  73. ]);
  74. await Promise.all([
  75. this.scanRuntimeVersions(),
  76. this.setupPassport(),
  77. this.setupSearcher(),
  78. this.setupMailer(),
  79. this.setupSlack(),
  80. this.setupCsrf(),
  81. this.setUpFileUpload(),
  82. this.setUpAcl(),
  83. this.setUpCustomize(),
  84. this.setUpRestQiitaAPI(),
  85. this.setupUserGroup(),
  86. this.setupExport(),
  87. this.setupImport(),
  88. ]);
  89. // globalNotification depends on slack and mailer
  90. await Promise.all([
  91. this.setUpGlobalNotification(),
  92. ]);
  93. };
  94. Crowi.prototype.initForTest = async function() {
  95. await this.setupModels();
  96. await this.setupConfigManager();
  97. // // customizeService depends on AppService and XssService
  98. // // passportService depends on appService
  99. // // slack depends on setUpSlacklNotification
  100. await Promise.all([
  101. this.setUpApp(),
  102. // this.setUpXss(),
  103. // this.setUpSlacklNotification(),
  104. // this.setUpGrowiBridge(),
  105. ]);
  106. await Promise.all([
  107. // this.scanRuntimeVersions(),
  108. this.setupPassport(),
  109. // this.setupSearcher(),
  110. // this.setupMailer(),
  111. // this.setupSlack(),
  112. // this.setupCsrf(),
  113. // this.setUpFileUpload(),
  114. this.setUpAcl(),
  115. // this.setUpCustomize(),
  116. // this.setUpRestQiitaAPI(),
  117. // this.setupUserGroup(),
  118. // this.setupExport(),
  119. // this.setupImport(),
  120. ]);
  121. // globalNotification depends on slack and mailer
  122. // await Promise.all([
  123. // this.setUpGlobalNotification(),
  124. // ]);
  125. };
  126. Crowi.prototype.isPageId = function(pageId) {
  127. if (!pageId) {
  128. return false;
  129. }
  130. if (typeof pageId === 'string' && pageId.match(/^[\da-f]{24}$/)) {
  131. return true;
  132. }
  133. return false;
  134. };
  135. Crowi.prototype.setConfig = function(config) {
  136. this.config = config;
  137. };
  138. Crowi.prototype.getConfig = function() {
  139. return this.config;
  140. };
  141. Crowi.prototype.getEnv = function() {
  142. return this.env;
  143. };
  144. // getter/setter of model instance
  145. //
  146. Crowi.prototype.model = function(name, model) {
  147. if (model != null) {
  148. this.models[name] = model;
  149. }
  150. return this.models[name];
  151. };
  152. // getter/setter of event instance
  153. Crowi.prototype.event = function(name, event) {
  154. if (event) {
  155. this.events[name] = event;
  156. }
  157. return this.events[name];
  158. };
  159. Crowi.prototype.setupDatabase = function() {
  160. mongoose.Promise = global.Promise;
  161. // mongoUri = mongodb://user:password@host/dbname
  162. const mongoUri = getMongoUri();
  163. return mongoose.connect(mongoUri, mongoOptions);
  164. };
  165. Crowi.prototype.setupSessionConfig = async function() {
  166. const session = require('express-session');
  167. const sessionAge = (1000 * 3600 * 24 * 30);
  168. const redisUrl = this.env.REDISTOGO_URL || this.env.REDIS_URI || this.env.REDIS_URL || null;
  169. const uid = require('uid-safe').sync;
  170. // generate pre-defined uid for healthcheck
  171. const healthcheckUid = uid(24);
  172. const sessionConfig = {
  173. rolling: true,
  174. secret: this.env.SECRET_TOKEN || 'this is default session secret',
  175. resave: false,
  176. saveUninitialized: true,
  177. cookie: {
  178. maxAge: sessionAge,
  179. },
  180. genid(req) {
  181. // return pre-defined uid when healthcheck
  182. if (req.path === '/_api/v3/healthcheck') {
  183. return healthcheckUid;
  184. }
  185. return uid(24);
  186. },
  187. };
  188. if (this.env.SESSION_NAME) {
  189. sessionConfig.name = this.env.SESSION_NAME;
  190. }
  191. // use Redis for session store
  192. if (redisUrl) {
  193. const redis = require('redis');
  194. const redisClient = redis.createClient({ url: redisUrl });
  195. const RedisStore = require('connect-redis')(session);
  196. sessionConfig.store = new RedisStore({ client: redisClient });
  197. }
  198. // use MongoDB for session store
  199. else {
  200. const MongoStore = require('connect-mongo')(session);
  201. sessionConfig.store = new MongoStore({ mongooseConnection: mongoose.connection });
  202. }
  203. this.sessionConfig = sessionConfig;
  204. };
  205. Crowi.prototype.setupConfigManager = async function() {
  206. const ConfigManager = require('../service/config-manager');
  207. this.configManager = new ConfigManager(this.model('Config'));
  208. await this.configManager.loadConfigs();
  209. // setup pubsub
  210. this.configPubsub = require('../service/config-pubsub')(this);
  211. if (this.configPubsub != null) {
  212. this.configPubsub.subscribe();
  213. this.configManager.setPubsub(this.configPubsub);
  214. // add as a message handler
  215. this.configPubsub.addMessageHandler(this.configManager);
  216. }
  217. };
  218. Crowi.prototype.setupModels = async function() {
  219. Object.keys(models).forEach((key) => {
  220. return this.model(key, models[key](this));
  221. });
  222. };
  223. Crowi.prototype.getIo = function() {
  224. return this.io;
  225. };
  226. Crowi.prototype.scanRuntimeVersions = async function() {
  227. const self = this;
  228. const check = require('check-node-version');
  229. return new Promise((resolve, reject) => {
  230. check((err, result) => {
  231. if (err) {
  232. reject(err);
  233. }
  234. self.runtimeVersions = result;
  235. resolve();
  236. });
  237. });
  238. };
  239. Crowi.prototype.getSlack = function() {
  240. return this.slack;
  241. };
  242. Crowi.prototype.getInterceptorManager = function() {
  243. return this.interceptorManager;
  244. };
  245. Crowi.prototype.getGlobalNotificationService = function() {
  246. return this.globalNotificationService;
  247. };
  248. Crowi.prototype.getRestQiitaAPIService = function() {
  249. return this.restQiitaAPIService;
  250. };
  251. Crowi.prototype.setupPassport = async function() {
  252. debug('Passport is enabled');
  253. // initialize service
  254. const PassportService = require('../service/passport');
  255. if (this.passportService == null) {
  256. this.passportService = new PassportService(this);
  257. }
  258. this.passportService.setupSerializer();
  259. // setup strategies
  260. try {
  261. this.passportService.setupStrategyById('local');
  262. this.passportService.setupStrategyById('ldap');
  263. this.passportService.setupStrategyById('saml');
  264. this.passportService.setupStrategyById('oidc');
  265. this.passportService.setupStrategyById('basic');
  266. this.passportService.setupStrategyById('google');
  267. this.passportService.setupStrategyById('github');
  268. this.passportService.setupStrategyById('twitter');
  269. }
  270. catch (err) {
  271. logger.error(err);
  272. }
  273. // add as a message handler
  274. this.configPubsub.addMessageHandler(this.passportService);
  275. return Promise.resolve();
  276. };
  277. Crowi.prototype.setupSearcher = async function() {
  278. const SearchService = require('@server/service/search');
  279. this.searchService = new SearchService(this);
  280. };
  281. Crowi.prototype.setupMailer = async function() {
  282. const MailService = require('@server/service/mail');
  283. this.mailService = new MailService(this);
  284. // add as a message handler
  285. this.configPubsub.addMessageHandler(this.mailService);
  286. };
  287. Crowi.prototype.setupSlack = async function() {
  288. const self = this;
  289. return new Promise(((resolve, reject) => {
  290. self.slack = require('../util/slack')(self);
  291. resolve();
  292. }));
  293. };
  294. Crowi.prototype.setupCsrf = async function() {
  295. const Tokens = require('csrf');
  296. this.tokens = new Tokens();
  297. return Promise.resolve();
  298. };
  299. Crowi.prototype.getTokens = function() {
  300. return this.tokens;
  301. };
  302. Crowi.prototype.start = async function() {
  303. // init CrowiDev
  304. if (this.node_env === 'development') {
  305. const CrowiDev = require('./dev');
  306. this.crowiDev = new CrowiDev(this);
  307. this.crowiDev.init();
  308. }
  309. await this.init();
  310. const express = await this.buildServer();
  311. // setup plugins
  312. this.pluginService = new PluginService(this, express);
  313. this.pluginService.autoDetectAndLoadPlugins();
  314. const server = (this.node_env === 'development') ? this.crowiDev.setupServer(express) : express;
  315. // listen
  316. const serverListening = server.listen(this.port, () => {
  317. logger.info(`[${this.node_env}] Express server is listening on port ${this.port}`);
  318. if (this.node_env === 'development') {
  319. this.crowiDev.setupExpressAfterListening(express);
  320. }
  321. });
  322. // setup WebSocket
  323. const io = require('socket.io')(serverListening);
  324. io.sockets.on('connection', (socket) => {
  325. });
  326. this.io = io;
  327. // setup Express Routes
  328. this.setupRoutesAtLast(express);
  329. return serverListening;
  330. };
  331. Crowi.prototype.buildServer = function() {
  332. const express = require('express')();
  333. const env = this.node_env;
  334. require('./express-init')(this, express);
  335. // use bunyan
  336. if (env === 'production') {
  337. const expressBunyanLogger = require('express-bunyan-logger');
  338. const logger = require('@alias/logger')('express');
  339. express.use(expressBunyanLogger({
  340. logger,
  341. excludes: ['*'],
  342. }));
  343. }
  344. // use morgan
  345. else {
  346. const morgan = require('morgan');
  347. express.use(morgan('dev'));
  348. }
  349. return Promise.resolve(express);
  350. };
  351. /**
  352. * setup Express Routes
  353. * !! this must be at last because it includes '/*' route !!
  354. */
  355. Crowi.prototype.setupRoutesAtLast = function(app) {
  356. require('../routes')(this, app);
  357. };
  358. /**
  359. * require API for plugins
  360. *
  361. * @param {string} modulePath relative path from /lib/crowi/index.js
  362. * @return {module}
  363. *
  364. * @memberof Crowi
  365. */
  366. Crowi.prototype.require = function(modulePath) {
  367. return require(modulePath);
  368. };
  369. /**
  370. * setup GlobalNotificationService
  371. */
  372. Crowi.prototype.setUpGlobalNotification = async function() {
  373. const GlobalNotificationService = require('../service/global-notification');
  374. if (this.globalNotificationService == null) {
  375. this.globalNotificationService = new GlobalNotificationService(this);
  376. }
  377. };
  378. /**
  379. * setup SlackNotificationService
  380. */
  381. Crowi.prototype.setUpSlacklNotification = async function() {
  382. const SlackNotificationService = require('../service/slack-notification');
  383. if (this.slackNotificationService == null) {
  384. this.slackNotificationService = new SlackNotificationService(this.configManager);
  385. }
  386. };
  387. /**
  388. * setup XssService
  389. */
  390. Crowi.prototype.setUpXss = async function() {
  391. const XssService = require('../service/xss');
  392. if (this.xssService == null) {
  393. this.xssService = new XssService(this.configManager);
  394. }
  395. };
  396. /**
  397. * setup AclService
  398. */
  399. Crowi.prototype.setUpAcl = async function() {
  400. const AclService = require('../service/acl');
  401. if (this.aclService == null) {
  402. this.aclService = new AclService(this.configManager);
  403. }
  404. };
  405. /**
  406. * setup CustomizeService
  407. */
  408. Crowi.prototype.setUpCustomize = async function() {
  409. const CustomizeService = require('../service/customize');
  410. if (this.customizeService == null) {
  411. this.customizeService = new CustomizeService(this.configManager, this.appService, this.xssService, this.configPubsub);
  412. this.customizeService.initCustomCss();
  413. this.customizeService.initCustomTitle();
  414. // add as a message handler
  415. this.configPubsub.addMessageHandler(this.customizeService);
  416. }
  417. };
  418. /**
  419. * setup AppService
  420. */
  421. Crowi.prototype.setUpApp = async function() {
  422. const AppService = require('../service/app');
  423. if (this.appService == null) {
  424. this.appService = new AppService(this.configManager);
  425. }
  426. };
  427. /**
  428. * setup FileUploadService
  429. */
  430. Crowi.prototype.setUpFileUpload = async function() {
  431. if (this.fileUploadService == null) {
  432. this.fileUploadService = require('../service/file-uploader')(this);
  433. }
  434. };
  435. /**
  436. * setup RestQiitaAPIService
  437. */
  438. Crowi.prototype.setUpRestQiitaAPI = async function() {
  439. const RestQiitaAPIService = require('../service/rest-qiita-API');
  440. if (this.restQiitaAPIService == null) {
  441. this.restQiitaAPIService = new RestQiitaAPIService(this);
  442. }
  443. };
  444. Crowi.prototype.setupUserGroup = async function() {
  445. const UserGroupService = require('../service/user-group');
  446. if (this.userGroupService == null) {
  447. this.userGroupService = new UserGroupService(this);
  448. return this.userGroupService.init();
  449. }
  450. };
  451. Crowi.prototype.setUpGrowiBridge = async function() {
  452. const GrowiBridgeService = require('../service/growi-bridge');
  453. if (this.growiBridgeService == null) {
  454. this.growiBridgeService = new GrowiBridgeService(this);
  455. }
  456. };
  457. Crowi.prototype.setupExport = async function() {
  458. const ExportService = require('../service/export');
  459. if (this.exportService == null) {
  460. this.exportService = new ExportService(this);
  461. }
  462. };
  463. Crowi.prototype.setupImport = async function() {
  464. const ImportService = require('../service/import');
  465. if (this.importService == null) {
  466. this.importService = new ImportService(this);
  467. }
  468. };
  469. module.exports = Crowi;