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)
| 294 | // use http.Dir: |
| 295 | // router.ServeFiles("/src/*filepath", http.Dir("/var/www")) |
| 296 | func (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 | |
| 309 | func (r *Router) recv(w http.ResponseWriter, req *http.Request) { |
| 310 | if rcv := recover(); rcv != nil { |