feat(proxy): Add reverse proxy feature
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.secnex.io/secnex/api-gateway/config"
|
||||
"git.secnex.io/secnex/api-gateway/middlewares"
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
@@ -12,22 +13,39 @@ import (
|
||||
|
||||
type Gateway struct {
|
||||
router *chi.Mux
|
||||
config config.Config
|
||||
config config.Configuration
|
||||
apis Apis
|
||||
routes *Routes
|
||||
proxy *Proxy
|
||||
}
|
||||
|
||||
func NewGateway(config config.Config) *Gateway {
|
||||
func (g *Gateway) configureProxies() {
|
||||
for _, api := range g.apis {
|
||||
masterlog.Info("Configuring proxy", map[string]interface{}{
|
||||
"id": api.ID,
|
||||
"host": api.Host.Domain,
|
||||
"target": api.Target.URL.String(),
|
||||
})
|
||||
originalDirector := api.Target.proxy.Director
|
||||
api.Target.proxy.Director = func(r *http.Request) {
|
||||
originalDirector(r)
|
||||
r.Host = api.Host.Domain
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewGateway(cfg *config.Configuration) *Gateway {
|
||||
r := chi.NewRouter()
|
||||
|
||||
for _, feature := range config.GetFeatures() {
|
||||
for _, feature := range cfg.Gateway.Features {
|
||||
switch feature {
|
||||
case "request_id":
|
||||
r.Use(middleware.RequestID)
|
||||
case "real_ip":
|
||||
r.Use(middleware.RealIP)
|
||||
case "logger":
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middlewares.LoggerMiddleware)
|
||||
case "host":
|
||||
r.Use(middlewares.HostMiddleware)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,30 +56,22 @@ func NewGateway(config config.Config) *Gateway {
|
||||
})
|
||||
})
|
||||
|
||||
return &Gateway{config: config, router: r, routes: nil, proxy: nil}
|
||||
}
|
||||
hosts := NewHosts(cfg)
|
||||
targets := NewTargets(cfg)
|
||||
apis := NewApis(cfg, hosts, targets)
|
||||
routes := NewRoutes(cfg, apis)
|
||||
|
||||
func (g *Gateway) SetRoutes(routes *Routes) {
|
||||
g.routes = routes
|
||||
}
|
||||
|
||||
func (g *Gateway) SetProxy(proxy *Proxy) {
|
||||
g.proxy = proxy
|
||||
return &Gateway{config: *cfg, router: r, apis: apis, routes: routes}
|
||||
}
|
||||
|
||||
func (g *Gateway) Start() {
|
||||
masterlog.Info("Starting gateway", map[string]interface{}{
|
||||
"host": g.config.GetGatewayConfiguration().Host,
|
||||
"port": g.config.GetGatewayConfiguration().Port,
|
||||
"host": g.config.Gateway.Host,
|
||||
"port": g.config.Gateway.Port,
|
||||
})
|
||||
for path, handler := range g.routes.handlers {
|
||||
masterlog.Info("Registering route", map[string]interface{}{
|
||||
"path": path,
|
||||
})
|
||||
g.router.Handle(path, handler)
|
||||
}
|
||||
|
||||
gatewayConfig := g.config.GetGatewayConfiguration()
|
||||
g.configureProxies()
|
||||
g.routes.Register(g.router)
|
||||
|
||||
http.ListenAndServe(fmt.Sprintf("%s:%d", gatewayConfig.Host, gatewayConfig.Port), g.router)
|
||||
http.ListenAndServe(fmt.Sprintf("%s:%d", g.config.Gateway.Host, g.config.Gateway.Port), g.router)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user