Skip to content

Commit

Permalink
tec: Serve files with Nginx in development
Browse files Browse the repository at this point in the history
The PHP server was really slow to stop. It gets a lot better with Nginx.
  • Loading branch information
marienfressinaud committed Jul 26, 2023
1 parent e406d09 commit 6d59863
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
run: make lint

- name: Run mock_server (background)
run: php -t ${{ github.workspace }} -S 127.0.0.1:8001 ${{ github.workspace }}/tests/mock_server.php &
run: php -t ${{ github.workspace }} -S 127.0.0.1:8001 ${{ github.workspace }}/tests/mock_server/index.php &

- name: Run the test suite
run: make test
Expand Down
21 changes: 5 additions & 16 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ services:
context: .
dockerfile: Dockerfile.php
restart: unless-stopped
command: php -t ./public/ -S 0.0.0.0:8000 ./public/index.php
ports:
- "8000:8000"
volumes:
- ..:/var/www/html:z
- composer:/tmp
environment:
- SEED
- MOCK_HOST=http://mock_server:8001
- MOCK_HOST=http://nginx:8001
user: $USER
links:
- database
- mock_server

job_worker:
image: flusio_php:dev
Expand All @@ -32,21 +26,16 @@ services:
- ..:/var/www/html:z
- composer:/tmp
user: $USER
links:
- database

mock_server:
image: flusio_php:dev
build:
context: .
dockerfile: Dockerfile.php
nginx:
image: nginx:alpine
restart: unless-stopped
command: php -t . -S 0.0.0.0:8001 ./tests/mock_server.php
ports:
- "8000:8000"
- "8001:8001"
volumes:
- ..:/var/www/html:z
user: $USER
- ./nginx.conf:/etc/nginx/conf.d/default.conf:z

database:
image: postgres:13-alpine
Expand Down
43 changes: 43 additions & 0 deletions docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
server {
listen 8000;
server_name _;

root /var/www/html/public;
index index.html index.php;

error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}

location ~ index.php$ {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi.conf;
}
}

server {
listen 8001;
server_name _;

root /var/www/html/tests/mock_server;
index index.html index.php;

error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}

location ~ index.php$ {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi.conf;
}
}
2 changes: 1 addition & 1 deletion docs/technical_stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ for the PHP files. JS files are linted with [ESLint](https://eslint.org/docs/rul
and CSS files with [stylelint](https://stylelint.io/).

The development environment is powered by Docker and Docker Compose setting up
4 containers: a PHP development server, a job worker, a PostgreSQL database and
5 containers: a PHP server, a Nginx server, a job worker, a PostgreSQL database and
a Node container running Parcel in watch mode. See the [`docker/` folder](/docker/)
for more information.
19 changes: 0 additions & 19 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,6 @@
// Get the http information and create a Request
$request = \Minz\Request::initFromGlobals();

// In development mode, all the requests are redirected to this file. However,
// if the file exists, we want to serve it as-is, which is done by returning
// false. It could be less hacky with Nginx, but I'd like to avoid to add
// another dependency as long as possible.
// @see https://www.php.net/manual/features.commandline.webserver.php
$current_filepath = $_SERVER['PHP_SELF'];
if (\Minz\Configuration::$environment === 'development' && $current_filepath !== '/index.php') {
$public_path = $app_path . '/public';
$filepath = realpath($public_path . $current_filepath);

if ($filepath === false) {
$filepath = '';
}

if (str_starts_with($filepath, $public_path) && file_exists($filepath)) {
return false;
}
}

// Initialize the Application and execute the request to get a Response
try {
$application = new \flusio\Application();
Expand Down
9 changes: 5 additions & 4 deletions tests/mock_server.php → tests/mock_server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* The mock server allows to test HTTP requests without network. It is used
* with the mockHttpWith* methods from the MockHttpHelper.
*
* The mock server is started via the Docker Compose file in development and in
* the CI workflow on GitHub Actions with the following command:
* The mock server is served by Nginx via the Docker Compose file in
* development and in the CI workflow on GitHub Actions with the following
* command:
*
* php -t . -S 0.0.0.0:8001 ./tests/mock_server.php
* php -t . -S 0.0.0.0:8001 ./tests/mock_server/index.php
*
* The mock host is then declared in the configuration file of the test
* environment via the MOCK_HOST environment variable. It is passed to the
Expand Down Expand Up @@ -60,7 +61,7 @@
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/

$app_path = realpath(__DIR__ . '/..');
$app_path = realpath(__DIR__ . '/../..');

assert($app_path !== false);

Expand Down

0 comments on commit 6d59863

Please sign in to comment.