2024-10-27 06:10:27 +01:00
# Goma Gateway - simple lightweight API Gateway and Reverse Proxy.
```
_____
/ ____ |
| | __ ___ _ __ ___ __ _
| | |_ |/ _ \| '_ ` _ \ / _` |
| |__| | (_) | | | | | | (_| |
\_____|\___/|_| |_| |_|\__,_|
```
Goma Gateway is a lightweight API Gateway and Reverse Proxy.
2024-10-29 09:39:31 +01:00
Simple, easy to use, and configure.
2024-10-27 07:24:50 +01:00
[](https://github.com/jkaninda/goma-gateway/actions/workflows/release.yml)
2024-10-29 09:39:31 +01:00
[](https://goreportcard.com/report/github.com/jkaninda/goma-gateway)
2024-10-27 06:10:27 +01:00
[](https://pkg.go.dev/github.com/jkaninda/goma-gateway)

## Links:
- [Docker Hub ](https://hub.docker.com/r/jkaninda/goma-gateway )
- [Github ](https://github.com/jkaninda/goma-gateway )
### Feature
- [x] Reverse proxy
- [x] API Gateway
2024-10-29 09:39:31 +01:00
- [x] Domain/host based request routing
- [x] Multi domain request routing
2024-10-27 06:10:27 +01:00
- [x] Cors
- [ ] Support TLS
2024-10-29 09:39:31 +01:00
- [x] Backend errors interceptor
2024-10-27 06:10:27 +01:00
- [x] Authentication middleware
- [x] JWT `HTTP Bearer Token`
- [x] Basic-Auth
2024-10-29 09:39:31 +01:00
- [ ] OAuth
2024-10-27 06:10:27 +01:00
- [x] Implement rate limiting
- [x] In-Memory Token Bucket based
- [x] In-Memory client IP based
- [ ] Distributed Rate Limiting for Token based across multiple instances using Redis
2024-10-29 09:39:31 +01:00
- [ ] Distributed Rate Limiting for In-Memory client IP based across multiple instances using Redis
2024-10-27 06:10:27 +01:00
## Usage
### 1. Initialize configuration
```shell
docker run --rm --name goma-gateway \
-v "${PWD}/config:/config" \
jkaninda/goma-gateway config init --output /config/goma.yml
```
### 2. Run server
```shell
docker run --rm --name goma-gateway \
-v "${PWD}/config:/config" \
-p 80:80 \
jkaninda/goma-gateway server
```
### 3. Start server with a custom config
```shell
docker run --rm --name goma-gateway \
-v "${PWD}/config:/config" \
-p 80:80 \
jkaninda/goma-gateway server --config /config/config.yml
```
### 4. Healthcheck
2024-10-27 07:24:50 +01:00
- Goma Gateway readiness: `/readyz`
- Routes health check: `/healthz`
2024-10-27 06:10:27 +01:00
[http://localhost/healthz ](http://localhost/healthz )
2024-10-27 07:24:50 +01:00
[http://localhost/readyz ](http://localhost/readyz )
2024-10-27 06:10:27 +01:00
> Healthcheck response body
```json
{
"status": "healthy",
"routes": [
{
"name": "Store",
"status": "healthy",
"error": ""
},
{
"name": "Authentication service",
"status": "unhealthy",
"error": "error performing HealthCheck request: Get \"http://authentication-service:8080/internal/health/ready\": dial tcp: lookup authentication-service on 127.0.0.11:53: no such host "
},
{
"name": "Notification",
"status": "undefined",
"error": ""
}
]
}
```
Create a config file in this format
## Customize configuration file
Example of configuration file
```yaml
## Goma - simple lightweight API Gateway and Reverse Proxy.
# Goma Gateway configurations
gateway:
########## Global settings
listenAddr: 0.0.0.0:80
# Proxy write timeout
writeTimeout: 15
# Proxy read timeout
readTimeout: 15
# Proxy idle timeout
idleTimeout: 60
# Proxy rate limit, it's In-Memory Token Bucket
# Distributed Rate Limiting for Token based across multiple instances is not yet integrated
rateLimiter: 0
accessLog: "/dev/Stdout"
errorLog: "/dev/stderr"
## Returns backend route healthcheck errors
disableRouteHealthCheckError: false
# Disable display routes on start
disableDisplayRouteOnStart: false
2024-10-29 09:39:31 +01:00
# interceptErrors intercepts backend errors based on defined the status codes
interceptErrors:
2024-10-29 19:38:43 +01:00
- 405
- 500
2024-10-29 09:39:31 +01:00
# - 400
2024-10-27 06:10:27 +01:00
# Proxy Global HTTP Cors
cors:
2024-10-28 10:16:44 +01:00
# Global routes cors for all routes
2024-10-27 06:10:27 +01:00
origins:
2024-10-29 19:38:43 +01:00
- http://localhost:8080
2024-10-27 06:10:27 +01:00
- https://example.com
2024-10-28 10:16:44 +01:00
# Global routes cors headers for all routes
2024-10-27 06:10:27 +01:00
headers:
Access-Control-Allow-Headers: 'Origin, Authorization, Accept, Content-Type, Access-Control-Allow-Headers, X-Client-Id, X-Session-Id'
Access-Control-Allow-Credentials: 'true'
Access-Control-Max-Age: 1728000
##### Define routes
routes:
# Example of a route | 1
2024-10-29 19:38:43 +01:00
- name: Public
2024-10-28 04:10:24 +01:00
# host Domain/host based request routing
2024-10-29 19:38:43 +01:00
host: ""
path: /public
2024-10-27 06:10:27 +01:00
## Rewrite a request path
# e.g rewrite: /store to /
2024-10-29 19:38:43 +01:00
rewrite: /healthz
destination: https://example.com
2024-10-27 06:10:27 +01:00
#DisableHeaderXForward Disable X-forwarded header.
# [X-Forwarded-Host, X-Forwarded-For, Host, Scheme ]
# It will not match the backend route, by default, it's disabled
disableHeaderXForward: false
# Internal health check
2024-10-29 19:38:43 +01:00
healthCheck: '' #/internal/health/ready
2024-10-28 10:16:44 +01:00
# Route Cors, global cors will be overridden by route
2024-10-27 06:10:27 +01:00
cors:
2024-10-28 10:16:44 +01:00
# Route Origins Cors, global cors will be overridden by route
origins:
- https://dev.example.com
- http://localhost:3000
2024-10-29 19:38:43 +01:00
- https://example.com
2024-10-29 22:55:09 +01:00
# Route Cors headers, route will override global cors
2024-10-27 06:10:27 +01:00
headers:
Access-Control-Allow-Methods: 'GET'
Access-Control-Allow-Headers: 'Origin, Authorization, Accept, Content-Type, Access-Control-Allow-Headers, X-Client-Id, X-Session-Id'
Access-Control-Allow-Credentials: 'true'
Access-Control-Max-Age: 1728000
#### Define route blocklist paths
blocklist:
- /swagger-ui/*
- /v2/swagger-ui/*
- /api-docs/*
- /internal/*
- /actuator/*
##### Define route middlewares from middlewares names
## The name must be unique
## List of middleware name
middlewares:
# path to protect
2024-10-29 19:38:43 +01:00
- path: /user
# Rules defines which specific middleware applies to a route path
rules:
2024-10-29 22:55:09 +01:00
- basic-auth
2024-10-29 19:38:43 +01:00
# path to protect
- path: /path-example
# Rules defines which specific middleware applies to a route path
rules:
2024-10-29 22:55:09 +01:00
- jwtAuth
2024-10-29 19:38:43 +01:00
# path to protect
- path: /admin
2024-10-27 06:10:27 +01:00
# Rules defines which specific middleware applies to a route path
rules:
2024-10-29 22:55:09 +01:00
- basic-auth
2024-10-27 06:10:27 +01:00
# path to protect
2024-10-29 19:38:43 +01:00
- path: /path-example
2024-10-27 06:10:27 +01:00
# Rules defines which specific middleware applies to a route path
rules:
2024-10-29 22:55:09 +01:00
- jwtAuth
2024-10-27 06:10:27 +01:00
- path: /history
http:
url: http://security-service:8080/security/authUser
headers:
#Key from backend authentication header, and inject to the request with custom key name
userId: X-Auth-UserId
userCountryId: X-Auth-UserCountryId
params:
userCountryId: X-countryId
# Example of a route | 2
- name: Authentication service
path: /auth
rewrite: /
destination: 'http://security-service:8080'
healthCheck: /internal/health/ready
cors: {}
blocklist: []
middlewares: []
# Example of a route | 3
2024-10-29 19:38:43 +01:00
- name: Basic auth
path: /protected
2024-10-27 06:10:27 +01:00
rewrite: /
destination: 'http://notification-service:8080'
healthCheck:
cors: {}
blocklist: []
middlewares: []
#Defines proxy middlewares
middlewares:
# Enable Basic auth authorization based
2024-10-29 22:55:09 +01:00
- name: local-auth-basic
2024-10-29 19:38:43 +01:00
# Authentication types | jwtAuth, basicAuth, auth0
2024-10-29 22:55:09 +01:00
type: basic
2024-10-27 06:10:27 +01:00
rule:
username: admin
password: admin
#Enables JWT authorization based on the result of a request and continues the request.
2024-10-29 22:55:09 +01:00
- name: google-auth
2024-10-29 19:38:43 +01:00
# Authentication types | jwtAuth, basicAuth, OAuth
2024-10-29 09:39:31 +01:00
# jwt authorization based on the result of backend's response and continue the request when the client is authorized
2024-10-29 22:55:09 +01:00
type: jwt
2024-10-27 06:10:27 +01:00
rule:
2024-10-29 19:38:43 +01:00
# This is an example URL
2024-10-27 06:10:27 +01:00
url: https://www.googleapis.com/auth/userinfo.email
# Required headers, if not present in the request, the proxy will return 403
requiredHeaders:
- Authorization
#Sets the request variable to the given value after the authorization request completes.
#
# Add header to the next request from AuthRequest header, depending on your requirements
# Key is AuthRequest's response header Key, and value is Request's header Key
# In case you want to get headers from the Authentication service and inject them into the next request's headers
#Sets the request variable to the given value after the authorization request completes.
#
# Add header to the next request from AuthRequest header, depending on your requirements
# Key is AuthRequest's response header Key, and value is Request's header Key
# In case you want to get headers from the Authentication service and inject them into the next request's headers
headers:
userId: X-Auth-UserId
userCountryId: X-Auth-UserCountryId
# In case you want to get headers from the Authentication service and inject them to the next request's params
params:
2024-10-28 02:47:52 +01:00
userCountryId: countryId
2024-10-27 06:10:27 +01:00
```
## Requirement
- Docker