7.6 KiB
7.6 KiB
Architecture
System Overview
The SecNex API Gateway follows a modular architecture with clear separation of concerns. The system is built using the chi/v5 HTTP router and implements a middleware pipeline pattern.
┌────────────────────────────────────────────────────────────┐
│ Client Request │
└─────────────────────────┬──────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────┐
│ Gateway (chi Router) │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Global Middleware Pipeline │ │
│ │ ┌────────────┐ ┌────────────┐ ┌──────────────┐ │ │
│ │ │ Request ID │→ │ Real IP │→ │ Logger │ │ │
│ │ └────────────┘ └────────────┘ └──────────────┘ │ │
│ └──────────────────────────────────────────────────────┘ │
└─────────────────────────┬──────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────┐
│ Route Handler │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Per-Route Middleware Chain │ │
│ │ ┌────────────┐ ┌──────────────┐ │ │
│ │ │ Strip │→ │ Reverse │ │ │
│ │ │ Prefix │ │ Proxy │ │ │
│ │ └────────────┘ └──────────────┘ │ │
│ └──────────────────────────────────────────────────────┘ │
└─────────────────────────┬──────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────────┐
│ Reverse Proxy (httputil.ReverseProxy) │
│ │ │
│ ▼ │
│ Backend Service │
└────────────────────────────────────────────────────────────┘
Component Architecture
Directory Structure
app/
├── main.go # Application entry point
├── config/ # Configuration management
│ ├── config.go # Config interface
│ ├── file.go # File-based config loader
│ └── database.go # Database configuration
├── server/ # Core server components
│ ├── gateway.go # Main gateway server
│ ├── routes.go # Route registration
│ ├── api.go # API definitions
│ ├── host.go # Host definitions
│ └── target.go # Target (backend) definitions
├── middlewares/ # HTTP middleware
│ ├── host.go # Host logging middleware
│ └── logger.go # Structured logging middleware
└── utils/ # Utility functions
Core Components
Gateway (server/gateway.go)
The Gateway is the main server component that:
- Initializes the chi router
- Applies global middleware (request_id, real_ip, logger, host)
- Configures proxy directors for each API
- Registers route handlers
- Starts the HTTP server
Routes (server/routes.go)
The Routes component handles:
- Creating route handlers from configuration
- Applying strip prefix middleware
- Registering routes with chi router (method-agnostic)
- Connecting routes to API backends
API (server/api.go)
API definitions that:
- Link hosts to backend targets
- Implement
http.Handlerinterface for proxying - Delegate requests to the reverse proxy
Hosts (server/host.go)
Host definitions for:
- Virtual hosting support
- Domain-based routing
- Host header validation
Targets (server/target.go)
Target (backend) definitions that:
- Store backend service URLs
- Create
httputil.ReverseProxyinstances - Handle proxy configuration
Middleware Chain
Global Middleware
Applied to all requests via chi middleware:
| Feature | Description |
|---|---|
request_id |
Adds unique request ID to context |
real_ip |
Determines the real client IP from headers |
logger |
Logs HTTP requests with structured JSON output |
host |
Logs the host header for each request |
Per-Route Middleware
Applied to each route handler:
- StripPrefix - Removes specified prefix from request path before proxying
Request Flow
- Client Request → Gateway receives HTTP request
- Global Middleware → Request ID, Real IP, Host logging, Logger applied
- Route Matching → Chi matches route pattern (e.g.,
/api/v1/*) - Per-Route Middleware → StripPrefix (if enabled)
- Reverse Proxy → Request forwarded to backend API
- Response → Backend response returned to client
Configuration Flow
- Load
gateway.yamlviaconfig.NewFile() - Parse YAML into
Configurationstruct - Create Hosts from configuration
- Create Targets from configuration
- Create APIs (linking Hosts to Targets)
- Create Routes (linking Routes to APIs)
- Initialize Gateway with all components
- Configure proxy directors
- Register routes with chi router
- Start HTTP server
Logging
The gateway uses structured JSON logging via masterlog:
- HTTP Request Logging - method, path, status, duration, host, IP
- Gateway Events - startup, route registration, proxy configuration
- Sensitive Field Pseudonymization - user_id, email, ip fields are pseudonymized
Example log output:
{
"level": "info",
"msg": "HTTP Request",
"method": "GET",
"path": "/api/v1/users",
"status": 200,
"duration": "45ms",
"host": "localhost:8080",
"ip": "127.0.0.1:52342"
}