MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / ServeHTTP

Method ServeHTTP

server/router.go:112–149  ·  view source on GitHub ↗

ServeHTTP serves routes

(rw http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

110
111// ServeHTTP serves routes
112func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
113 // Attach timer to the context
114 req, timeoutCancel := StartRequestTimer(req, r.config.RequestTimeout)
115 defer timeoutCancel()
116
117 // Create the [ResponseWriter]
118 rww := r.rwFactory.NewWriter(rw)
119
120 // Get/create request ID
121 reqID := r.getRequestID(req)
122
123 // Replace request IP from headers
124 r.replaceRemoteAddr(req)
125
126 rww.Header().Set(httpheaders.Server, DefaultServerName)
127 rww.Header().Set(httpheaders.XRequestID, reqID)
128
129 for _, rr := range r.routes {
130 if !rr.isMatch(req) {
131 continue
132 }
133
134 // Set req.Pattern. We use it to trim path prefixes in handlers.
135 req.Pattern = rr.path
136
137 if !rr.silent {
138 LogRequest(reqID, req)
139 }
140
141 rr.handler(reqID, rww, req)
142 return
143 }
144
145 // Means that we have not found matching route
146 LogRequest(reqID, req)
147 LogResponse(reqID, req, http.StatusNotFound, newRouteNotDefinedError(req.URL.Path))
148 r.NotFoundHandler(reqID, rww, req)
149}
150
151// NotFoundHandler is default 404 handler
152func (r *Router) NotFoundHandler(reqID string, rw ResponseWriter, req *http.Request) *Error {

Callers 13

TestWithCORSMethod · 0.80
TestWithSecretMethod · 0.80
TestIntoSuccessMethod · 0.80
TestIntoWithErrorMethod · 0.80
TestHTTPMethodsMethod · 0.80
TestMiddlewareOrderMethod · 0.80
TestServeHTTPMethod · 0.80
TestRequestIDMethod · 0.80

Calls 11

getRequestIDMethod · 0.95
replaceRemoteAddrMethod · 0.95
NotFoundHandlerMethod · 0.95
StartRequestTimerFunction · 0.85
LogRequestFunction · 0.85
LogResponseFunction · 0.85
newRouteNotDefinedErrorFunction · 0.85
NewWriterMethod · 0.80
isMatchMethod · 0.80
handlerMethod · 0.80
SetMethod · 0.45

Tested by 13

TestWithCORSMethod · 0.64
TestWithSecretMethod · 0.64
TestIntoSuccessMethod · 0.64
TestIntoWithErrorMethod · 0.64
TestHTTPMethodsMethod · 0.64
TestMiddlewareOrderMethod · 0.64
TestServeHTTPMethod · 0.64
TestRequestIDMethod · 0.64