static.go 921 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package binding
  2. import "strings"
  3. func Static(binder *Binder, item *StaticBindItem) {
  4. item.bind(binder)
  5. }
  6. func StaticFile(binder *Binder, item *StaticFileBindItem) {
  7. item.bind(binder)
  8. }
  9. // StaticBindItem 静态路由item
  10. type StaticBindItem struct {
  11. RelativePath string
  12. Root string
  13. WithBasePath bool
  14. }
  15. func (item *StaticBindItem) bind(binder *Binder) {
  16. if item.WithBasePath {
  17. binder.router.Static(strings.TrimPrefix(item.RelativePath, binder.router.BasePath()), item.Root)
  18. } else {
  19. binder.router.Static(item.RelativePath, item.Root)
  20. }
  21. }
  22. type StaticFileBindItem struct {
  23. RelativePath string
  24. FilePath string
  25. WithBasePath bool
  26. }
  27. func (item *StaticFileBindItem) bind(binder *Binder) {
  28. if item.WithBasePath {
  29. binder.router.StaticFile(strings.TrimPrefix(item.RelativePath, binder.router.BasePath()), item.FilePath)
  30. } else {
  31. binder.router.StaticFile(item.RelativePath, item.FilePath)
  32. }
  33. }