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

Домашнее задание к уроку №5 #1184

Open
wants to merge 4 commits into
base: DMatrenin/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: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
.env
.env
/vendor/
code/composer.lock
code/vendor
21 changes: 21 additions & 0 deletions code/app/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Dmatrenin\Bracket;

use Exception;

class Application
{
public static function run()
{
try {
http_response_code(200);
return Validator::validate($_POST['string']);
} catch (Exception $e) {
http_response_code($e->getCode());
return $e->getMessage();
}
}
}
39 changes: 39 additions & 0 deletions code/app/Validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Dmatrenin\Bracket;

use Exception;

class Validator
{
/**
* @throws Exception
*/
public static function validate(string $bracket): string
{
$counter = 0;
$bracket = preg_replace('/[^()]/', '', $bracket);

if ($bracket === '') {
throw new Exception("Пустая строка", 400);
}

foreach (str_split($bracket) as $char) {
if ($char === '(') {
$counter++;
} elseif ($char === ')') {
$counter--;
}

if ($counter < 0) {
throw new Exception("Не хватает открывающей скобки", 400);
}
}

if ($counter > 0) {
throw new Exception("Не хватает зактывающей скобки", 400);
}

return $bracket;
}
}
15 changes: 15 additions & 0 deletions code/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "dmatrenin/bracket",
"type": "project",
"autoload": {
"psr-4": {
"Dmatrenin\\Bracket\\": "app"
}
},
"authors": [
{
"name": "Dmireiy Matrenin"
}
],
"require": {}
}
9 changes: 9 additions & 0 deletions code/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Dmatrenin\Bracket\Application;

require_once "../vendor/autoload.php";

echo Application::run();
80 changes: 80 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
version: '3.8'

services:
balancer:
container_name: balancer
build:
context: docker/balancer
dockerfile: Dockerfile
ports:
- 8080:80
networks:
- app-network
depends_on:
- nginx-1
- nginx-2

nginx-1:
container_name: nginx-1
build:
context: docker/nginx
dockerfile: Dockerfile
image: webserver/nginx
volumes:
- ./code:/data/application.local
networks:
- app-network
depends_on:
- app-1
- app-2

nginx-2:
container_name: nginx-2
build:
context: docker/nginx
dockerfile: Dockerfile
image: webserver/nginx
volumes:
- ./code:/data/application.local
networks:
- app-network
depends_on:
- app-1
- app-2

app-1: #2.2. php-fpm (соединяется с nginx через unix-сокет)
container_name: app-1
build:
context: docker/fpm
dockerfile: Dockerfile
image: webserver/app
volumes:
- ./code:/data/application.local
networks:
- app-network

app-2: #2.2. php-fpm (соединяется с nginx через unix-сокет)
container_name: app-2
build:
context: docker/fpm
dockerfile: Dockerfile
image: webserver/app
volumes:
- ./code:/data/application.local
networks:
- app-network

memcached: #2.4. memcached (соединяется с php по порту)
image: memcached
container_name: memcached
ports:
- "11211:11211"
networks:
- app-network


networks:
app-network:
driver: bridge
...
7 changes: 7 additions & 0 deletions docker/balancer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM nginx:latest

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

EXPOSE 80

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

server {
listen 80;
server_name _;

# restrict methods. Принимаем только POST-запросы
if ($request_method !~ ^(POST)$) {
return '405';
}

location / {
proxy_pass http://balancer;
include /etc/nginx/fastcgi_params;
}
}
22 changes: 22 additions & 0 deletions docker/fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM php:8.2-fpm

RUN apt-get update && apt-get install -y \
libfreetype-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd pdo_mysql mysqli \
&& pecl install redis memcache \
&& docker-php-ext-enable redis memcache \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # Освобождаем место от мусора

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

WORKDIR /data

VOLUME /data

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"
6 changes: 6 additions & 0 deletions docker/fpm/zz-docker.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[global]
daemonize = no

[www]
listen = /var/run/php-fpm.sock
listen.mode = 0666
11 changes: 11 additions & 0 deletions docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM nginx:latest

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

WORKDIR /data

VOLUME /data

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
30 changes: 30 additions & 0 deletions docker/nginx/application.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
upstream apps {
least_conn;
server app-1:9000;
server app-2:9000;
}

server {
listen 80;
server_name application.local;
root /data/application.local/public;
index index.php index.html;

location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

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

location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass apps;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Loading