RegisterServerFile register ServerFile router with routeMethod method on http.FileServer simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*", "/var/www", nil) simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*filepath", "/var/www", []string{".zip", ".rar"})
(routeMethod string, path string, fileRoot string, excludeExtension []string)
| 418 | // simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*", "/var/www", nil) |
| 419 | // simple demo:server.RegisterServerFile(RouteMethod_GET, "/src/*filepath", "/var/www", []string{".zip", ".rar"}) |
| 420 | func (r *router) RegisterServerFile(routeMethod string, path string, fileRoot string, excludeExtension []string) RouterNode { |
| 421 | realPath := r.server.VirtualPath() + path |
| 422 | node := &Node{} |
| 423 | if len(realPath) < 2 { |
| 424 | panic("path length must be greater than or equal to 2") |
| 425 | } |
| 426 | if realPath[len(realPath)-2:] == "/*" { // fixed for #125 |
| 427 | realPath = realPath + "filepath" |
| 428 | } |
| 429 | if len(realPath) < 10 || realPath[len(realPath)-10:] != "/*filepath" { |
| 430 | panic("path must end with /*filepath or /* in path '" + realPath + "'") |
| 431 | } |
| 432 | var root http.FileSystem |
| 433 | root = http.Dir(fileRoot) |
| 434 | if !r.server.ServerConfig().EnabledListDir { |
| 435 | root = &core.HideReaddirFS{FileSystem: root} |
| 436 | } |
| 437 | fileServer := http.FileServer(root) |
| 438 | r.add(routeMethod, realPath, r.wrapFileHandle(fileServer, excludeExtension)) |
| 439 | node = r.getNode(routeMethod, realPath) |
| 440 | |
| 441 | if r.server.ServerConfig().EnabledAutoHEAD { |
| 442 | if !r.existsRouter(RouteMethod_HEAD, realPath) { |
| 443 | r.add(RouteMethod_HEAD, realPath, r.wrapFileHandle(fileServer, excludeExtension)) |
| 444 | } |
| 445 | } |
| 446 | if r.server.ServerConfig().EnabledAutoOPTIONS { |
| 447 | if !r.existsRouter(RouteMethod_OPTIONS, realPath) { |
| 448 | r.add(RouteMethod_OPTIONS, realPath, r.wrapRouterHandle(DefaultAutoOPTIONSHandler, false)) |
| 449 | } |
| 450 | } |
| 451 | return node |
| 452 | } |
| 453 | |
| 454 | func handlerName(h HttpHandle) string { |
| 455 | t := reflect.ValueOf(h).Type() |
no test coverage detected