This project is the base structure for a microservice, applying Domain-Driven Design (DDD) and Command Query Responsibility Segregation (CQRS) with Event Sourcing (ES). It has the necessary files to act as api or as web. It uses PHP 8.1 and symfony 6.0 although it is decoupled from symfony. Use MySQL as the database and dbal, but an ORM can be changed if the team prefers. Once you have decided which type of microservice is going to be used, it is recommended that you delete the files related to each type.
How to install.
Steps:
- Copy .env.example to .env and edit with correct values. Environment files are located in config/environments folder.
- Run service
docker-compose -f docker-compose-dev.yml up -d
- Install dependencies:
docker exec -it <container_name> php composer.phar install
<container_name>
: The name in the docker-compose-dev.yml file
- Add hook so that commits cannot be made if validations do not pass
Add .git/hooks/pre-commit file
Make it executable chmod +x .git/hooks/pre-commit
Add content:
#!/bin/bash
exec < /dev/tty
docker exec <container_name> php composer.phar csstan &&
docker exec <container_name> php bin/phpunit --configuration /var/www/phpunit.xml.dist
rc=$?
exit $rc
8001
so the requests have to be like this:
http://localhost:8001/healthcheck
http://localhost:8001/
curl --location --request PUT 'localhost:8001/v1/user/a95c0896-1d05-4785-95ec-a7bc0229f356' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Jonh Doe",
"email": "jonh@doe.com",
"password": "jonhdoe"
}'
curl --location --request PUT 'localhost:8001/v1/ticket/a95c0896-1d05-4785-95ec-a7bc0229f352' \
--header 'Content-Type: application/json' \
--data '{
"user_id": "bad30a5b-6f7a-472f-9400-9462d90f0a8f",
"comment_id": "2dd78746-a677-49eb-947b-5678261bc21f",
"comment_title": "Opening a new ticket",
"comment_description": "Help me please...",
}'
#TODO: List files to delete for API type
#TODO: List files to delete for Web type
- Edit phpstan.neon package name
- Edit composer.json and change:
- project name
- project description
- autoload and autoload-dev package names
- Edit app/Kernel.php package name
- Edit public/index.php Kernel package name import
- Edit bin/console.php Kernel package name import
- Edit config/services.yaml packages name for app and src
- Edit docker-compose-dev.yml service name, container name, image and ports
- Edit the log related classes namespace (/src/Shared/Log)
- Edit this file removing these instructions and filling content
Currently events are fired and consumed in memory InMemorySymfonyEventBus, but the project is also prepared to store and consume them with rabbitMQ RabbitMqEventBus.
Supervisor has been added to be in charge of picking up the consumers. To do this, a configuration file UserCreatedSendEmailConsumer.conf has been created as an example, which is responsible for launching the rabbitMQ command LaunchRabbitMqConsumerCommand.
php bin/console rabbitmq:consume UserCreatedSendEmailConsumer
This ApiExceptionListener listener is responsible for catching the exceptions that are thrown in the microservice and returning a json with the error.
This middleware BasicHttpAuthMiddleware serves to authenticate the requests made to the microservice. It remains to implement AuthenticateUserCommand to check the username and password. It can be modified to handle tokens.
This middleware BasicHttpAuthMiddleware serves to authenticate the requests made to the microservice. It remains to implement AuthenticateUserCommand to check the username and password. It can be modified to handle tokens.
Go to this route (in browser): http://localhost:<container_binding_port>/healthcheck
There are several scripts in composer:
How to run:
docker exec -it <container_name> php composer.phar <script_name>
Scripts that should be executed automatically each time together. You can run them manually by doing:
composer auto-scripts
This scripts in here will be executed after the install
command has been
executed with a lock file present.
This scripts in here will be executed after the update
command is executed or
before the install
command is executed without a lock file present
With this command you can know what changes you should do to your code to fix code style. You can run it by executing:
composer cs
With this command you can fix php code style automatically by executing:
composer cs-fix
With this command you can run the static analysis tool phpstan by executing:
composer stan
With this command you can fix your code style and run the static analysis tool phpstan by executing:
composer csstan
docker exec -it <container_name> php bin/phpunit
docker exec -it <container_name> php bin/console <command_name>
docker exec -it <container_name> symfony <command_name>
tail -f var/log/syslog.log
docker-compose -f docker-compose-dev.yml down
This command run the application containers in a separate docker network for the integration tests.
When you build the image, the content of your project is copied into the container so it is the code you have locally that is tested.
docker-compose -f docker-compose-test.yml up -d --build
- Add Event Sourcing (ES):
- Add Auth Middleware:
- Add Transaction Middleware
-
Add DomainEventStorer(remove) - Add Healthcheck
- Change Postgres to MySQL
- Fix errors from cs-fixer, phpstan
- Do Make file
- Add more tests
- Migrate to PHP 8.2
- List files to delete according to type of microservice