From 24dfac41e2e131c0c2ed2cf0018f5368057078b8 Mon Sep 17 00:00:00 2001 From: Bharat Kathi Date: Tue, 27 Aug 2024 22:29:30 -0700 Subject: [PATCH] fixed bug where route methods would not be stacked --- model/route.go | 4 ++-- service/route.go | 42 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/model/route.go b/model/route.go index 8f126e3..9e17b15 100644 --- a/model/route.go +++ b/model/route.go @@ -7,7 +7,7 @@ import ( "time" ) -var validMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD", "*"} +var ValidMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD", "*"} type Route struct { ID string `json:"id" gorm:"primaryKey"` @@ -24,7 +24,7 @@ func (Route) TableName() string { func (r *Route) IsMethodValid() bool { methods := strings.Split(r.Method, ",") for _, method := range methods { - if !slices.Contains(validMethods, method) { + if !slices.Contains(ValidMethods, method) { return false } } diff --git a/service/route.go b/service/route.go index b6d16c5..46c9213 100644 --- a/service/route.go +++ b/service/route.go @@ -109,11 +109,15 @@ func CreateRoute(route model.Route) error { overlapRoutes := GetOverlappingRoutes(route) if len(overlapRoutes) == 1 && overlapRoutes[0].ServiceName == route.ServiceName { - utils.SugarLogger.Debugf("replacing existing route %s for service %s", route.Route, route.ServiceName) + utils.SugarLogger.Debugf("stacking existing route %s for service %s", route.Route, route.ServiceName) + route.Method = StackMethods(route.Method, overlapRoutes[0].Method) DeleteRoute(overlapRoutes[0].ID) } else if len(overlapRoutes) > 0 { if config.OverwriteRoutes == "true" { for _, r := range overlapRoutes { + if r.ServiceName == route.ServiceName { + route.Method = StackMethods(route.Method, r.Method) + } DeleteRoute(r.ID) } } else { @@ -142,14 +146,19 @@ func PrintRouteArray(routes []model.Route) string { } func GetOverlappingRoutes(route model.Route) []model.Route { + methodMap := make(map[string]string) + for _, method := range model.ValidMethods { + methodMap[method] = "" + } + route.Method = strings.ToUpper(route.Method) route.ServiceName = utils.NormalizeName(route.ServiceName) overlapRoutes := make([]model.Route, 0) existingRoutes := GetRoutesByRoute(route.Route) - takenMethods := make(map[string]string) + for _, r := range existingRoutes { for _, m := range strings.Split(r.Method, ",") { - takenMethods[m] = r.ServiceName + methodMap[m] = r.ServiceName if m == "*" { return existingRoutes } @@ -159,13 +168,36 @@ func GetOverlappingRoutes(route model.Route) []model.Route { if m == "*" { return existingRoutes } - if takenMethods[m] != "" { - overlapRoutes = append(overlapRoutes, GetRouteByRouteAndService(route.Route, takenMethods[m])) + if methodMap[m] != "" { + overlapRoutes = append(overlapRoutes, GetRouteByRouteAndService(route.Route, methodMap[m])) } } return overlapRoutes } +func StackMethods(m1 string, m2 string) string { + methodMap := make(map[string]bool) + for _, method := range model.ValidMethods { + methodMap[method] = false + } + for _, method := range strings.Split(m1, ",") { + methodMap[method] = true + } + for _, method := range strings.Split(m2, ",") { + methodMap[method] = true + } + if methodMap["*"] { + return "*" + } + methods := make([]string, 0) + for method, enabled := range methodMap { + if enabled { + methods = append(methods, method) + } + } + return strings.Join(methods, ",") +} + func DeleteRoute(id string) { if config.StorageMode == "sql" { database.DB.Where("id = ?", id).Delete(&model.Route{})