Skip to content

Commit

Permalink
fixed bug where route methods would not be stacked
Browse files Browse the repository at this point in the history
  • Loading branch information
BK1031 committed Aug 28, 2024
1 parent dc97a0c commit 24dfac4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions model/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
}
Expand Down
42 changes: 37 additions & 5 deletions service/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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{})
Expand Down

0 comments on commit 24dfac4

Please sign in to comment.