Refactor module binding to rely on Type
This commit is contained in:
@@ -35,7 +35,7 @@ func RegisterModuleHandler(key string, handler server.ProxyHandler) {
|
||||
MustRegisterModule(ModuleRegistration{Key: key, Handler: handler})
|
||||
}
|
||||
|
||||
// Handle 实现 server.ProxyHandler,根据 route.ModuleKey 选择 handler。
|
||||
// Handle 实现 server.ProxyHandler,根据 route.Module.Key 选择 handler。
|
||||
func (f *Forwarder) Handle(c fiber.Ctx, route *server.HubRoute) error {
|
||||
requestID := server.RequestID(c)
|
||||
handler := f.lookup(route)
|
||||
@@ -90,7 +90,7 @@ func (f *Forwarder) logModuleError(route *server.HubRoute, code string, err erro
|
||||
|
||||
func (f *Forwarder) lookup(route *server.HubRoute) server.ProxyHandler {
|
||||
if route != nil {
|
||||
if handler := lookupModuleHandler(route.ModuleKey); handler != nil {
|
||||
if handler := lookupModuleHandler(route.Module.Key); handler != nil {
|
||||
return handler
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ func (f *Forwarder) routeFields(route *server.HubRoute, requestID string) logrus
|
||||
route.Config.Domain,
|
||||
route.Config.Type,
|
||||
route.Config.AuthMode(),
|
||||
route.ModuleKey,
|
||||
route.Module.Key,
|
||||
false,
|
||||
)
|
||||
if requestID != "" {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
"github.com/any-hub/any-hub/internal/config"
|
||||
"github.com/any-hub/any-hub/internal/hubmodule"
|
||||
"github.com/any-hub/any-hub/internal/server"
|
||||
)
|
||||
|
||||
@@ -102,6 +103,8 @@ func testRouteWithModule(moduleKey string) *server.HubRoute {
|
||||
Domain: "test.local",
|
||||
Type: "custom",
|
||||
},
|
||||
ModuleKey: moduleKey,
|
||||
Module: hubmodule.ModuleMetadata{
|
||||
Key: moduleKey,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func buildHookContext(route *server.HubRoute, c fiber.Ctx) *hooks.RequestContext
|
||||
HubName: route.Config.Name,
|
||||
Domain: route.Config.Domain,
|
||||
HubType: route.Config.Type,
|
||||
ModuleKey: route.ModuleKey,
|
||||
ModuleKey: route.Module.Key,
|
||||
UpstreamHost: baseHost,
|
||||
Method: c.Method(),
|
||||
}
|
||||
@@ -83,7 +83,7 @@ func hasHook(def hooks.Hooks) bool {
|
||||
func (h *Handler) Handle(c fiber.Ctx, route *server.HubRoute) error {
|
||||
started := time.Now()
|
||||
requestID := server.RequestID(c)
|
||||
hooksDef, ok := hooks.Fetch(route.ModuleKey)
|
||||
hooksDef, ok := hooks.Fetch(route.Module.Key)
|
||||
hookCtx := buildHookContext(route, c)
|
||||
rawQuery := append([]byte(nil), c.Request().URI().QueryString()...)
|
||||
cleanPath := normalizeRequestPath(route, string(c.Request().URI().Path()))
|
||||
@@ -120,7 +120,7 @@ func (h *Handler) Handle(c fiber.Ctx, route *server.HubRoute) error {
|
||||
// miss, continue
|
||||
default:
|
||||
h.logger.WithError(err).
|
||||
WithFields(logrus.Fields{"hub": route.Config.Name, "module_key": route.ModuleKey}).
|
||||
WithFields(logrus.Fields{"hub": route.Config.Name, "module_key": route.Module.Key}).
|
||||
Warn("cache_get_failed")
|
||||
}
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func (h *Handler) Handle(c fiber.Ctx, route *server.HubRoute) error {
|
||||
fresh, err := h.isCacheFresh(c, route, locator, cached.Entry, &hookState)
|
||||
if err != nil {
|
||||
h.logger.WithError(err).
|
||||
WithFields(logrus.Fields{"hub": route.Config.Name, "module_key": route.ModuleKey}).
|
||||
WithFields(logrus.Fields{"hub": route.Config.Name, "module_key": route.Module.Key}).
|
||||
Warn("cache_revalidate_failed")
|
||||
serve = false
|
||||
} else if !fresh {
|
||||
@@ -517,7 +517,7 @@ func (h *Handler) logResult(
|
||||
route.Config.Domain,
|
||||
route.Config.Type,
|
||||
route.Config.AuthMode(),
|
||||
route.ModuleKey,
|
||||
route.Module.Key,
|
||||
cacheHit,
|
||||
)
|
||||
fields["action"] = "proxy"
|
||||
@@ -968,7 +968,7 @@ func (h *Handler) logAuthRetry(route *server.HubRoute, upstream string, requestI
|
||||
route.Config.Domain,
|
||||
route.Config.Type,
|
||||
route.Config.AuthMode(),
|
||||
route.ModuleKey,
|
||||
route.Module.Key,
|
||||
false,
|
||||
)
|
||||
fields["action"] = "proxy_retry"
|
||||
@@ -987,7 +987,7 @@ func (h *Handler) logAuthFailure(route *server.HubRoute, upstream string, reques
|
||||
route.Config.Domain,
|
||||
route.Config.Type,
|
||||
route.Config.AuthMode(),
|
||||
route.ModuleKey,
|
||||
route.Module.Key,
|
||||
false,
|
||||
)
|
||||
fields["action"] = "proxy"
|
||||
|
||||
Reference in New Issue
Block a user