From 71c1639032be8aa176558a30dde8032fa39b1a19 Mon Sep 17 00:00:00 2001 From: ShuNing Date: Mon, 5 Aug 2024 16:39:39 +0800 Subject: [PATCH] api: Fix the api to ensure it works correctly. (#256) close pingcap/ng-monitoring#255 --- service/http/http.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/service/http/http.go b/service/http/http.go index 9360c5a..fa937f4 100644 --- a/service/http/http.go +++ b/service/http/http.go @@ -65,10 +65,12 @@ func ServeHTTP(l *config.Log, listener net.Listener) { promGroup.Any("", func(c *gin.Context) { promHandler.ServeHTTP(c.Writer, c.Request) }) - - wh := &wrapHeander{ngHanlder: ng} + // compatible with victoria-metrics handlers + ng.NoRoute(func(c *gin.Context) { + handlerNoRouter(c) + }) httpServer = &http.Server{ - Handler: wh, + Handler: ng, ReadHeaderTimeout: 5 * time.Second, } if err = httpServer.Serve(listener); err != nil && err != http.ErrServerClosed { @@ -76,21 +78,23 @@ func ServeHTTP(l *config.Log, listener net.Listener) { } } -type wrapHeander struct { - ngHanlder http.Handler -} - -func (wrap *wrapHeander) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if vminsert.RequestHandler(w, r) { +// Try Victoria-Metrics' handlers first. If not handled, then return a 404 error. +func handlerNoRouter(c *gin.Context) { + //reset to default + c.Writer.WriteHeader(http.StatusOK) + if vminsert.RequestHandler(c.Writer, c.Request) { return } - if vmselect.RequestHandler(w, r) { + + if vmselect.RequestHandler(c.Writer, c.Request) { return } - if vmstorage.RequestHandler(w, r) { + + if vmstorage.RequestHandler(c.Writer, c.Request) { return } - wrap.ngHanlder.ServeHTTP(w, r) + + c.String(http.StatusNotFound, "404 page not found") } type Status struct {