telebit.js/lib/admin/js/app.js

182 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-10-16 02:37:07 +00:00
;(function () {
'use strict';
var Vue = window.Vue;
2018-10-18 07:52:30 +00:00
var Telebit = window.TELEBIT;
2018-10-16 02:37:07 +00:00
var api = {};
2018-10-21 05:25:14 +00:00
/*globals AbortController*/
function safeFetch(url, opts) {
var controller = new AbortController();
var tok = setTimeout(function () {
controller.abort();
}, 4000);
if (!opts) {
opts = {};
}
opts.signal = controller.signal;
return window.fetch(url, opts).finally(function () {
clearTimeout(tok);
});
}
2018-10-16 02:37:07 +00:00
api.config = function apiConfig() {
2018-10-21 05:25:14 +00:00
return safeFetch("/api/config", {
method: "GET"
}).then(function (resp) {
2018-10-16 02:37:07 +00:00
return resp.json().then(function (json) {
appData.config = json;
return json;
});
});
};
api.status = function apiStatus() {
2018-10-21 05:25:14 +00:00
return safeFetch("/api/status", { method: "GET" }).then(function (resp) {
2018-10-16 02:37:07 +00:00
return resp.json().then(function (json) {
appData.status = json;
return json;
});
});
};
2018-10-21 05:25:14 +00:00
api.initialize = function apiInitialize() {
var opts = {
method: "POST"
, headers: {
'Content-Type': 'application/json'
}
, body: JSON.stringify({
foo: 'bar'
})
};
return safeFetch("/api/init", opts).then(function (resp) {
return resp.json().then(function (json) {
appData.initResult = json;
window.alert("Error: [success] " + JSON.stringify(json, null, 2));
return json;
}).catch(function (err) {
window.alert("Error: [init] " + (err.message || JSON.stringify(err, null, 2)));
});
});
};
2018-10-16 02:37:07 +00:00
2018-10-18 07:11:37 +00:00
// TODO test for internet connectivity (and telebit connectivity)
var DEFAULT_RELAY = 'telebit.cloud';
var BETA_RELAY = 'telebit.ppl.family';
2018-10-21 05:25:14 +00:00
var TELEBIT_RELAYS = [
DEFAULT_RELAY
, BETA_RELAY
];
var PRODUCTION_ACME = 'https://acme-v02.api.letsencrypt.org/directory';
var STAGING_ACME = 'https://acme-staging-v02.api.letsencrypt.org/directory';
2018-10-16 02:37:07 +00:00
var appData = {
config: null
, status: null
2018-10-18 07:11:37 +00:00
, init: {
teletos: true
, letos: true
, notifications: "important"
, relay: DEFAULT_RELAY
2018-10-21 05:25:14 +00:00
, telemetry: true
, acmeServer: PRODUCTION_ACME
2018-10-18 07:11:37 +00:00
}
2018-10-16 02:37:07 +00:00
, http: null
, tcp: null
, ssh: null
2018-10-18 07:11:37 +00:00
, views: {
section: {
2018-10-21 05:25:14 +00:00
setup: false
, advanced: false
2018-10-18 07:11:37 +00:00
}
}
2018-10-16 02:37:07 +00:00
};
2018-10-21 05:25:14 +00:00
var telebitState = {};
2018-10-16 02:37:07 +00:00
var appMethods = {
initialize: function () {
console.log("call initialize");
2018-10-18 07:11:37 +00:00
if (!appData.init.relay) {
appData.init.relay = DEFAULT_RELAY;
}
2018-10-21 05:25:14 +00:00
appData.init.relay = appData.init.relay.toLowerCase();
telebitState = { relay: appData.init.relay };
return Telebit.api.directory(telebitState).then(function (dir) {
if (!dir.api_host) {
window.alert("Error: '" + telebitState.relay + "' does not appear to be a valid telebit service");
2018-10-18 07:52:30 +00:00
return;
}
2018-10-21 05:25:14 +00:00
if (-1 !== TELEBIT_RELAYS.indexOf(appData.init.relay)) {
return api.initialize();
} else {
changeState('advanced');
}
}).catch(function (err) {
window.alert("Error: [directory] " + (err.message || JSON.stringify(err, null, 2)));
2018-10-18 07:52:30 +00:00
});
2018-10-18 07:11:37 +00:00
}
2018-10-21 05:25:14 +00:00
, advance: function () {
return api.initialize();
}
, productionAcme: function () {
console.log("prod acme:");
appData.init.acmeServer = PRODUCTION_ACME;
console.log(appData.init.acmeServer);
}
, stagingAcme: function () {
console.log("staging acme:");
appData.init.acmeServer = STAGING_ACME;
console.log(appData.init.acmeServer);
}
2018-10-18 07:11:37 +00:00
, defaultRelay: function () {
appData.init.relay = DEFAULT_RELAY;
}
, betaRelay: function () {
appData.init.relay = BETA_RELAY;
2018-10-16 02:37:07 +00:00
}
2018-10-21 05:25:14 +00:00
, defaultRhubarb: function () {
appData.init.rhubarb = DEFAULT_RELAY;
}
, betaRhubarb: function () {
appData.init.rhubarb = BETA_RELAY;
}
};
var appStates = {
setup: function () {
appData.views.section = { setup: true };
}
, advanced: function () {
appData.views.section = { advanced: true };
}
2018-10-16 02:37:07 +00:00
};
2018-10-21 05:25:14 +00:00
function changeState(newstate) {
location.hash = '#/' + newstate + '/';
}
window.addEventListener('hashchange', setState, false);
function setState(/*ev*/) {
//ev.oldURL
//ev.newURL
var parts = location.hash.substr(1).replace(/^\//, '').replace(/\/$/, '').split('/');
var fn = appStates;
parts.forEach(function (s) {
console.log("state:", s);
fn = fn[s];
});
fn();
//appMethods.states[newstate]();
}
2018-10-16 02:37:07 +00:00
new Vue({
el: ".v-app"
, data: appData
, methods: appMethods
});
2018-10-21 05:25:14 +00:00
2018-10-16 02:37:07 +00:00
api.config();
2018-10-21 05:25:14 +00:00
api.status().then(function () {
changeState('setup');
setState();
});
2018-10-16 02:37:07 +00:00
window.api = api;
}());