MCPcopy Create free account
hub / github.com/kataras/iris / HandleDir

Method HandleDir

core/router/api_builder.go:639–685  ·  view source on GitHub ↗

HandleDir registers a handler that serves HTTP requests with the contents of a file system (physical or embedded). first parameter : the route path second parameter : the file system needs to be served third parameter : not required, the serve directory options. Alternatively, to get just the ha

(requestPath string, fsOrDir interface{}, opts ...DirOptions)

Source from the content-addressed store, hash-verified

637// Examples:
638// https://github.com/kataras/iris/tree/main/_examples/file-server
639func (api *APIBuilder) HandleDir(requestPath string, fsOrDir interface{}, opts ...DirOptions) (routes []*Route) {
640 options := DefaultDirOptions
641 if len(opts) > 0 {
642 options = opts[0]
643 }
644
645 fs := context.ResolveHTTPFS(fsOrDir)
646 h := FileServer(fs, options)
647 description := "file server"
648 if d, ok := fs.(http.Dir); ok {
649 description = string(d)
650 }
651
652 fileName, lineNumber := context.HandlerFileLine(h) // take those before StripPrefix.
653
654 // if subdomain, we get the full path of the path only,
655 // because a subdomain can have parties as well
656 // and we need that path to call the `StripPrefix`.
657 _, fullpath := splitSubdomainAndPath(joinPath(api.relativePath, requestPath))
658 if fullpath != "/" {
659 h = StripPrefix(fullpath, h)
660 }
661
662 if api.GetRouteByPath(fullpath) == nil {
663 // register index if not registered by the end-developer.
664 routes = api.CreateRoutes([]string{http.MethodGet, http.MethodHead}, requestPath, h)
665 }
666
667 requestPath = joinPath(requestPath, WildcardFileParam())
668
669 routes = append(routes, api.CreateRoutes([]string{http.MethodGet, http.MethodHead}, requestPath, h)...)
670
671 for _, route := range routes {
672 if route.Method == http.MethodHead {
673 } else {
674 route.Describe(description)
675 route.SetSourceLine(fileName, lineNumber)
676 }
677
678 if _, err := api.routes.register(route, api.routeRegisterRule); err != nil {
679 api.logger.Error(err)
680 break
681 }
682 }
683
684 return routes
685}
686
687// CreateRoutes returns a list of Party-based Routes.
688// It does NOT registers the route. Use `Handle, Get...` methods instead.

Callers

nothing calls this directly

Calls 13

GetRouteByPathMethod · 0.95
CreateRoutesMethod · 0.95
HandlerFileLineFunction · 0.92
FileServerFunction · 0.85
splitSubdomainAndPathFunction · 0.85
joinPathFunction · 0.85
StripPrefixFunction · 0.85
WildcardFileParamFunction · 0.85
ResolveHTTPFSMethod · 0.80
DescribeMethod · 0.80
SetSourceLineMethod · 0.80
ErrorMethod · 0.65

Tested by

no test coverage detected