fix: routes health check

This commit is contained in:
Jonas Kaninda
2024-11-15 08:19:22 +01:00
parent fca775dd5f
commit cb31faf65f
4 changed files with 34 additions and 4 deletions

View File

@@ -67,12 +67,12 @@ func ProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
// HealthCheckHandler handles health check of routes // HealthCheckHandler handles health check of routes
func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *http.Request) { func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
logger.Debug("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent()) logger.Debug("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent())
healthRoutes := healthCheckRoutes(heathRoute.Routes)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(heathRoute.Routes)) wg.Add(len(healthRoutes))
var routes []HealthCheckRouteResponse var routes []HealthCheckRouteResponse
for _, health := range healthCheckRoutes(heathRoute.Routes) { for _, health := range healthRoutes {
go func() { go func() {
defer wg.Done()
err := health.Check() err := health.Check()
if err != nil { if err != nil {
if heathRoute.DisableRouteHealthCheckError { if heathRoute.DisableRouteHealthCheckError {
@@ -83,6 +83,7 @@ func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *
logger.Debug("Route %s is healthy", health.Name) logger.Debug("Route %s is healthy", health.Name)
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""}) routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""})
} }
defer wg.Done()
}() }()

View File

@@ -236,10 +236,15 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
logger.Info("Block common exploits enabled") logger.Info("Block common exploits enabled")
router.Use(middleware.BlockExploitsMiddleware) 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 // Apply route rate limit
if route.RateLimit > 0 { if route.RateLimit > 0 {
rateLimit := middleware.RateLimit{ rateLimit := middleware.RateLimit{
Id: string(rune(rIndex)), // Use route index as ID Id: id, // Use route index as ID
Requests: route.RateLimit, Requests: route.RateLimit,
Window: time.Minute, // requests per minute Window: time.Minute, // requests per minute
Origins: route.Cors.Origins, Origins: route.Cors.Origins,

View File

@@ -54,6 +54,15 @@ func TestStart(t *testing.T) {
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode) 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() ctx := context.Background()
go func() { go func() {
err = gatewayServer.Start(ctx) err = gatewayServer.Start(ctx)
@@ -67,6 +76,7 @@ func TestStart(t *testing.T) {
s := httptest.NewServer(route) s := httptest.NewServer(route)
defer s.Close() defer s.Close()
assertResponseBody(t, s) assertResponseBody(t, s)
assertRoutesResponseBody(t, s)
}) })
ctx.Done() ctx.Done()
} }

View File

@@ -142,3 +142,17 @@ func ParseDuration(durationStr string) (time.Duration, error) {
} }
return duration, nil 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
}