Implements the ping-only status check option into frontend

This commit is contained in:
Alicia Sykes 2021-09-18 00:42:28 +01:00
parent cefe56c0be
commit 5f677e1fdd
2 changed files with 27 additions and 9 deletions

View File

@ -73,8 +73,9 @@ export default {
itemSize: String, itemSize: String,
enableStatusCheck: Boolean, enableStatusCheck: Boolean,
statusCheckHeaders: Object, statusCheckHeaders: Object,
statusCheckUrl: String, statusCheckEndpoint: String,
statusCheckInterval: Number, statusCheckInterval: Number,
statusCheckPingOnly: Boolean,
statusCheckAllowInsecure: Boolean, statusCheckAllowInsecure: Boolean,
}, },
data() { data() {
@ -157,16 +158,16 @@ export default {
} }
}, },
/* Pulls together all user options, returns URL + Get params for ping endpoint */ /* Pulls together all user options, returns URL + Get params for ping endpoint */
makeApiUrl() { makeStatusCheckGetEndpoint() {
const { const {
url, statusCheckUrl, statusCheckHeaders, statusCheckAllowInsecure, url, statusCheckEndpoint, statusCheckHeaders, statusCheckAllowInsecure,
} = this; } = this;
const encode = (str) => encodeURIComponent(str); const encode = (str) => encodeURIComponent(str);
this.statusResponse = undefined; this.statusResponse = undefined;
// Find base URL, where the API is hosted // Find base URL, where the API is hosted
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin;
// Find correct URL to check, and encode // Find correct URL to check, and encode
const urlToCheck = `?&url=${encode(statusCheckUrl || url)}`; const urlToCheck = `?&url=${encode(statusCheckEndpoint || url)}`;
// Get, stringify and encode any headers // Get, stringify and encode any headers
const headers = statusCheckHeaders const headers = statusCheckHeaders
? `&headers=${encode(JSON.stringify(statusCheckHeaders))}` : ''; ? `&headers=${encode(JSON.stringify(statusCheckHeaders))}` : '';
@ -175,9 +176,15 @@ export default {
// Construct the full API endpoint's URL with GET params // Construct the full API endpoint's URL with GET params
return `${baseUrl}${serviceEndpoints.statusCheck}/${urlToCheck}${headers}${enableInsecure}`; return `${baseUrl}${serviceEndpoints.statusCheck}/${urlToCheck}${headers}${enableInsecure}`;
}, },
/* Return the endpoint for ping-only status checks */
makeStatusCheckPingEndpoint() {
const { statusCheckEndpoint, url } = this;
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin;
const ipToCheck = `?&ip=${statusCheckEndpoint || url}`;
return `${baseUrl}${serviceEndpoints.statusPing}/${ipToCheck}`;
},
/* Checks if a given service is currently online */ /* Checks if a given service is currently online */
checkWebsiteStatus() { checkWebsiteStatus(endpoint) {
const endpoint = this.makeApiUrl();
axios.get(endpoint) axios.get(endpoint)
.then((response) => { .then((response) => {
if (response.data) this.statusResponse = response.data; if (response.data) this.statusResponse = response.data;
@ -223,13 +230,23 @@ export default {
lastUsed[itemId] = new Date().getTime(); lastUsed[itemId] = new Date().getTime();
localStorage.setItem(localStorageKeys.LAST_USED, JSON.stringify(lastUsed)); localStorage.setItem(localStorageKeys.LAST_USED, JSON.stringify(lastUsed));
}, },
/* Starts off the status check, with info from the above functions */
kickOffCheck() {
if (this.enableStatusCheck) {
// Determine endpoint function, depending if using GET or ping-only option
const endpoint = this.statusCheckPingOnly
? this.makeStatusCheckPingEndpoint() : this.makeStatusCheckGetEndpoint();
// Initiate status check
this.checkWebsiteStatus(endpoint);
}
},
}, },
mounted() { mounted() {
// If ststus checking is enabled, then check service status // If ststus checking is enabled, then check service status
if (this.enableStatusCheck) this.checkWebsiteStatus(); this.kickOffCheck();
// If continious status checking is enabled, then start ever-lasting loop // If continious status checking is enabled, then start ever-lasting loop
if (this.statusCheckInterval > 0) { if (this.statusCheckInterval > 0) {
setInterval(this.checkWebsiteStatus, this.statusCheckInterval * 1000); setInterval(this.kickOffCheck, this.statusCheckInterval * 1000);
} }
}, },
}; };

View File

@ -27,8 +27,9 @@
:target="item.target" :target="item.target"
:color="item.color" :color="item.color"
:backgroundColor="item.backgroundColor" :backgroundColor="item.backgroundColor"
:statusCheckUrl="item.statusCheckUrl" :statusCheckEndpoint="item.statusCheckEndpoint || item.statusCheckUrl"
:statusCheckHeaders="item.statusCheckHeaders" :statusCheckHeaders="item.statusCheckHeaders"
:statusCheckPingOnly="item.statusCheckPingOnly"
:itemSize="newItemSize" :itemSize="newItemSize"
:hotkey="item.hotkey" :hotkey="item.hotkey"
:provider="item.provider" :provider="item.provider"