index.js 13 KB

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