Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Latest commit

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Embedder

Generate constant variable by globbing any kind of text file such as html or sql. The values are gzip compressed and base64 encoded (using standard encoding, not URL encoding). This is useful for keeping web app to a single file, therefore simplifying deployment.

For other frontend stuff you may also want to also consider ZipFS as it's better suited for code generated by a task runners or other automated system such as grunt, gulp, webpack, rollup or taskforce 😉.

Have fun! 😄

Installation

$ go get github.com/cjtoolkit/embedder

Usage

$ embedder DestPackageName, DestFilename, GlobPattern...

Also add the following to anywhere in your codebase, so you can decode the constant values, make sure the package name is correct and you can change it to your taste.

package embedderTools
import (
"bytes""compress/gzip""encoding/base64""io""log"
)
funcDecodeValue(valuestring) []byte {
raw, err:=base64.StdEncoding.DecodeString(value)
errCheck(err)
r, err:=gzip.NewReader(bytes.NewReader(raw))
errCheck(err)
buf:=&bytes.Buffer{}
_, err=io.Copy(buf, r)
errCheck(err)
returnbuf.Bytes()
}
funcerrCheck(errerror) {
iferr!=nil {
log.Panic(err)
}
}

Example

Input

resources/example1.html

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>{{.PageTitle}}</h1><ul>
{{range .Todos}}
{{if .Done}}
<liclass="done">{{.Title}}</li>
{{else}}
<li>{{.Title}}</li>
{{end}}
{{end}}
</ul></body></html>

resources/example2.html

<h1>{{.PageTitle}}</h1><ul>
{{range .Todos}}
{{if .Done}}
<liclass="done">{{.Title}}</li>
{{else}}
<li>{{.Title}}</li>
{{end}}
{{end}}
</ul>

generate.go

//go:generate embedder example const.go resources/*.htmlpackage example

and than run $ go generate

Output

const.go

// Code generated by Embedder. DO NOT EDIT.package example
const (
// Source: resources/example1.htmlExample1=`H4sIAAAAAAAC/3yQO08EIRDHez7FSO+S6yxmaTxtvQILSzzGXZI5SBYsDOG7G2A1WxgbHv/HjwfenV8ezdvlCdZ8Yy2wTcA2LLOkIJtA1mkBAIA3yhauq90S5Vm+muf7B7lb2WcmbdqIamwEqtHF9+i+GumkS5kudqGeqxXVetICP3lAStlsWAgmE11MtXZxGP4DpnMMdBD7sezhyjalWboYSDb+L5u9PhCI0x/t/wrB7fmfNap2U1T7c1T/se8AAAD//2oeexlBAQAA`// Source: resources/example2.htmlExample2=`H4sIAAAAAAAC/7LJMLSrrtYLSExPDcksyUmtrbXRzzC047IpzbHjUlBQUKiuLkrMS09V0AvJT8kvrq0FC0IkMtMU9Fzy81KRBEHAJidTITknsbjYViklPy9VCWQ+3OycTDskE1JzirHoxqchLwWqHsa20S/NsQMEAAD//wbsY17GAAAA`
)

About

🚄 Automatic Embedder

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages