MCPcopy
hub / github.com/gin-gonic/gin / createStaticHandler

Method createStaticHandler

routergroup.go:216–239  ·  view source on GitHub ↗
(relativePath string, fs http.FileSystem)

Source from the content-addressed store, hash-verified

214}
215
216func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
217 absolutePath := group.calculateAbsolutePath(relativePath)
218 fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
219
220 return func(c *Context) {
221 if _, noListing := fs.(*OnlyFilesFS); noListing {
222 c.Writer.WriteHeader(http.StatusNotFound)
223 }
224
225 file := c.Param("filepath")
226 // Check if file exists and/or if we have permission to access it
227 f, err := fs.Open(file)
228 if err != nil {
229 c.Writer.WriteHeader(http.StatusNotFound)
230 c.handlers = group.engine.noRoute
231 // Reset index
232 c.index = -1
233 return
234 }
235 f.Close()
236
237 fileServer.ServeHTTP(c.Writer, c.Request)
238 }
239}
240
241func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {
242 finalSize := len(group.Handlers) + len(handlers)

Callers 1

StaticFSMethod · 0.95

Calls 6

calculateAbsolutePathMethod · 0.95
ParamMethod · 0.80
CloseMethod · 0.80
WriteHeaderMethod · 0.45
OpenMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected