浏览代码

compress the all-structures api response

Olivier Massot 4 年之前
父节点
当前提交
3bad761d22

+ 30 - 0
ot_core/Resources/Public/getStructures.php

@@ -0,0 +1,30 @@
+<?php
+
+// Get all of the Opentalent structures from the Opentalent API,
+// gzip the response, and send it to the client
+
+$default_uri = "https://api.opentalent.fr";
+$variants_uris = [
+    "preprod.opentalent.fr" => "https://api.preprod.opentalent.fr",
+    "local.sub.opentalent.fr" => "http://docker.nginx.opentalent.fr",
+    "typo3" => "http://docker.nginx.opentalent.fr"
+];
+
+$base_uri = $variants_uris[$_SERVER['HTTP_HOST']] ?? $default_uri;
+$apiGetUrl = $base_uri . '/api/public/federation_structures?_format=json&page=1&itemsPerPage=99999';
+
+$ch = curl_init($apiGetUrl);
+curl_setopt($ch, CURLOPT_HEADER, 0);
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+
+$response = curl_exec($ch);
+
+$errors = curl_error($ch);
+$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
+curl_close($ch);
+
+$response = gzdeflate($response, 9, ZLIB_ENCODING_DEFLATE);
+
+header('Content-Encoding: deflate');
+echo $response;

+ 25 - 27
ot_templating/Resources/Public/assets/Classic/script/structures.js

@@ -1,15 +1,6 @@
 // Specific code for the 'federation-structures' page
 
-const default_uri = "https://api.opentalent.fr";
-const variants_uris = {
-    "preprod.opentalent.fr": "https://api.preprod.opentalent.fr",
-    "local.sub.opentalent.fr": "https://local.api.opentalent.fr",
-    "typo3": "http://docker.nginx.opentalent.fr"
-};
-
-const base_uri = variants_uris[$(location).attr('hostname')] ?? default_uri;
-let apiGetUrl = base_uri + '/api/public/federation_structures?_format=json&page=1&itemsPerPage=99999';
-
+const apiGetUrl = "http://local.sub.opentalent.fr/typo3conf/ext/ot_core/Resources/Public/getStructures.php";
 
 // Converts numeric degrees to radians
 function toRad(Value)
@@ -368,23 +359,30 @@ $(document).ready(function() {
 
     // #### Load structures data and refresh
     var structures = [];
-    $.ajax({
-        type: 'GET',
-        url: apiGetUrl,
-        dataType: "json",
-        contentType: "application/json; charset=utf-8",
-        timeout : 5000
-    })
-    .done(function(res) {
-        structures = res["hydra:member"];
-        populateFederationsSelect();
-        refresh();
-    })
-    .fail(function(xhr, textStatus, errorThrown) {
-        pleaseWaitSpan.hide();
-        errorMsgSpan.show();
-        throw 'Error while fetching the API data: ' + textStatus  + ' - ' + errorThrown;
-    });
+
+    function loadStructures() {
+        structures = []
+
+        $.ajax({
+            type: 'GET',
+            url: apiGetUrl,
+            dataType: "json",
+            contentType: "application/json; charset=utf-8",
+            headers : {'Accept-Encoding': 'deflate '},
+            timeout : 5000,
+        })
+        .done(function(res) {
+            structures = res["hydra:member"];
+            populateFederationsSelect();
+            refresh();
+        })
+        .fail(function(xhr, textStatus, errorThrown) {
+            pleaseWaitSpan.hide();
+            errorMsgSpan.show();
+            throw 'Error while fetching the API data: ' + textStatus  + ' - ' + errorThrown;
+        });
+    }
+    loadStructures();
 
 
     // #### Events