fix: routes health check
This commit is contained in:
@@ -67,12 +67,12 @@ func ProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
|
||||
// HealthCheckHandler handles health check of routes
|
||||
func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||
logger.Debug("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent())
|
||||
healthRoutes := healthCheckRoutes(heathRoute.Routes)
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(heathRoute.Routes))
|
||||
wg.Add(len(healthRoutes))
|
||||
var routes []HealthCheckRouteResponse
|
||||
for _, health := range healthCheckRoutes(heathRoute.Routes) {
|
||||
for _, health := range healthRoutes {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := health.Check()
|
||||
if err != nil {
|
||||
if heathRoute.DisableRouteHealthCheckError {
|
||||
@@ -83,6 +83,7 @@ func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *
|
||||
logger.Debug("Route %s is healthy", health.Name)
|
||||
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""})
|
||||
}
|
||||
defer wg.Done()
|
||||
|
||||
}()
|
||||
|
||||
|
||||
@@ -236,10 +236,15 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
||||
logger.Info("Block common exploits enabled")
|
||||
router.Use(middleware.BlockExploitsMiddleware)
|
||||
}
|
||||
id := string(rune(rIndex))
|
||||
if len(route.Name) != 0 {
|
||||
// Use route name as ID
|
||||
id = util.Slug(route.Name)
|
||||
}
|
||||
// Apply route rate limit
|
||||
if route.RateLimit > 0 {
|
||||
rateLimit := middleware.RateLimit{
|
||||
Id: string(rune(rIndex)), // Use route index as ID
|
||||
Id: id, // Use route index as ID
|
||||
Requests: route.RateLimit,
|
||||
Window: time.Minute, // requests per minute
|
||||
Origins: route.Cors.Origins,
|
||||
|
||||
@@ -54,6 +54,15 @@ func TestStart(t *testing.T) {
|
||||
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
assertRoutesResponseBody := func(t *testing.T, s *httptest.Server) {
|
||||
resp, err := s.Client().Get(s.URL + "/health/routes")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error getting from server: %v", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
ctx := context.Background()
|
||||
go func() {
|
||||
err = gatewayServer.Start(ctx)
|
||||
@@ -67,6 +76,7 @@ func TestStart(t *testing.T) {
|
||||
s := httptest.NewServer(route)
|
||||
defer s.Close()
|
||||
assertResponseBody(t, s)
|
||||
assertRoutesResponseBody(t, s)
|
||||
})
|
||||
ctx.Done()
|
||||
}
|
||||
|
||||
@@ -142,3 +142,17 @@ func ParseDuration(durationStr string) (time.Duration, error) {
|
||||
}
|
||||
return duration, nil
|
||||
}
|
||||
|
||||
func Slug(text string) string {
|
||||
// Convert to lowercase
|
||||
text = strings.ToLower(text)
|
||||
|
||||
// Replace spaces and special characters with hyphens
|
||||
re := regexp.MustCompile(`[^\w]+`)
|
||||
text = re.ReplaceAllString(text, "-")
|
||||
|
||||
// Remove leading and trailing hyphens
|
||||
text = strings.Trim(text, "-")
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user