static.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package tango
  2. import (
  3. "os"
  4. "path"
  5. "path/filepath"
  6. "strings"
  7. )
  8. type StaticOptions struct {
  9. RootPath string
  10. Prefix string
  11. IndexFiles []string
  12. ListDir bool
  13. FilterExts []string
  14. }
  15. func prepareStaticOptions(options []StaticOptions) StaticOptions {
  16. var opt StaticOptions
  17. if len(options) > 0 {
  18. opt = options[0]
  19. }
  20. // Defaults
  21. if len(opt.RootPath) == 0 {
  22. opt.RootPath = "./public"
  23. }
  24. if len(opt.IndexFiles) == 0 {
  25. opt.IndexFiles = []string{"index.html", "index.htm"}
  26. }
  27. return opt
  28. }
  29. func Static(opts ...StaticOptions) HandlerFunc {
  30. return func(ctx *Context) {
  31. if ctx.Req().Method != "GET" && ctx.Req().Method != "HEAD" {
  32. ctx.Next()
  33. return
  34. }
  35. opt := prepareStaticOptions(opts)
  36. var rPath = ctx.Req().URL.Path
  37. // if defined prefix, then only check prefix
  38. if opt.Prefix != "" {
  39. if !strings.HasPrefix(ctx.Req().URL.Path, "/"+opt.Prefix) {
  40. ctx.Next()
  41. return
  42. } else {
  43. if len("/"+opt.Prefix) == len(ctx.Req().URL.Path) {
  44. rPath = ""
  45. } else {
  46. rPath = ctx.Req().URL.Path[len("/"+opt.Prefix):]
  47. }
  48. }
  49. }
  50. fPath, _ := filepath.Abs(filepath.Join(opt.RootPath, rPath))
  51. finfo, err := os.Stat(fPath)
  52. if err != nil {
  53. if !os.IsNotExist(err) {
  54. ctx.Result = InternalServerError(err.Error())
  55. ctx.HandleError()
  56. return
  57. }
  58. } else if !finfo.IsDir() {
  59. if len(opt.FilterExts) > 0 {
  60. var matched bool
  61. for _, ext := range opt.FilterExts {
  62. if filepath.Ext(fPath) == ext {
  63. matched = true
  64. break
  65. }
  66. }
  67. if !matched {
  68. ctx.Next()
  69. return
  70. }
  71. }
  72. err := ctx.ServeFile(fPath)
  73. if err != nil {
  74. ctx.Result = InternalServerError(err.Error())
  75. ctx.HandleError()
  76. }
  77. return
  78. } else {
  79. // try serving index.html or index.htm
  80. if len(opt.IndexFiles) > 0 {
  81. for _, index := range opt.IndexFiles {
  82. nPath := filepath.Join(fPath, index)
  83. finfo, err = os.Stat(nPath)
  84. if err != nil {
  85. if !os.IsNotExist(err) {
  86. ctx.Result = InternalServerError(err.Error())
  87. ctx.HandleError()
  88. return
  89. }
  90. } else if !finfo.IsDir() {
  91. err = ctx.ServeFile(nPath)
  92. if err != nil {
  93. ctx.Result = InternalServerError(err.Error())
  94. ctx.HandleError()
  95. }
  96. return
  97. }
  98. }
  99. }
  100. // list dir files
  101. if opt.ListDir {
  102. ctx.Header().Set("Content-Type", "text/html")
  103. ctx.Write([]byte(`<ul style="list-style-type:none;line-height:32px;">`))
  104. rootPath, _ := filepath.Abs(opt.RootPath)
  105. rPath, _ := filepath.Rel(rootPath, fPath)
  106. if fPath != rootPath {
  107. ctx.Write([]byte(`<li>&nbsp; &nbsp; <a href="/` + path.Join(opt.Prefix, filepath.Dir(rPath)) + `">..</a></li>`))
  108. }
  109. err = filepath.Walk(fPath, func(p string, fi os.FileInfo, err error) error {
  110. rPath, _ := filepath.Rel(fPath, p)
  111. if rPath == "." || len(strings.Split(rPath, string(filepath.Separator))) > 1 {
  112. return nil
  113. }
  114. rPath, _ = filepath.Rel(rootPath, p)
  115. ps, _ := os.Stat(p)
  116. if ps.IsDir() {
  117. ctx.Write([]byte(`<li>┖ <a href="/` + path.Join(opt.Prefix, rPath) + `">` + filepath.Base(p) + `</a></li>`))
  118. } else {
  119. if len(opt.FilterExts) > 0 {
  120. var matched bool
  121. for _, ext := range opt.FilterExts {
  122. if filepath.Ext(p) == ext {
  123. matched = true
  124. break
  125. }
  126. }
  127. if !matched {
  128. return nil
  129. }
  130. }
  131. ctx.Write([]byte(`<li>&nbsp; &nbsp; <a href="/` + path.Join(opt.Prefix, rPath) + `">` + filepath.Base(p) + `</a></li>`))
  132. }
  133. return nil
  134. })
  135. ctx.Write([]byte("</ul>"))
  136. return
  137. }
  138. }
  139. ctx.Next()
  140. }
  141. }