Skip to content

Commit

Permalink
minor (see prev commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Sep 25, 2022
1 parent 512ed6f commit 68daa8d
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 69 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import "github.com/kataras/iris/v12"
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x/errors"
)

// $ go install github.com/go-bindata/go-bindata/v3/go-bindata@latest
// $ go-bindata -fs ./data/...
// $ go-bindata -prefix "data" -fs ./data/...
// $ go run .

var page = struct {
Expand All @@ -13,19 +16,21 @@ var page = struct {
func newApp() *iris.Application {
app := iris.New()

app.RegisterView(iris.HTML(AssetFile(), ".html").RootDir("views"))

// Using the iris.PrefixDir you can select
// which directories to use under a particular file system,
// e.g. for views the ./data/views and for static files
// the ./data/public.
templatesFS := iris.PrefixDir("./data/views", AssetFile())
app.RegisterView(iris.HTML(templatesFS, ".html"))

publicFS := iris.PrefixDir("./data/public", AssetFile())
// e.g. for views the ./public:
// publicFS := iris.PrefixDir("./public", AssetFile())
publicFS := iris.PrefixDir("./public", AssetFile())
app.HandleDir("/", publicFS)

app.Get("/", func(ctx iris.Context) {
ctx.ViewData("Page", page)
ctx.View("index.html")
if err := ctx.View("index.html"); err != nil {
errors.InvalidArgument.Err(ctx, err)
return
}
})

return app
Expand Down
5 changes: 5 additions & 0 deletions aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ func PrefixDir(prefix string, fs http.FileSystem) http.FileSystem {
return &prefixedDir{prefix, fs}
}

// PrefixFS same as "PrefixDir" but for `fs.FS` type.
func PrefixFS(fileSystem fs.FS, dir string) (fs.FS, error) {
return fs.Sub(fileSystem, dir)
}

type prefixedDir struct {
prefix string
fs http.FileSystem
Expand Down
7 changes: 7 additions & 0 deletions view/amber.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (s *AmberEngine) AddFunc(funcName string, funcBody interface{}) {
//
// Returns an error if something bad happens, user is responsible to catch it.
func (s *AmberEngine) Load() error {
rootDirName := getRootDirName(s.fs)

return walk(s.fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -167,6 +169,11 @@ func (s *AmberEngine) Load() error {
}
}

if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}

contents, err := asset(s.fs, path)
if err != nil {
return fmt.Errorf("%s: %w", path, err)
Expand Down
2 changes: 1 addition & 1 deletion view/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *BlocksEngine) Name() string {
}

// RootDir sets the directory to use as the root one inside the provided File System.
func (s *BlocksEngine) RootDir(root string) *BlocksEngine { // TODO: update blocks for the new fs.FS interface and use it for Sub.
func (s *BlocksEngine) RootDir(root string) *BlocksEngine {
s.Engine.RootDir(root)
return s
}
Expand Down
7 changes: 7 additions & 0 deletions view/django.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ 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 {
rootDirName := getRootDirName(s.fs)

return walk(s.fs, "", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -237,6 +239,11 @@ func (s *DjangoEngine) Load() error {
}
}

if s.rootDir == rootDirName {
path = strings.TrimPrefix(path, rootDirName)
path = strings.TrimPrefix(path, "/")
}

contents, err := asset(s.fs, path)
if err != nil {
return err
Expand Down
31 changes: 26 additions & 5 deletions view/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {
if err != nil {
return err
}

fileSystem = sub
}

Expand All @@ -25,13 +24,13 @@ func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {

return fs.WalkDir(fileSystem, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("%s: %w", path, err)
return fmt.Errorf("walk: %s: %w", path, err)
}

info, err := d.Info()
if err != nil {
if err != filepath.SkipDir {
return fmt.Errorf("%s: %w", path, err)
return fmt.Errorf("walk stat: %s: %w", path, err)
}

return nil
Expand All @@ -41,15 +40,37 @@ func walk(fileSystem fs.FS, root string, walkFn filepath.WalkFunc) error {
return nil
}

return walkFn(path, info, err)
walkFnErr := walkFn(path, info, err)
if walkFnErr != nil {
return fmt.Errorf("walk: walkFn: %w", walkFnErr)
}

return nil
})

}

func asset(fileSystem fs.FS, name string) ([]byte, error) {
return fs.ReadFile(fileSystem, name)
data, err := fs.ReadFile(fileSystem, name)
if err != nil {
return nil, fmt.Errorf("asset: read file: %w", err)
}

return data, nil
}

func getFS(fsOrDir interface{}) fs.FS {
return context.ResolveFS(fsOrDir)
}

func getRootDirName(fileSystem fs.FS) string {
rootDirFile, err := fileSystem.Open(".")
if err == nil {
rootDirStat, err := rootDirFile.Stat()
if err == nil {
return rootDirStat.Name()
}
}

return ""
}
Loading

0 comments on commit 68daa8d

Please sign in to comment.