Skip to content

Commit

Permalink
existing route with no method overlap now correctly stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
BK1031 committed Aug 28, 2024
1 parent 24dfac4 commit 99bbc59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"rincon/api"
"rincon/config"
"rincon/database"
"rincon/model"
"rincon/service"
"rincon/utils"
)
Expand All @@ -18,6 +19,19 @@ func main() {
service.RegisterSelf()
service.InitializeHeartbeat()

service.CreateService(model.Service{
Name: "New York",
Endpoint: "http://localhost:3000",
HealthCheck: "http://localhost:3000/health",
Version: "1.0.0",
})
service.CreateService(model.Service{
Name: "San Francisco",
Endpoint: "http://localhost:4000",
HealthCheck: "http://localhost:4000/health",
Version: "1.0.0",
})

Check warning on line 34 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L22-L34

Added lines #L22 - L34 were not covered by tests
router := api.SetupRouter()
api.InitializeRoutes(router)
err := router.Run(":" + config.Port)
Expand Down
11 changes: 11 additions & 0 deletions service/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,31 @@ func CreateRoute(route model.Route) error {
route.CreatedAt = time.Now()

overlapRoutes := GetOverlappingRoutes(route)
fmt.Printf("overlapRoutes: %+v\n", overlapRoutes)
if len(overlapRoutes) == 1 && overlapRoutes[0].ServiceName == 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)
}

Check warning on line 135 in service/route.go

View check run for this annotation

Codecov / codecov/patch

service/route.go#L132-L135

Added lines #L132 - L135 were not covered by tests

if config.StorageMode == "sql" {
database.DB.Create(&route)
Expand Down Expand Up @@ -155,6 +164,7 @@ func GetOverlappingRoutes(route model.Route) []model.Route {
route.ServiceName = utils.NormalizeName(route.ServiceName)
overlapRoutes := make([]model.Route, 0)
existingRoutes := GetRoutesByRoute(route.Route)
fmt.Printf("existingRoutes: %+v\n", existingRoutes)

for _, r := range existingRoutes {
for _, m := range strings.Split(r.Method, ",") {
Expand All @@ -164,6 +174,7 @@ func GetOverlappingRoutes(route model.Route) []model.Route {
}
}
}
fmt.Printf("methodMap: %+v\n", methodMap)
for _, m := range strings.Split(route.Method, ",") {
if m == "*" {
return existingRoutes
Expand Down

0 comments on commit 99bbc59

Please sign in to comment.