Skip to content

Commit

Permalink
Added 3 php-fpm, added validator for '()'
Browse files Browse the repository at this point in the history
  • Loading branch information
DolgovAleksandr committed Sep 10, 2023
1 parent edaa9e8 commit 0422267
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 62 deletions.
58 changes: 28 additions & 30 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ services:
networks:
- app-network

app:
build:
context: ./..
dockerfile: docker/fpm/Dockerfile
image: myapp/php
container_name: app
volumes:
- ../src:/app
networks:
- app-network

memcached:
build:
context: ./memcached
Expand All @@ -35,31 +24,40 @@ services:
networks:
- app-network

redis:
app1:
build:
context: ./redis
dockerfile: Dockerfile
ports:
- "6379:6379"
container_name: app-redis
context: ./..
dockerfile: docker/fpm/Dockerfile
image: myapp/php
container_name: app1
volumes:
- ../src:/app
networks:
- app-network

mysql:
# second php-fpm
app2:
build:
context: ./mysql
dockerfile: Dockerfile
restart: always
ports:
- "3306:3306"
container_name: app-mysql
context: ./..
dockerfile: docker/fpm/Dockerfile
image: myapp/php
container_name: app2
volumes:
- ../src:/app
networks:
- app-network

# second php-fpm
app3:
build:
context: ./..
dockerfile: docker/fpm/Dockerfile
image: myapp/php
container_name: app3
volumes:
- ../mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
- ../src:/app
networks:
- app-network

networks:
app-network:
Expand Down
2 changes: 0 additions & 2 deletions docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ FROM nginx

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

COPY ./index.html /data/application.local/index.html

WORKDIR /app

VOLUME /app
Expand Down
10 changes: 8 additions & 2 deletions docker/nginx/application.local.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
upstream php-fpm-backend {
least_conn;
server app1:9000;
server app2:9000;
server app3:9000;
}

server {
# указываем 80 порт для соединения
listen 80;
Expand Down Expand Up @@ -27,8 +34,7 @@ server {
location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app:9000;
#fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_pass php-fpm-backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
Expand Down
10 changes: 0 additions & 10 deletions docker/nginx/index.html

This file was deleted.

1 change: 0 additions & 1 deletion docker/redis/Dockerfile

This file was deleted.

19 changes: 2 additions & 17 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

echo "<h1>This is " . $_SERVER['HOSTNAME'] . "</h1>";
// Создание объекта Memcached
$memcached = new Memcached();

Expand All @@ -21,20 +22,4 @@
echo "Memcached is not working!";
}

$redis = new Redis();
$redis->connect('redis', 6379);

$key = 'test_key';
$value = 'Hello, Redis!';

// Запись данных
$redis->set($key, $value);

// Чтение данных
$result = $redis->get($key);

if ($result === $value) {
echo sprintf('Redis is working! Your value: %s', $result);
} else {
echo "Redis is not working!";
}
phpinfo();
35 changes: 35 additions & 0 deletions src/validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Aleksandrdolgov\OtusPhp2023;

use Exception;

function validateString(string $string): bool {
if (preg_match('/^(\((?1)*\))*$/', $string)) {
return true;
} else {
return false;
}
}

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 (validateString($string)) {
echo sprintf('Скобки в строке "<b>%s</b>" прошли валидацию', $string);
} else {
echo sprintf('Строка "<b>%s</b>" не прошла валидацию на скобки', $string);
}
} catch (Exception $exception) {
header("HTTP/1.1 {$exception->getCode()} {$exception->getMessage()}");
}

0 comments on commit 0422267

Please sign in to comment.