Skip to content

Commit 8907e6e

Browse files
committed
Swagger and redis cached CRUD
README Cleanup
1 parent 4ef26c5 commit 8907e6e

25 files changed

+1849
-289
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
11
# php-swoole-crud-microservice
2+
3+
## Overview
4+
5+
A high-performance CRUD microservice built with PHP and Swoole, featuring MySQL, Redis, Prometheus, Grafana, Caddy, and Swagger UI integration. Designed for scalable, observable, and containerized deployments.
6+
7+
## Features
8+
9+
- Fast HTTP server powered by Swoole
10+
- MySQL database with connection pooling
11+
- Redis caching and pooling
12+
- Metrics endpoint for Prometheus
13+
- Grafana dashboards for monitoring
14+
- Caddy for HTTPS and reverse proxy
15+
- Swagger UI for API documentation
16+
- Health checks for all services
17+
18+
## Getting Started
19+
20+
### Prerequisites
21+
22+
- Docker & Docker Compose
23+
24+
### Quick Start
25+
26+
```bash
27+
# Install composer
28+
composer install
29+
30+
# Start all services in detached mode
31+
docker compose up -d --build
32+
```
33+
34+
### Database Migration
35+
36+
```bash
37+
# Run migrations inside the app container
38+
docker compose exec app php scripts/migrate.php
39+
```
40+
41+
### API Documentation
42+
43+
```bash
44+
# Generate OpenAPI spec
45+
php bin/generate-swagger.php
46+
```
47+
48+
Visit Swagger http://localhost:8080
49+
50+
![Preview](cover.png)
51+
52+
### Example API Usage
53+
54+
```bash
55+
# Create a user
56+
curl -s -X POST http://localhost:9501/users \
57+
-H 'Content-Type: application/json' \
58+
-d '{"name":"alice","email":"alice@example.com"}'
59+
60+
# Get all users
61+
curl -s -X GET http://localhost:9501/users -H 'Content-Type: application/json' | jq
62+
63+
# Get a user by ID
64+
curl -s -X GET http://localhost:9501/users/1 -H 'Content-Type: application/json' | jq
65+
66+
# Update a user
67+
curl -i -X PUT http://localhost:9501/users/1 \
68+
-H 'Content-Type: application/json' \
69+
-d '{"name":"alice-updated","email":"alice-updated@example.com"}'
70+
71+
# Delete a user
72+
curl -i -X DELETE http://localhost:9501/users/1 -H 'Content-Type: application/json'
73+
```
74+
75+
### Benchmarking
76+
77+
```bash
78+
# Run ApacheBench for health check endpoint
79+
ab -n 100000 -c 100 -v 4 http://localhost:9501/users/1 2>&1 | tee ab.log
80+
```
81+
82+
### Monitoring
83+
84+
- Prometheus scrapes metrics from the app and MySQL exporter.
85+
- Grafana visualizes metrics (default port: 3000).
86+
87+
### Environment Variables
88+
89+
See `docker-compose.yml` for all configurable options.
90+
91+
## License
92+
93+
MIT

bin/generate-swagger.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
<?php
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use OpenApi\Generator;
6+
7+
$output = __DIR__ . '/../public/swagger.json';
8+
9+
// ✅ instantiate generator and call generate()
10+
$generator = new Generator();
11+
print "Generating Swagger JSON... \n";
12+
13+
$openapi = $generator->generate([__DIR__ . '/../src/']);
14+
15+
file_put_contents(
16+
$output,
17+
$openapi->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
18+
);
19+
20+
echo "✅ Swagger JSON generated at $output\n";

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
"phpmd/phpmd": "^2.15",
1212
"phpstan/phpstan": "^2.1",
1313
"phpunit/phpunit": "^10",
14-
"squizlabs/php_codesniffer": "^3.13"
14+
"squizlabs/php_codesniffer": "^3.13",
15+
"zircote/swagger-php": "^5.3"
1516
},
1617
"scripts": {
1718
"start": "php public/index.php",
1819
"test": "vendor/bin/phpunit",
20+
"openapi:generate": "vendor/bin/openapi --output public/openapi.json src",
1921
"cs": "vendor/bin/phpcs -p --standard=phpcs.xml",
2022
"cbf": "vendor/bin/phpcbf -p --standard=phpcs.xml"
2123
},

composer.lock

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

cover.png

1.04 MB
Loading

0 commit comments

Comments
 (0)