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

Alex dolgov/hw5 #576

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
COMPOSE_FILE=docker/docker-compose.yaml:docker/docker-compose.yaml
MYSQL_ROOT_PASSWORD=
MYSQL_DATABASE=
MYSQL_USER=
MYSQL_PASSWORD=
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
vendor
mysql-data
.idea
composer.lock
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "aleksandrdolgov/otus_php_2023",
"type": "project",
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.7"
}
}
90 changes: 90 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
version: '3'

services:
nginx-root:
image: nginx:stable-alpine
container_name: nginx-root
working_dir: /app
ports:
- "80:80"
networks:
- app-network
volumes:
- ../docker/nginx-root/application.local.conf:/etc/nginx/conf.d/application.local.conf

nginx-child-1:
image: nginx:stable-alpine
container_name: nginx-child-1
working_dir: /app
volumes:
- ../docker/nginx-child/application.local.conf:/etc/nginx/conf.d/application.local.conf
- ../app:/app
networks:
- app-network

nginx-child-2:
image: nginx:stable-alpine
container_name: nginx-child-2
working_dir: /app
volumes:
- ../docker/nginx-child/application.local.conf:/etc/nginx/conf.d/application.local.conf
- ..app:/app
networks:
- app-network

nginx-child-3:
image: nginx:stable-alpine
container_name: nginx-child-3
working_dir: /app
volumes:
- ../docker/nginx-child/application.local.conf:/etc/nginx/conf.d/application.local.conf
- ..app:/app
networks:
- app-network

memcached:
image: memcached:latest
ports:
- "11211:11211"
container_name: app-memcached
networks:
- app-network

php-fpm-1:
build:
context: ./..
dockerfile: docker/fpm/Dockerfile
container_name: php-fpm-1
volumes:
- ../:/app
working_dir: /app
networks:
- app-network

# second php-fpm
php-fpm-2:
build:
context: ./..
dockerfile: docker/fpm/Dockerfile
container_name: php-fpm-2
volumes:
- ../:/app
working_dir: /app
networks:
- app-network

# third php-fpm
php-fpm-3:
build:
context: ./..
dockerfile: docker/fpm/Dockerfile
container_name: php-fpm-3
volumes:
- ../:/app
working_dir: /app
networks:
- app-network

networks:
app-network:
driver: bridge
36 changes: 36 additions & 0 deletions docker/fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM php:8.2-fpm

# ставим необходимые для нормальной работы модули
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libonig-dev \
libzip-dev \
libmemcached11 \
zlib1g-dev libssl-dev \
libmemcached-dev \
libmcrypt-dev \
&& pecl install mcrypt \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-install -j$(nproc) iconv mbstring mysqli pdo_mysql zip \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& chmod +x /usr/local/bin/install-php-extensions \
&& install-php-extensions redis \
&& install-php-extensions @composer-2.2.0 \
&& install-php-extensions memcached

COPY docker/fpm/php.ini /usr/local/etc/php/conf.d/php-custom.ini

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

USER www-data:www-data

COPY composer.json composer.json

RUN composer install

CMD ["php-fpm"]
2 changes: 2 additions & 0 deletions docker/fpm/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
session.save_handler = memcache
session.save_path = "tcp://memcache:11211"
11 changes: 11 additions & 0 deletions docker/nginx-child/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM nginx

COPY ./application.local.conf /etc/nginx/conf.d/application.local.conf

WORKDIR /app

VOLUME /app

EXPOSE 7878

#CMD ["nginx", "-g", "daemon off;"]
25 changes: 25 additions & 0 deletions docker/nginx-child/application.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
upstream backend {
least_conn;
server php-fpm-1:9000;
server php-fpm-2:9000;
server php-fpm-3:9000;
}

server {
listen 80;

root /app/public;
index index.php index.html;

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

# и наконец правило обращения к php-fpm
location ~* .php$ {
fastcgi_pass backend;
try_files $uri = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
11 changes: 11 additions & 0 deletions docker/nginx-root/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#FROM nginx
#
#COPY ./application.local.conf /etc/nginx/conf.d/application.local.conf
#
#WORKDIR /app
#
#VOLUME /app
#
#EXPOSE 80

#CMD ["nginx", "-g", "daemon off;"]
14 changes: 14 additions & 0 deletions docker/nginx-root/application.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
upstream nginx-balancer{
server nginx-child-1:80;
server nginx-child-2:80;
server nginx-child-3:80;
}

server {
listen 80;
server_name application.local;

location / {
proxy_pass http://nginx-balancer;
}
}
10 changes: 10 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use App\AppValidator;

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

$app = new AppValidator();
$app->runApp();
36 changes: 36 additions & 0 deletions src/AppValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App;

use App\Validators\BracketValidator;
use Exception;

class AppValidator
{
public function runApp(): void
{
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception('Use POST method', 400);
}

$string = $_POST['string'] ?? '';

if (empty($string)) {
throw new Exception('Send me not empty "string" param', 400);
}

if (BracketValidator::validateString($string)) {
echo "<h1>This is " . $_SERVER['HOSTNAME'] . "</h1>";
echo sprintf('Валидация скобок в строке "<b>%s</b>" прошла успешно', $string);
phpinfo();
} else {
echo sprintf('Строка "<b>%s</b>" не прошла валидацию на скобки', $string);
}
} catch (Exception $exception) {
header("HTTP/1.1 {$exception->getCode()} {$exception->getMessage()}");
}
}
}
17 changes: 17 additions & 0 deletions src/Validators/BracketValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Validators;

class BracketValidator
{
public static function validateString(string $string): bool
{
if (preg_match('/^(\((?1)*\))*$/', $string)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно просто return preg_match('/^(\((?1)*\))*$/', $string)

return true;
} else {
return false;
}
}
}
Loading