Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return HandlerChain from Routes() #2518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

superatrain
Copy link

This PR adds HandlersChain []HandlerFunc to the RouteInfo object returned from Engine.Routes(). This will return the handlers, in order, associated with each route.

Unlike context.HandlerNames(), this can be called for all routes, and can be used at service startup, during unit tests, etc. Our service at work applies different middleware to different routes dynamically, and my intention is to use this to catch unintended changes to the chain.

Inspired by #1131 and #1729

Sample usage:

type RouteDetail struct {
	Method   string   `json:"method"`
	Path     string   `json:"path"`
	Handlers []string `json:"handlers"`
}
routeDetails := make([]RouteDetail, 0)
for _, route := range engine.Routes() {
	handlers := []string{}
	for _, handler := range route.HandlersChain {
		handlers = append(handlers, runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name())
	}
	routeDetails = append(routeDetails, RouteDetail{
		Method:   route.Method,
		Path:     route.Path,
		Handlers: handlers,
	})
}
file, _ := json.MarshalIndent(routeDetails, "", " ")
_ = ioutil.WriteFile("routes.json", file, 0644)

@codecov
Copy link

codecov bot commented Oct 2, 2020

Codecov Report

Merging #2518 into master will increase coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2518   +/-   ##
=======================================
  Coverage   98.45%   98.46%           
=======================================
  Files          41       41           
  Lines        1948     1949    +1     
=======================================
+ Hits         1918     1919    +1     
  Misses         17       17           
  Partials       13       13           
Impacted Files Coverage Δ
gin.go 99.01% <ø> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 540b1ef...76811fa. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant