The PrefixDir function

The iris.PrefixDir function wraps an existing "fs" file system and returns a new one which open files by prepending the path with the given "prefix".

func PrefixDir(prefix string, fs http.FileSystem) http.FileSystem

It is extremely useful when you use a go-bindata file to embed both view templates and public static files. With PrefixDir you can select which directory to use to serve static files and which to render views.


│   main.go
|   bindata.go
│         ...your templates
│         ...your public assets

Let's build the bindata.go file which will contain the embedded ./data directory:

$ go get -u
$ go-bindata -fs ./data/...

Use the ./data/views for templates and ./data/public for file server:

templatesFS := iris.PrefixDir("./data/views", AssetFile())
app.RegisterView(iris.HTML(templatesFS, ".html"))

publicFS := iris.PrefixDir("./data/public", AssetFile())
app.HandleDir("/", publicFS)

Last updated