Giter Club home page Giter Club logo

faucet-pipeline-go's Introduction

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

faucet-pipeline-go's People

Contributors

reabmax avatar

Stargazers

Lucas Dohmen avatar

Watchers

Lucas Dohmen avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.