Skip to content

Commit

Permalink
feat: added check if maintenance mode is enabled, refactored API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed May 30, 2024
1 parent 7c68fa6 commit c1f774c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 32 deletions.
3 changes: 2 additions & 1 deletion phpmyfaq/admin/assets/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export * from './category';
export * from './faqs';
export * from './glossary';
export * from './group';
export * from './news';
export * from './statistics';
export * from './tags';
export * from './upgrade';
export * from './user';
export * from './news';
2 changes: 1 addition & 1 deletion phpmyfaq/admin/assets/src/api/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2023 phpMyFAQ Team
* @copyright 2023-2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-04-12
Expand Down
32 changes: 32 additions & 0 deletions phpmyfaq/admin/assets/src/api/upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Upgrade API functionality
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <[email protected]>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-05-30
*/

export const fetchHealthCheck = async () => {
try {
const response = await fetch(`./api/health-check`, {
method: 'GET',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
});

return await response.json();
} catch (error) {
console.error(error);
}
};
50 changes: 22 additions & 28 deletions phpmyfaq/admin/assets/src/configuration/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* @link https://www.phpmyfaq.de
* @since 2023-07-11
*/

import { addElement } from '../../../../assets/src/utils';
import { fetchHealthCheck } from '../api';

export const handleCheckForUpdates = () => {
const checkHealthButton = document.getElementById('pmf-button-check-health');
Expand All @@ -24,37 +26,29 @@ export const handleCheckForUpdates = () => {

// Health Check
if (checkHealthButton) {
checkHealthButton.addEventListener('click', (event) => {
checkHealthButton.addEventListener('click', async (event) => {
event.preventDefault();
fetch(window.location.pathname + 'api/health-check', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
})
.then(async (response) => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok: ', { cause: { response } });
})
.then((response) => {
const result = document.getElementById('result-check-health');
const card = document.getElementById('pmf-update-step-health-check');
if (result) {
card.classList.add('text-bg-success');
if (response.success === 'ok') {
result.replaceWith(addElement('p', { innerText: response.message }));
} else {
result.replaceWith(addElement('p', { innerText: response.message }));
}
}
})
.catch(async (error) => {
try {
const responseData = await fetchHealthCheck();
const result = document.getElementById('result-check-health');
const card = document.getElementById('pmf-update-step-health-check');

if (responseData.success) {
card.classList.add('text-bg-success');
result.replaceWith(addElement('p', { innerText: responseData.success }));
}
if (responseData.error) {
card.classList.add('text-bg-danger');
result.replaceWith(addElement('p', { innerText: responseData.error }));
}
} catch (error) {
if (error.cause && error.cause.response) {
const errorMessage = await error.cause.response.json();
console.error(errorMessage);
});
} else {
console.error(error.message);
}
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,29 @@ public function healthCheck(): JsonResponse
$dateLastChecked = $dateTime->format(DateTimeInterface::ATOM);
$upgrade = new Upgrade(new System(), $configuration);

if (!$upgrade->isMaintenanceEnabled()) {
return $this->json(
[
'error' => Translation::get('msgNotInMaintenanceMode'),
'dateLastChecked' => $dateLastChecked,
],
Response::HTTP_CONFLICT
);
}

try {
$upgrade->checkFilesystem();
return $this->json(
[
'message' => Translation::get('healthCheckOkay'),
'success' => Translation::get('healthCheckOkay'),
'dateLastChecked' => $dateLastChecked,
],
Response::HTTP_OK
);
} catch (Exception $exception) {
return $this->json(
[
'message' => $exception->getMessage(),
'error' => $exception->getMessage(),
'dateLastChecked' => $dateLastChecked,
],
Response::HTTP_BAD_REQUEST
Expand Down
15 changes: 15 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Upgrade extends Setup

private bool $isNightly;

private bool $isMaintenanceEnabled = false;

public function __construct(protected System $system, private readonly Configuration $configuration)
{
parent::__construct($this->system);
Expand Down Expand Up @@ -404,4 +406,17 @@ public function setIsNightly(bool $isNightly): void
{
$this->isNightly = $isNightly;
}

public function isMaintenanceEnabled(): bool
{
return $this->isMaintenanceEnabled = $this->configuration->get('main.maintenanceMode');
}

public function setIsMaintenanceEnabled(bool $isMaintenanceEnabled): Upgrade
{
$this->isMaintenanceEnabled = $isMaintenanceEnabled;
$this->configuration->set('main.maintenanceMode', $isMaintenanceEnabled);

return $this;
}
}
1 change: 1 addition & 0 deletions phpmyfaq/translations/language_de.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,5 +1425,6 @@

// added v4.0.0-alpha.2 - 2024-04-30 by Thorsten
$PMF_LANG['msgNoQuestionAndAnswer'] = 'Keine Frage und Antwort gefunden.';
$PMF_LANG['msgNotInMaintenanceMode'] = 'Die FAQ ist nicht im Wartungs-Modus.';

return $PMF_LANG;
1 change: 1 addition & 0 deletions phpmyfaq/translations/language_en.php
Original file line number Diff line number Diff line change
Expand Up @@ -1445,5 +1445,6 @@

// added v4.0.0-alpha.2 - 2024-04-30 by Thorsten
$PMF_LANG['msgNoQuestionAndAnswer'] = 'No question and answer found.';
$PMF_LANG['msgNotInMaintenanceMode'] = 'The FAQ is not in maintenance mode.';

return $PMF_LANG;

0 comments on commit c1f774c

Please sign in to comment.