3.9 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
SecNex API Gateway is a Go-based API gateway built with chi/v5 routing. It acts as a reverse proxy with configurable features including path-based routing, prefix stripping, and structured logging.
Build and Run
cd app
go run main.go
The gateway loads configuration from gateway.yaml (sibling to the app/ directory).
Architecture
Directory Structure
app/
├── main.go # Entry point
├── config/ # Configuration loading and types
├── server/ # Gateway, routes, hosts, targets, and API definitions
├── middlewares/ # HTTP middleware (logger, host)
└── utils/ # Utility functions
Core Components
Gateway (server/gateway.go)
- Main server using chi/v5 router
- Dynamically enables features via config:
request_id,real_ip,logger,host - Configures proxy directors for each API
- Registers route handlers and starts HTTP server
Routes (server/routes.go)
- Creates handlers for each route configured in
gateway.yaml - For each route:
- Finds the matching API backend target
- Creates a handler with optional strip prefix middleware
- Registers with chi router using
r.Handle()(method-agnostic)
API (server/api.go)
- Links hosts to backend targets
- Implements
http.Handlerinterface - Delegates to the reverse proxy
Hosts (server/host.go)
- Host definitions for virtual hosting
- Domain-based routing support
Targets (server/target.go)
- Backend service definitions
- Creates
httputil.NewSingleHostReverseProxyinstances
Configuration Flow
config.NewFile("../gateway.yaml")loads YAMLserver.NewGateway(cfg)creates gateway with chi routerserver.NewRoutes(cfg, apis)creates route handlersroutes.Register(g.router)registers handlers- Gateway starts HTTP server
Middleware Chain
Global Middleware (applied via chi middleware):
- RequestID - adds unique request ID
- RealIP - determines real client IP
- Logger - structured JSON logging
- Host - logs host header
Per-Route Middleware (applied in order):
- StripPrefix - removes prefix from path before proxying (if enabled)
Key Implementation Details
Strip Prefix Middleware (server/routes.go)
- Removes specified prefix from request path before proxying
- Ensures resulting path starts with
/
Route Handler Pattern
Each route in gateway.yaml must have:
id- unique route identifierapi- API ID to use (references an API entry)path- chi route pattern (e.g.,/api/v1/*)strip_prefix- optional prefix removal
Module Structure
The codebase uses an internal Go module:
git.secnex.io/secnex/api-gateway
Internal imports use this path (e.g., git.secnex.io/secnex/api-gateway/config).
Logging
Uses git.secnex.io/secnex/masterlog with:
- JSON encoder
- Pseudonymizer for sensitive fields (
user_id,email,ip) - Debug-level logging enabled in main.go
- Custom
LoggerMiddlewarefor structured HTTP request logging
Dependencies
github.com/go-chi/chi/v5- HTTP routergit.secnex.io/secnex/masterlog- Structured logginggo.yaml.in/yaml/v3- YAML configuration parsinggorm.io/gorm- Database ORM (for future use)gorm.io/driver/postgres- PostgreSQL driver (for future use)
Configuration Reference
gateway:
host: "0.0.0.0"
port: 8080
features:
- request_id
- real_ip
- logger
- host
hosts:
- id: "host-001"
name: "localhost"
domain: "localhost:8080"
targets:
- id: "target-001"
name: "httpbin"
url: "https://httpbin.org"
apis:
- id: "api-001"
host: "host-001"
target: "target-001"
routes:
- id: "route-001"
api: "api-001"
path: "/api/v1/*"
strip_prefix:
enabled: true
prefix: "/api/v1"