{"id":25369724,"url":"https://github.com/albertkarapetyan/api-gateway","last_synced_at":"2025-10-24T00:57:50.984Z","repository":{"id":276328713,"uuid":"928618661","full_name":"AlbertKarapetyan/api-gateway","owner":"AlbertKarapetyan","description":"This API Gateway is built in Go and provides load balancing between multiple backend services. It supports multiple load balancing strategies, including \"Round Robin\" and \"Least Connections\"","archived":false,"fork":false,"pushed_at":"2025-02-07T22:05:01.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-15T01:34:11.639Z","etag":null,"topics":["api-gateway","autoreload-config","dependencyinjection","dynamic-backend","flexibility","health-check","jwt-authentication","jwt-claims","jwt-token","least-connections","load-balancer","middleware","reverse-proxy","round-robin","routing","scalability","signingmethodhs256","validation-rules","wire","zero-downtime"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlbertKarapetyan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-06T23:45:09.000Z","updated_at":"2025-02-07T22:05:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"4855a5fe-d38e-43d9-8620-3ae03bcfc4bd","html_url":"https://github.com/AlbertKarapetyan/api-gateway","commit_stats":null,"previous_names":["albertkarapetyan/api-gateway"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertKarapetyan%2Fapi-gateway","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertKarapetyan%2Fapi-gateway/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertKarapetyan%2Fapi-gateway/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbertKarapetyan%2Fapi-gateway/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlbertKarapetyan","download_url":"https://codeload.github.com/AlbertKarapetyan/api-gateway/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247994135,"owners_count":21030050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-gateway","autoreload-config","dependencyinjection","dynamic-backend","flexibility","health-check","jwt-authentication","jwt-claims","jwt-token","least-connections","load-balancer","middleware","reverse-proxy","round-robin","routing","scalability","signingmethodhs256","validation-rules","wire","zero-downtime"],"created_at":"2025-02-15T01:31:08.463Z","updated_at":"2025-10-24T00:57:45.937Z","avatar_url":"https://github.com/AlbertKarapetyan.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# API Gateway with Load Balancer\n\n## Description\nThis API Gateway is built in Go and provides load balancing between multiple backend services. It supports multiple load balancing strategies, including:\n\n- **Round Robin**: Requests are distributed evenly across backend servers.\n- **Least Connections**: Requests are directed to the backend with the fewest active connections.\n\n## Features\n- Dynamic backend server registration\n- Configurable load balancing strategies\n- Reverse proxy for handling client requests\n- Health checks for backend services\n- Test servers for development and debugging\n- Dynamic Routing with Auto-Reload: Automatically reloads routing configuration without restarting the server.\n- JWT Authentication Middleware for secure access control\n- Public Routes support for endpoints that do not require authentication\n\n## Branches\nThis repository contains multiple branches for different versions of the API Gateway:\n\n### [feature-simple-load-balancer](https://github.com/AlbertKarapetyan/api-gateway/tree/feature-simple-load-balancer)\nThis branch contains a **basic load balancer** implementation that distributes requests across multiple backend services using simple round-robin or least connections algorithms.\n\n### [feature-routing](https://github.com/AlbertKarapetyan/api-gateway/tree/feature-routing)\nThis branch adds **dynamic routing** capabilities, allowing requests to be forwarded to different backend services based on the path defined in the configuration file.\n\n### [feature-check-jwt](https://github.com/AlbertKarapetyan/api-gateway/tree/feature-check-jwt)\nThis branch introduces **JWT authentication middleware**, ensuring that only authorized requests can access protected routes, while allowing public routes to remain accessible without authentication.\n\n## Installation\n\n### Prerequisites\n- [Go](https://golang.org/dl/) (version 1.18 or later)\n\n### Steps\n1. Clone the repository:\n   ```sh\n   git clone \u003crepository-url\u003e\n   cd api-gateway\n   ```\n2. Install dependencies:\n   ```sh\n   go mod tidy\n   ```\n3. Configure the gateway by editing `config.json`:\n   ```json\n   {\n     \"load_balancer\": \"round_robin\",\n     \"health_check_interval\": 5,\n     \n     \"servers\": {\n        \"user\": [\n          \"http://localhost:8081\",\n          \"http://localhost:8082\"\n        ],\n        \"wallet\": [\n          \"http://localhost:8083\",\n          \"http://localhost:8084\"\n        ]\n      },\n      \"routes\": {\n        \"user/signin\": \"/api/auth\",\n        \"user/signup\": \"/api/register\",\n        \"wallet/get_balance\": \"/api/get_balance\"\n      },\n      \"public_routes\": {\n        \"/user/signin\": true,\n        \"/user/signup\": true\n      },\n      \"secret_key\": \"your-secret-key\"\n   }\n   ```\n4. Start the gateway:\n   ```sh\n   go run main.go\n   ```\n\n## Running Test Servers\nFor testing, you can start mock backend servers:\n```sh\ncd testServers/server1 \u0026\u0026 go run main.go \u0026\ncd testServers/server2 \u0026\u0026 go run main.go \u0026\ncd testServers/server3 \u0026\u0026 go run main.go \u0026\n```\n\n## Usage\nOnce running, send requests to the gateway:\n```sh\ncurl http://localhost:8080/user/signin\n```\nThe gateway will forward the request to a backend server based on the load balancing strategy.\n\n---\n## JWT Authentication Middleware\n\nThe API Gateway includes JWT-based authentication to secure access to protected endpoints. Requests must include a valid JWT token in the `Authorization` header:\n\n### Middleware Implementation\n- The middleware extracts the JWT token from the request header.\n- It verifies the token signature using the configured secret key.\n- If the token is valid, the request proceeds to the backend service.\n- If the token is missing or invalid, the request is rejected with a `401 Unauthorized` response.\n\n### Example Request with JWT\n```sh\ncurl -H \"Authorization: Bearer \u003cyour-jwt-token\u003e\" http://localhost:8080/protected-route\n```\n\n### Public Routes\nSome endpoints, such as authentication-related routes, should be accessible without requiring a JWT token. These routes are specified in the `config.json` file under `public_routes`:\n\n```json\n\"public_routes\": {\n  \"/user/signin\": true,\n  \"/user/signup\": true\n}\n```\n\nThe middleware will bypass authentication for these routes, allowing unauthenticated users to access them.\n\n---\n## Dynamic Routing with Auto-Reload\nThe API Gateway supports **dynamic routing** and **auto-reloading** of the configuration without requiring a server restart. This feature allows you to update the `config.json` file while the server is running, and the gateway will automatically apply the changes.\n\n### How It Works\n1. **Dynamic Routing:**\n- Routes are defined in the `config.json` file under the `routes` section.\n- Each route maps an API gateway path (e.g., `/user/signin`) to a backend service path (e.g., `/api/auth`).\n- The gateway dynamically routes requests to the appropriate backend service based on the configuration.\n\n2. **Auto-Reload:**\n- The gateway monitors the `config.json` file for changes.\n- When a change is detected, the gateway reloads the configuration and updates the routing and server lists.\n- No server restart is required—changes take effect within a few seconds.\n\n### Example\n1. Update `config.json` to add a new route:\n```json\n{\n  \"routes\": {\n    \"user/signin\": \"/api/auth\",\n    \"user/signup\": \"/api/register\",\n    \"wallet/get_balance\": \"/api/get_balance\",\n    \"wallet/transactions\": \"/api/transactions\" // New route\n  }\n}\n```\n2. Save the file. The gateway will automatically reload the configuration and start routing requests for `/wallet/transactions` to the specified backend path.\n\n### Benefits\n- **Zero Downtime:** Update routes and servers without restarting the gateway.\n- **Flexibility:** Easily add, remove, or modify routes and backend servers.\n- **Scalability:** Supports multiple services and routes dynamically.\n\n---\n\n## License\nMIT License\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falbertkarapetyan%2Fapi-gateway","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falbertkarapetyan%2Fapi-gateway","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falbertkarapetyan%2Fapi-gateway/lists"}