Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/IM-125 | Draft Tracing Demo #23

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UseCase52/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
42 changes: 42 additions & 0 deletions UseCase52/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM php:7.3-fpm
RUN apt-get update -y && apt-get install -y unzip procps
RUN apt-get install -y libgmp-dev
RUN apt-get install -y libpq-dev \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql
RUN ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/local/include/
RUN docker-php-ext-configure gmp
RUN docker-php-ext-install gmp
RUN docker-php-ext-install sockets
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis

ARG PROJECT_NAME=use_case_52

ARG INSTALL_XDEBUG=true
ARG COMPOSER_INSTALL=false

ENV PROJECT_DIR=/var/www/html/prefabfitness/${PROJECT_NAME}.neighborhoods.com
ENV IS_DOCKER=1

RUN usermod -u 1000 www-data
RUN mkdir -p $PROJECT_DIR
WORKDIR $PROJECT_DIR

COPY . $PROJECT_DIR

# Copy xdebug configration for remote debugging
COPY docker/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
COPY docker/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

RUN bash docker/build.sh \
--xdebug ${INSTALL_XDEBUG} \
--composer-install ${COMPOSER_INSTALL}

RUN if [ ! -d "data/cache/" ]; then mkdir -p data/cache/; fi
RUN chmod -R a+rw data/cache/

CMD ["php-fpm"]

EXPOSE 9000
Empty file added UseCase52/Logs/.gitkeep
Empty file.
26 changes: 26 additions & 0 deletions UseCase52/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use Case 52
### Generating a single listing endpoint

This use case demonstrates how to generate an HTTP endpoint using Prefab. Notice the only file inside `src` is a single file named `Listing.prefab.definition.yml`. For more information on how to create these files, see the [Prefab README](https://github.com/neighborhoods/Prefab/blob/5.x/README.md).

#### Getting Started
1. Navigate to the root of UseCase52 on your terminal
1. `composer install` to install the project dependencies
1. Run `./expose-nhds-tld`. This allows us to make local requests to the `nhds` top level domain in the test request below.
1. `docker-compose build && docker-compose up -d` to build and start the Nginx, Postgres, Redis, and PHP containers
1. `docker-compose exec prefab_fitness bash -c './bin/database_setup.sh'` to create the prefab_fitness database, run migrations, and seed the database with mock data
1. `./vendor/bin/prefab` to run Prefab and generate the Listing HTTP endpoint
- Note to Docker For Mac users: This command can be run either inside the container or on the host machine. However, filesystem access using Docker For Mac is often extremely slow. Since Prefab writes a large number of files to disk, running Prefab inside the Docker container can take a significant amount of time. Therefore, it is recommended you run Prefab on the host machine.

Your project is now ready! Notice you now have a `fab/` directory with `Listing` machinery under `fab/MV1` and general HTTP machinery under `fab/Prefab5`. You can now query your database over HTTP using
[Search Criteria](https://github.com/neighborhoods/Prefab/blob/5.x/README.md#search-criteria).

This project is configured to listen for requests to `use_case_52.local.nhds`. Here is an example cURL request to get all listings with an `id` less than 20:

```
curl -g -X GET \
'http://use_case_52.local.nhds/v1/listing/?searchCriteria[filters][0][glue]=and&searchCriteria[filters][0][field]=id&searchCriteria[filters][0][condition]=lt&searchCriteria[filters][0][values][0]=20'
```


View performance traces in the JaegerUI using http://localhost:16686/
38 changes: 38 additions & 0 deletions UseCase52/bin/create_database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

$databaseName = getenv('DATABASE_NAME');

$connection = new \Doctrine\DBAL\Connection(
[
'name' => $databaseName,
'adapter' => getenv('DATABASE_ADAPTER'),
'host' => getenv('DATABASE_HOST'),
'user' => getenv('DATABASE_USERNAME'),
'password' => getenv('DATABASE_PASSWORD'),
'port' => getenv('DATABASE_PORT'),
'charset' => 'utf8',
],
new \Doctrine\DBAL\Driver\PDOPgSql\Driver()
);

$testDbExistsQuery = $connection->prepare(
'SELECT datname FROM pg_database WHERE datname = :datname'
);

$testDbExistsQuery->execute([$databaseName]);
$exists = $testDbExistsQuery->fetch();

if (empty($exists)) {
print_r('Creating database ' . $databaseName . PHP_EOL);
$connection->exec(
"CREATE DATABASE " . $databaseName
);

exit(0);
}

exit(1);
5 changes: 5 additions & 0 deletions UseCase52/bin/database_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
./bin/create_database.php
if [ $? -eq 0 ]; then
./vendor/bin/phinx migrate;
./vendor/bin/phinx seed:run;
fi
36 changes: 36 additions & 0 deletions UseCase52/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "neighborhoods/prefab-fitness-use-case-52",
"description": "A standard Prefab use case.",
"type": "project",
"keywords": [
"prefab"
],
"config": {
"sort-packages": true
},
"require": {
"fzaninotto/faker": "^1.8",
"jukylin/jaeger-php": "^2.0",
"opentracing/opentracing": "1.0.0-beta5",
"neighborhoods/prefab": "^5.0",
"robmorgan/phinx": "^0.11.1"
},
"autoload": {
"psr-4": {
"Neighborhoods\\PrefabFitnessUseCase52\\": [
"src",
"fab"
]
}
},
"autoload-dev": {
"psr-4": {
"Neighborhoods\\PrefabFitnessUseCase52\\": [
"src/",
"test-fab/"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
1 change: 1 addition & 0 deletions UseCase52/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
development.config.php
2 changes: 2 additions & 0 deletions UseCase52/config/autoload/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local.php
*.local.php
28 changes: 28 additions & 0 deletions UseCase52/config/autoload/dependencies.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

return [
// Provides application-wide services.
// We recommend using fully-qualified class names whenever possible as
// service names.
'dependencies' => [
// Use 'aliases' to alias a service name to another service. The
// key is the alias name, the value is the service to which it points.
'aliases' => [
// Fully\Qualified\ClassOrInterfaceName::class => Fully\Qualified\ClassName::class,
],
// Use 'invokables' for constructor-less services, or services that do
// not require arguments to the constructor. Map a service name to the
// class name.
'invokables' => [
// Fully\Qualified\InterfaceName::class => Fully\Qualified\ClassName::class,
],
// Use 'factories' for services provided by callbacks/factory classes.
'factories' => [
// Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class,
\Zend\ProblemDetails\ProblemDetailsResponseFactory::class => \Neighborhoods\PrefabFitnessUseCase52\Prefab5\Zend\ProblemDetails\ProblemDetailsResponseFactoryFactory::class,
Neighborhoods\PrefabFitnessUseCase52\Prefab5\NewRelic\TransactionNameMiddleware::class => Neighborhoods\PrefabFitnessUseCase52\Prefab5\NewRelic\TransactionNameMiddleware\Factory::class,
],
],
];
35 changes: 35 additions & 0 deletions UseCase52/config/autoload/development.local.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Development-only configuration.
*
* Put settings you want enabled when under development mode in this file, and
* check it into your repository.
*
* Developers on your team will then automatically enable them by calling on
* `composer development-enable`.
*/

declare(strict_types=1);

use Zend\Expressive\Container;
use Zend\Expressive\Middleware\ErrorResponseGenerator;

return [
'dependencies' => [
'invokables' => [
],
'factories' => [
ErrorResponseGenerator::class => Container\WhoopsErrorResponseGeneratorFactory::class,
'Zend\Expressive\Whoops' => Container\WhoopsFactory::class,
'Zend\Expressive\WhoopsPageHandler' => Container\WhoopsPageHandlerFactory::class,
],
],

'whoops' => [
'json_exceptions' => [
'display' => true,
'show_trace' => true,
'ajax_only' => true,
],
],
];
12 changes: 12 additions & 0 deletions UseCase52/config/autoload/local.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Local configuration.
*
* Copy this file to `local.php` and change its settings as required.
* `local.php` is ignored by git and safe to use for local and sensitive data like usernames and passwords.
*/

declare(strict_types=1);

return [
];
25 changes: 25 additions & 0 deletions UseCase52/config/autoload/zend-expressive.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Zend\ConfigAggregator\ConfigAggregator;

return [
// Toggle the configuration cache. Set this to boolean false, or remove the
// directive, to disable configuration caching. Toggling development mode
// will also disable it by default; clear the configuration cache using
// `composer clear-config-cache`.
ConfigAggregator::ENABLE_CACHE => true,

// Enable debugging; typically used to provide debugging information within templates.
'debug' => false,

'zend-expressive' => [
// Provide templates for the error handling middleware to use when
// generating responses.
'error_handler' => [
'template_404' => 'error::404',
'template_error' => 'error::error',
],
],
];
42 changes: 42 additions & 0 deletions UseCase52/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use Zend\ConfigAggregator\ArrayProvider;
use Zend\ConfigAggregator\ConfigAggregator;
use Zend\ConfigAggregator\PhpFileProvider;

// To enable or disable caching, set the `ConfigAggregator::ENABLE_CACHE` boolean in
// `config/autoload/local.php`.
$cacheConfig = [
'config_cache_path' => 'data/cache/config-cache.php',
];

$aggregator = new ConfigAggregator([
\Zend\ProblemDetails\ConfigProvider::class,
\Zend\Expressive\Router\FastRouteRouter\ConfigProvider::class,
\Zend\HttpHandlerRunner\ConfigProvider::class,
// Include cache configuration
new ArrayProvider($cacheConfig),

\Zend\Expressive\Helper\ConfigProvider::class,
\Zend\Expressive\ConfigProvider::class,
\Zend\Expressive\Router\ConfigProvider::class,

// Swoole config to overwrite some services (if installed)
class_exists(\Zend\Expressive\Swoole\ConfigProvider::class)
? \Zend\Expressive\Swoole\ConfigProvider::class
: function(){ return[]; },
// Load application config in a pre-defined order in such a way that local settings
// overwrite global settings. (Loaded as first to last):
// - `global.php`
// - `*.global.php`
// - `local.php`
// - `*.local.php`
new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'),

// Load development config if it exists
new PhpFileProvider(realpath(__DIR__) . '/development.config.php'),
], $cacheConfig['config_cache_path']);

return $aggregator->getMergedConfig();
11 changes: 11 additions & 0 deletions UseCase52/config/container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use JSoumelidis\SymfonyDI\Config\Config;
use JSoumelidis\SymfonyDI\Config\ContainerFactory;

$config = require realpath(__DIR__) . '/config.php';
$factory = new ContainerFactory();

return $factory(new Config($config));
30 changes: 30 additions & 0 deletions UseCase52/config/development.config.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* File required to allow enablement of development mode.
*
* For use with the zf-development-mode tool.
*
* Usage:
* $ composer development-disable
* $ composer development-enable
* $ composer development-status
*
* DO NOT MODIFY THIS FILE.
*
* Provide your own development-mode settings by editing the file
* `config/autoload/development.local.php.dist`.
*
* Because this file is aggregated last, it simply ensures:
*
* - The `debug` flag is _enabled_.
* - Configuration caching is _disabled_.
*/

declare(strict_types=1);

use Zend\ConfigAggregator\ConfigAggregator;

return [
'debug' => true,
ConfigAggregator::ENABLE_CACHE => false,
];
Loading