Skip to content

Commit 4c455d3

Browse files
authored
Merge pull request #35 from iazaran/feature/grpc
Feature/grpc
2 parents b9ac5e9 + e319775 commit 4c455d3

File tree

13 files changed

+139
-110
lines changed

13 files changed

+139
-110
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +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 ⚠)_
8-
9-
💡 TODO: WebSocket
7+
💡 TODO: SEO & WebSocket
108

119
#### Features:
1210
**List of features related with structure**
@@ -25,7 +23,7 @@ Contains all classes that used in codes like PDO, Middleware, Router & ...
2523
- **src/Console**
2624
Contains all scripts to run multiple times via Cron Jobs _(Scripts should be registered in /commands.php with custom timing, they will run by independent service in docker-compose)_
2725
- **src/Controllers**
28-
Controllers related with your routes separated for web and API. API folder includes both RESTful API and gRPC API. If you want use gRPC _(Under working now and server isn't ready to handle gRPC requests)_, you can find .proto file in API folder. Updating it will need to generate PHP codes again by
26+
Controllers related with your routes separated for web and API. API folder includes both RESTful API and gRPC API. If you want use gRPC _(gRPC client & server are not completed, and I ignored them for now. So be careful about the bugs in gRPC ⚠ and if you have an idea or a solution, only by PHP, please make a new discussion/issue/PR)_, you can find .proto file in API folder. Updating it will need to generate PHP codes again by
2927
```
3028
docker-compose exec php-mvc-app protoc -I=src/Controllers/API \
3129
src/Controllers/API/blog.proto \
@@ -71,7 +69,7 @@ Register an event listener and trigger it when needed
7169
#### Run Web App:
7270
- Install docker and docker-compose if needed
7371
- Uncomment `// createTables();` in `src/routes`
74-
- Run `docker-compose up --build -d` _(A compatibility issue for PHP8.1 and protobuf will fix soon in here: [#9359](https://github.com/protocolbuffers/protobuf/pull/9359))_
72+
- Run `docker-compose up --build -d`
7573
- Open your browser and open web app in `localhost:8080` _(It will create tables related with migrations.php and then will comment `createTables();` automatically.)_
7674
- You can run `docker-compose down` to stop and remove containers
7775
- Next time you can use `docker-compose up -d`
@@ -80,7 +78,7 @@ Register an event listener and trigger it when needed
8078
Consider a route for your form like `/blog/create`; now use `blog-create` as an ID for form, and `blog-create-submit` for submit button ID. All form's buttons need to have constant `form-button` class.
8179

8280
#### RESTful API samples
83-
Ready to use PostMan collection for RESTful API side: _(gRPC API side will be added later)_
81+
Ready to use PostMan collection for RESTful API side:
8482

8583
[![Run in Postman](https://run.pstmn.io/button.svg)](https://documenter.getpostman.com/view/6224358/UV5agGTG)
8684

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
},
2222
"config": {
2323
"platform": {
24-
"php": "8.0"
24+
"php": "8.1"
2525
}
2626
},
2727
"require": {
28-
"php": ">=8.0",
28+
"php": ">=8.1",
2929
"ext-pdo": "*",
3030
"ext-json": "*",
3131
"ext-gd": "*",

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
66
libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
77
jpegoptim optipng pngquant gifsicle \
88
cron \
9-
protobuf-compiler-grpc \
9+
protobuf-compiler-grpc libprotobuf-dev \
1010
&& apt-get clean \
1111
&& rm -rf /var/lib/apt/lists/*
1212

docker/nginx_http2/default.conf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ server {
33
client_max_body_size 108M;
44
access_log /var/log/nginx/grpc.access.log;
55
error_log /var/log/nginx/grpc.error.log;
6+
index index.php;
7+
root /var/www/grpc;
68

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;
9+
add_trailer grpc-status $sent_http_grpc_status;
10+
add_trailer grpc-message $sent_http_grpc_message;
11+
12+
rewrite /blog /index.php;
13+
14+
location ~ \.php$ {
15+
include fastcgi_params;
16+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
17+
fastcgi_pass php-mvc-app:9000;
18+
fastcgi_index index.php;
19+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
20+
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
21+
fastcgi_param PATH_INFO $fastcgi_path_info;
1022
}
1123
}

grpc/index.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
// Thanks for great codes: https://github.com/bakins/nginx-php-grpc
3+
4+
/**
5+
* Required headers
6+
*/
7+
header("grpc-status: 0");
8+
header("Content-Type: application/grpc+proto");
9+
10+
use Controllers\API\BlogGrpcController;
11+
12+
/**
13+
* Configure auto-loading
14+
*/
15+
require_once dirname(__DIR__) . '/vendor/autoload.php';
16+
17+
$body = file_get_contents('php://input');
18+
19+
/**
20+
* https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
21+
* each message is preceded by a flag that denotes if the
22+
* message is compressed and the length of the message
23+
*/
24+
$array = unpack("Cflag/Nlength", $body);
25+
26+
/**
27+
* Message begins after the prefix, and should have
28+
* the length defined in the prefix
29+
*/
30+
$message = substr($body, 5, $array['length']);
31+
32+
// Call appropriate method
33+
$blogGrpcController = new BlogGrpcController();
34+
35+
// TODO: Make correct request and return correct response based on request type
36+
$request = new \Blog\PostRequest();
37+
try {
38+
$request->mergeFromString($message);
39+
} catch (Exception $e) {
40+
echo $e->getMessage();
41+
}
42+
$response = $blogGrpcController->show($request);
43+
44+
$out = $response->serializeToString();
45+
46+
// Add grpc-status as a trailer in the nginx configuration
47+
echo pack('CN', 0, strlen($out));
48+
echo $out;

0 commit comments

Comments
 (0)