Use constants for API endpoints

This commit is contained in:
Alicia Sykes 2021-09-17 22:40:39 +01:00
parent 17402fb8f7
commit f0449969e7
4 changed files with 16 additions and 13 deletions

View File

@ -17,12 +17,15 @@ const bodyParser = require('body-parser');
require('./services/update-checker'); // Checks if there are any updates available, prints message require('./services/update-checker'); // Checks if there are any updates available, prints message
require('./services/config-validator'); // Include and kicks off the config file validation script require('./services/config-validator'); // Include and kicks off the config file validation script
/* Include helper functions and route handlers */ /* Include route handlers for API endpoints */
const pingUrl = require('./services/ping'); // Used by the status check feature, to ping services const statusCheck = require('./services/status-check'); // Used by the status check feature, to ping services
const saveConfig = require('./services/save-config'); // Saves users new conf.yml to file-system const saveConfig = require('./services/save-config'); // Saves users new conf.yml to file-system
const printMessage = require('./services/print-message'); // Function to print welcome msg on start
const rebuild = require('./services/rebuild-app'); // A script to programmatically trigger a build const rebuild = require('./services/rebuild-app'); // A script to programmatically trigger a build
/* Helper functions, and default config */
const printMessage = require('./services/print-message'); // Function to print welcome msg on start
const ENDPOINTS = require('./src/utils/defaults').serviceEndpoints; // API endpoint URL paths
/* Checks if app is running within a container, from env var */ /* Checks if app is running within a container, from env var */
const isDocker = !!process.env.IS_DOCKER; const isDocker = !!process.env.IS_DOCKER;
@ -59,9 +62,9 @@ try {
// During build, a custom page will be served before the app is available // During build, a custom page will be served before the app is available
.use(serveStatic(`${__dirname}/public`, { index: 'default.html' })) .use(serveStatic(`${__dirname}/public`, { index: 'default.html' }))
// This root returns the status of a given service - used for uptime monitoring // This root returns the status of a given service - used for uptime monitoring
.use('/ping', (req, res) => { .use(ENDPOINTS.statusCheck, (req, res) => {
try { try {
pingUrl(req.url, async (results) => { statusCheck(req.url, async (results) => {
await res.end(results); await res.end(results);
}); });
} catch (e) { } catch (e) {
@ -69,7 +72,7 @@ try {
} }
}) })
// POST Endpoint used to save config, by writing conf.yml to disk // POST Endpoint used to save config, by writing conf.yml to disk
.use('/config-manager/save', method('POST', (req, res) => { .use(ENDPOINTS.save, method('POST', (req, res) => {
try { try {
saveConfig(req.body, (results) => { saveConfig(req.body, (results) => {
res.end(results); res.end(results);
@ -79,7 +82,7 @@ try {
} }
})) }))
// GET endpoint to trigger a build, and respond with success status and output // GET endpoint to trigger a build, and respond with success status and output
.use('/config-manager/rebuild', (req, res) => { .use(ENDPOINTS.rebuild, (req, res) => {
rebuild().then((response) => { rebuild().then((response) => {
res.end(JSON.stringify(response)); res.end(JSON.stringify(response));
}).catch((response) => { }).catch((response) => {

View File

@ -64,7 +64,7 @@ import VJsoneditor from 'v-jsoneditor';
import ErrorHandler, { InfoHandler } from '@/utils/ErrorHandler'; import ErrorHandler, { InfoHandler } from '@/utils/ErrorHandler';
import configSchema from '@/utils/ConfigSchema.json'; import configSchema from '@/utils/ConfigSchema.json';
import JsonToYaml from '@/utils/JsonToYaml'; import JsonToYaml from '@/utils/JsonToYaml';
import { localStorageKeys } from '@/utils/defaults'; import { localStorageKeys, serviceEndpoints } from '@/utils/defaults';
import { isUserAdmin } from '@/utils/Auth'; import { isUserAdmin } from '@/utils/Auth';
export default { export default {
@ -121,7 +121,7 @@ export default {
const yaml = this.jsonParser(this.jsonData); const yaml = this.jsonParser(this.jsonData);
// 2. Prepare the request // 2. Prepare the request
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin;
const endpoint = `${baseUrl}/config-manager/save`; const endpoint = `${baseUrl}${serviceEndpoints.save}`;
const headers = { 'Content-Type': 'text/plain' }; const headers = { 'Content-Type': 'text/plain' };
const body = { config: yaml, timestamp: new Date() }; const body = { config: yaml, timestamp: new Date() };
const request = axios.post(endpoint, body, headers); const request = axios.post(endpoint, body, headers);

View File

@ -48,10 +48,10 @@
import axios from 'axios'; import axios from 'axios';
import ProgressBar from 'rsup-progress'; import ProgressBar from 'rsup-progress';
import Button from '@/components/FormElements/Button'; import Button from '@/components/FormElements/Button';
import { modalNames } from '@/utils/defaults';
import RebuildIcon from '@/assets/interface-icons/application-rebuild.svg'; import RebuildIcon from '@/assets/interface-icons/application-rebuild.svg';
import ReloadIcon from '@/assets/interface-icons/application-reload.svg'; import ReloadIcon from '@/assets/interface-icons/application-reload.svg';
import LoadingAnimation from '@/assets/interface-icons/loader.svg'; import LoadingAnimation from '@/assets/interface-icons/loader.svg';
import { modalNames, serviceEndpoints } from '@/utils/defaults';
export default { export default {
name: 'RebuildApp', name: 'RebuildApp',
@ -76,7 +76,7 @@ export default {
/* Calls to the rebuild endpoint, to kickoff the app build */ /* Calls to the rebuild endpoint, to kickoff the app build */
startBuild() { startBuild() {
const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin; const baseUrl = process.env.VUE_APP_DOMAIN || window.location.origin;
const endpoint = `${baseUrl}/config-manager/rebuild`; const endpoint = `${baseUrl}${serviceEndpoints.rebuild}`;
this.loading = true; this.loading = true;
this.progress.start(); this.progress.start();
axios.get(endpoint) axios.get(endpoint)

View File

@ -49,7 +49,7 @@ import Icon from '@/components/LinkItems/ItemIcon.vue';
import ItemOpenMethodIcon from '@/components/LinkItems/ItemOpenMethodIcon'; import ItemOpenMethodIcon from '@/components/LinkItems/ItemOpenMethodIcon';
import StatusIndicator from '@/components/LinkItems/StatusIndicator'; import StatusIndicator from '@/components/LinkItems/StatusIndicator';
import ContextMenu from '@/components/LinkItems/ContextMenu'; import ContextMenu from '@/components/LinkItems/ContextMenu';
import { localStorageKeys } from '@/utils/defaults'; import { localStorageKeys, serviceEndpoints } from '@/utils/defaults';
export default { export default {
name: 'Item', name: 'Item',
@ -173,7 +173,7 @@ export default {
// Deterimine if user disabled security // Deterimine if user disabled security
const enableInsecure = statusCheckAllowInsecure ? '&enableInsecure=true' : ''; const enableInsecure = statusCheckAllowInsecure ? '&enableInsecure=true' : '';
// Construct the full API endpoint's URL with GET params // Construct the full API endpoint's URL with GET params
return `${baseUrl}/ping/${urlToCheck}${headers}${enableInsecure}`; return `${baseUrl}${serviceEndpoints.statusCheck}/${urlToCheck}${headers}${enableInsecure}`;
}, },
/* Checks if a given service is currently online */ /* Checks if a given service is currently online */
checkWebsiteStatus() { checkWebsiteStatus() {