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

hw4 #94

Open
wants to merge 2 commits into
base: RMulyukov/main
Choose a base branch
from
Open

hw4 #94

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 app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
52 changes: 52 additions & 0 deletions app/app/BracesChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Rmulyukov\Hw4;

final class BracesChecker
{
private array $braces = [];

public function check(string $braces): bool
{
if (empty($braces)) {
return false;
}

for ($i = 0; $i < strlen($braces); $i++) {
if (!$this->handle($braces[$i])) {
return false;
}
}
return !$this->hasBraces();
}

private function handle(string $brace): bool
{
if ($brace === ')') {
return $this->removeBrace();
}
$this->addBrace();
return true;
}

private function removeBrace(): bool
{
if (!$this->hasBraces()) {
return false;
}
array_pop($this->braces);
return true;
}

private function addBrace(): void
{
$this->braces[] = ')';
}

private function hasBraces(): bool
{
return count($this->braces) > 0;
}
}
21 changes: 21 additions & 0 deletions app/app/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rmulyukov\Hw4;

final readonly class Request
{
private array $post;

public function __construct()
{
$this->post = $_POST;
}

public function get(string $key, string $default = ''): string
{
$value = $this->post[$key] ?? null;
return is_string($value) ? $value : $default;
}
}
38 changes: 38 additions & 0 deletions app/app/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rmulyukov\Hw4;

final class Response
{
private string $message;
private string $header;

public function __construct(bool $success)
{
if ($success) {
$this->initSuccess();
} else {
$this->initError();
}
}

public function send(): string
{
header($this->header);
return $this->message;
}

private function initSuccess(): void
{
$this->header = 'HTTP/1.1 200 OK';
$this->message = 'OK';
Copy link

Choose a reason for hiding this comment

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

Зачем нам тут устанавливать внутреннее значения сообщения?

Copy link
Author

Choose a reason for hiding this comment

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

в задании написано
• если строка корректна, то пользователю возвращается ответ 200 OK, с информационным текстом, что всё хорошо;
• если строка некорректна, то пользователю возвращается ответ 400 Bad Request, с информационным текстом, что всё плохо.

$this->message содержит этот информационный текст

Copy link

Choose a reason for hiding this comment

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

Вопрос в том, что тут мы устанавливаем заголовок и записываем значение в message, чтобы потом его получить и напечатать. Не будет ли лучше сразу напечатать и всё?

}

private function initError(): void
{
$this->header = 'HTTP/1.1 400 Bad request';
$this->message = 'Incorrect string';
}
}
23 changes: 23 additions & 0 deletions app/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "rmulyukov/hw4",
"type": "project",
"authors": [
{
"name": "Mulyukov Rustyam"
}
],
"autoload": {
"psr-4": {
"Rmulyukov\\Hw4\\": "app/"
}
},
"config": {
"platform": {
"php": "8.2"
}
},
"require": {
"php":"^8.2",
"ext-memcache": "*"
}
}
23 changes: 23 additions & 0 deletions app/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions app/public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rmulyukov\Hw4\BracesChecker;
use Rmulyukov\Hw4\Request;
use Rmulyukov\Hw4\Response;

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

$string = (new Request())->get('string');
$isCorrectString = (new BracesChecker())->check($string);
echo (new Response($isCorrectString))->send();
68 changes: 68 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
version: '3.9'

services:
nginx-proxy:
container_name: proxy-hw4
hostname: proxy-hw4
build:
context: ./docker
dockerfile: proxy-nginx/Dockerfile
ports:
- "8080:80"
networks:
- hw4-network

nginx-balancer1:
container_name: balancer1-hw4
hostname: balancer1-hw4
build:
context: ./docker
dockerfile: balancer-nginx/Dockerfile
volumes:
- ./app:/hw4/app
networks:
- hw4-network

nginx-balancer2:
container_name: balancer2-hw4
hostname: balancer2-hw4
build:
context: ./docker
dockerfile: balancer-nginx/Dockerfile
volumes:
- ./app:/hw4/app
networks:
- hw4-network

php1:
container_name: php1-hw4
hostname: php1-hw4
build:
context: ./docker
dockerfile: php/Dockerfile
volumes:
- ./app:/hw4/app
networks:
- hw4-network

php2:
container_name: php2-hw4
hostname: php2-hw4
build:
context: ./docker
dockerfile: php/Dockerfile
volumes:
- ./app:/hw4/app
networks:
- hw4-network

memcache:
container_name: memcache-hw4
hostname: memcache-hw4
image: memcached:1.6.23-alpine
networks:
- hw4-network

networks:
hw4-network:
driver: bridge
5 changes: 5 additions & 0 deletions docker/balancer-nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM nginx:1.25.0-alpine

COPY ./balancer-nginx/conf.d /etc/nginx/conf.d

WORKDIR /hw4/app
26 changes: 26 additions & 0 deletions docker/balancer-nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
upstream php {
server php1-hw4:9000;
server php2-hw4:9000;
}

server {
index index.php;
root /hw4/app/public;

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

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

location ~/\.ht {
deny all;
}
}
16 changes: 16 additions & 0 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM php:8.2-fpm-alpine

RUN apk update && apk add --no-cache --virtual .build-temp-deps \
autoconf make gcc g++ zlib-dev \
&& apk update && apk add --no-cache libmemcached-dev \
#memcache
&& pecl install memcache \
&& docker-php-ext-enable memcache \
#composer
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer --quiet \
&& rm -rf /root/.composer/cache \
&& apk del --no-cache -f .build-temp-deps

COPY ./php/conf.d /usr/local/etc/php/conf.d

WORKDIR /hw4/app
2 changes: 2 additions & 0 deletions docker/php/conf.d/session.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
session.save_handler = memcached
session.save_path = "memcached:11211"
3 changes: 3 additions & 0 deletions docker/proxy-nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM nginx:1.25.0-alpine

COPY ./proxy-nginx/conf.d /etc/nginx/conf.d
17 changes: 17 additions & 0 deletions docker/proxy-nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
upstream nginx-balancers {
least_conn;
server balancer1-hw4:80;
server balancer2-hw4:80;
}

server {
listen 80;

location / {
proxy_pass http://nginx-balancers;
}

location ~/\.ht {
deny all;
}
}
Loading