Skip to content

Commit 3878a06

Browse files
committed
Simplify and condense README documentation
The README has been streamlined to focus on quick installation and configuration steps, removing detailed middleware and usage documentation. For comprehensive guides and examples, users are now directed to the project's wiki.
1 parent 855ea13 commit 3878a06

File tree

1 file changed

+10
-316
lines changed

1 file changed

+10
-316
lines changed

README.md

Lines changed: 10 additions & 316 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
11
# laravel-microservice-core
22

3-
[![Packagist Version](https://img.shields.io/packagist/v/kroderdev/laravel-microservice-core.svg)](https://packagist.org/packages/kroderdev/laravel-microservice-core) [![Downloads](https://img.shields.io/packagist/dt/kroderdev/laravel-microservice-core.svg)](https://packagist.org/packages/kroderdev/laravel-microservice-core) [![License](https://img.shields.io/packagist/l/kroderdev/laravel-microservice-core.svg)](LICENSE)
3+
[![Packagist Version](https://img.shields.io/packagist/v/kroderdev/laravel-microservice-core.svg)](https://packagist.org/packages/kroderdev/laravel-microservice-core)
4+
[![Downloads](https://img.shields.io/packagist/dt/kroderdev/laravel-microservice-core.svg)](https://packagist.org/packages/kroderdev/laravel-microservice-core)
5+
[![License](https://img.shields.io/packagist/l/kroderdev/laravel-microservice-core.svg)](LICENSE)
46

57
A toolkit that turns **Laravel 12** into a lightweight base for distributed microservices.
68

7-
## Overview
9+
## Quick start
810

9-
This package provides a robust foundation for building Laravel-based microservices, focusing on stateless authentication, authorization, and distributed system best practices. Out of the box, you’ll find:
10-
11-
- **JWT Authentication Middleware:**
12-
Securely validates JWT tokens and hydrates the authenticated user for each request.
13-
14-
- **Role & Permission Middleware:**
15-
Restrict access to routes based on user roles or permissions, with support for both JWT-embedded and dynamically loaded access control.
16-
17-
- **LoadAccess Middleware:**
18-
Optionally fetches the latest roles and permissions for the authenticated user from a centralized permission service or API Gateway.
19-
20-
- **Gateway Guard:**
21-
Session-based guard that refreshes JWT tokens via the API Gateway and loads permissions automatically.
22-
23-
- **Correlation ID Middleware:**
24-
Automatically generates or propagates a unique correlation ID for every request, enabling end-to-end tracing across distributed systems.
25-
26-
- **Configurable Middleware Aliases & Groups:**
27-
Easily enable, disable, or rename middleware via configuration, and use convenient groups like `microservice.auth` group for full authentication and authorization in one step.
28-
29-
- **HTTP Client Macros:**
30-
Pre-configured HTTP clients for communicating with your API Gateway or other services. They automatically forward the current correlation ID header and propagate gateway errors to the caller.
31-
32-
- **Ready-to-publish Configuration:**
33-
All settings are customizable via a single config file, making it easy to adapt the package to your environment.
34-
35-
This toolkit is designed to be flexible, extensible, and easy to integrate into any Laravel microservice architecture.
36-
37-
## Installation
38-
39-
Install directly from Packagist via Composer:
40-
41-
1. Require the package via Composer:
11+
Install via Composer:
4212

4313
```bash
4414
composer require kroderdev/laravel-microservice-core
4515
```
4616

47-
>[!NOTE]
48-
> To install the dev branch, append `:dev-main` to the package name.
49-
5017
### Publish Configuration
5118

5219
After installation, publish the configuration file to your Laravel project:
@@ -60,289 +27,16 @@ You can now customize the settings to match your microservice environment.
6027

6128
---
6229

63-
## Middlewares
64-
65-
This package provides a set of middleware designed to enhance your Laravel microservice with common cross-cutting concerns. You can enable, disable or rename each middleware by editing the `middleware_aliases` section in `config/microservice.php`.
66-
67-
```php
68-
// config/microservice.php
69-
70-
'middleware_aliases' => [
71-
'jwt_auth' => 'jwt.auth', // alias for JWT Authentication middleware
72-
'correlation_id' => 'correlation.id', // alias for Correlation ID middleware
73-
'disabled_mw' => '', // empty string disables a middleware
74-
],
75-
```
76-
77-
### JWT Authentication
78-
79-
**Alias**: the string you assign to `jwt_auth` in `middleware_aliases`, e.g. `jwt.auth`.
80-
**Class**: `Kroderdev\LaravelMicroserviceCore\Http\Middleware\ValidateJwt`
81-
82-
#### Description
83-
84-
Validates the presence and integrity of a JSON Web Token (JWT) in the `Authorization` header of incoming requests. On success, the decoded payload is attached to the request for downstream use; on failure, a 401 response is returned.
85-
86-
#### Configuration (`config/microservice.php`)
87-
88-
```php
89-
'auth' => [
90-
'jwt_public_key' => env('JWT_PUBLIC_KEY_PATH'),
91-
'jwt_algorithm' => env('JWT_ALGORITHM', 'RS256'),
92-
'jwt_cache_ttl' => env('JWT_CACHE_TTL', 3600),
93-
'header' => 'Authorization',
94-
'prefix' => 'Bearer',
95-
],
96-
```
97-
98-
#### Usage
99-
100-
Register the middleware on routes or globally:
101-
102-
```php
103-
// In routes/api.php
104-
Route::middleware(['jwt.auth'])->group(function () {
105-
// protected routes…
106-
});
107-
```
108-
109-
#### Example Response on Failure
110-
```json
111-
// Missing or invalid token
112-
{
113-
"error": "unauthorized",
114-
"message": "Invalid or expired token"
115-
}
116-
```
117-
118-
### LoadAccess
119-
120-
**Alias**: the string you assign to `load_access` in `middleware_aliases`, e.g. `load.access`.
121-
**Class**: `Kroderdev\LaravelMicroserviceCore\Http\Middleware\LoadAccess`
122-
123-
#### Description
124-
125-
Loads the authenticated user's roles and permissions, typically from a centralized permission service (such as an API Gateway or dedicated permissions microservice).
126-
By default, the `ValidateJwt` middleware will automatically load `roles` and `permissions` from the JWT payload if they are present.
127-
However, if you have a centralized permission service, you can use `LoadAccess` to fetch and hydrate the latest roles and permissions for the user, ensuring up-to-date authorization data.
128-
129-
#### Configuration (`config/microservice.php`)
130-
131-
```php
132-
'permissions_cache_ttl' => env('PERMISSIONS_CACHE_TTL', 60),
133-
```
134-
135-
#### Usage
136-
137-
Apply after JWT authentication, or use the `microservice.auth` group for both:
138-
139-
```php
140-
// In routes/api.php
141-
Route::middleware(['jwt.auth', 'load.access'])->group(function () {
142-
// protected routes with up-to-date permissions…
143-
});
144-
145-
// Or simply:
146-
Route::middleware('microservice.auth')->group(function () {
147-
// protected routes…
148-
});
149-
```
150-
151-
---
152-
153-
### Permission Middleware
154-
155-
**Alias**: the string you assign to `permission` in `middleware_aliases`, e.g. `permission`.
156-
**Class**: `Kroderdev\LaravelMicroserviceCore\Http\Middleware\PermissionMiddleware`
157-
158-
#### Description
159-
160-
Restricts access to routes based on user permissions.
161-
Checks if the authenticated user has the required permission(s) before allowing access.
162-
Returns a 403 Forbidden response if the user lacks the necessary permission.
163-
164-
#### Usage
165-
166-
```php
167-
// In routes/api.php
168-
Route::middleware(['permission:orders.view'])->get('/orders', [OrderController::class, 'index']);
169-
Route::middleware(['permission:orders.create'])->post('/orders', [OrderController::class, 'store']);
170-
```
171-
172-
---
173-
174-
### Role Middleware
175-
176-
**Alias**: the string you assign to `role` in `middleware_aliases`, e.g. `role`.
177-
**Class**: `Kroderdev\LaravelMicroserviceCore\Http\Middleware\RoleMiddleware`
178-
179-
#### Description
180-
181-
Restricts access to routes based on user roles.
182-
Checks if the authenticated user has the required role(s) before allowing access.
183-
Returns a 403 Forbidden response if the user does not have the required role.
184-
185-
#### Usage
186-
187-
```php
188-
// In routes/api.php
189-
Route::middleware(['role:admin'])->get('/admin', [AdminController::class, 'dashboard']);
190-
Route::middleware(['role:manager'])->post('/reports', [ReportController::class, 'generate']);
191-
```
192-
193-
---
194-
195-
### Correlation ID
30+
## Publish Configuration
19631

197-
**Alias**: the string you assign to `correlation_id` in `middleware_aliases`, e.g. `correlation.id`.
198-
**Class**: `Kroderdev\LaravelMicroserviceCore\Http\Middleware\CorrelationId`
32+
To publish the configuration file, run:
19933

200-
#### Description
201-
202-
Generates or propagates a unique `X-Correlation-ID` header for each request, enabling end-to-end tracing across distributed systems. If the incoming request already has the header, it will be reused; otherwise, a new UUID is generated. The header is added to both the request and the response.
203-
204-
#### Configuration (`config/microservice.php`)
205-
206-
```php
207-
'correlation' => [
208-
'header' => 'X-Correlation-ID',
209-
'length' => 36,
210-
],
211-
```
212-
213-
#### Usage
214-
215-
Automatically prepended to the `api` middleware group (when enabled), or apply explicitly:
216-
217-
```php
218-
// In routes/api.php
219-
Route::middleware(['correlation.id'])->group(function () {
220-
// traced routes…
221-
});
222-
```
223-
224-
#### Example Header
225-
226-
```
227-
X-Correlation-ID: 123e4567-e89b-12d3-a456-426614174000
228-
```
229-
230-
---
231-
232-
### Auth Middleware Group
233-
234-
For convenience, you can use the built-in `microservice.auth` group, which runs:
235-
236-
1. **ValidateJwt** – decode JWT, hydrate `ExternalUser`, set `Auth::user()`
237-
2. **LoadAccess** – fetch roles & permissions via ApiGateway
238-
239-
#### Usage
240-
241-
```php
242-
// routes/api.php
243-
244-
Route::middleware('microservice.auth')->group(function () {
245-
// Here you already have a valid ExternalUser with roles & permissions loaded:
246-
Route::get('/orders', [OrderController::class, 'index']);
247-
Route::post('/orders', [OrderController::class, 'store']);
248-
});
249-
```
250-
251-
You no longer need to stack `jwt.auth` + `load.access` manually—just use `microservice.auth` wherever you need full auth + authorization.
252-
253-
---
254-
255-
### Authorization
256-
257-
This package hooks into Laravel's Gate so Blade directives work with your roles and permissions. Any ability is treated as a permission by default; prefix an ability with `role:` or `permission:` to be explicit.
258-
259-
```blade
260-
@can('posts.create')
261-
<!-- user can create posts -->
262-
@endcan
263-
264-
@cannot('permission:posts.delete')
265-
<!-- no delete rights -->
266-
@endcannot
267-
268-
@canany(['role:admin', 'permission:posts.update'])
269-
<!-- admin or user with update permission -->
270-
@endcanany
271-
```
272-
273-
---
274-
275-
## Endpoints
276-
277-
### Health Check Endpoint
278-
279-
This package exposes a JSON endpoint at `/api/health` providing basic service details.
280-
281-
#### Configuration (`config/microservice.php`)
282-
283-
```php
284-
'health' => [
285-
'enabled' => env('HEALTH_ENDPOINT_ENABLED', true),
286-
'path' => '/api/health',
287-
],
288-
```
289-
290-
When enabled (default), visiting `/api/health` returns:
291-
292-
```json
293-
{
294-
"status": "ok",
295-
"app": "your-app-name",
296-
"environment": "testing",
297-
"laravel": "12.x-dev",
298-
"timestamp": "2025-01-01T12:00:00Z"
299-
}
34+
```bash
35+
php artisan vendor:publish --provider="Kroderdev\LaravelMicroserviceCore\Providers\MicroserviceServiceProvider"
30036
```
30137

302-
### HTTP Client Macros
303-
304-
Use `Http::apiGateway()` and its related macros for service-to-service calls. Each macro forwards the current correlation ID header when available.
305-
306-
If the gateway responds with an error (for example, a `503`), an `ApiGatewayException` is thrown and automatically returned to the client with the same status code.
307-
308-
---
309-
310-
## Public Release and Roadmap
311-
312-
This toolkit is still maturing. Upcoming plans include:
313-
314-
- Strengthening middleware modules
315-
- Increasing test coverage
316-
- Publishing more integration examples
317-
318-
A stable **v1.0** is on the horizon. Please share feedback or ideas in the issue tracker!
319-
320-
---
321-
322-
## Extending the Core
323-
324-
This package keeps its dependencies minimal so you can tailor each microservice as needed. Consider these optional packages when expanding your stack:
325-
326-
- **RabbitMQ Messaging** – Integrate [iamfarhad/LaravelRabbitMQ](https://github.com/iamfarhad/LaravelRabbitMQ) if you rely on RabbitMQ for queues. Install the package via Composer and update your queue connection to `rabbitmq`.
327-
- **Prometheus Metrics** – Expose service metrics with [spatie/laravel-prometheus](https://github.com/spatie/laravel-prometheus). Publish the configuration and register the `/metrics` route in your service.
328-
- **Additional Tools** – Depending on your requirements, you might add packages for service discovery, distributed tracing, or improved concurrency (e.g., Laravel Octane or Horizon).
329-
330-
These extensions remain completely optional, allowing you to keep the core lightweight while adding features specific to your environment.
331-
33238
---
33339

33440
## Documentation
33541

336-
Additional guides and examples are available in the [wiki](https://github.com/KroderDev/laravel-microservice-core/wiki).
337-
338-
---
339-
340-
## Contributing
341-
342-
Community contributions are encouraged! To get started:
343-
344-
1. Fork the repository and create a feature branch.
345-
2. Commit your changes with tests when possible.
346-
3. Open a pull request describing your work.
347-
348-
Feel free to start a discussion or issue if you have questions about the roadmap or want to propose a feature.
42+
See the [wiki](https://github.com/KroderDev/laravel-microservice-core/wiki) for full documentation.

0 commit comments

Comments
 (0)