File: filter_adapter.go

package info (click to toggle)
golang-github-emicklei-go-restful 3.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 792 kB
  • sloc: makefile: 9; sh: 6
file content (21 lines) | stat: -rw-r--r-- 661 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package restful

import (
	"net/http"
)

// HttpMiddlewareHandler is a function that takes a http.Handler and returns a http.Handler
type HttpMiddlewareHandler func(http.Handler) http.Handler

// HttpMiddlewareHandlerToFilter converts a HttpMiddlewareHandler to a FilterFunction.
func HttpMiddlewareHandlerToFilter(middleware HttpMiddlewareHandler) FilterFunction {
	return func(req *Request, resp *Response, chain *FilterChain) {
		next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
			req.Request = r
			resp.ResponseWriter = rw
			chain.ProcessFilter(req, resp)
		})

		middleware(next).ServeHTTP(resp.ResponseWriter, req.Request)
	}
}