main.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. const _ = require('underscore');
  3. const Backbone = require('backbone');
  4. const Mn = require('../lib/marionette');
  5. const Cache = require('./cache');
  6. const Controller = require('./controller');
  7. const Router = require('./router');
  8. const Api = require('./api');
  9. const Tokens = require('./tokens');
  10. const UI = require('./ui/main');
  11. const App = Mn.Application.extend({
  12. Cache: Cache,
  13. Api: Api,
  14. UI: null,
  15. Controller: Controller,
  16. version: null,
  17. region: {
  18. el: '#app',
  19. replaceElement: true
  20. },
  21. onStart: function (app, options) {
  22. console.log('Welcome to Nginx Proxy Manager');
  23. // Check if token is coming through
  24. if (this.getParam('token')) {
  25. Tokens.addToken(this.getParam('token'));
  26. }
  27. // Check if we are still logged in by refreshing the token
  28. Api.status()
  29. .then(result => {
  30. this.version = [result.version.major, result.version.minor, result.version.revision].join('.');
  31. })
  32. .then(Api.Tokens.refresh)
  33. .then(this.bootstrap)
  34. .then(() => {
  35. console.info('You are logged in');
  36. this.bootstrapTimer();
  37. this.refreshTokenTimer();
  38. this.UI = new UI();
  39. this.UI.on('render', () => {
  40. new Router(options);
  41. Backbone.history.start({pushState: true});
  42. });
  43. this.getRegion().show(this.UI);
  44. })
  45. .catch(err => {
  46. console.warn('Not logged in:', err.message);
  47. Controller.showLogin();
  48. });
  49. },
  50. History: {
  51. replace: function (data) {
  52. window.history.replaceState(_.extend(window.history.state || {}, data), document.title);
  53. },
  54. get: function (attr) {
  55. return window.history.state ? window.history.state[attr] : undefined;
  56. }
  57. },
  58. Error: function (code, message, debug) {
  59. let temp = Error.call(this, message);
  60. temp.name = this.name = 'AppError';
  61. this.stack = temp.stack;
  62. this.message = temp.message;
  63. this.code = code;
  64. this.debug = debug;
  65. },
  66. showError: function () {
  67. let ErrorView = Mn.View.extend({
  68. tagName: 'section',
  69. id: 'error',
  70. template: _.template('Error loading stuff. Please reload the app.')
  71. });
  72. this.getRegion().show(new ErrorView());
  73. },
  74. getParam: function (name) {
  75. name = name.replace(/[\[\]]/g, '\\$&');
  76. let url = window.location.href;
  77. let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
  78. let results = regex.exec(url);
  79. if (!results) {
  80. return null;
  81. }
  82. if (!results[2]) {
  83. return '';
  84. }
  85. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  86. },
  87. /**
  88. * Get user and other base info to start prime the cache and the application
  89. *
  90. * @returns {Promise}
  91. */
  92. bootstrap: function () {
  93. return Api.Users.getById('me', ['permissions'])
  94. .then(response => {
  95. Cache.User.set(response);
  96. Tokens.setCurrentName(response.nickname || response.name);
  97. });
  98. },
  99. /**
  100. * Bootstraps the user from time to time
  101. */
  102. bootstrapTimer: function () {
  103. setTimeout(() => {
  104. Api.status()
  105. .then(result => {
  106. let version = [result.version.major, result.version.minor, result.version.revision].join('.');
  107. if (version !== this.version) {
  108. document.location.reload();
  109. }
  110. })
  111. .then(this.bootstrap)
  112. .then(() => {
  113. this.bootstrapTimer();
  114. })
  115. .catch(err => {
  116. if (err.message !== 'timeout' && err.code && err.code !== 400) {
  117. console.log(err);
  118. console.error(err.message);
  119. console.info('Not logged in?');
  120. Controller.showLogin();
  121. } else {
  122. this.bootstrapTimer();
  123. }
  124. });
  125. }, 30 * 1000); // 30 seconds
  126. },
  127. refreshTokenTimer: function () {
  128. setTimeout(() => {
  129. return Api.Tokens.refresh()
  130. .then(this.bootstrap)
  131. .then(() => {
  132. this.refreshTokenTimer();
  133. })
  134. .catch(err => {
  135. if (err.message !== 'timeout' && err.code && err.code !== 400) {
  136. console.log(err);
  137. console.error(err.message);
  138. console.info('Not logged in?');
  139. Controller.showLogin();
  140. } else {
  141. this.refreshTokenTimer();
  142. }
  143. });
  144. }, 10 * 60 * 1000);
  145. }
  146. });
  147. const app = new App();
  148. module.exports = app;