zgok

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

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.

Documentation

Overview

Package zgok provides a simple library to create a single binary with asset files in Go (Golang).

Example:

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)
}

Index

Constants

View Source
const (
	APP_BYTE_SIZE       = 8  // Byte size of the signature app field.
	SIGNATURE_BYTE_SIZE = 64 // Byte size of the signature.
)
View Source
const (
	APP   = "zgok" // Application name.
	MAJOR = 0      // Major version.
	MINOR = 0      // Minor version.
	REV   = 1      // Revision.
)

Variables

This section is empty.

Functions

func Version

func Version() string

Get version string.

Types

type Builder

type Builder interface {
	SetExePath(exePath string) error
	AddZipPath(zipPath string) error
	SetOutPath(outPath string)
	Build() error
}

Builder interface.

func NewZgokBuilder

func NewZgokBuilder() Builder

Initialize new zgok builder.

type File

type File interface {
	SetPath(path string)                          // Set file path.
	Path() string                                 // Get file path.
	SetFileInfo(fileInfo os.FileInfo)             // Set file info.
	FileInfo() os.FileInfo                        // Get file info.
	SetBytes(content []byte)                      // Set content bytes.
	Bytes() []byte                                // Get content bytes.
	SetNewReader()                                // Set a new reader.
	Close() error                                 // Implements [net/http.File.Close]
	Read(p []byte) (int, error)                   // Implements [net/http.File.Read]
	Readdir(count int) ([]os.FileInfo, error)     // Implements [net/http.File.Readdir]
	Seek(offset int64, whence int) (int64, error) // Implements [net/http.File.Seek]
	Stat() (os.FileInfo, error)                   // Implements [net/http.File.Stat]
}

File interface.

func NewZgokFile

func NewZgokFile() File

Create a new zgok file.

type FileSystem

type FileSystem interface {
	AddFile(file File)
	GetFile(path string) (File, error)
	ReadFile(path string) ([]byte, error)
	ReadFileString(path string) (string, error)
	Paths() []string
	SubFileSystem(rootPath string) (FileSystem, error)
	Signature() Signature
	SetSignature(signature Signature)
	String() string
	Open(name string) (http.File, error)     // Implements [net/http.FileSystem.Open]
	FileServer(basePath string) http.Handler // Get a static file server.
}

File system interface. Implements net/http.FileSystem

func NewFileSystem

func NewFileSystem() FileSystem

Create a new file system.

func RestoreFileSystem

func RestoreFileSystem(path string) (FileSystem, error)

Restore file system.

type Signature

type Signature interface {
	ExeSize() int64
	SetExeSize(exeSize int64)
	ZipSize() int64
	SetZipSize(zipSize int64)
	TotalSize() int64
	String() string
	Dump() ([]byte, error)
}

Signature interface.

func NewSignature

func NewSignature() Signature

Initialize signature.

func RestoreSignature

func RestoreSignature(data []byte) (Signature, error)

Restore signature from bytes.

type Unzipper

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

Unzipper.

func NewUnzipper

func NewUnzipper(zipBytes *[]byte) *Unzipper

Create new unzipper.

func (*Unzipper) Unzip

func (u *Unzipper) Unzip() (FileSystem, error)

Unzip all the files in zip.

type Zipper

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

func NewZipper

func NewZipper() *Zipper

Create new zipper.

func (*Zipper) Add

func (z *Zipper) Add(path string) error

Add files in the path to zip.

func (*Zipper) Bytes

func (z *Zipper) Bytes() ([]byte, error)

Get bytes of zip.

func (*Zipper) Close

func (z *Zipper) Close() error

Close zip writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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