Skip to content

REABMAX/faucet-pipeline-go

Repository files navigation

GoReport Card Test Workflow Code Coverage

Go with faucet-pipeline

This go module provides a simple integration for faucet-pipeline in go. Faucet is able to generate a manifest.json file with fingerprinted names of the generated assets. Using the fingerprint mechanism it’s not possible anymore to hardcode URIs in your go templates, therefore a bit of integration code is needed to dynamically read the correct paths from the manifest.

Installation

To install faucet-pipeline-go run

go get github.com/REABMAX/faucet-pipeline-go

Usage

Import faucet-pipeline-go. Then, in your main func create a new PipelineAdapter with the the path to your manifest.json as parameter:

package example

import "github.com/REABMAX/faucet-pipeline-go"

func main() {
	faucetPipeline := faucet.NewPipelineAdapter("path/to/manifest.json")

	// you can enable/disable/check for hot reloading:
	faucetPipeline.EnableHotReload()
	if faucetPipeline.HotReloadIsEnabled() {
	    faucetPipeline.DisableHotReload()
	}
}

The faucet pipeline adapter implements the following interface:

package faucet

type IPipelineAdapter interface {
	TemplateFunc() (string, error) // TemplateFunc is a custom function that can be added to go templates as part of a template.FuncMap
	EnableHotReload() // EnableHotReload enables hot reloading functionality for TemplateFunc
	DisableHotReload() // DisableHotReload disables hot reloading functionality for TemplateFunc
	HotReloadIsEnabled() bool // HotReloadIsEnabled checks if hot reload functionality is enabled and returns the fitting boolean value
}

Now we can add our template func for go’s template engine. This example uses gin but you can use it with plain go template or any other framework as well.

router := gin.Default()
router.Static("/dist/", "./assets/dist")
router.FuncMap = template.FuncMap{
    "assets": faucetPipeline.TemplateFunc,
}
router.LoadHTMLGlob("templates/*")
router.GET("/", func(context *gin.Context) {
    context.HTML(http.StatusOK, "index.html", nil)
})

err = router.Run(":3000")

We can use our template func with the assets shortcut in our template:

<html>
    <head>
        <script type="text/javascript" src="{{ "dist/bundle.js" | assets }}"></script>
        <link rel="stylesheet" type="text/css" href="{{ "dist/bundle.css" | assets }}">
    </head>
    <body>
        <h1>faucet-pipeline rules!</h1>
    </body>
</body>
</html>

faucet-pipeline-go will look for the keys dist/bundle.js and dist/bundle.css in faucets manifest.json and add the fingerprinted file path to the template.

Tests

For testing ginkgo is used. To run the tests:

ginkgo -r