feat(proxy): Add reverse proxy feature
This commit is contained in:
@@ -1,85 +1,81 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type Routes struct {
|
||||
routes []config.RouteConfiguration
|
||||
handlers map[string]http.Handler
|
||||
routes []Route
|
||||
}
|
||||
|
||||
func NewRoutes(routes []config.RouteConfiguration, apis []config.ApiConfiguration) *Routes {
|
||||
handlers := createHandlers(routes, apis)
|
||||
return &Routes{routes: routes, handlers: handlers}
|
||||
type Route struct {
|
||||
ID string
|
||||
Path string
|
||||
StripPrefix config.StripPrefix
|
||||
Api *Api
|
||||
}
|
||||
|
||||
func findApi(apis []config.ApiConfiguration, id string) *config.ApiConfiguration {
|
||||
for _, api := range apis {
|
||||
if api.ID == id {
|
||||
return &api
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createHandlers(routes []config.RouteConfiguration, apis []config.ApiConfiguration) map[string]http.Handler {
|
||||
handlers := make(map[string]http.Handler)
|
||||
for _, route := range routes {
|
||||
masterlog.Debug("Creating handler for route", map[string]interface{}{
|
||||
"path": route.Path,
|
||||
"id": route.ID,
|
||||
func NewRoutes(cfg *config.Configuration, apis Apis) *Routes {
|
||||
routes := make([]Route, 0)
|
||||
for _, route := range cfg.Routes {
|
||||
routes = append(routes, Route{
|
||||
ID: route.ID,
|
||||
Path: route.Path,
|
||||
StripPrefix: route.StripPrefix,
|
||||
Api: apis[route.Api],
|
||||
})
|
||||
api := findApi(apis, route.ID)
|
||||
if api == nil {
|
||||
log.Fatalf("API not found: %s", route.ID)
|
||||
continue
|
||||
}
|
||||
backendUrl, err := url.Parse(
|
||||
api.Target,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse backend URL: %v", err)
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(backendUrl)
|
||||
handlers[route.Path] = proxy
|
||||
if route.StripPrefix.Enabled {
|
||||
masterlog.Debug("Stripping prefix", map[string]interface{}{
|
||||
"id": route.ID,
|
||||
"path": route.Path,
|
||||
"prefix": route.StripPrefix.Prefix,
|
||||
})
|
||||
handlers[route.Path] = http.StripPrefix(route.StripPrefix.Prefix, handlers[route.Path])
|
||||
}
|
||||
if route.Security.WAF.Enabled {
|
||||
masterlog.Debug("Applying WAF middleware", map[string]interface{}{
|
||||
"id": route.ID,
|
||||
"path": route.Path,
|
||||
"methods": route.Security.WAF.Methods,
|
||||
})
|
||||
handlers[route.Path] = middlewares.WAF(handlers[route.Path], route.Security.WAF)
|
||||
}
|
||||
if route.Security.Auth.Enabled {
|
||||
masterlog.Debug("Applying auth middleware", map[string]interface{}{
|
||||
"id": route.ID,
|
||||
"path": route.Path,
|
||||
"type": route.Security.Auth.Type,
|
||||
"header": route.Security.Auth.Header,
|
||||
})
|
||||
handlers[route.Path] = middlewares.Auth(
|
||||
handlers[route.Path],
|
||||
route.Security.Auth.Type,
|
||||
route.Security.Auth.Header,
|
||||
route.Security.Auth.Path,
|
||||
)
|
||||
}
|
||||
return &Routes{routes: routes}
|
||||
}
|
||||
|
||||
func (rs *Routes) Register(r *chi.Mux) {
|
||||
for _, route := range rs.routes {
|
||||
masterlog.Info("Registering route", map[string]interface{}{
|
||||
"id": route.ID,
|
||||
"path": route.Path,
|
||||
"api": route.Api.ID,
|
||||
})
|
||||
|
||||
handler := route.createHandler()
|
||||
r.Handle(route.Path, handler)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Route) createHandler() http.Handler {
|
||||
handler := http.Handler(r.Api)
|
||||
|
||||
if r.StripPrefix.Enabled {
|
||||
handler = newStripPrefixMiddleware(r.StripPrefix.Prefix, handler)
|
||||
}
|
||||
|
||||
return handler
|
||||
}
|
||||
|
||||
type stripPrefixMiddleware struct {
|
||||
prefix string
|
||||
handler http.Handler
|
||||
}
|
||||
|
||||
func newStripPrefixMiddleware(prefix string, handler http.Handler) http.Handler {
|
||||
return &stripPrefixMiddleware{
|
||||
prefix: prefix,
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *stripPrefixMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Remove prefix from path
|
||||
if strings.HasPrefix(r.URL.Path, m.prefix) {
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, m.prefix)
|
||||
// Ensure path starts with /
|
||||
if !strings.HasPrefix(r.URL.Path, "/") {
|
||||
r.URL.Path = "/" + r.URL.Path
|
||||
}
|
||||
}
|
||||
return handlers
|
||||
m.handler.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user