package api

import (
	"net/http"
	"strings"
)

// hack with http.Handler before https://github.com/gin-gonic/gin/pull/2857 merged and release
// uriRewriteRouter process uri and path like `/clusters/{clusterId}:getComponentNodeInfos`
type customVerbRewriteRouter struct {
	http.Handler

	verbs map[string]string
}

func (r *customVerbRewriteRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	for suffix, verb := range r.verbs {
		if strings.HasSuffix(req.URL.Path, suffix) {
			req.URL.Path = strings.Replace(req.URL.Path, suffix, verb, 1)
			req.URL.RawPath = strings.Replace(req.URL.Path, suffix, verb, 1)
			break
		}
	}
	r.Handler.ServeHTTP(w, req)
}
