From 9fdcf28b55ba9156170c60c438a84fa668832277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 2 Jul 2024 23:15:48 +0200 Subject: [PATCH] feat: X-Sendfile example --- Caddyfile | 25 ++++++++++++------- README.md | 1 + benchmark.Caddyfile | 16 ++++++++++++ config/packages/framework.yaml | 2 ++ ...epageController.php => MainController.php} | 12 ++++++++- static-build.Dockerfile | 1 - 6 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 benchmark.Caddyfile rename src/Controller/{HomepageController.php => MainController.php} (50%) diff --git a/Caddyfile b/Caddyfile index 8dd9aec..a75c0a4 100644 --- a/Caddyfile +++ b/Caddyfile @@ -1,18 +1,25 @@ { - # This file is only used for dev and benchmarking. - # Please see /etc/Caddyfile in the container, for the one that is actual used in this demo - - #debug frankenphp { worker ./public/index.php } } localhost { - #log - route { - root * public/ - #php_fastcgi 127.0.0.1:9000 - php_server + log + + root * public/ + + request_header X-Sendfile-Type x-accel-redirect + intercept { + @sendfile header X-Accel-Redirect * + handle_response @sendfile { + root * private-files/ + rewrite * {resp.header.X-Accel-Redirect} + method * GET + header -X-Accel-Redirect + file_server + } } + + php_server } diff --git a/README.md b/README.md index 21ca29b..2df2810 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Run the project with Docker (worker mode): ```console docker run \ -e FRANKENPHP_CONFIG="worker ./public/index.php" \ + -v $PWD/Caddyfile:/etc/caddy/Caddyfile \ -v $PWD:/app \ -p 80:80 -p 443:443/tcp -p 443:443/udp \ --name FrankenPHP-demo \ diff --git a/benchmark.Caddyfile b/benchmark.Caddyfile new file mode 100644 index 0000000..2dbd104 --- /dev/null +++ b/benchmark.Caddyfile @@ -0,0 +1,16 @@ +{ + # This file is only used for benchmarking. + # Please see Caddyfile, for the one that is actual used in this demo + + frankenphp { + worker ./public/index.php + } +} + +localhost { + route { + root * public/ + #php_fastcgi 127.0.0.1:9000 + php_server + } +} diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index 355bd4c..fbee5d2 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -4,6 +4,8 @@ framework: #csrf_protection: true handle_all_throwables: true + trust_x_sendfile_type_header: true + # Enables session support. Note that the session will ONLY be started if you read or write from it. # Remove or comment this section to explicitly disable session support. session: diff --git a/src/Controller/HomepageController.php b/src/Controller/MainController.php similarity index 50% rename from src/Controller/HomepageController.php rename to src/Controller/MainController.php index a86f07b..258521b 100644 --- a/src/Controller/HomepageController.php +++ b/src/Controller/MainController.php @@ -3,10 +3,11 @@ namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -class HomepageController extends AbstractController +class MainController extends AbstractController { #[Route("/", name: "homepage")] public function index(): Response @@ -15,4 +16,13 @@ public function index(): Response 'controller_name' => 'HomepageController', ]); } + + #[Route("/download-logo", name: "download_logo")] + public function downloadLogo(): BinaryFileResponse + { + $response = new BinaryFileResponse(__DIR__.'/../../private-files/frankenphp.png'); + $response->headers->set('Content-Type', 'image/png'); + + return $response; + } } diff --git a/static-build.Dockerfile b/static-build.Dockerfile index 4aadaca..63722ec 100644 --- a/static-build.Dockerfile +++ b/static-build.Dockerfile @@ -11,5 +11,4 @@ RUN echo APP_ENV=prod > .env.local ; \ # Build the static binary WORKDIR /go/src/app/ RUN EMBED=dist/app/ \ - PHP_EXTENSIONS=ctype,iconv,pdo_sqlite \ ./build-static.sh