Files
gogwapi/README.md
Björn Benouarets 0c9d70df97 feat(proxy): Implement reverse proxy with path transformation
Add reverse proxy functionality that routes requests based on host headers and transforms paths by removing configured prefixes. Includes comprehensive documentation and configuration files.

- Add reverse proxy with host-based routing
- Implement path transformation and URL rewriting
- Add YAML-based configuration for targets and endpoints
- Include PostgreSQL integration with GORM models
- Add comprehensive documentation and README
- Add Docker configuration for deployment
2025-11-29 03:10:40 +01:00

222 lines
5.0 KiB
Markdown

# GoGWAPI - Gateway API Service
A high-performance reverse proxy service written in Go that manages domain routing, path transformation, and traffic forwarding to multiple backend services.
## Features
- **Reverse Proxy**: Routes incoming requests to backend services based on host and path
- **Path Transformation**: Removes configured prefixes from request paths
- **YAML Configuration**: Simple configuration-based routing rules
- **PostgreSQL Integration**: Stores configuration and routing data in PostgreSQL
- **Automatic Migration**: Database schema is automatically migrated on startup
- **Structured Logging**: Comprehensive logging with masterlog integration
## Architecture
The service routes requests based on the following hierarchy:
```
Target Domain (e.g., secnex.io)
└── Record (e.g., patchxg → patchxg.secnex.io)
└── Endpoint (e.g., /api → http://backend-service:8080)
```
When a request to `patchxg.secnex.io/api/test` is received:
1. The service matches the host to the target and record
2. Finds the matching endpoint by path prefix (`/api`)
3. Transforms the path by removing the prefix (`/api/test``/test`)
4. Forwards the request to the backend URL (`http://backend-service:8080/test`)
## Configuration
The service is configured via a YAML file (default: `config.yaml`):
```yaml
server:
host: 0.0.0.0
port: 3000
database:
host: localhost
port: 5432
user: postgres
password: postgres
database: gogwapi
targets:
- name: "secnex.io"
group: "secnex"
records:
- record: "patchxg"
endpoints:
- path: "/api"
url: "https://httpbin.org"
```
### Configuration Parameters
- **Server**: HTTP server configuration
- **Database**: PostgreSQL connection parameters
- **Targets**: Array of routing targets
- **name**: Target domain (e.g., "secnex.io")
- **group**: Optional grouping for organization
- **records**: Array of DNS records
- **record**: DNS record name (e.g., "patchxg")
- **endpoints**: Array of endpoint configurations
- **path**: URL path prefix to match (e.g., "/api")
- **url**: Backend service URL to forward to
## Development
### Prerequisites
- Go 1.25.3 or later
- PostgreSQL 12 or later
- Git
### Setup
1. Clone the repository:
```bash
git clone <repository-url>
cd gogwapi/app
```
2. Install dependencies:
```bash
go mod tidy
```
3. Configure the database:
```bash
# Create database
createdb gogwapi
# Update config.yaml with your database credentials
```
4. Run the service:
```bash
go run .
```
### Building
```bash
# Build for current platform
go build -o gogwapi .
# Build for production
CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o gogwapi .
```
### Testing
```bash
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Test specific package
go test ./proxy
```
## Usage
### Basic Proxy Example
With the example configuration, requests are routed as follows:
- `patchxg.secnex.io/api/users``https://httpbin.org/users`
- `patchxg.secnex.io/api/posts/123``https://httpbin.org/posts/123`
### Headers Added
The proxy adds the following headers to forwarded requests:
- `X-Forwarded-Host`: Original request host
- `X-Forwarded-For`: Client IP address
- `X-Forwarded-Path`: Original request path for debugging
### Environment Variables
Database configuration can be overridden with environment variables:
- `DB_HOST`: Database host (default: localhost)
- `DB_PORT`: Database port (default: 5432)
- `DB_USER`: Database user (default: postgres)
- `DB_PASSWORD`: Database password (default: postgres)
- `DB_DATABASE`: Database name (default: gogwapi)
- `SERVER_PORT`: Server port (default: 3000)
## Data Model
The service uses the following database schema:
- **domains**: Target domain information
- **records**: DNS records for domains
- **endpoints**: API endpoints with path mappings
- **endpoint_groups**: Grouping for endpoints
## Performance
The service is optimized for high-throughput scenarios:
- HTTP/2 support
- Efficient routing lookup
- Minimal request overhead
- Connection pooling for database operations
## Production Deployment
### Docker
```dockerfile
FROM golang:1.25.3-alpine AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o gogwapi .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/gogwapi .
COPY --from=builder /app/config.yaml .
EXPOSE 3000
CMD ["./gogwapi"]
```
### Kubernetes
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: gogwapi
spec:
replicas: 3
selector:
matchLabels:
app: gogwapi
template:
metadata:
labels:
app: gogwapi
spec:
containers:
- name: gogwapi
image: gogwapi:latest
ports:
- containerPort: 3000
env:
- name: DB_HOST
value: "postgres-service"
```
## License
[License information]
## Support
For support and questions, please create an issue in the repository or contact the development team.