Added /status for independant api uptime status

This commit is contained in:
Jonas_Jones 2024-02-09 02:25:36 +01:00
parent ab6bc04c01
commit a37baf6736

View file

@ -24,6 +24,19 @@ addEventListener('fetch', event => {
try {
const apiResponse = await fetch(apiRequest);
if (pathname === '/status' && apiResponse.ok) {
// If the request is to the /status path and the API request is successful, return a custom response
return new Response('API is up and running', {
status: 200,
statusText: 'OK'
})} else if (pathname === '/status' && !apiResponse.ok) {
// If the request is to the /status path and the API request fails, return a custom response
return new Response('API is down', {
status: 503,
statusText: 'Service Unavailable'
})};
if (apiResponse.status === 502 || apiResponse.status === 530) {
// If the API request fails, return an error response
return new Response('Service Unavailable', {
@ -35,10 +48,11 @@ addEventListener('fetch', event => {
return apiResponse;
}
} catch (error) {
console.log(error);
// If an error occurs during the API request, return an error response
return new Response('API backend is down temporarily', {
status: 420,
statusText: 'API Backend Downtime Error'
return new Response('Internal Server Error', {
status: 500,
statusText: 'Internal Server Error'
});
}
}