go-chi / chi

lightweight, idiomatic and composable router for building Go HTTP services
https://go-chi.io
MIT License
18.2k stars 980 forks source link

Heartbeat middleware is not invoked when added to a subrouter #821

Open mo-rieger opened 1 year ago

mo-rieger commented 1 year ago

Hi there,

The Heartbeat middleware is not invoked when attached to a sub router, whereas other middlewares are.

The expected behaviour would be that /connectivity/ping would return 200. Instead, a 404 is returned.

package main

import (
    "net/http"

    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

func main() {
    r := chi.NewRouter()

    r.Use(middleware.RequestID)
    r.Use(middleware.RealIP)
    r.Use(middleware.Recoverer)

    r.Mount("/connectivity", NewSubrouter())

    http.ListenAndServe(":3333", r)
}

func NewSubrouter() chi.Router {
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Use(middleware.Heartbeat("/ping"))

    r.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("."))
    })

    return r
}

Best, Moritz

kubosuke commented 1 year ago

I created PR for this issue, please check

VojtechVitek commented 5 days ago

Please see https://github.com/go-chi/chi/pull/822#pullrequestreview-2313031493