refactor: eliminate redundant switch blocks in router.go LocationEngine functions

This commit is contained in:
xfy 2026-06-03 17:54:51 +08:00
parent 6e481c36c5
commit c1796dadc5

View File

@ -115,33 +115,13 @@ func (s *Server) registerProxyRoutesWithLocationEngine(serverCfg *config.ServerC
locType = matcher.LocationTypePrefix locType = matcher.LocationTypePrefix
} }
switch locType { path := proxyCfg.Path
case matcher.LocationTypeExact: if locType == matcher.LocationTypeNamed && proxyCfg.LocationName != "" {
if err := s.registerRoute(locType, proxyCfg.Path, p.ServeHTTP, proxyCfg.Internal, "proxy"); err != nil { path = "@" + proxyCfg.LocationName
return err }
}
case matcher.LocationTypePrefixPriority: if err := s.registerRoute(locType, path, p.ServeHTTP, proxyCfg.Internal, "proxy"); err != nil {
if err := s.registerRoute(locType, proxyCfg.Path, p.ServeHTTP, proxyCfg.Internal, "proxy"); err != nil { return err
return err
}
case matcher.LocationTypeRegex, matcher.LocationTypeRegexCaseless:
if err := s.registerRoute(locType, proxyCfg.Path, p.ServeHTTP, proxyCfg.Internal, "proxy"); err != nil {
return err
}
case matcher.LocationTypeNamed:
if proxyCfg.LocationName != "" {
if err := s.registerRoute(locType, "@"+proxyCfg.LocationName, p.ServeHTTP, false, "proxy"); err != nil {
return err
}
}
case matcher.LocationTypePrefix:
if err := s.registerRoute(locType, proxyCfg.Path, p.ServeHTTP, proxyCfg.Internal, "proxy"); err != nil {
return err
}
default:
if err := s.registerRoute(locType, proxyCfg.Path, p.ServeHTTP, proxyCfg.Internal, "proxy"); err != nil {
return err
}
} }
} }
return nil return nil
@ -214,23 +194,8 @@ func (s *Server) registerStaticHandlersWithLocationEngine(cfg *config.ServerConf
locType = matcher.LocationTypePrefix locType = matcher.LocationTypePrefix
} }
switch locType { if err := s.registerRoute(locType, path, staticHandler.Handle, static.Internal, "static"); err != nil {
case matcher.LocationTypeExact: return err
if err := s.registerRoute(locType, path, staticHandler.Handle, static.Internal, "static"); err != nil {
return err
}
case matcher.LocationTypePrefixPriority:
if err := s.registerRoute(locType, path, staticHandler.Handle, static.Internal, "static"); err != nil {
return err
}
case matcher.LocationTypePrefix:
if err := s.registerRoute(locType, path, staticHandler.Handle, static.Internal, "static"); err != nil {
return err
}
default:
if err := s.registerRoute(locType, path, staticHandler.Handle, static.Internal, "static"); err != nil {
return err
}
} }
} }
return nil return nil
@ -399,27 +364,8 @@ func (s *Server) registerLuaRoutesWithLocationEngine(serverCfg *config.ServerCon
routeType = matcher.LocationTypePrefix routeType = matcher.LocationTypePrefix
} }
switch routeType { if err := s.registerRoute(routeType, script.Route, handler, false, "lua"); err != nil {
case matcher.LocationTypeExact: return err
if err := s.registerRoute(routeType, script.Route, handler, false, "lua"); err != nil {
return err
}
case matcher.LocationTypePrefixPriority:
if err := s.registerRoute(routeType, script.Route, handler, false, "lua"); err != nil {
return err
}
case matcher.LocationTypeRegex, matcher.LocationTypeRegexCaseless:
if err := s.registerRoute(routeType, script.Route, handler, false, "lua"); err != nil {
return err
}
case matcher.LocationTypePrefix:
if err := s.registerRoute(routeType, script.Route, handler, false, "lua"); err != nil {
return err
}
default:
if err := s.registerRoute(routeType, script.Route, handler, false, "lua"); err != nil {
return err
}
} }
} }
return nil return nil