From 1b4e1468fc3fd0b5e3400197a45277ccc4ff544e Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Thu, 4 Dec 2025 08:43:19 -0500 Subject: [PATCH] feat: update gRPC server configuration and enhance Docker setup --- .rr.yaml | 7 +-- Dockerfile | 2 + README.md | 139 +++++++++++++++++++++++++++++++++++---------- config.php | 1 - docker-compose.yml | 17 ++++++ grpc-worker.php | 2 +- src/Grpc.php | 2 +- 7 files changed, 131 insertions(+), 39 deletions(-) diff --git a/.rr.yaml b/.rr.yaml index 763504a..180c970 100644 --- a/.rr.yaml +++ b/.rr.yaml @@ -11,12 +11,9 @@ grpc: pool: command: "php grpc-worker.php" num_workers: ${GRPC_WORKERS:-4} - allocate_timeout: 5s - reset_timeout: 5s - destroy_timeout: 5s - stream_timeout: 5s + debug: ${DEBUG:-false} reflection: ${GRPC_REFLECTION:-true} - health_check: ${GRPC_HEALTH_CHECK:-true} + destroy_timeout: 5s proto: - "protos/example.proto" diff --git a/Dockerfile b/Dockerfile index 9b86277..885b972 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,11 +43,13 @@ COPY --from=library /app/vendor /app/vendor # Copy the RoadRunner configuration file and source ADD src src/ ADD generated generated/ +ADD protos protos/ ADD server.php . ADD .rr.yaml . ADD config.php . EXPOSE 9501 +EXPOSE 9001 # Entrypoint command to run the RoadRunner server with the specified configuration ENTRYPOINT ["rr", "serve", "-c", ".rr.yaml", "-s"] \ No newline at end of file diff --git a/README.md b/README.md index c60c60e..072f710 100644 --- a/README.md +++ b/README.md @@ -2,55 +2,132 @@ ![pipeline status](https://gitea.siteworxpro.com/siteworxpro/Php-Template/actions/workflows/tests.yml/badge.svg?branch=master&style=flat-square) +## Overview + +This is a PHP project template that provides a structured development environment using Docker Compose and Make. +It includes tools for code quality, testing, dependency management, and gRPC support. + ## Dev Environment -### Prerequisites -- Docker -- Docker Compose +This project uses Docker Compose and Make to manage the development environment. The `makefile` provides convenient +commands for common development tasks. -### migrations +## Prerequisites -create a new migration -```shell -docker run --rm -v $(PWD):/app siteworxpro/migrate:v4.18.3 create -ext sql -dir /app/db/migrations -seq create_users_table +- Docker and Docker Compose +- Make +- protoc (Protocol Buffers compiler) - for gRPC code generation + +## Quick Start + +```bash +# Install PHP dependencies +make composer-install + +# Start the development container +make start + +# Run the application server +make run ``` -```text -postgres://siteworxpro:password@localhost:5432/siteworxpro?sslmode=disable +## Available Commands + +### Container Management + +- `make start` - Start the development runtime container +- `make stop` - Stop and remove all containers +- `make restart` - Restart the development container +- `make rebuild` - Rebuild containers (use after Dockerfile changes) +- `make sh` - Open a shell in the development container +- `make ps` - Show running containers + +### Application + +- `make run` - Run the application server (RoadRunner) +- `make migrate` - Run database migrations + +### Composer & Dependencies + +- `make composer-install` - Install PHP dependencies +- `make composer-update` - Update PHP dependencies +- `make composer-require package=vendor/package` - Add a new dependency +- `make composer-require-dev package=vendor/package` - Add a new dev dependency + +### Code Quality + +- `make lint` - Run linters (phpcs and phpstan) +- `make fmt` - Format code with php-cs-fixer +- `make test` - Run the test suite (phpunit) +- `make license-check` - Check license headers in source files + +### Debugging & Coverage + +- `make enable-debug` - Enable Xdebug for debugging +- `make enable-coverage` - Enable PCOV for code coverage + +### gRPC + +- `make protoc` - Generate PHP gRPC code from `.proto` files + +### CI Workflow + +- `make ci` - Run the full CI pipeline locally (install, license check, lint, test) + +### Help + +- `make help` - Show all available commands with descriptions + +## Common Workflows + +### Starting Development + +```bash +make composer-install +make start +make run ``` -```shell -docker run --rm -v $(PWD):/app siteworxpro/migrate:v4.18.3 -database "postgres://siteworxpro:password@localhost:5432/siteworxpro?sslmode=disable" -path /app/db/migrations up +### Adding a New Package + +```bash +make composer-require package=vendor/package-name ``` -### Starting the Runtime -```shell -docker-compose up -d -``` -### Start the server -```shell -docker exec -it template-dev-runtime-1 rr serve +### Running Tests + +```bash +make test ``` -You can access the api at `http://localhost:9501/` +### Code Quality Check -### Xdebug - -xdebug needs to be built into the container before it will work -```shell - docker exec -it php-template-composer-runtime-1 bin/xdebug.sh +```bash +make lint +make fmt ``` -### Install the dependencies -```shell -docker run --rm -v $(PWD):/app siteworxpro/composer install --ignore-platform-reqs +### Debugging + +```bash +make enable-debug +make run ``` -### Running all tests -```shell -docker run --rm -v $(PWD):/app siteworxpro/composer run tests:all -``` +## Notes +- All commands run inside Docker containers, ensuring a consistent environment +- The development runtime uses RoadRunner as the application server +- Composer commands run in a separate `composer-runtime` container +- Database migrations run in a dedicated `migration-container` + +## Additional Information + +### Accessing Services + +- You can access the api at [https://localhost](https://localhost) +- Traefik dashboard is at [https://127.0.0.1/dashboard/](https://127.0.0.1/dashboard/) +- the grpc server is at [tcp://localhost:9001](tcp://localhost:9001) ## License diff --git a/config.php b/config.php index 48dd820..4b00213 100644 --- a/config.php +++ b/config.php @@ -14,7 +14,6 @@ return [ */ 'server' => [ 'port' => Env::get('HTTP_PORT', 9501, 'int'), - 'dev_mode' => Env::get('DEV_MODE', false, 'bool'), ], /** diff --git a/docker-compose.yml b/docker-compose.yml index a7c1f4c..aad53f4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,12 @@ volumes: services: traefik: + labels: + - "traefik.enable=true" + - "traefik.http.routers.traefik.entrypoints=web-secure" + - "traefik.http.routers.traefik.rule=Host(`127.0.0.1`) && (PathPrefix(`/dashboard`) || PathPrefix(`/api`))" + - "traefik.http.routers.traefik.tls=true" + - "traefik.http.routers.traefik.service=api@internal" image: traefik:latest container_name: traefik healthcheck: @@ -15,15 +21,18 @@ services: ports: - "80:80" - "443:443" + - "9001:9001" volumes: - "/var/run/docker.sock:/var/run/docker.sock" restart: always command: - "--providers.docker=true" + - "--api.insecure=true" - "--ping" - "--providers.docker.exposedByDefault=false" - "--entrypoints.web.address=:80" - "--entrypoints.web-secure.address=:443" + - "--entrypoints.grpc.address=:9001" - "--accesslog=true" - "--entrypoints.web.http.redirections.entryPoint.to=web-secure" - "--entrypoints.web.http.redirections.entryPoint.scheme=https" @@ -79,6 +88,13 @@ services: - "traefik.http.services.api.loadbalancer.healthcheck.path=/healthz" - "traefik.http.services.api.loadbalancer.healthcheck.interval=5s" - "traefik.http.services.api.loadbalancer.healthcheck.timeout=60s" + - "traefik.tcp.services.api.loadbalancer.server.port=9001" + - "traefik.http.services.api.loadbalancer.server.port=9501" + - "traefik.tcp.routers.grpc.entrypoints=grpc" + - "traefik.tcp.routers.grpc.rule=HostSNI(`localhost`) || HostSNI(`127.0.0.1`)" + - "traefik.tcp.routers.grpc.tls=true" + - "traefik.tcp.routers.grpc.service=api" + container_name: dev-runtime volumes: - .:/app build: @@ -103,6 +119,7 @@ services: QUEUE_BROKER: redis PHP_IDE_CONFIG: serverName=localhost WORKERS: 1 + GRPC_WORKERS: 1 DEBUG: 1 REDIS_HOST: redis DB_HOST: postgres diff --git a/grpc-worker.php b/grpc-worker.php index 6f7ed2b..0dbc05c 100644 --- a/grpc-worker.php +++ b/grpc-worker.php @@ -6,7 +6,7 @@ require __DIR__ . '/vendor/autoload.php'; try { $server = new Grpc(); - $server->start(); + exit($server->start()); } catch (\Exception $e) { echo $e->getMessage(); diff --git a/src/Grpc.php b/src/Grpc.php index 9cc9d6f..d8c2779 100644 --- a/src/Grpc.php +++ b/src/Grpc.php @@ -36,7 +36,7 @@ class Grpc public function start(): int { $server = new Server(new Invoker(), [ - 'debug' => (bool) Config::get('app.debug'), + 'debug' => Config::get('app.dev_mode'), ]); $server->registerService(GreeterInterface::class, new GreeterHandler());