From 0abe3cbc93c854ca703bab13846bfb7c6ad8f1a1 Mon Sep 17 00:00:00 2001 From: kocsismate Date: Sun, 4 Nov 2018 15:09:32 +0100 Subject: [PATCH] Add webserver for the examples --- .env.dist | 1 + .gitignore | 1 + Makefile | 27 ++++++++++++++++++ README.md | 56 ++++++++++++++++++++++++------------- build/nginx.conf | 11 ++++++++ build/nginx.sh | 12 ++++++++ build/site.conf | 21 ++++++++++++++ docker-compose.examples.yml | 28 +++++++++++++++++++ examples/index.php | 9 ++---- 9 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 .env.dist create mode 100755 Makefile create mode 100644 build/nginx.conf create mode 100755 build/nginx.sh create mode 100644 build/site.conf create mode 100644 docker-compose.examples.yml diff --git a/.env.dist b/.env.dist new file mode 100644 index 00000000..46d62796 --- /dev/null +++ b/.env.dist @@ -0,0 +1 @@ +HOST_WEB_PORT=8080 diff --git a/.gitignore b/.gitignore index c49a5d8d..7fa23fc4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/ composer.lock phpunit.xml +.env diff --git a/Makefile b/Makefile new file mode 100755 index 00000000..8787098c --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +.PHONY: help +.DEFAULT_GOAL := help + +$(VERBOSE).SILENT: + +help: + grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ + cut -d: -f2- | \ + sort -d | \ + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}' + +.PHONY: up down composer-install composer-update test + +up: + docker-compose -f docker-compose.examples.yml stop --timeout=2 && docker-compose -f docker-compose.examples.yml up -d + +down: + docker-compose -f docker-compose.examples.yml stop --timeout=2 + +composer-install: + docker run --rm --interactive --tty --volume $(PWD):/app --user $(id -u):$(id -g) composer install --ignore-platform-reqs + +composer-update: + docker run --rm --interactive --tty --volume $(PWD):/app --user $(id -u):$(id -g) composer update --ignore-platform-reqs + +test: + docker-compose -f docker-compose.yml up diff --git a/README.md b/README.md index 75842f3e..6f404f1c 100644 --- a/README.md +++ b/README.md @@ -1159,25 +1159,43 @@ public function updateBookRelationship(JsonApi $jsonApi): ResponseInterface ``` ### How to try it out -If you want to know more about how Yin works, have a look at the -[examples](https://github.com/woohoolabs/yin/tree/master/examples). Set up a web server, run `composer install` in -Yin's root directory and visit the URLs listed below. You can restrict the retrieved fields and relationships with -the `fields` and `include` parameters as specified by JSON:API. - -Example URLs for the book resources: -- `GET examples/?path=/books/1`: Fetch a book -- `GET examples/?path=/books/1/relationships/authors`: Fetch the authors relationship -- `GET examples/?path=/books/1/relationships/publisher`: Fetch the publisher relationship -- `GET examples/?path=/books/1/authors`: Fetch the authors of a book -- `POST examples/?path=/books`: Create a new book -- `PATCH examples/?path=/books/1`: Update a book -- `PATCH examples/?path=/books/1/relationships/author`: Update the authors of the book -- `PATCH examples/?path=/books/1/relationships/publisher`: Update the publisher of the book - -Example URLs for the user resources: -- `GET examples/?path=/users`: Fetch users -- `GET examples/?path=/users/1`: Fetch a user -- `GET examples/?path=/users/1/relationships/contacts`: Fetch the contacts relationship + +If you want to see how Yin works, have a look at the [examples](https://github.com/woohoolabs/yin/tree/master/examples). +If `docker-compose` and `make` is available on your system, then just run the following commands in order to try out the +example API: + +```bash +cp .env.dist .env # You can now edit the settings in the .env file +make composer-install # Install the Composer dependencies +make up # Start the webserver +``` + +And finally, just visit the following URL: `localhost:8080`. You can even restrict the retrieved fields and relationships +via the `fields` and `include` parameters as specified by JSON:API. + +Example URIs for the book examples: +- `GET /books/1`: Fetch a book +- `GET /books/1/relationships/authors`: Fetch the authors relationship +- `GET /books/1/relationships/publisher`: Fetch the publisher relationship +- `GET /books/1/authors`: Fetch the authors of a book +- `POST /books`: Create a new book +- `PATCH /books/1`: Update a book +- `PATCH /books/1/relationships/author`: Update the authors of the book +- `PATCH /books/1/relationships/publisher`: Update the publisher of the book + +Example URIs for the user examples: +- `GET /users`: Fetch users +- `GET /users/1`: Fetch a user +- `GET /users/1/relationships/contacts`: Fetch the contacts relationship + +When you finished your work, simply stop the webserver: + +```bash +make down +``` + +If the prerequisites are not available for you, you have to set up a webserver, and install PHP on your host system as +well as the dependencies via `Composer`. ## Integrations diff --git a/build/nginx.conf b/build/nginx.conf new file mode 100644 index 00000000..5b9c18b6 --- /dev/null +++ b/build/nginx.conf @@ -0,0 +1,11 @@ +daemon off; +worker_processes auto; +pid /run/nginx.pid; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/conf.d/*.conf; +} diff --git a/build/nginx.sh b/build/nginx.sh new file mode 100755 index 00000000..be77ba63 --- /dev/null +++ b/build/nginx.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -e + +export DOLLAR='$' + +rm -f /etc/nginx/conf.d/* +rm -Rf /var/www/html + +envsubst < /var/www/build/nginx.conf > /etc/nginx/nginx.conf +envsubst < /var/www/build/site.conf > /etc/nginx/conf.d/20-site.conf + +nginx diff --git a/build/site.conf b/build/site.conf new file mode 100644 index 00000000..af8bb01e --- /dev/null +++ b/build/site.conf @@ -0,0 +1,21 @@ +server { + listen 80; + + server_name _; + root /var/www/examples; + index index.php; + + access_log off; + log_not_found off; + + location / { + try_files ${DOLLAR}uri /index.php${DOLLAR}is_args${DOLLAR}args; + } + + location ~ \.php$ { + fastcgi_pass yin-examples-php-fpm:9000; + internal; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME ${DOLLAR}document_root${DOLLAR}fastcgi_script_name; + } +} diff --git a/docker-compose.examples.yml b/docker-compose.examples.yml new file mode 100644 index 00000000..31d3d89f --- /dev/null +++ b/docker-compose.examples.yml @@ -0,0 +1,28 @@ +version: '2' + +services: + yin-examples-nginx: + image: nginx:1.15-alpine + container_name: yin-examples-nginx + networks: + - yin-examples + volumes: + - .:/var/www/ + ports: + - "$HOST_WEB_PORT:80" + command: /var/www/build/nginx.sh + env_file: + - .env + depends_on: + - yin-examples-php-fpm + + yin-examples-php-fpm: + container_name: yin-examples-php-fpm + image: php:7.2-fpm + networks: + - yin-examples + volumes: + - .:/var/www/ + +networks: + yin-examples: diff --git a/examples/index.php b/examples/index.php index 66ce3bab..5d1f77d2 100644 --- a/examples/index.php +++ b/examples/index.php @@ -94,14 +94,9 @@ function findRoute(Request $request, array $routes): Request { - $queryParams = $request->getQueryParams(); - if (isset($queryParams["path"]) === false) { - die("You must provide the 'path' query parameter!"); - } - + $path = $request->getUri()->getPath(); $method = $request->getMethod(); - $path = $queryParams["path"]; - $requestLine = $method . " " . $path; + $requestLine = "$method $path"; foreach ($routes as $pattern => $route) { $matches = [];