mirror of
https://github.com/hanzoai/gateway.git
synced 2026-10-30 15:04:44 +00:00
1.8 KiB
1.8 KiB
Gateway Plugin Architecture
Overview
Gateway loads backend services as in-process Go plugins instead of reverse-proxying to separate containers.
gateway --plugins=svc-a,svc-b,kms serve
Single binary serves: API gateway + each registered plugin. Each plugin gets its own SQLite DB.
Interface
type BackendPlugin interface {
Name() string
PathPrefix() string // e.g., "/v1/svc-a"
Init(cfg PluginConfig) error // bootstrap DB, migrations, hooks
Handler() (http.Handler, error) // return route handler
Shutdown(ctx context.Context) error
}
How It Works
- Base-powered services expose
NewPlugin()→BackendPlugin - Plugin
Init()callscore.NewBaseApp()+app.Bootstrap()(no HTTP server) - Plugin
Handler()callsapis.NewRouter(app)→router.BuildMux()→http.Handler - Gateway mounts via
gin.WrapH(http.StripPrefix(prefix, handler)) - Auth: gateway middleware validates JWT first, plugins trust
X-User-Idheaders
Build
# Plugin mode (single binary)
go build -tags "plugin_svc_a,plugin_svc_b,plugin_kms" ./cmd/gateway
# Standalone mode (separate containers, unchanged)
cd path/to/service && go build .
Config
GATEWAY_PLUGINS=svc-a,svc-b,kms
GATEWAY_PLUGIN_DATA_DIR=/data
IAM_ENDPOINT=https://iam.dev.<your-domain>
Implementation Order
BackendPlugininterface in gateway- Refactor ATS →
plugin.gowithNewPlugin() PluginManagerin gateway- Mount in
router_engine.goviagin.WrapH() - BD, TA, KMS (same pattern)
- IAM (Beego — keep as reverse proxy initially)
Container Reduction
Before: gateway + iam + ats + bd + ta + kms = 6 containers
After: gateway (with plugins) + iam = 2 containers
Local: gateway --plugins=iam,ats,bd,ta,kms = 1 container