zgok

Golang library to create a single binary with asset files.

  • Owner: srtkkou/zgok
  • Platform:
  • License:: Apache License 2.0
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Zgok

GoDoc Build Status

Zgok is a simple utility to embed any static file
into the binary executable file for Go.

日本語: README.jp.md

Features

  • It can create a single binary file containing all static files. Easy to deploy!
  • No dependency. Available on Windows, Linux, Mac and ARM.

Installation

Use the following to install the library and command line program:

go get -u github.com/srtkkou/zgok/...

Usage

Use the following command to build the zgok executable file.

$GOPATH/bin/zgok build -e exePath -z zipPath1 -z zipPath2 -o outPath

If you want to read the embedded file in the code, you can do like the
following.

package main

import (
	"fmt"
	"github.com/srtkkou/zgok"
	"io/ioutil"
	"os"
)

func main() {
	var content []byte
	path := "test.txt"
	zfs, _ := zgok.RestoreFileSystem(os.Args[0])
	if zfs != nil {
		// For release.
		content, _ = zfs.ReadFile(path)
	} else {
		// For development.
		content, _ = ioutil.ReadFile(path)
	}
	fmt.Println(string(content))
}

If you want to serve zgok embedded files as static files in the
web application, you can do like the following.
Note: Assuming that static assets are stored in [./web/public/*].

  1. This is the minimal example to use the zgok in web application.
package main

import (
	"net/http"
	"github.com/srtkkou/zgok"
	"os"
)

func main() {
	zfs, err := zgok.RestoreFileSystem(os.Args[0])
	if err != nil {
		panic(err)
	}
	assetServer := zfs.FileServer("web/public")
	http.Handle("/assets/", http.StripPrefix("/assets/", assetServer))
	http.ListenAndServe(":8080", nil)
}
  1. Build the zgok executable file.

    go build -o web web.go
    $GOPATH/bin/zgok build -e web -z web/public -o web_all

  2. Access the URL like [http://localhost:8080/assets/css/sample.css] on browser.

Description

The file format of Linux executable file (ELF) and that of the Windows (PE)
starts with a header section containing the sizes of each segments in the file.
In other words, the executable file knows its REAL size by themselves.
So you can add some extra data at the end of the executable file, and still
use it normally.

The format of the zgok executable file looks like the following., Header section, --------------, Section 1, Section 2, ..., Section n, Zip data, Zgok signature, Zgok will unzip the files in the zip data section and add the content of them
in a map accessible by their path.

License

Apache License Version 2.0. See the LICENSE file for details.

Main metrics

Overview
Name With Ownersrtkkou/zgok
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:Apache License 2.0
所有者活动
Created At2015-11-04 08:51:39
Pushed At2021-05-12 05:58:49
Last Commit At
Release Count2
Last Release Namev0.0.1 (Posted on )
First Release Namev0.0.0 (Posted on 2019-09-17 11:55:26)
用户参与
Stargazers Count68
Watchers Count1
Fork Count5
Commits Count54
Has Issues Enabled
Issues Count2
Issue Open Count0
Pull Requests Count0
Pull Requests Open Count0
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private