MCPcopy
hub / github.com/julienschmidt/httprouter / ServeFiles

Method ServeFiles

router.go:296–307  ·  view source on GitHub ↗

ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is

(path string, root http.FileSystem)

Source from the content-addressed store, hash-verified

294// use http.Dir:
295// router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
296func (r *Router) ServeFiles(path string, root http.FileSystem) {
297 if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
298 panic("path must end with /*filepath in path '" + path + "'")
299 }
300
301 fileServer := http.FileServer(root)
302
303 r.GET(path, func(w http.ResponseWriter, req *http.Request, ps Params) {
304 req.URL.Path = ps.ByName("filepath")
305 fileServer.ServeHTTP(w, req)
306 })
307}
308
309func (r *Router) recv(w http.ResponseWriter, req *http.Request) {
310 if rcv := recover(); rcv != nil {

Callers 1

TestRouterServeFilesFunction · 0.80

Calls 3

GETMethod · 0.95
ByNameMethod · 0.80
ServeHTTPMethod · 0.45

Tested by 1

TestRouterServeFilesFunction · 0.64