Skip to content
Merged
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
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

> 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.

🚧 WIP: gRPC server _(gRPC client not completed yet, and gRPC server is under working, so be careful about the bugs in gRPC ⚠)_

💡 TODO: WebSocket
💡 TODO: SEO & WebSocket

#### Features:
**List of features related with structure**
Expand All @@ -25,7 +23,7 @@ Contains all classes that used in codes like PDO, Middleware, Router & ...
- **src/Console**
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)_
- **src/Controllers**
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
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
```
docker-compose exec php-mvc-app protoc -I=src/Controllers/API \
src/Controllers/API/blog.proto \
Expand Down Expand Up @@ -71,7 +69,7 @@ Register an event listener and trigger it when needed
#### Run Web App:
- Install docker and docker-compose if needed
- Uncomment `// createTables();` in `src/routes`
- 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))_
- Run `docker-compose up --build -d`
- Open your browser and open web app in `localhost:8080` _(It will create tables related with migrations.php and then will comment `createTables();` automatically.)_
- You can run `docker-compose down` to stop and remove containers
- Next time you can use `docker-compose up -d`
Expand All @@ -80,7 +78,7 @@ Register an event listener and trigger it when needed
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.

#### RESTful API samples
Ready to use PostMan collection for RESTful API side: _(gRPC API side will be added later)_
Ready to use PostMan collection for RESTful API side:

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

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
},
"config": {
"platform": {
"php": "8.0"
"php": "8.1"
}
},
"require": {
"php": ">=8.0",
"php": ">=8.1",
"ext-pdo": "*",
"ext-json": "*",
"ext-gd": "*",
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

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

2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
jpegoptim optipng pngquant gifsicle \
cron \
protobuf-compiler-grpc \
protobuf-compiler-grpc libprotobuf-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

Expand Down
18 changes: 15 additions & 3 deletions docker/nginx_http2/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ server {
client_max_body_size 108M;
access_log /var/log/nginx/grpc.access.log;
error_log /var/log/nginx/grpc.error.log;
index index.php;
root /var/www/grpc;

# Listening for unencrypted gRPC traffic on port 80 and forwarding requests to the server on port 50051
location / {
grpc_pass grpc://localhost:50051;
add_trailer grpc-status $sent_http_grpc_status;
add_trailer grpc-message $sent_http_grpc_message;

rewrite /blog /index.php;

location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-mvc-app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
48 changes: 48 additions & 0 deletions grpc/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// Thanks for great codes: https://github.com/bakins/nginx-php-grpc

/**
* Required headers
*/
header("grpc-status: 0");
header("Content-Type: application/grpc+proto");

use Controllers\API\BlogGrpcController;

/**
* Configure auto-loading
*/
require_once dirname(__DIR__) . '/vendor/autoload.php';

$body = file_get_contents('php://input');

/**
* https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
* each message is preceded by a flag that denotes if the
* message is compressed and the length of the message
*/
$array = unpack("Cflag/Nlength", $body);

/**
* Message begins after the prefix, and should have
* the length defined in the prefix
*/
$message = substr($body, 5, $array['length']);

// Call appropriate method
$blogGrpcController = new BlogGrpcController();

// TODO: Make correct request and return correct response based on request type
$request = new \Blog\PostRequest();
try {
$request->mergeFromString($message);
} catch (Exception $e) {
echo $e->getMessage();
}
$response = $blogGrpcController->show($request);

$out = $response->serializeToString();

// Add grpc-status as a trailer in the nginx configuration
echo pack('CN', 0, strlen($out));
echo $out;
Loading