Skip to content
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
12 changes: 12 additions & 0 deletions .docker/balancer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:latest

WORKDIR /var/www

RUN apt-get update \
&& apt-get install -y nginx

COPY ./hosts/trusted-brackets.local.conf /etc/nginx/sites-enabled/trusted-brackets.local.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
14 changes: 14 additions & 0 deletions .docker/balancer/hosts/trusted-brackets.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
upstream balancer {
server webserver1;
server webserver2;
}

server {
listen 80;
server_name trusted-brackets.local;

location / {
proxy_set_header Host $host;
proxy_pass http://balancer;
}
}
10 changes: 10 additions & 0 deletions .docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM ubuntu:latest

RUN apt-get update && apt-get install -y nginx

COPY ./hosts/trusted-brackets.local.conf /etc/nginx/sites-enabled/trusted-brackets.local.conf

WORKDIR /var/www/trusted-bracets.local
VOLUME /var/www/trusted-bracets.local

CMD [ "nginx", "-g", "daemon off;"]
33 changes: 33 additions & 0 deletions .docker/nginx/hosts/trusted-brackets.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
upstream php_upstream {
server brackets-php1:9000;
server brackets-php2:9000;
}


server {
listen 80;
index index.php index.html index.htm;
server_name trusted-brackets.local;
root /var/www/trusted-brackets.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;

location ~* .(jpg|jpeg|gif|css|png|js|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 php_upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}
30 changes: 30 additions & 0 deletions .docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM php:7.4-fpm

RUN apt-get update && apt-get install -y \
curl \
wget \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libonig-dev \
libzip-dev \
libmcrypt-dev \
libmemcached-dev\
&& pecl install mcrypt-1.0.3 \
&& 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 \
&& pecl install memcached \
&& docker-php-ext-enable memcached

RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin/ --filename=composer

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

WORKDIR /var/www/trusted-brackets.local

VOLUME /var/www/trusted-brackets.local

CMD ["php-fpm"]
3 changes: 3 additions & 0 deletions .docker/php/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
session.save_handler = memcached
session.save_path = "memcached1:11211"
session.auto_start = 1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor
.env
dbdata

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# PHP2021

https://otus.ru/lessons/razrabotchik-php/?utm_source=github&utm_medium=free&utm_campaign=otus

## HW-4. PHP-Webservers

###Нет ключа "string"
![No key](screenshots/no_key.png)


###Нет ключа "string"
![empty string](screenshots/empty_string.png)


###Неверные символы строки
![invalid_chars](screenshots/invalid_chars.png)

###Разное количество скобок
![not_equals](screenshots/not_equals.png)

###Тест пройден
![success](screenshots/success.png)
97 changes: 97 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
version: "3"
services:

balancer:
build:
context: .docker/balancer
dockerfile: Dockerfile
container_name: brackets-balancer
ports:
- "80:80"
depends_on:
- webserver1
- webserver2
networks:
- trusted-brackets


webserver1:
build:
context: .docker/nginx
dockerfile: Dockerfile
container_name: webserver1
ports:
- "3000:80"
depends_on:
- php1
- php2
volumes:
- ./src:/var/www/trusted-brackets.local
networks:
- trusted-brackets

webserver2:
build:
context: .docker/nginx
dockerfile: Dockerfile
container_name: webserver2
ports:
- "3001:80"
depends_on:
- php1
- php2
volumes:
- ./src:/var/www/trusted-brackets.local
networks:
- trusted-brackets

php1:
build:
context: ./.docker/php
dockerfile: Dockerfile
image: myapp/php
container_name: brackets-php1
volumes:
- ./src:/var/www/trusted-brackets.local
networks:
- trusted-brackets

php2:
build:
context: ./.docker/php
dockerfile: Dockerfile
image: myapp/php
container_name: brackets-php2
volumes:
- ./src:/var/www/trusted-brackets.local
networks:
- trusted-brackets


memcached1:
image: oktec/repcached:latest
container_name: brackets-memcached1
restart: unless-stopped
environment:
SLAVE: memcached2
ports:
- "11221:11211"
networks:
- trusted-brackets


memcached2:
image: oktec/repcached:latest
container_name: brackets-memcached2
restart: unless-stopped
environment:
SLAVE: memcached1
ports:
- "11222:11211"
networks:
- trusted-brackets


networks:
trusted-brackets:
driver: bridge
Binary file added screenshots/empty_string.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/invalid_chars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/no_key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/not_equals.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions src/app/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Ivanboriev\TrustedBrackets;


use Ivanboriev\TrustedBrackets\Exceptions\BadRequestMethodException;
use Ivanboriev\TrustedBrackets\Exceptions\InvalidArgumentException;
use Ivanboriev\TrustedBrackets\Request\Request;
use Ivanboriev\TrustedBrackets\Response\Response;
use Ivanboriev\TrustedBrackets\Validator\Rules\EndsWithRule;
use Ivanboriev\TrustedBrackets\Validator\Rules\EqualsRule;
use Ivanboriev\TrustedBrackets\Validator\Rules\NotEmptyRule;
use Ivanboriev\TrustedBrackets\Validator\Rules\OnlyRule;
use Ivanboriev\TrustedBrackets\Validator\Rules\RequiredRule;
use Ivanboriev\TrustedBrackets\Validator\Rules\StartsWithRule;
use Ivanboriev\TrustedBrackets\Validator\Validator;

class App
{
private Request $request;

private Validator $validator;



public function __construct()
{
$this->request = new Request;

$this->validator = new Validator;
}


/**
* @throws BadRequestMethodException
* @throws InvalidArgumentException
*/
public function run(): void
{
if (!$this->request->isPost()) {
throw new BadRequestMethodException;
}

$this->validator->make($this->request->payload(), [
'string' => [
new RequiredRule,
new NotEmptyRule,
new OnlyRule(['(', ')']),
new StartsWithRule('('),
new EndsWithRule(')'),
new EqualsRule('(', ')')
]
]);

if ($this->validator->fails()) {
throw new InvalidArgumentException($this->validator->error());
}

Response::success("Bracket pair test passed!");

}

}
8 changes: 8 additions & 0 deletions src/app/Exceptions/BadRequestMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Ivanboriev\TrustedBrackets\Exceptions;

class BadRequestMethodException extends \Exception
{
public $message = 'Bad request method! Available methods: [POST]';
}
8 changes: 8 additions & 0 deletions src/app/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Ivanboriev\TrustedBrackets\Exceptions;

class InvalidArgumentException extends \Exception
{

}
61 changes: 61 additions & 0 deletions src/app/Request/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Ivanboriev\TrustedBrackets\Request;

class Request
{
private string $method;

private static string $contentType;

private array $payload = [];

public function __construct()
{
$this->method = $_SERVER['REQUEST_METHOD'];
self::$contentType = $_SERVER['CONTENT_TYPE'];
$this->payload = self::getPayload($this->method);
}


/**
* @param $method
* @return array|mixed
*/
private static function getPayload($method)
{
if ($method === "GET") {
return $_GET;
}

if ($method === "POST") {
return self::$contentType === 'application/json' ? json_decode(file_get_contents('php://input'), true) : $_POST;
}

return [];
}

/**
* @return mixed|string
*/
public function method()
{
return $this->method;
}

/**
* @return array|mixed
*/
public function payload()
{
return $this->payload;
}

/**
* @return bool
*/
public function isPost(): bool
{
return $this->method() === "POST";
}
}
Loading