Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package middleware
  24. import (
  25. "net/http"
  26. "path"
  27. "strings"
  28. )
  29. // Static serves as file server for static assets,
  30. // as convention, the given dir name will be used as the URL prefix.
  31. func Static(dir string) func(http.Handler) http.Handler {
  32. var (
  33. fs = http.Dir(dir)
  34. prefix = path.Join("/", dir)
  35. )
  36. return func(next http.Handler) http.Handler {
  37. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  38. // Accepts http GET | HEAD Only, ignores all requests not started with prefix.
  39. if r.Method != "GET" && r.Method != "HEAD" {
  40. next.ServeHTTP(w, r)
  41. return
  42. } else if !strings.HasPrefix(r.URL.Path, prefix) {
  43. next.ServeHTTP(w, r)
  44. return
  45. }
  46. filename := strings.TrimPrefix(r.URL.Path, prefix)
  47. if filename != "" && filename[0] != '/' {
  48. next.ServeHTTP(w, r)
  49. return
  50. }
  51. file, err := fs.Open(filename)
  52. if err != nil {
  53. next.ServeHTTP(w, r)
  54. return
  55. }
  56. defer file.Close()
  57. stat, err := file.Stat()
  58. if err != nil {
  59. next.ServeHTTP(w, r)
  60. return
  61. }
  62. // try to serve index filename
  63. if stat.IsDir() {
  64. // redirect if missing trailing slash
  65. if !strings.HasSuffix(r.URL.Path, "/") {
  66. http.Redirect(w, r, r.URL.Path+"/", http.StatusFound)
  67. return
  68. }
  69. filename = path.Join(filename, "index.html")
  70. file, err = fs.Open(filename)
  71. if err != nil {
  72. next.ServeHTTP(w, r)
  73. return
  74. }
  75. defer file.Close()
  76. stat, err = file.Stat()
  77. if err != nil || stat.IsDir() {
  78. next.ServeHTTP(w, r)
  79. return
  80. }
  81. }
  82. http.ServeContent(w, r, filename, stat.ModTime(), file)
  83. })
  84. }
  85. }