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