You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bindata.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 Dustin Webber
  2. // Copyright 2015 The Macaron Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // Package bindata is a helper module that allows to use in-memory static and template files for Macaron.
  16. package bindata
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io"
  21. "os"
  22. "github.com/elazarl/go-bindata-assetfs"
  23. "gopkg.in/macaron.v1"
  24. )
  25. const _VERSION = "0.1.1"
  26. func Version() string {
  27. return _VERSION
  28. }
  29. type (
  30. templateFileSystem struct {
  31. files []macaron.TemplateFile
  32. }
  33. templateFile struct {
  34. name string
  35. data []byte
  36. ext string
  37. }
  38. Options struct {
  39. // Asset should return content of file in path if exists
  40. Asset func(path string) ([]byte, error)
  41. // AssetDir should return list of files in the path
  42. AssetDir func(path string) ([]string, error)
  43. // AssetInfo should return the info of file in path if exists
  44. AssetInfo func(path string) (os.FileInfo, error)
  45. // AssetNames should return list of all asset names
  46. AssetNames func() []string
  47. // Prefix would be prepended to http requests
  48. Prefix string
  49. }
  50. )
  51. func Static(opt Options) *assetfs.AssetFS {
  52. fs := &assetfs.AssetFS{
  53. Asset: opt.Asset,
  54. AssetDir: opt.AssetDir,
  55. AssetInfo: opt.AssetInfo,
  56. Prefix: opt.Prefix,
  57. }
  58. return fs
  59. }
  60. func (templates templateFileSystem) ListFiles() []macaron.TemplateFile {
  61. return templates.files
  62. }
  63. func (templates templateFileSystem) Get(name string) (io.Reader, error) {
  64. for i := range templates.files {
  65. if templates.files[i].Name()+templates.files[i].Ext() == name {
  66. return bytes.NewReader(templates.files[i].Data()), nil
  67. }
  68. }
  69. return nil, fmt.Errorf("file '%s' not found", name)
  70. }
  71. func (f *templateFile) Name() string {
  72. return f.name
  73. }
  74. func (f *templateFile) Data() []byte {
  75. return f.data
  76. }
  77. func (f *templateFile) Ext() string {
  78. return f.ext
  79. }
  80. func Templates(opt Options) templateFileSystem {
  81. fs := templateFileSystem{}
  82. fs.files = make([]macaron.TemplateFile, 0, 10)
  83. list := opt.AssetNames()
  84. for _, key := range list {
  85. ext := macaron.GetExt(key)
  86. data, err := opt.Asset(key)
  87. if err != nil {
  88. continue
  89. }
  90. name := (key[0 : len(key)-len(ext)])
  91. fs.files = append(fs.files, &templateFile{name, data, ext})
  92. }
  93. return fs
  94. }