Skip to content

Commit

Permalink
Merge pull request #10 from BK1031/bk1031/stack-routes-bug
Browse files Browse the repository at this point in the history
Stack routes bug
  • Loading branch information
BK1031 authored Aug 28, 2024
2 parents 25970e8 + bf175f6 commit 971b521
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 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
50 changes: 45 additions & 5 deletions service/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,29 @@ 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)
route.ID = fmt.Sprintf("%s-[%s]", route.Route, route.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)
route.ID = fmt.Sprintf("%s-[%s]", route.Route, route.Method)
}
DeleteRoute(r.ID)
}
} else {
return fmt.Errorf("route with id %s overlaps with existing routes [%s]", route.ID, PrintRouteArray(overlapRoutes))
}
}
existingRoute := GetRouteByRouteAndService(route.Route, route.ServiceName)
if existingRoute.ID != "" {
route.Method = StackMethods(route.Method, existingRoute.Method)
route.ID = fmt.Sprintf("%s-[%s]", route.Route, route.Method)
DeleteRoute(existingRoute.ID)
}

if config.StorageMode == "sql" {
database.DB.Create(&route)
Expand All @@ -142,14 +154,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 +176,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
38 changes: 37 additions & 1 deletion service/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,50 @@ func TestCreateRouteLocal(t *testing.T) {
route := model.Route{
Route: "/test",
ServiceName: "Service 1",
Method: "POST",
}
err := CreateRoute(route)
if err != nil {
t.Errorf("Error when creating route: %v", err)
}
route = GetRouteByRouteAndService("/test", "Service 1")
if route.Method != "POST,GET" {
t.Errorf("Route method not updated, found %s", route.Method)
}
})
t.Run("Test Route Exists Upgrade Method Overlap", func(t *testing.T) {
route := model.Route{
Route: "/test",
ServiceName: "Service 1",
Method: "POST,PUT",
}
err := CreateRoute(route)
if err != nil {
t.Errorf("Error when creating route: %v", err)
}
route = GetRouteByRouteAndService("/test", "Service 1")
if route.Method != "GET,POST,PUT" {
t.Errorf("Route method not updated, found %s", route.Method)
}
})
t.Run("Test Route Exists Upgrade Wildcard Method", func(t *testing.T) {
route := model.Route{
Route: "/test/**",
ServiceName: "Service 1",
Method: "GET,POST",
}
CreateRoute(route)
route = model.Route{
Route: "/test/**",
ServiceName: "Service 1",
Method: "*",
}
err := CreateRoute(route)
if err != nil {
t.Errorf("Error when creating route: %v", err)
}
route = GetRouteByRouteAndService("/test", "Service 1")
if route.Method != "GET,POST" {
if route.Method != "*" {
t.Errorf("Route method not updated, found %s", route.Method)
}
})
Expand Down

0 comments on commit 971b521

Please sign in to comment.