Skip to content

Commit

Permalink
Add lumen example app
Browse files Browse the repository at this point in the history
added . files

Update composer.json

Fix for folder rename in composer

Rename to PYR

Pyr prefix and grouping

Change in description

Fix redis prefix change

restore history fixes

Fix for env vars on start/stop commands

Readme modifications
  • Loading branch information
Maria Katsamperi committed Apr 12, 2019
1 parent f889fb1 commit d5e0190
Show file tree
Hide file tree
Showing 58 changed files with 2,261 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/examples export-ignore
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ vendor
bin
coverage
coverage.xml
.php_cs.cache
.php_cs.cache
examples/lumen-app/vendor
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,12 @@ class ExampleCollector implements CollectorInterface
}
}
```

## Example

Head to `examples/lumen-app` to check out the included example application.
To get it you'll have to clone the `Pyr` repo, as it's not included when
downloaded from composer.

The example is a full project containing it's own `README.md` so you can check the
library's functionality and the way it's intended to be used.
8 changes: 8 additions & 0 deletions examples/lumen-app/.env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
EXAMPLE_APP_NGINX_PORT=8080
EXAMPLE_APP_REDIS_PORT=6479
EXAMPLE_APP_MYSQL_PORT=3326
EXAMPLE_APP_PROMETHEUS_PORT=9290
EXAMPLE_APP_PUSH_GATEWAY_PORT=9291
EXAMPLE_APP_GRAFANA_PORT=3001

TIMES_TO_CALL_EXAMPLE_ROUTE=100
21 changes: 21 additions & 0 deletions examples/lumen-app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost:8080
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

DB_CONNECTION=mysql
DB_HOST=my-sql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_CONNECTION=sync

PYR_STORAGE_ADAPTER=apc
5 changes: 5 additions & 0 deletions examples/lumen-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
83 changes: 83 additions & 0 deletions examples/lumen-app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
help:
@echo "Please use 'make <target>' where <target> is one of the following:"
@echo " up to create and start project services."
@echo " start to start project services."
@echo " down to stop and remove all project services."
@echo " stop to stop project services."
@echo " rebuild to destroy the example app container and rebuild from Dockerfile."
@echo " provision to provision Grafana datasources and dashboards"
@echo " generate-links to generate the possible services links"
@echo " create-db to create the database."
@echo " create-db to create the database."
@echo " migrate to run db migrations."
@echo " update to perform Composer update."
@echo " call to perform a number of requests to a default test route."

SHELL := /bin/bash
up:
set -a ;\
source .env.docker ;\
docker-compose up -d ;\
make setup-env ;\
make install ;\
make create-db ;\
make migrate ;\
make provision ;\
make generate-links ;

SHELL := /bin/bash
start:
set -a ;\
source .env.docker ;\
docker-compose start
make setup-env
make create-db
make migrate
make generate-links

SHELL := /bin/bash
stop:
set -a ;\
source .env.docker ;\
docker-compose stop

SHELL := /bin/bash
down:
set -a ;\
source .env.docker ;\
docker-compose down

SHELL := /bin/bash
rebuild:
make down
docker rmi -f lumen-example-app_app
set -a ;\
source .env.docker ;\
docker-compose up -d ;\

provision:
docker exec -it lumen-example-app scripts/grafana_provision.sh

generate-links:
docker exec -it lumen-example-app scripts/url_print.sh

create-db:
docker exec lumen-example-app php artisan db:create

setup-env:
docker exec lumen-example-app cp .env.example .env

migrate:
docker exec -it lumen-example-app php artisan migrate

update:
docker exec -it lumen-example-app composer update

install:
docker exec -it lumen-example-app composer install

SHELL := /bin/bash
call:
set -a ;\
source .env.docker ;\
sh scripts/call_test_route.sh ;\
107 changes: 107 additions & 0 deletions examples/lumen-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Test Application
This application was created in order to be able to test various aspects
and functionality of the prometheus exporter project. It may also act as
a dockerized test-bed installation of Lumen.

![test application diagram](https://github.com/erifili117/lumen-example-app/blob/master/docs/example_app.png)

## Installation
In order to run this application you'll need [Docker](https://docs.docker.com/install/)
along with [docker-compose](https://docs.docker.com/compose/install/).
For some functions you will also need cURL.

## Usage
The most commands needed exist in the `Makefile`. This includes
common commands for Docker, as well as commands for extra functionality,
like printing url of the available services to stdout.

### Basic usage

- To run this project and test the results you simply need to run `$ make up`,

- head to `localhost:8080/test` (default port) make a few requests or use:

```bash
$ make call
```

`call` will perform a number of requests via curl. Default is 100 requests.
If you wish to change the number head to `.env.docker` and set `TIMES_TO_CALL_EXAMPLE_ROUTE`
to your preference.

- The last step is to open Grafana and see your metrics!
Grafana listens to `localhost:3326` (default port).

### Useful commands

- To print all the available commands
```bash
$ make help
```

- To create and start the application services run:
```bash
$ make up
```

- To start the application services run:
```bash
$ make start
```

- To stop the application services run:
```bash
$ make stop
```

- To completely remove the application services run:
```bash
$ make down
```

### Change application ports

The ports registered in the example application might be already
taken. If you happen to have conflicts you need to change the port having
conflicts in `.env.docker`.

E.g.
The Grafana port is registered at `3326`

```dotenv
EXAMPLE_APP_GRAFANA_PORT=3326
```

You'll need to change it as:

```dotenv
EXAMPLE_APP_GRAFANA_PORT=3111
```

### Rebuild the example app container

To remove the built image and create it again:
```bash
$ make rebuild
```

### Calling the route

The example route for testing is at `localhost:8080/test`

Each time you perform `make up` all the services get printed.
To print all the available services use:

```bash
$ make generate-links
```

### Updating Grafana

Each time you perform `make up` the `dashboard.json` Grafana dashboards along with
the available data-sources, get updated.
To perform the update by yourself use:

```bash
$ make provision
```
Empty file.
88 changes: 88 additions & 0 deletions examples/lumen-app/app/Console/Commands/DatabaseCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use PDO;
use PDOException;

class DatabaseCreate extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'db:create';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This command creates a new database';

/**
* The console command signature.
*
* @var string
*/
protected $signature = 'db:create';

/**
* Execute the console command.
*/
public function handle()
{
$defaultConnection = config('database.default');
$database = config("database.connections.{$defaultConnection}.database");
$host = config("database.connections.{$defaultConnection}.host");
$port = config("database.connections.{$defaultConnection}.port");
$username = config("database.connections.{$defaultConnection}.username");
$password = config("database.connections.{$defaultConnection}.password");

if (App::environment() === 'production') {
$this->error('Command supports only local and testing environments!');

return;
}

if (!$database) {
$this->info('Skipping creation of database as env(DB_DATABASE) is empty');

return;
}

try {
$pdo = $this->getPDOConnection($host, $port, $username, $password);

$res = $pdo->exec(sprintf(
'CREATE DATABASE IF NOT EXISTS %s',
$database
));

if ($res) {
$this->info(sprintf('Successfully created %s database', $database));

return;
}
throw new PDOException;
} catch (PDOException $exception) {
$this->error(sprintf('Failed to create %s database, %s', $database, $exception->getMessage()));
}
}

/**
* @param string $host
* @param int $port
* @param string $username
* @param string $password
*
* @return PDO
*/
private function getPDOConnection($host, $port, $username, $password)
{
return new PDO(sprintf('mysql:host=%s;port=%d;', $host, $port), $username, $password);
}
}
29 changes: 29 additions & 0 deletions examples/lumen-app/app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\DatabaseCreate::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}
10 changes: 10 additions & 0 deletions examples/lumen-app/app/Events/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Events;

use Illuminate\Queue\SerializesModels;

abstract class Event
{
use SerializesModels;
}
Loading

0 comments on commit d5e0190

Please sign in to comment.