1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-23 12:57:05 +00:00

Full support of the http.FileSystem on all view engines as requested at #1575

Also, the HandleDir accepts both string and http.FileSystem (interface{}) (like the view's fs)
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-05 08:34:09 +03:00
parent 7e6d453dad
commit e1f25eb098
50 changed files with 1613 additions and 1226 deletions

View File

@@ -2,9 +2,8 @@ package view
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
stdPath "path"
"path/filepath"
@@ -60,41 +59,40 @@ var AsValue = pongo2.AsValue
var AsSafeValue = pongo2.AsSafeValue
type tDjangoAssetLoader struct {
baseDir string
assetGet func(name string) ([]byte, error)
rootDir string
fs http.FileSystem
}
// Abs calculates the path to a given template. Whenever a path must be resolved
// due to an import from another template, the base equals the parent template's path.
func (dal *tDjangoAssetLoader) Abs(base, name string) string {
func (l *tDjangoAssetLoader) Abs(base, name string) string {
if stdPath.IsAbs(name) {
return name
}
return stdPath.Join(dal.baseDir, name)
return stdPath.Join(l.rootDir, name)
}
// Get returns an io.Reader where the template's content can be read from.
func (dal *tDjangoAssetLoader) Get(path string) (io.Reader, error) {
func (l *tDjangoAssetLoader) Get(path string) (io.Reader, error) {
if stdPath.IsAbs(path) {
path = path[1:]
}
res, err := dal.assetGet(path)
res, err := asset(l.fs, path)
if err != nil {
return nil, err
}
return bytes.NewBuffer(res), nil
return bytes.NewReader(res), nil
}
// DjangoEngine contains the django view engine structure.
type DjangoEngine struct {
fs http.FileSystem
// files configuration
directory string
rootDir string
extension string
assetFn func(name string) ([]byte, error) // for embedded, in combination with directory & extension
namesFn func() []string // for embedded, in combination with directory & extension
reload bool
//
rmu sync.RWMutex // locks for filters, globals and `ExecuteWiter` when `reload` is true.
@@ -102,7 +100,6 @@ type DjangoEngine struct {
filters map[string]FilterFunction
// globals share context fields between templates.
globals map[string]interface{}
mu sync.Mutex // locks for template cache
templateCache map[string]*pongo2.Template
}
@@ -113,9 +110,15 @@ var (
// Django creates and returns a new django view engine.
// The given "extension" MUST begin with a dot.
func Django(directory, extension string) *DjangoEngine {
//
// Usage:
// Django("./views", ".html") or
// Django(iris.Dir("./views"), ".html") or
// Django(AssetFile(), ".html") for embedded data.
func Django(fs interface{}, extension string) *DjangoEngine {
s := &DjangoEngine{
directory: directory,
fs: getFS(fs),
rootDir: "/",
extension: extension,
globals: make(map[string]interface{}),
filters: make(map[string]FilterFunction),
@@ -125,20 +128,18 @@ func Django(directory, extension string) *DjangoEngine {
return s
}
// RootDir sets the directory to be used as a starting point
// to load templates from the provided file system.
func (s *DjangoEngine) RootDir(root string) *DjangoEngine {
s.rootDir = filepath.ToSlash(root)
return s
}
// Ext returns the file extension which this view engine is responsible to render.
func (s *DjangoEngine) Ext() string {
return s.extension
}
// Binary optionally, use it when template files are distributed
// inside the app executable (.go generated files).
//
// The assetFn and namesFn can come from the go-bindata library.
func (s *DjangoEngine) Binary(assetFn func(name string) ([]byte, error), namesFn func() []string) *DjangoEngine {
s.assetFn, s.namesFn = assetFn, namesFn
return s
}
// Reload if set to true the templates are reloading on each render,
// use it when you're in development and you're boring of restarting
// the whole app when you edit a template file.
@@ -203,133 +204,32 @@ func (s *DjangoEngine) RegisterTag(tagName string, fn TagParser) error {
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *DjangoEngine) Load() error {
if s.assetFn != nil && s.namesFn != nil {
// embedded
return s.loadAssets()
}
// load from directory, make the dir absolute here too.
dir, err := filepath.Abs(s.directory)
if err != nil {
return err
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
return err
}
// change the directory field configuration, load happens after directory has been set, so we will not have any problems here.
s.directory = dir
return s.loadDirectory()
}
// LoadDirectory loads the templates from directory.
func (s *DjangoEngine) loadDirectory() (templateErr error) {
dir, extension := s.directory, s.extension
fsLoader, err := pongo2.NewLocalFileSystemLoader(dir) // I see that this doesn't read the content if already parsed, so do it manually via filepath.Walk
if err != nil {
return err
}
set := pongo2.NewSet("", fsLoader)
set := pongo2.NewSet("", &tDjangoAssetLoader{fs: s.fs, rootDir: s.rootDir})
set.Globals = getPongoContext(s.globals)
s.mu.Lock()
defer s.mu.Unlock()
s.rmu.Lock()
defer s.rmu.Unlock()
// Walk the supplied directory and compile any files that match our extension list.
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// Fix same-extension-dirs bug: some dir might be named to: "users.tmpl", "local.html".
// These dirs should be excluded as they are not valid golang templates, but files under
// them should be treat as normal.
// If is a dir, return immediately (dir is not a valid golang template).
return walk(s.fs, s.rootDir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
} else {
return nil
}
rel, err := filepath.Rel(dir, path)
if err != nil {
templateErr = err
return err
if s.extension != "" {
if !strings.HasSuffix(path, s.extension) {
return nil
}
ext := filepath.Ext(rel)
if ext == extension {
buf, err := ioutil.ReadFile(path)
if err != nil {
templateErr = err
return err
}
name := filepath.ToSlash(rel)
s.templateCache[name], templateErr = set.FromString(string(buf))
if templateErr != nil {
return templateErr
}
}
}
return nil
})
if err != nil {
return err
}
return
}
// loadAssets loads the templates by binary (go-bindata for embedded).
func (s *DjangoEngine) loadAssets() error {
virtualDirectory, virtualExtension := s.directory, s.extension
assetFn, namesFn := s.assetFn, s.namesFn
// Make a file set with a template loader based on asset function
set := pongo2.NewSet("", &tDjangoAssetLoader{baseDir: s.directory, assetGet: s.assetFn})
set.Globals = getPongoContext(s.globals)
if len(virtualDirectory) > 0 {
if virtualDirectory[0] == '.' { // first check for .wrong
virtualDirectory = virtualDirectory[1:]
}
if virtualDirectory[0] == '/' || virtualDirectory[0] == os.PathSeparator { // second check for /something, (or ./something if we had dot on 0 it will be removed
virtualDirectory = virtualDirectory[1:]
}
}
s.mu.Lock()
defer s.mu.Unlock()
names := namesFn()
for _, path := range names {
if !strings.HasPrefix(path, virtualDirectory) {
continue
}
rel, err := filepath.Rel(virtualDirectory, path)
buf, err := asset(s.fs, path)
if err != nil {
return err
}
ext := filepath.Ext(rel)
if ext == virtualExtension {
buf, err := assetFn(path)
if err != nil {
return err
}
name := filepath.ToSlash(rel)
s.templateCache[name], err = set.FromString(string(buf))
if err != nil {
return err
}
}
}
return nil
name := strings.TrimPrefix(path, "/")
s.templateCache[name], err = set.FromBytes(buf)
return err
})
}
// getPongoContext returns the pongo2.Context from map[string]interface{} or from pongo2.Context, used internaly
@@ -350,15 +250,14 @@ func getPongoContext(templateData interface{}) pongo2.Context {
}
func (s *DjangoEngine) fromCache(relativeName string) *pongo2.Template {
s.mu.Lock()
if s.reload {
s.rmu.RLock()
defer s.rmu.RUnlock()
}
tmpl, ok := s.templateCache[relativeName]
if ok {
s.mu.Unlock()
if tmpl, ok := s.templateCache[relativeName]; ok {
return tmpl
}
s.mu.Unlock()
return nil
}
@@ -367,10 +266,6 @@ func (s *DjangoEngine) fromCache(relativeName string) *pongo2.Template {
func (s *DjangoEngine) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error {
// re-parse the templates if reload is enabled.
if s.reload {
// locks to fix #872, it's the simplest solution and the most correct,
// to execute writers with "wait list", one at a time.
s.rmu.Lock()
defer s.rmu.Unlock()
if err := s.Load(); err != nil {
return err
}
@@ -380,5 +275,5 @@ func (s *DjangoEngine) ExecuteWriter(w io.Writer, filename string, layout string
return tmpl.ExecuteWriter(getPongoContext(bindingData), w)
}
return fmt.Errorf("template with name: %s ddoes not exist in the dir: %s", filename, s.directory)
return ErrNotExist{filename, false}
}