You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.js 3.0KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var express = require('express');
  4. var path = require('path');
  5. var favicon = require('serve-favicon');
  6. var logger = require('morgan');
  7. var cookieParser = require('cookie-parser');
  8. var bodyParser = require('body-parser');
  9. var session = require("express-session");
  10. var env = require("./app/env.js");
  11. var simpleGit = require('simple-git');
  12. var utils = require("./app/utils.js");
  13. var moment = require("moment");
  14. var Decimal = require('decimal.js');
  15. var bitcoin = require("bitcoin");
  16. var momentDurationFormat = require("moment-duration-format");
  17. var baseActionsRouter = require('./routes/baseActionsRouter');
  18. var app = express();
  19. // view engine setup
  20. app.set('views', path.join(__dirname, 'views'));
  21. app.set('view engine', 'pug');
  22. // uncomment after placing your favicon in /public
  23. //app.use(favicon(__dirname + '/public/favicon.ico'));
  24. app.use(logger('dev'));
  25. app.use(bodyParser.json());
  26. app.use(bodyParser.urlencoded({ extended: false }));
  27. app.use(cookieParser());
  28. app.use(session({
  29. secret: env.cookiePassword,
  30. resave: false,
  31. saveUninitialized: false
  32. }));
  33. app.use(express.static(path.join(__dirname, 'public')));
  34. // Make our db accessible to our router
  35. app.use(function(req, res, next) {
  36. // make session available in templates
  37. res.locals.session = req.session;
  38. res.locals.debug = env.debug;
  39. if (env.smartcashd && env.smartcashd.rpc) {
  40. req.session.host = env.smartcashd.host;
  41. req.session.port = env.smartcashd.port;
  42. req.session.username = env.smartcashd.rpc.username;
  43. global.client = new bitcoin.Client({
  44. host: env.smartcashd.host,
  45. port: env.smartcashd.port,
  46. user: env.smartcashd.rpc.username,
  47. pass: env.smartcashd.rpc.password,
  48. timeout: 5000
  49. });
  50. }
  51. res.locals.host = req.session.host;
  52. res.locals.port = req.session.port;
  53. /* if (!["/", "/connect"].includes(req.originalUrl)) {
  54. if (utils.redirectToConnectPageIfNeeded(req, res)) {
  55. return;
  56. }
  57. }
  58. */
  59. if (req.session.userMessage) {
  60. res.locals.userMessage = req.session.userMessage;
  61. if (req.session.userMessageType) {
  62. res.locals.userMessageType = req.session.userMessageType;
  63. } else {
  64. res.locals.userMessageType = "info";
  65. }
  66. }
  67. req.session.userMessage = null;
  68. req.session.userMessageType = null;
  69. // make some var available to all request
  70. // ex: req.cheeseStr = "cheese";
  71. next();
  72. });
  73. app.use('/', baseActionsRouter);
  74. /// catch 404 and forwarding to error handler
  75. app.use(function(req, res, next) {
  76. var err = new Error('Not Found');
  77. err.status = 404;
  78. next(err);
  79. });
  80. /// error handlers
  81. // development error handler
  82. // will print stacktrace
  83. if (app.get('env') === 'development') {
  84. app.use(function(err, req, res, next) {
  85. res.status(err.status || 500);
  86. res.render('error', {
  87. message: err.message,
  88. error: err
  89. });
  90. });
  91. }
  92. // production error handler
  93. // no stacktraces leaked to user
  94. app.use(function(err, req, res, next) {
  95. res.status(err.status || 500);
  96. res.render('error', {
  97. message: err.message,
  98. error: {}
  99. });
  100. });
  101. app.locals.moment = moment;
  102. app.locals.Decimal = Decimal;
  103. app.locals.utils = utils;
  104. module.exports = app;