assettube

package module
v0.0.0-...-66597b2 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2016 License: MIT Imports: 15 Imported by: 0

README

AssetTube

GoDoc

AssetTube is a tool fingerprints and serves asset files automatically. It's built to connect your webpack-processed assets to Go application.

How it works

AssetTube copys your asset files into a subdirectory named assettube and fingerprints them, in runtime. Every time the server is restarted, it will remove previously generated files and generates new files.

You could check out the example to have better idea of how it works.

Runtime Usage example

package main

import (
	"html/template"
	"net/http"
	"os"

	"github.com/theplant/assettube"
)

func init() {
	assettube.SetConfig(assettube.Config{
		Fingerprint:          true,
		URLPrefix:            "assets",
		SubresourceIntegrity: true,
	})
	assettube.Add("assets")
}

func main() {
	var tmpl = template.New("")
	tmpl.Funcs(template.FuncMap{
		"asset_path": assettube.AssetPath,
		"integrity":  assettube.Integrity,
	})
	tmpl.Parse(`<!DOCTYPE html>
<html>
<head>
	<title>Assetstube</title>
	<link rel="stylesheet" type="text/css" href="{{asset_path "css/app.css"}}">
	<script type="text/javascript" src="{{asset_path "js/app.js"}}" integrity="{{integrity "js/app.js"}}"></script>
</head>
<body>

</body>
</html>
`)

	tmpl.Execute(os.Stdout, nil)
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tmpl.Execute(w, nil)
	})
	http.HandleFunc("/assets/", assettube.ServeHTTP) // Note the trailing "/", whihc is necessary
	http.ListenAndServe(":8080", nil)
}

Webpack Usage

Install assettube npm package:

npm install assettube

In your webpack config file:

var path = require('path');
var webpack = require('webpack');
var AssetTube = require('assettube');

var config = {
	entry: { app: './index.js'},
	output: {
		path: path.join(__dirname, './output'),
		filename: '[name].[chunkhash].js',
		publicPath: './public'
	},
	plugins: [
		new AssetTube({
			// configurations
			// hostname: '',
			// urlPrefix: '',
			// basePath: '',
			// fileName: 'assettube.json',
			// stripSrc: null,
			// transformExtensions: /^(gz|map)$/i,
			// cache: null
		})
	]
};

module.exports = config;

Go server:

m, err := NewManagerManifest("path/to/assettube.json")
if err != nil {
	t.Fatal(err)
}

// the reset same as runtime mode

Documentation

Overview

package assettube fingerprints and servers your assets processed by webpack/gulp/other-tools from your Go application.

AssetTube copys your asset files into a subdirectory named `assettube` and fingerprints them, in runtime. Every time the server is restarted, it will remove previously generated files and generates new files.

Index

Constants

This section is empty.

Variables

View Source
var DefaultManager, _ = NewManager(Config{})

Functions

func Add

func Add(root string) error

Add includes path in Manager serving scope. It also copies and fingerprints assets into a subdirectory named "assettube". Every time it's called it removes the subdirectory and create a new one, then copy all the matched files into the new directory.

func AssetPath

func AssetPath(p string) string

AssetPath returns the fingerprinted filename, with Hostname and URLPrefix if configured. It's mostly used as a template function for package html/template or text/template.

func Integrity

func Integrity(p string) string

Integrity returns the SRI hash of corresponding file. You could specify which digest hash to use by Config.HashType. https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

func JSCSSOnly

func JSCSSOnly(path string, info os.FileInfo) bool
func Link(p string, attrs ...string) template.HTML

func Script

func Script(p string, attrs ...string) template.HTML

func ServeHTTP

func ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP returns the file content based on URL, stripped of URLPrefix.

func SetConfig

func SetConfig(cfg Config) error

SetConfig updates Manager configurations. Under the hood, it creates a new manager and reprocesses all the monitored directories.

Types

type Config

type Config struct {
	Fingerprint bool   // Fingerprint option. False by default, so you could only enable it for production server.
	URLPrefix   string // URLPrefix prepends url in the fingerprinted filename returned from Manager.
	Hostname    string // Hostname specifies CDN hostname. Empty by default.

	// Matcher decides what files AssetTube Manager should fingerprint and serve.
	// The default matcher only handle JS and CSS files (i.e.: *.js, *.css).
	Matcher func(path string, info os.FileInfo) bool

	// Enable SubresourceIntegrity support and specify digest hash method by HashType.
	// Note: SubresourceIntegrity only works when Fingerprint is enabled.
	SubresourceIntegrity bool
	HashType             HashType // Default HashType is HTSHA384(SHA-384).

	Logger io.Writer
}

type HashType

type HashType int

HashType represents the hash function used in Subresource Integrity.

const (
	// Three hash digest types supported in SubresourceIntegrity.
	HTSHA384 HashType = iota
	HTSHA256
	HTSHA512
)

func (HashType) Hash

func (h HashType) Hash() hash.Hash

Hash returns corresponding Hash functions for checksum calculation.

func (HashType) String

func (h HashType) String() string

Strings return HashType's string name.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager processes and serves the file content.

func NewManager

func NewManager(cfg Config, paths ...string) (*Manager, error)

NewManager returns an AssetTube Manager.

func NewManagerManifest

func NewManagerManifest(path string) (*Manager, error)

func (*Manager) Add

func (m *Manager) Add(root string) error

Add includes path in Manager serving scope. It also copies and fingerprints assets into a subdirectory named "assettube". Every time it's called it removes the subdirectory and create a new one, then copy all the matched files into the new directory.

func (*Manager) AssetPath

func (m *Manager) AssetPath(p string) string

AssetPath returns the fingerprinted filename, with Hostname and URLPrefix if configured. It's mostly used as a template function for package html/template or text/template.

func (*Manager) Integrity

func (m *Manager) Integrity(p string) string

Integrity returns the SRI hash of corresponding file. You could specify which digest hash to use by Config.HashType. https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

func (m *Manager) Link(p string, attrs ...string) template.HTML

func (*Manager) Script

func (m *Manager) Script(p string, attrs ...string) template.HTML

func (*Manager) ServeHTTP

func (m *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP returns the file content based on URL, stripped of URLPrefix.

func (*Manager) SetConfig

func (m *Manager) SetConfig(cfg Config) error

SetConfig updates Manager configurations. It overrides all configs with the new ones. Under the hood, it creates a new manager and reprocesses all the monitored directories.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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