Categorygithub.com/devplayg/eggcrate
modulepackage
1.0.3
Repository: https://github.com/devplayg/eggcrate.git
Documentation: pkg.go.dev

# README

Eggcrate

Build Status

EggCrate embeds static files into Go source encoded base64

Encode

config := eggcrate.Config{
    Dir:        "/path/to/dir",
    OutFile:    "output.go",
    UriPrefix:  "/assets",
    Extensions: "", // js,png,css,eot,svg,ttf,woff,woff2,js,html
}
_, err := eggcrate.Encode(&config)

output.go

File map, map[string][]byte, is generated and encoded to base64 by encode() and stored into assetData.go

// Code generated by eggcrate. DO NOT EDIT.
/*
	/assets/css/app.bundle.css
	/assets/css/fa-brands.css
	/assets/css/fa-regular.css
    ...
*/

package main

var assetData = "Dv+BBAEC/4IAAQwBCg..."

Decode

var fileMap map[string][]byte
fileMap, _ = eggcrate.Decode(assetData) 

With http/net

func startHttpServer() {
    r := mux.NewRouter()
    r.HandleFunc("/assets/{d1}/{d2}", AssetHandler)
    r.HandleFunc("/assets/{d1}/{d2}/{d3}", AssetHandler)

    http.Handle("/", r)
    srv := &http.Server{
        Handler:      r,
        Addr:         "127.0.0.1:8000",
    }
    log.Fatal(srv.ListenAndServe())
}

func AssetHandler(w http.ResponseWriter, r *http.Request) {
    u, _ := url.Parse(r.RequestURI)
    val, ok := fileMap[u.Path]
    if !ok {
        return
    }
    w.Header().Set("Content-Type", "text/javascript;charset=UTF-8")
    w.Header().Set("Content-Encoding", "gzip")
    w.WriteHeader(http.StatusOK)
    w.Write(val)
}

Directory structure

/gohome/assets
|-- css
|   |-- app.bundle.css
|   |-- themes
|   |   |-- cust-theme-1.css
|   |   |-- cust-theme-2.css
|   |   `-- cust-theme-3.css.map
|   `-- vendors.bundle.css.map
|-- js
|   |-- app.bundle.js
|   |-- toastr.js
|   `-- vendors.bundle.js
`-- webfonts
    |-- summernote.eot
    |-- summernote.ttf
    `-- summernote.woff

Map structure

Key (string)Value ([]byte)
/assets/css/custom.css[]byte(zipped)
/assets/img/logo.png[]byte(zipped)
/assets/js/custom.js[]byte(zipped)

Exasmple

https://github.com/devplayg/eggcrate/blob/master/examples/eggcrate.go

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author

# Structs

No description provided by the author