Skip to content

Commit b9be129

Browse files
authored
Merge pull request #32 from iazaran/feature/grpc
Feature/grpc
2 parents 4fe18fe + c8abe40 commit b9be129

File tree

11 files changed

+54
-21
lines changed

11 files changed

+54
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> This project tries to cover some PHP features in a simple MVC structure with minimum installed composer packages. Then developers can use packages for specific requirements. Please add your ideas in Discussions, ask features or report bugs in issues.
66
7-
🚧 WIP: gRPC server _(gRPC client not completed yet, and gRPC server is under working, so be careful about the bugs in gRPC)_
7+
🚧 WIP: gRPC server _(gRPC client not completed yet, and gRPC server is under working, so be careful about the bugs in gRPC)_
88

99
💡 TODO: WebSocket
1010

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"files": [
1414
"env.php",
1515
"src/migrations.php",
16-
"src/routes.php"
16+
"src/routes.php",
17+
"src/grpc_route.php"
1718
],
1819
"classmap": [
1920
"src/"

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
version: '3.1'
22

33
services:
4-
php-mvc-server:
4+
php-mvc-app-server:
55
image: nginx:alpine
6-
container_name: php-mvc-server
6+
container_name: php-mvc-app-server
77
volumes:
88
- ./:/var/www
9-
- ./docker/nginx:/etc/nginx/conf.d/
9+
- ./docker/nginx_http1:/etc/nginx/conf.d/
1010
ports:
1111
- '8080:80'
1212
networks:
1313
- php-mvc-network
1414

15+
php-mvc-grpc-server:
16+
image: nginx:alpine
17+
container_name: php-mvc-grpc-server
18+
volumes:
19+
- ./:/var/www
20+
- ./docker/nginx_http2:/etc/nginx/conf.d/
21+
ports:
22+
- '8585:80'
23+
networks:
24+
- php-mvc-network
25+
1526
php-mvc-app:
1627
build:
1728
context: .

docker/nginx/default.conf renamed to docker/nginx_http1/default.conf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
server {
22
listen 80;
33
client_max_body_size 108M;
4-
access_log /var/log/nginx/application.access.log;
4+
access_log /var/log/nginx/app.access.log;
5+
error_log /var/log/nginx/app.error.log;
56
index index.php;
6-
error_log /var/log/nginx/error.log;
7-
access_log /var/log/nginx/access.log;
87
root /var/www/public;
98

10-
# try to serve file directly, fallback to index.php
9+
# Try to serve file directly, fallback to index.php
1110
location / {
1211
try_files $uri /index.php$is_args$args;
1312
gzip_static on;
@@ -21,5 +20,4 @@ server {
2120
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
2221
fastcgi_param PATH_INFO $fastcgi_path_info;
2322
}
24-
2523
}

docker/nginx_http2/default.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
listen 80 http2;
3+
client_max_body_size 108M;
4+
access_log /var/log/nginx/grpc.access.log;
5+
error_log /var/log/nginx/grpc.error.log;
6+
7+
# Listening for unencrypted gRPC traffic on port 80 and forwarding requests to the server on port 50051
8+
location / {
9+
grpc_pass grpc://localhost:50051;
10+
}
11+
}

src/App/HandleForm.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,12 @@ public static function upload(
186186
}
187187
imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
188188

189-
$target2 = $target1 . $baseName . '.' . $fileExtension;
190-
imagejpeg($newImage, $target2, 100);
191-
$overlayImage = imagecreatefrompng($overlay);
192-
imagecopyresampled($newImage, $overlayImage, 0, 0, 0, 0, $overlayWidth, $overlayHeight, $overlayWidth, $overlayHeight);
189+
$target2 = $target . $baseName . '.' . $fileExtension;
190+
if (!empty($overlay)) {
191+
imagejpeg($newImage, $target2, 100);
192+
$overlayImage = imagecreatefrompng($overlay);
193+
imagecopyresampled($newImage, $overlayImage, 0, 0, 0, 0, $overlayWidth, $overlayHeight, $overlayWidth, $overlayHeight);
194+
}
193195
imagejpeg($newImage, $target2, $compressRate);
194196

195197
unlink($target1);

src/Controllers/API/BlogGrpcController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BlogGrpcController implements BlogInterface
3535
*
3636
* @param string $host
3737
*/
38-
public function __construct(string $host)
38+
public function __construct(string $host = 'localhost:50051')
3939
{
4040
$this->client = new BlogClient($host, ['credentials' => ChannelCredentials::createDefault()]);
4141
}

src/Models/Auth.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ public static function verify(object $request): bool
5252
!is_null(Database::fetch())
5353
&& !is_null(Database::fetch()['user_token'])
5454
&& $request->user_token == Database::fetch()['user_token']
55-
&& setcookie('loggedin', base64_encode($request->email), time() + (86400 * COOKIE_DAYS))
5655
) {
5756
Database::query("UPDATE users SET verified = :verified WHERE email = :email");
5857
Database::bind([
5958
':verified' => 1,
6059
':email' => $request->email,
6160
]);
6261

63-
if (Database::execute()) return true;
62+
if (Database::execute()) {
63+
setcookie('loggedin', base64_encode($request->email), time() + (86400 * COOKIE_DAYS));
64+
return true;
65+
}
6466
}
6567
return false;
6668
}
@@ -111,8 +113,10 @@ public static function login(object $request): bool
111113
&& password_verify($request->password, Database::fetch()['password'])
112114
&& !is_null(Database::fetch()['user_token'])
113115
&& Database::fetch()['verified']
114-
&& setcookie('loggedin', base64_encode($request->email), time() + (86400 * COOKIE_DAYS))
115-
) return true;
116+
) {
117+
setcookie('loggedin', base64_encode($request->email), time() + (86400 * COOKIE_DAYS));
118+
return true;
119+
}
116120
return false;
117121
}
118122

src/grpc_route.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$server = new \Grpc\RpcServer([]);
4+
$server->addHttp2Port('localhost:50051');
5+
$server->handle(new \Controllers\API\BlogGrpcController());
6+
$server->run();

0 commit comments

Comments
 (0)