Nenhuma Descrição

app.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. var path = require('path');
  2. var koa = require('koa');
  3. var Router = require('koa-router');
  4. var bodyParser = require('koa-bodyparser');
  5. var views = require('koa-views');
  6. var serve = require('koa-static');
  7. var session = require('koa-session');
  8. var passport = require('koa-passport');
  9. var co = require('co');
  10. // React
  11. var ReactApp = require('./build/server.js');
  12. // Loading settings
  13. var settings = require('./lib/config.js');
  14. if (!settings) {
  15. console.error('Failed to load settings');
  16. process.exit(1);
  17. }
  18. // Libraries
  19. var Utils = require('./lib/utils');
  20. //var Mailer = require('./lib/mailer');
  21. //var Database = require('./lib/database');
  22. var Passport = require('./lib/passport');
  23. var app = koa();
  24. // Static file path
  25. app.use(serve(path.join(__dirname, 'public')));
  26. // Enabling BODY
  27. app.use(bodyParser());
  28. // Initializing authenication
  29. Passport.init(passport);
  30. Passport.local(passport);
  31. // Setup 3rd-party authorization
  32. if (settings.general.authorization.github.enabled)
  33. Passport.github(passport);
  34. if (settings.general.authorization.facebook.enabled)
  35. Passport.facebook(passport);
  36. if (settings.general.authorization.google.enabled)
  37. Passport.google(passport);
  38. if (settings.general.authorization.linkedin.enabled)
  39. Passport.linkedin(passport);
  40. app.use(passport.initialize());
  41. app.use(passport.session());
  42. // Create render
  43. app.use(views(__dirname + '/views', {
  44. ext: 'jade',
  45. map: {
  46. html: 'jade'
  47. }
  48. }));
  49. // Initializing session mechanism
  50. app.keys = settings.general.session.keys || [];
  51. app.use(session(app));
  52. // Initializing locals to make template be able to get
  53. app.use(function *(next) {
  54. this.state.user = this.req.user || undefined;
  55. yield next;
  56. });
  57. // Routes
  58. app.use(require('./routes/codebender').middleware());
  59. /*
  60. app.use(require('./routes/auth').middleware());
  61. app.use(require('./routes/user').middleware());
  62. app.use(require('./routes/admin/dashboard').middleware());
  63. app.use(require('./routes/admin/users').middleware());
  64. */
  65. co(function *() {
  66. // Initializing APIs
  67. // yield Mailer.init();
  68. // yield Database.init();
  69. // Initializing routes for front-end rendering
  70. var router = new Router();
  71. for (var index in ReactApp.routes) {
  72. var route = ReactApp.routes[index];
  73. // NotFound Page
  74. if (!route.path) {
  75. app.use(function *pageNotFound(next) {
  76. // Be the last handler
  77. yield next;
  78. if (this.status != 404)
  79. return;
  80. if (this.json || this.body || !this.idempotent)
  81. return;
  82. // Rendering
  83. var page = yield ReactApp.render(this.request.path);
  84. yield this.render('index', {
  85. title: settings.general.service.name,
  86. content: page.content,
  87. state: page.state
  88. });
  89. // Do not trigger koa's 404 handling
  90. this.message = null;
  91. });
  92. continue;
  93. }
  94. // Redirect
  95. if (route.redirect) {
  96. (function(route) {
  97. router.get(route.path, function *() {
  98. this.redirect(route.redirect);
  99. });
  100. })(route);
  101. continue;
  102. }
  103. // Register path for pages
  104. router.get(route.path, function *() {
  105. // It must create a new instance for rending react page asynchronously
  106. delete require.cache[require.resolve('./build/server.js')];
  107. var ReactApp = require('./build/server.js');
  108. ReactApp.init({
  109. externalUrl: Utils.getExternalUrl(),
  110. cookie: this.req.headers.cookie
  111. });
  112. // Reset initial state with session for new page
  113. var curState = {
  114. User: this.state.user || {}
  115. };
  116. curState.User.logined = this.isAuthenticated();
  117. // Rendering page and pass state to client-side
  118. var page = yield ReactApp.render(this.request.path, curState);
  119. yield this.render('index', {
  120. title: settings.general.service.name,
  121. content: page.content,
  122. state: page.state
  123. });
  124. });
  125. }
  126. app.use(router.middleware());
  127. app.listen(settings.general.server.port, function() {
  128. console.log('server is ready');
  129. });
  130. });