fsbox

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2021 License: MIT Imports: 8 Imported by: 0

README

Test

FSbox

FSbox is a packd.Box implementation that uses go 1.16's io/fs package. It facilitates the packing of your assets, templates and other things into the Buffalo binary using the new embed and io/fs libraries and ditching Packr or any other third-party tool.

On development mode (GO_ENV == development or empty) FSbox falls back to open files using os.Open to provide the ability to do changes in those files without the need to recompile the app, facilitating an agile and iterative approach while developing.

⚠️ This package ONLY works with Go 1.16x or higher version of it.

Usage

You need to have a variable that embeds your templates and assets into the binary.

// embed.go
package app

var (
    
    //go:embed templates public migrations
    fsys embed.FS

    // The boxes your app may need.
    Assets  = fsbox.New(fsys, "public")
    Templates = fsbox.New(fsys, "templates")
    Migrations = fbox.New(fsys, "migrations")
)

These two boxes will be used in your Buffalo application for plush templates and assets serving as you can see next:

...
    // Adding custom partialFeeder
    helpers["partialFeeder"] = app.Templates.FindString
    
    // Render engine initialization
    Engine = render.New(render.Options{
		HTMLLayout:   "application.plush.html",
		TemplatesBox: app.TemplatesBox,
		AssetsBox:    app.AssetsBox,
		...
	})
...

Defining the partialFeeder is an important step since the default partialFeeder that Buffalo uses adds an underscore prefix to partials, and the embed functionality seems not to support embedding underscore prefixed files.

⚠️ Don't forget to rename your partials without the underscore prefix, otherwise these will not be embedded in the binary.

// Serving assets
bapp.ServeFiles("/", app.AssetsBox)
Can I use this in Buffalo?

Yes, however this could imply some changes in your application configuration and packages.

Documentation

Overview

fsbox package contains the implementation of a gobuffalo/packd box that uses the io/fs package to embed files. It is dependent on Go 1.16.

Index

Constants

View Source
const (
	//OptionFSIgnoreGoEnv allways uses the Open method of the filesystem
	OptionFSIgnoreGoEnv = Options("FS_IGNORE_GO_ENV")
)

Variables

View Source
var ErrCannotAdd = errors.New("stdbox does not allow to add")

ErrCannotAdd is returned if you want to add to box

Functions

func New

func New(fsys fs.FS, prefix string, options ...Options) *box

New box with the given filesystem

Types

type Options added in v1.1.3

type Options string

Options for the box.

Directories

Path Synopsis
internal
env

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL