From 882624a7df86cf0fd694a5541522718e07ef5c6e Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Fri, 26 Jul 2024 12:39:19 +0800 Subject: [PATCH 01/12] feat: use environment variable for LARAVEL_ECHO_SERVER_HOST --- docker-compose.yml | 3 +++ nginx-template-variables | 3 +++ nginx.conf | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 nginx-template-variables diff --git a/docker-compose.yml b/docker-compose.yml index ff3f76de..22aa9fb2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,8 +20,11 @@ services: - CACHE_DRIVER=redis - QUEUE_DRIVER=redis - ECHO_HOST_MODE=path + - LARAVEL_ECHO_SERVER_HOST=http://laravel-echo-server:6001 depends_on: - redis + volumes: + - ./nginx-template-variables:/etc/nginx/templates/10-variables.conf.template:ro redis: container_name: "webhook-redis" diff --git a/nginx-template-variables b/nginx-template-variables new file mode 100644 index 00000000..0fae1683 --- /dev/null +++ b/nginx-template-variables @@ -0,0 +1,3 @@ +map $host $laravel_echo_server_host { + default "$LARAVEL_ECHO_SERVER_HOST"; +} \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 8bc992c1..b43aa54d 100644 --- a/nginx.conf +++ b/nginx.conf @@ -24,7 +24,7 @@ server { # socket.io location /socket.io { - proxy_pass http://laravel-echo-server:6001; + proxy_pass $laravel_echo_server_host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; From 7145a4111ed8e0a51e9a1d1b67e40674af9dd234 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Fri, 26 Jul 2024 15:55:01 +0800 Subject: [PATCH 02/12] feat: docker entrypoint to replace nginx environment variable --- Dockerfile | 5 ++++- docker-entrypoint.sh | 10 ++++++++++ nginx.conf => nginx.conf.template | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100755 docker-entrypoint.sh rename nginx.conf => nginx.conf.template (97%) diff --git a/Dockerfile b/Dockerfile index 62f1cfd0..bd3a545e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,7 @@ FROM bkuhl/fpm-nginx:7.3 WORKDIR /var/www/html # Contains laravel echo server proxy configuration -COPY /nginx.conf /etc/nginx/conf.d +COPY /nginx.conf.template /etc/nginx USER www-data @@ -49,4 +49,7 @@ ADD --chown=www-data:www-data /resources /var/www/html/resources COPY --chown=www-data:www-data --from=npm /app/public/css /var/www/html/public/css COPY --chown=www-data:www-data --from=npm /app/public/js /var/www/html/public/js +COPY docker-entrypoint.sh / +ENTRYPOINT ["/docker-entrypoint.sh"] + USER root \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 00000000..6de0937b --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env sh +set -eu + +envsubst '${LARAVEL_ECHO_SERVER_HOST}' < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/nginx.conf + +# Start PHP-FPM in the background +php-fpm -D + +# Start Nginx in the foreground +exec nginx -g 'daemon off;' diff --git a/nginx.conf b/nginx.conf.template similarity index 97% rename from nginx.conf rename to nginx.conf.template index b43aa54d..d5f095f2 100644 --- a/nginx.conf +++ b/nginx.conf.template @@ -24,7 +24,7 @@ server { # socket.io location /socket.io { - proxy_pass $laravel_echo_server_host; + proxy_pass $LARAVEL_ECHO_SERVER_HOST; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; From 27b0ce881e0b004879e09c0eaaa915d9431774df Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Fri, 26 Jul 2024 16:13:49 +0800 Subject: [PATCH 03/12] chore: remove unused map --- docker-compose.yml | 2 -- nginx-template-variables | 3 --- 2 files changed, 5 deletions(-) delete mode 100644 nginx-template-variables diff --git a/docker-compose.yml b/docker-compose.yml index 22aa9fb2..c0365b5c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,8 +23,6 @@ services: - LARAVEL_ECHO_SERVER_HOST=http://laravel-echo-server:6001 depends_on: - redis - volumes: - - ./nginx-template-variables:/etc/nginx/templates/10-variables.conf.template:ro redis: container_name: "webhook-redis" diff --git a/nginx-template-variables b/nginx-template-variables deleted file mode 100644 index 0fae1683..00000000 --- a/nginx-template-variables +++ /dev/null @@ -1,3 +0,0 @@ -map $host $laravel_echo_server_host { - default "$LARAVEL_ECHO_SERVER_HOST"; -} \ No newline at end of file From 849e8bb748f43e862f48b99d480b8de61d5be21c Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 14:57:53 +0800 Subject: [PATCH 04/12] feat: sorting on list requests --- app/Http/Controllers/RequestController.php | 3 +- app/Storage/Redis/RequestStore.php | 39 ++++++++++++++++------ app/Storage/RequestStore.php | 3 +- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/RequestController.php b/app/Http/Controllers/RequestController.php index ff024de3..f0185269 100644 --- a/app/Http/Controllers/RequestController.php +++ b/app/Http/Controllers/RequestController.php @@ -100,9 +100,10 @@ private function guardOverQuota($token) public function all(HttpRequest $httpRequest, $tokenId) { $token = $this->tokens->find($tokenId); + $sorting = $httpRequest->get('sorting', 'oldest'); $page = (int)$httpRequest->get('page', 1); $perPage = (int)$httpRequest->get('per_page', 50); - $requests = $this->requests->all($token, $page, $perPage); + $requests = $this->requests->all($token, $page, $perPage, $sorting); $total = $this->tokens->countRequests($token); return new JsonResponse([ diff --git a/app/Storage/Redis/RequestStore.php b/app/Storage/Redis/RequestStore.php index 8f2c3965..ebba65ae 100644 --- a/app/Storage/Redis/RequestStore.php +++ b/app/Storage/Redis/RequestStore.php @@ -47,11 +47,12 @@ public function find(Token $token, $requestId) * @param Token $token * @param int $page * @param int $perPage + * @param string $sort * @return Collection|static */ - public function all(Token $token, $page = 1, $perPage = 50) + public function all(Token $token, $page = 1, $perPage = 50, $sorting = "oldest") { - return collect( + $requests = collect( $this->redis->hgetall(Request::getIdentifier($token->uuid)) ) ->filter() @@ -59,15 +60,31 @@ public function all(Token $token, $page = 1, $perPage = 50) function ($request) { return json_decode($request); } - )->sortBy( - function ($request) { - return Carbon::createFromFormat( - 'Y-m-d H:i:s', - $request->created_at - )->getTimestamp(); - }, - SORT_DESC - )->forPage( + ); + + if ($sorting === "newest") { + $requests = $requests->sortByDesc( + function ($request) { + return Carbon::createFromFormat( + 'Y-m-d H:i:s', + $request->created_at + )->getTimestamp(); + }, + SORT_DESC + ); + } else { + $requests = $requests->sortBy( + function ($request) { + return Carbon::createFromFormat( + 'Y-m-d H:i:s', + $request->created_at + )->getTimestamp(); + }, + SORT_DESC + ); + } + + return $requests->forPage( $page, $perPage )->values(); diff --git a/app/Storage/RequestStore.php b/app/Storage/RequestStore.php index f105cbb4..40ffcac4 100644 --- a/app/Storage/RequestStore.php +++ b/app/Storage/RequestStore.php @@ -17,9 +17,10 @@ public function find(Token $token, $requestId); * @param Token $token * @param int $page * @param int $perPage + * @param string $sorting * @return Collection */ - public function all(Token $token, $page = 1, $perPage = 50); + public function all(Token $token, $page = 1, $perPage = 50, $sorting = "oldest"); /** * @param Token $token From f6e402e6f06504b78409087dbd6784dec1c707d1 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 15:07:53 +0800 Subject: [PATCH 05/12] test: sorting test --- tests/RequestControllerTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/RequestControllerTest.php b/tests/RequestControllerTest.php index 89ed5a27..65895ef8 100644 --- a/tests/RequestControllerTest.php +++ b/tests/RequestControllerTest.php @@ -65,4 +65,37 @@ public function testPagination() 'to' => 175, ]); } + + public function testSorting() { + // Prevent throttling + $this->withoutMiddleware(); + + $number = 175; + + $tokenId = $this->json('POST', 'token')->json()['uuid']; + + for ($i = 0; $i < $number; $i++) { + $this->call('GET', $tokenId); + } + + $requests = $this->json('GET', "token/$tokenId/requests?sorting=newest"); + + $requests->assertJson([ + 'total' => $number, + 'per_page' => 50, + 'current_page' => 1, + 'is_last_page' => false, + 'from' => 1, + 'to' => 50, + ]); + + $data = $requests->json()['data']; + + // assert that the first request is the newest + $timestamps = array_column($data, 'created_at'); + $sortedTimestamps = $timestamps; + rsort($sortedTimestamps); + + $this->assertSame($sortedTimestamps, $timestamps, "The 'created_at' field is not sorted in descending order."); + } } From e3de4177e9fe8e43a6b94465fff53b032d24cd2d Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 15:18:50 +0800 Subject: [PATCH 06/12] Revert "feat: docker entrypoint to replace nginx environment variable" This reverts commit 7145a4111ed8e0a51e9a1d1b67e40674af9dd234. --- Dockerfile | 5 +---- docker-entrypoint.sh | 10 ---------- nginx.conf.template => nginx.conf | 2 +- 3 files changed, 2 insertions(+), 15 deletions(-) delete mode 100755 docker-entrypoint.sh rename nginx.conf.template => nginx.conf (97%) diff --git a/Dockerfile b/Dockerfile index bd3a545e..62f1cfd0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,7 @@ FROM bkuhl/fpm-nginx:7.3 WORKDIR /var/www/html # Contains laravel echo server proxy configuration -COPY /nginx.conf.template /etc/nginx +COPY /nginx.conf /etc/nginx/conf.d USER www-data @@ -49,7 +49,4 @@ ADD --chown=www-data:www-data /resources /var/www/html/resources COPY --chown=www-data:www-data --from=npm /app/public/css /var/www/html/public/css COPY --chown=www-data:www-data --from=npm /app/public/js /var/www/html/public/js -COPY docker-entrypoint.sh / -ENTRYPOINT ["/docker-entrypoint.sh"] - USER root \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh deleted file mode 100755 index 6de0937b..00000000 --- a/docker-entrypoint.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env sh -set -eu - -envsubst '${LARAVEL_ECHO_SERVER_HOST}' < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/nginx.conf - -# Start PHP-FPM in the background -php-fpm -D - -# Start Nginx in the foreground -exec nginx -g 'daemon off;' diff --git a/nginx.conf.template b/nginx.conf similarity index 97% rename from nginx.conf.template rename to nginx.conf index d5f095f2..b43aa54d 100644 --- a/nginx.conf.template +++ b/nginx.conf @@ -24,7 +24,7 @@ server { # socket.io location /socket.io { - proxy_pass $LARAVEL_ECHO_SERVER_HOST; + proxy_pass $laravel_echo_server_host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; From 2c8a7b8a786a0ee59a0ce4ca917cab5c2cf6e04c Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 15:18:57 +0800 Subject: [PATCH 07/12] Revert "chore: remove unused map" This reverts commit 27b0ce881e0b004879e09c0eaaa915d9431774df. --- docker-compose.yml | 2 ++ nginx-template-variables | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 nginx-template-variables diff --git a/docker-compose.yml b/docker-compose.yml index c0365b5c..22aa9fb2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,6 +23,8 @@ services: - LARAVEL_ECHO_SERVER_HOST=http://laravel-echo-server:6001 depends_on: - redis + volumes: + - ./nginx-template-variables:/etc/nginx/templates/10-variables.conf.template:ro redis: container_name: "webhook-redis" diff --git a/nginx-template-variables b/nginx-template-variables new file mode 100644 index 00000000..0fae1683 --- /dev/null +++ b/nginx-template-variables @@ -0,0 +1,3 @@ +map $host $laravel_echo_server_host { + default "$LARAVEL_ECHO_SERVER_HOST"; +} \ No newline at end of file From 97c47b8823d386ae5b19e41fa775f47c58c6e2c4 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 15:19:08 +0800 Subject: [PATCH 08/12] Revert "feat: use environment variable for LARAVEL_ECHO_SERVER_HOST" This reverts commit 882624a7df86cf0fd694a5541522718e07ef5c6e. --- docker-compose.yml | 3 --- nginx-template-variables | 3 --- nginx.conf | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 nginx-template-variables diff --git a/docker-compose.yml b/docker-compose.yml index 22aa9fb2..ff3f76de 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,11 +20,8 @@ services: - CACHE_DRIVER=redis - QUEUE_DRIVER=redis - ECHO_HOST_MODE=path - - LARAVEL_ECHO_SERVER_HOST=http://laravel-echo-server:6001 depends_on: - redis - volumes: - - ./nginx-template-variables:/etc/nginx/templates/10-variables.conf.template:ro redis: container_name: "webhook-redis" diff --git a/nginx-template-variables b/nginx-template-variables deleted file mode 100644 index 0fae1683..00000000 --- a/nginx-template-variables +++ /dev/null @@ -1,3 +0,0 @@ -map $host $laravel_echo_server_host { - default "$LARAVEL_ECHO_SERVER_HOST"; -} \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index b43aa54d..8bc992c1 100644 --- a/nginx.conf +++ b/nginx.conf @@ -24,7 +24,7 @@ server { # socket.io location /socket.io { - proxy_pass $laravel_echo_server_host; + proxy_pass http://laravel-echo-server:6001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; From a93efae11256148cf74e4df7df71e3fdb8598681 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 16:13:52 +0800 Subject: [PATCH 09/12] fix: use single quote --- app/Storage/Redis/RequestStore.php | 4 ++-- app/Storage/RequestStore.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Storage/Redis/RequestStore.php b/app/Storage/Redis/RequestStore.php index ebba65ae..526649d8 100644 --- a/app/Storage/Redis/RequestStore.php +++ b/app/Storage/Redis/RequestStore.php @@ -50,7 +50,7 @@ public function find(Token $token, $requestId) * @param string $sort * @return Collection|static */ - public function all(Token $token, $page = 1, $perPage = 50, $sorting = "oldest") + public function all(Token $token, $page = 1, $perPage = 50, $sorting = 'oldest') { $requests = collect( $this->redis->hgetall(Request::getIdentifier($token->uuid)) @@ -62,7 +62,7 @@ function ($request) { } ); - if ($sorting === "newest") { + if ($sorting === 'newest') { $requests = $requests->sortByDesc( function ($request) { return Carbon::createFromFormat( diff --git a/app/Storage/RequestStore.php b/app/Storage/RequestStore.php index 40ffcac4..7c29c456 100644 --- a/app/Storage/RequestStore.php +++ b/app/Storage/RequestStore.php @@ -20,7 +20,7 @@ public function find(Token $token, $requestId); * @param string $sorting * @return Collection */ - public function all(Token $token, $page = 1, $perPage = 50, $sorting = "oldest"); + public function all(Token $token, $page = 1, $perPage = 50, $sorting = 'oldest'); /** * @param Token $token From 43f6de1a149e0b4d00e2466f38a9672b1c181962 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Wed, 19 Mar 2025 16:23:52 +0800 Subject: [PATCH 10/12] test: sorting oldest --- tests/RequestControllerTest.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/RequestControllerTest.php b/tests/RequestControllerTest.php index 65895ef8..78e0fe04 100644 --- a/tests/RequestControllerTest.php +++ b/tests/RequestControllerTest.php @@ -78,6 +78,7 @@ public function testSorting() { $this->call('GET', $tokenId); } + // Test newest $requests = $this->json('GET', "token/$tokenId/requests?sorting=newest"); $requests->assertJson([ @@ -90,12 +91,27 @@ public function testSorting() { ]); $data = $requests->json()['data']; - - // assert that the first request is the newest $timestamps = array_column($data, 'created_at'); - $sortedTimestamps = $timestamps; - rsort($sortedTimestamps); + $sortedDescTimestamps = $timestamps; + rsort($sortedDescTimestamps); + $this->assertSame($sortedDescTimestamps, $timestamps, "The 'created_at' field is not sorted in descending order."); + + // Test oldest + $requests = $this->json('GET', "token/$tokenId/requests?sorting=oldest"); - $this->assertSame($sortedTimestamps, $timestamps, "The 'created_at' field is not sorted in descending order."); + $requests->assertJson([ + 'total' => $number, + 'per_page' => 50, + 'current_page' => 1, + 'is_last_page' => false, + 'from' => 1, + 'to' => 50, + ]); + + $data = $requests->json()['data']; + $timestamps = array_column($data, 'created_at'); + $sortedAscTimestamps = $timestamps; + sort($sortedAscTimestamps); + $this->assertSame($sortedAscTimestamps, $timestamps,'The "created_at" field is not sorted in ascending order.'); } } From 7be54d0d6dca4af669de8027f37ab3052155bbb8 Mon Sep 17 00:00:00 2001 From: Edrich Hans Chua Date: Thu, 20 Mar 2025 09:57:25 +0800 Subject: [PATCH 11/12] refactor: optimize sort syntax Co-authored-by: Simon Fredsted --- app/Storage/Redis/RequestStore.php | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/app/Storage/Redis/RequestStore.php b/app/Storage/Redis/RequestStore.php index 526649d8..8c3d2d1c 100644 --- a/app/Storage/Redis/RequestStore.php +++ b/app/Storage/Redis/RequestStore.php @@ -62,27 +62,15 @@ function ($request) { } ); - if ($sorting === 'newest') { - $requests = $requests->sortByDesc( - function ($request) { - return Carbon::createFromFormat( - 'Y-m-d H:i:s', - $request->created_at - )->getTimestamp(); - }, - SORT_DESC - ); - } else { - $requests = $requests->sortBy( - function ($request) { - return Carbon::createFromFormat( - 'Y-m-d H:i:s', - $request->created_at - )->getTimestamp(); - }, - SORT_DESC - ); - } + $requests = $requests->sortBy( + function ($request) { + return Carbon::createFromFormat( + 'Y-m-d H:i:s', + $request->created_at + )->getTimestamp(); + }, + $sorting === 'newest' ? SORT_DESC : SORT_ASC + ); return $requests->forPage( $page, From af93ba4eea784412d765631beb264ea769d5d778 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Thu, 20 Mar 2025 10:06:58 +0800 Subject: [PATCH 12/12] fix: shortened sortBy options --- app/Storage/Redis/RequestStore.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Storage/Redis/RequestStore.php b/app/Storage/Redis/RequestStore.php index 8c3d2d1c..01ba1276 100644 --- a/app/Storage/Redis/RequestStore.php +++ b/app/Storage/Redis/RequestStore.php @@ -69,7 +69,8 @@ function ($request) { $request->created_at )->getTimestamp(); }, - $sorting === 'newest' ? SORT_DESC : SORT_ASC + SORT_REGULAR, + $sorting === 'newest' ); return $requests->forPage(