Skip to content

Commit

Permalink
Add webserver for the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 4, 2018
1 parent 01d5bd4 commit 0abe3cb
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HOST_WEB_PORT=8080
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
phpunit.xml
.env
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
56 changes: 37 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions build/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
daemon off;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/conf.d/*.conf;
}
12 changes: 12 additions & 0 deletions build/nginx.sh
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions build/site.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions docker-compose.examples.yml
Original file line number Diff line number Diff line change
@@ -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:
9 changes: 2 additions & 7 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 0abe3cb

Please sign in to comment.