Part 5️⃣ - Ocelot Gateway API๐ Build .NET 8 eCommerce Microservice ๐with API Gateway, Rate Limiting, Caching & more ๐งบ
Ocelot Configuration File - ocelot.json
This configuration file sets up routes for an API Gateway using Ocelot. The gateway manages requests for three main services: Authentication, Product, and Order APIs. Here's a detailed description of each section:
Authentication Routes
- Path:
/api/authentication/{everything}
- Host:
localhost:5000
- Methods: GET, POST, PUT, DELETE
- Rate Limiting: Allows 1 request per 10 seconds.
Product Routes
- Get All Products
- Path:
/api/products
- Host:
localhost:5001
- Method: GET
- Caching: 60 seconds with custom header
eCommerce-Caching-Control
.
- Path:
- Get Product By ID
- Path:
/api/products/{id}
- Host:
localhost:5001
- Method: GET
- Caching: 60 seconds with custom header
eCommerce-Caching-Control
.
- Path:
- Modify Products
- Path:
/api/products/{everything}
- Host:
localhost:5001
- Methods: POST, PUT, DELETE
- Authentication: Bearer token required.
- Path:
Order Routes
- Get All Orders
- Path:
/api/orders
- Host:
localhost:5002
- Method: GET
- Caching: 20 seconds with custom header
eCommerce-Caching-Control
. - Authentication: Bearer token required.
- Path:
- Get Order By ID
- Path:
/api/orders/{id}
- Host:
localhost:5002
- Method: GET
- Caching: 60 seconds with custom header
eCommerce-Caching-Control
. - Authentication: Bearer token required.
- Path:
- Modify Orders
- Path:
/api/orders/{everything}
- Host:
localhost:5002
- Methods: POST, PUT, DELETE
- Authentication: Bearer token required.
- Path:
Global Configuration
- Base URL:
https://localhost:5003
- Rate Limiting: Custom message for exceeding quota: "You are overwhelming me with multiple requests. Relax small."
This setup ensures that your API Gateway effectively manages and routes requests to your microservices, implementing rate limiting, caching, and authentication where necessary.
Program Registration File - Program.cs
Configuration and Services Setup
Add Ocelot Configuration File
- Loads the Ocelot configuration from
ocelot.json
, ensuring it's mandatory and can be reloaded dynamically on changes.
- Loads the Ocelot configuration from
Add JWT Authentication Scheme
- Configures JWT-based authentication using the application's configuration settings for secure access control.
Add Ocelot and Cache Manager
- Integrates Ocelot middleware to handle API Gateway functionalities and configures an in-memory caching mechanism with Cache Manager to improve performance.
Add CORS Policy
- Configures Cross-Origin Resource Sharing (CORS) to allow any header, method, and origin, enabling the application to handle requests from different domains.
Middleware and Application Pipeline
Custom Middleware
- Adds a middleware component that attaches a signature to each request, enhancing security or tracking.
HTTPS Redirection
- Ensures all HTTP requests are redirected to HTTPS, enhancing security by enforcing encrypted communication.
CORS Middleware
- Enables the configured CORS policy, allowing the application to accept cross-origin requests.
Ocelot Middleware
- Activates Ocelot to manage request routing, load balancing, and other API Gateway functionalities.
Interception Middleware - AttachSignatureToRequest.cs
AttachSignatureToRequest Middleware
- Adds a custom middleware that injects a signature into the request headers for enhanced security or tracking. This middleware adds a header
Api-Gateway
with the valueSigned
.
- Adds a custom middleware that injects a signature into the request headers for enhanced security or tracking. This middleware adds a header
HTTPS Redirection
- Ensures all HTTP requests are redirected to HTTPS, enhancing security by enforcing encrypted communication.
CORS Middleware
- Enables the configured CORS policy, allowing the application to accept cross-origin requests.
Ocelot Middleware
- Activates Ocelot to manage request routing, load balancing, and other API Gateway functionalities.
By setting up these configurations and services, the application ensures secure, efficient, and flexible handling of requests, while also supporting dynamic configuration updates and enhanced cross-origin resource sharing.
# Here's a follow-up section to encourage engagement and support for Netcode-Hub:
๐ Get in touch with Netcode-Hub! ๐ซ
1. GitHub: [Explore Repositories] ๐
2. Twitter: [Stay Updated] ๐ฆ
3. Facebook: [Connect Here]๐
4. LinkedIn: [Professional Network]๐
5. Email: [business.netcodehub@gmail.com] ๐ง
# ☕️ If you've found value in Netcode-Hub's work, consider supporting the channel with a coffee!
Comments
Post a Comment