Move of API folder from /app/front/api to app/api

This commit is contained in:
jokob-sk
2024-12-08 19:17:10 +11:00
parent d7858c6042
commit f7160f0843
27 changed files with 115 additions and 75 deletions

View File

@@ -1,2 +0,0 @@
*
!.gitignore

View File

@@ -68,7 +68,7 @@
// some race condition, need to implement delay
setTimeout(() => {
$.get('api/table_settings.json?nocache=' + Date.now(), function(res) {
$.get('/php/server/query_json.php', { file: 'table_settings.json', nocache: Date.now() }, function(res) {
settingsData = res["data"];

View File

@@ -199,26 +199,31 @@ function getDevicesTotals() {
// Fetch data via AJAX
$.ajax({
url: '/api/table_devices_tiles.json?nocache=' + Date.now(),
type: "GET",
dataType: "json",
success: function(response) {
if (response && response.data) {
resultJSON = response.data[0]; // Assuming the structure {"data": [ ... ]}
// Save the result to cache
setCache("getDevicesTotals", JSON.stringify(resultJSON));
url: '/php/server/query_json.php',
type: "GET",
dataType: "json",
data: {
file: 'table_devices_tiles.json', // Pass the file parameter
nocache: Date.now() // Prevent caching with a timestamp
},
success: function(response) {
if (response && response.data) {
resultJSON = response.data[0]; // Assuming the structure {"data": [ ... ]}
// Save the result to cache
setCache("getDevicesTotals", JSON.stringify(resultJSON));
// Process the fetched data
processDeviceTotals(resultJSON);
} else {
console.error("Invalid response format from API");
// Process the fetched data
processDeviceTotals(resultJSON);
} else {
console.error("Invalid response format from API");
}
},
error: function(xhr, status, error) {
console.error("Failed to fetch devices data:", error);
}
},
error: function(xhr, status, error) {
console.error("Failed to fetch devices data:", error);
}
});
}

View File

@@ -115,10 +115,10 @@ function cacheSettings()
return new Promise((resolve, reject) => {
if(!getCache('completedCalls').includes('cacheSettings'))
{
$.get('api/table_settings.json?nocache=' + Date.now(), function(resSet) {
$.get('api/plugins.json?nocache=' + Date.now(), function(resPlug) {
$.get('/php/server/query_json.php', { file: 'table_settings.json', nocache: Date.now() }, function(resSet) {
$.get('/php/server/query_json.php', { file: 'plugins.json', nocache: Date.now() }, function(resPlug) {
pluginsData = resPlug["data"];
settingsData = resSet["data"];
@@ -225,7 +225,7 @@ function cacheStrings() {
});
// Fetch strings and translations from plugins
$.get(`api/table_plugins_language_strings.json?nocache=${Date.now()}`)
$.get('/php/server/query_json.php', { file: 'table_plugins_language_strings.json', nocache: Date.now() })
.done((pluginRes) => {
const data = pluginRes["data"];
@@ -702,7 +702,7 @@ function forceLoadUrl(relativeUrl) {
// -----------------------------------------------------------------------------
function navigateToDeviceWithIp (ip) {
$.get('api/table_devices.json?nocache=' + Date.now(), function(res) {
$.get('/php/server/query_json.php', { file: 'table_devices.json', nocache: Date.now() }, function(res) {
devices = res["data"];
@@ -924,7 +924,7 @@ function cacheDevices()
// if(!getCache('completedCalls').includes('cacheDevices'))
// {
$.get('api/table_devices.json?nocache=' + Date.now(), function(data) {
$.get('/php/server/query_json.php', { file: 'table_devices.json', nocache: Date.now() }, function(data) {
// console.log(data)
@@ -1290,7 +1290,7 @@ function clearCache() {
// -----------------------------------------------------------------------------
// Function to check if cache needs to be refreshed because of setting changes
function checkSettingChanges() {
$.get('api/app_state.json?nocache=' + Date.now(), function(appState) {
$.get('/php/server/query_json.php', { file: 'app_state.json', nocache: Date.now() }, function(appState) {
const importedMilliseconds = parseInt(appState["settingsImported"] * 1000);
const lastReloaded = parseInt(sessionStorage.getItem(sessionStorageKey + '_time'));
@@ -1345,7 +1345,7 @@ async function waitForGraphQLServer() {
// Returns 1 if running, 0 otherwise
async function isGraphQLServerRunning() {
try {
const response = await $.get('api/app_state.json?nocache=' + Date.now());
const response = await $.get('/php/server/query_json.php', { file: 'app_state.json', nocache: Date.now()});
console.log("graphQLServerStarted: " + response["graphQLServerStarted"]);
setCache("graphQLServerStarted", response["graphQLServerStarted"]);
return response["graphQLServerStarted"];

View File

@@ -33,7 +33,7 @@ function versionUpdateUI(){
// Checks if a new version is available via the global app_state.json
function checkIfNewVersionAvailable()
{
$.get('api/app_state.json?nocache=' + Date.now(), function(appState) {
$.get('/php/server/query_json.php', { file: 'app_state.json', nocache: Date.now() }, function(appState) {
// console.log(appState["isNewVersionChecked"])
// console.log(appState["isNewVersion"])

View File

@@ -77,7 +77,7 @@
// some race condition, need to implement delay
setTimeout(() => {
$.get('api/table_settings.json?nocache=' + Date.now(), function(res) {
$.get('/php/server/query_json.php', { file: 'table_settings.json', nocache: Date.now() }, function(res) {
settingsData = res["data"];

View File

@@ -8,7 +8,7 @@
<script src="js/graph_online_history.js"></script>
<script>
$.get('api/table_online_history.json?nocache=' + Date.now(), function(res) {
$.get('/php/server/query_json.php', { file: 'table_online_history.json', nocache: Date.now() }, function(res) {
// Extracting data from the JSON response
var timeStamps = [];
var onlineCounts = [];

38
front/php/server/query_json.php Executable file
View File

@@ -0,0 +1,38 @@
<?php
// ---- IMPORTS ----
//------------------------------------------------------------------------------
// Check if authenticated
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/templates/security.php';
// Get init.php
require dirname(__FILE__).'/../server/init.php';
// ---- IMPORTS ----
//------------------------------------------------------------------------------
// Handle incoming requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// Get query string parameter ?file=settings_table.json
$file = isset($_GET['file']) ? $_GET['file'] : null;
// Check if file parameter is provided
if ($file) {
// Define the folder where files are located
$filePath = "/app/api/" . basename($file);
// Check if the file exists
if (file_exists($filePath)) {
// Send the response back to the client
header('Content-Type: application/json');
echo file_get_contents($filePath);
} else {
// File not found response
http_response_code(404);
echo json_encode(["error" => "File not found"]);
}
} else {
// Missing file parameter response
http_response_code(400);
echo json_encode(["error" => "Missing 'file' parameter"]);
}
}
?>

View File

@@ -430,7 +430,7 @@ function getString ($setKey, $default) {
// -------------------------------------------------------------------------------------------
function getSettingValue($setKey) {
// Define the JSON endpoint URL
$url = dirname(__FILE__).'/../../../front/api/table_settings.json';
$url = dirname(__FILE__).'/../../../api/table_settings.json';
// Fetch the JSON data
$json = file_get_contents($url);

View File

@@ -64,7 +64,7 @@ function generate_guid() {
// ----------------------------------------------------------------------------------------
// Logs a notification in in-app notification system
function write_notification($content, $level = "interrupt") {
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
$NOTIFICATION_API_FILE = '/app/api/user_notifications.json';
// Generate GUID
$guid = generate_guid();
@@ -97,7 +97,7 @@ function write_notification($content, $level = "interrupt") {
// ----------------------------------------------------------------------------------------
// Removes a notification based on GUID
function remove_notification($guid) {
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
$NOTIFICATION_API_FILE = '/app/api/user_notifications.json';
// Read existing notifications
$notifications = json_decode(file_get_contents($NOTIFICATION_API_FILE), true);
@@ -114,7 +114,7 @@ function remove_notification($guid) {
// ----------------------------------------------------------------------------------------
// Deletes all notifications
function notifications_clear() {
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
$NOTIFICATION_API_FILE = '/app/api/user_notifications.json';
// Clear notifications by writing an empty array to the file
file_put_contents($NOTIFICATION_API_FILE, json_encode(array()));
@@ -123,7 +123,7 @@ function notifications_clear() {
// ----------------------------------------------------------------------------------------
// Mark a notification read based on GUID
function mark_notification_as_read($guid) {
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
$NOTIFICATION_API_FILE = '/app/api/user_notifications.json';
$max_attempts = 3;
$attempts = 0;
@@ -172,7 +172,7 @@ function notifications_mark_all_read() {
// ----------------------------------------------------------------------------------------
function get_unread_notifications() {
$NOTIFICATION_API_FILE = '/app/front/api/user_notifications.json';
$NOTIFICATION_API_FILE = '/app/api/user_notifications.json';
// Read existing notifications
if (file_exists($NOTIFICATION_API_FILE) && is_readable($NOTIFICATION_API_FILE)) {

View File

@@ -91,7 +91,7 @@
<script>
function updateState(){
$.get('api/app_state.json?nocache=' + Date.now(), function(appState) {
$.get('/php/server/query_json.php', { file: 'app_state.json', nocache: Date.now() }, function(appState) {
document.getElementById('state').innerHTML = appState["currentState"].replaceAll('"', '');

View File

@@ -45,7 +45,7 @@ function jsonResponse($status, $data = '', $message = '') {
if ($method === 'GET') {
checkAuthorization($method);
$file_path = "/app/front/api/table_devices.json";
$file_path = "/app/api/table_devices.json";
$data = file_get_contents($file_path);

View File

@@ -104,7 +104,7 @@ def main():
# PUSHING/SENDING devices
if send_devices:
file_path = f"{INSTALL_PATH}/front/api/table_devices.json"
file_path = f"{INSTALL_PATH}/api/table_devices.json"
plugin_folder = 'sync'
pref = 'SYNC'

View File

@@ -246,19 +246,19 @@ function getData(){
// Show the loading spinner while generating
showSpinner();
$.get('api/plugins.json', function(res) {
$.get('php/server/query_json.php?file=plugins.json', function(res) {
pluginDefinitions = res["data"];
$.get('api/table_plugins_events.json', function(res) {
$.get('php/server/query_json.php?file=table_plugins_events.json', function(res) {
pluginUnprocessedEvents = res["data"];
$.get('api/table_plugins_objects.json', function(res) {
$.get('php/server/query_json.php?file=table_plugins_objects.json', function(res) {
pluginObjects = res["data"];
$.get('api/table_plugins_history.json', function(res) {
$.get('php/server/query_json.php?file=table_plugins_history.json', function(res) {
pluginHistory = res["data"];

View File

@@ -99,7 +99,7 @@
// Function to update the displayed data and timestamp based on the selected format and index
function updateData(format, index) {
// Fetch data from the API endpoint
fetch('api/table_notifications.json?nocache=' + Date.now())
fetch(`/php/server/query_json.php?file=table_notifications.json&nocache=${Date.now()}`)
.then(response => response.json())
.then(data => {
if (index < 0) {

View File

@@ -17,7 +17,7 @@ checkPermissions([$dbPath, $confPath]);
// get settings from the API json file
// path to your JSON file
$file = '../front/api/table_settings.json';
$file = '../api/table_settings.json';
// put the content of the file in a variable
$data = file_get_contents($file);
// JSON decode
@@ -219,7 +219,7 @@ $settingsJSON_DB = json_encode($settings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX
}, 3000);
} else
{
$.get('api/plugins.json?nocache=' + Date.now(), function(res) {
$.get('/php/server/query_json.php', { file: 'plugins.json', nocache: Date.now() }, function(res) {
pluginsData = res["data"];
@@ -556,7 +556,7 @@ $settingsJSON_DB = json_encode($settings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX
// collect values for each of the different input form controls
// get settings to determine setting type to store values appropriately
$.get('api/table_settings.json?nocache=' + Date.now(), function(res) {
$.get('/php/server/query_json.php', { file: 'table_settings.json', nocache: Date.now() }, function(res) {
// loop through the settings definitions from the json
res["data"].forEach(set => {
@@ -733,7 +733,7 @@ $settingsJSON_DB = json_encode($settings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX
} else
{
// check if config file has been updated
$.get('api/app_state.json?nocache=' + Date.now(), function(appState) {
$.get('/php/server/query_json.php', { file: 'app_state.json', nocache: Date.now() }, function(appState) {
fileModificationTime = <?php echo filemtime($confPath)*1000;?>;

View File

@@ -59,28 +59,23 @@ require 'php/templates/header.php';
<script>
function fetchData(callback) {
$.ajax({
url: 'api/user_notifications.json?nocache=' + Date.now(),
method: 'GET',
dataType: 'json',
success: function(response) {
console.log(response);
$.get('/php/server/query_json.php', { file: 'user_notifications.json', nocache: Date.now() })
.done(function(response) {
if (response == "[]" || response == "") {
callback([]);
callback([]);
} else if (response.error) {
alert("Error: " + response.error);
callback([]);
alert("Error: " + response.error);
callback([]);
} else if (!Array.isArray(response)) {
alert("Unexpected response format");
callback([]);
alert("Unexpected response format");
callback([]);
} else {
callback(response);
callback(response);
}
},
error: function(xhr, status, error) {
console.log("An error occurred while fetching data: " + error);
})
.fail(function(xhr, status, error) {
console.error("An error occurred while fetching data:", error);
callback([]);
}
});
}