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

194 lines
4.8 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
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-21 05:25:14 +00:00
2018-10-16 02:37:07 +00:00
api.config = function apiConfig() {
return Telebit.reqLocalAsync({
url: "/api/config"
, method: "GET"
2018-10-21 05:25:14 +00:00
}).then(function (resp) {
var json = resp.body;
appData.config = json;
return json;
2018-10-16 02:37:07 +00:00
});
};
api.status = function apiStatus() {
return Telebit.reqLocalAsync({ url: "/api/status", method: "GET" }).then(function (resp) {
var json = resp.body;
appData.status = json;
return json;
2018-10-16 02:37:07 +00:00
});
};
2018-10-21 05:25:14 +00:00
api.initialize = function apiInitialize() {
var opts = {
url: "/api/init"
, method: "POST"
2018-10-21 05:25:14 +00:00
, headers: {
'Content-Type': 'application/json'
}
, body: JSON.stringify({
foo: 'bar'
})
};
return Telebit.reqLocalAsync(opts).then(function (resp) {
var json = resp.body;
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-21 05:25:14 +00:00
});
};
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)) {
if (!telebitState.config) { telebitState.config = {}; }
if (!telebitState.config.relay) { telebitState.config.relay = telebitState.relay; }
telebitState.config.email = appData.init.email;
telebitState.config._otp = Telebit.otp();
return Telebit.authorize(telebitState).then(function () {
console.log('1 api.init...');
return api.initialize();
}).catch(function (err) {
console.error(err);
window.alert("Error: [authorize] " + (err.message || JSON.stringify(err, null, 2)));
});
2018-10-21 05:25:14 +00:00
} else {
changeState('advanced');
}
}).catch(function (err) {
console.error(err);
2018-10-21 05:25:14 +00:00
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 () {
console.log('2 api.init...');
2018-10-21 05:25:14 +00:00
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;
}());