a better customizable tool to embed files in go; also update embedded files remotely without restarting the server
a better customizable tool to embed files in go; also update embedded files remotely without restarting the server
A better customizable tool to embed files in go.
It is an alternative to go-bindata that have better features and organized configuration.
a better go-bindata
go-bindata?| Feature | fileb0x | go-bindata |
|---|---|---|
| gofmt | yes (optional) | no |
| golint | safe | unsafe |
| gzip compression | yes | yes |
| gzip decompression | yes (optional: runtime) | yes (on read) |
| gzip compression levels | yes | no |
| separated prefix / base for each file | yes | no (all files only) |
| different build tags for each file | yes | no |
| exclude / ignore files | yes (glob) | yes (regex) |
| spread files | yes | no (single file only) |
| unexported vars/funcs | yes (optional) | no |
| virtual memory file system | yes | no |
| http file system / handler | yes | no |
| replace text in files | yes | no |
| glob support | yes | no (walk folders only) |
| regex support | no | yes (ignore files only) |
| config file | yes (config file only) | no (cmd args only) |
| update files remotely | yes | no |
By using a virtual memory file system you can have access to files like when they're stored in a hard drive instead of a map[string][]byte you would be able to use IO writer and reader.
This means you can read, write, remove, stat and rename files also make, remove and stat directories.
Virtual Memory File System has similar functions as a hdd stored files would have.
golint safe code output
optional: gzip compression (with optional run-time decompression)
optional: formatted code (gofmt)
optional: spread files
optional: unexporTed variables, functions and types
optional: include multiple files and folders
optional: exclude files or/and folders
optional: replace text in files
optional: custom base and prefix path
Virtual Memory FileSystem - webdav
HTTP FileSystem and Handler
glob support - doublestar
json / yaml / toml support
optional: Update files remotely
optional: Build tags for each file
MIT
Here's the get-you-going in 30 seconds or less:
git clone https://github.com/UnnoTed/fileb0x.git
cd fileb0x
cd _example/simple
go generate
go build
./simple
mod.go defines the package as example.com/foo/simpleb0x.yaml defines the sub-package static from the folder publicmain.go includes the comment //go:generate go run github.com/UnnoTed/fileb0x b0x.yamlmain.go also includes the import example.com/foo/simple/staticgo generate locally installs fileb0x which generates ./static according to bax.yamlgo build creates the binary simple from package main in the current folder./simple runs the self-contained standalone webserver with built-in files from publicHow to use it?
go get -u github.com/UnnoTed/fileb0x
First you need to create a config file, it can be *.json, *.yaml or *.toml. (* means any file name)
Now write into the file the configuration you wish, you can use the example files as a start.
json config file example b0x.json
yaml config file example b0x.yaml
toml config file example b0x.toml
if you prefer to use it from the cmd or terminal edit and run the command below.
fileb0x YOUR_CONFIG_FILE.yaml
or if you wish to generate the embedded files through go generate just add and edit the line below into your main.go.
//go:generate fileb0x YOUR_CONFIG_FILE.yaml
What functions and variables fileb0x let me access and what are they for?
var HTTP http.FileSystem
A In-Memory HTTP File System.
Serve files through a HTTP FileServer.
// http.ListenAndServe will create a server at the port 8080
// it will take http.FileServer() as a param
//
// http.FileServer() will use HTTP as a file system so all your files
// can be avialable through the port 8080
http.ListenAndServe(":8080", http.FileServer(myEmbeddedFiles.HTTP))
How to use it with echo?
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
// your embedded files import here ...
"github.com/UnnoTed/fileb0x/_example/echo/myEmbeddedFiles"
)
func main() {
e := echo.New()
// enable any filename to be loaded from in-memory file system
e.GET("/*", echo.WrapHandler(myEmbeddedFiles.Handler))
// http://localhost:1337/public/README.md
e.Start(":1337")
}
echo?package main
import (
"github.com/labstack/echo"
// your embedded files import here ...
"github.com/UnnoTed/fileb0x/_example/echo/myEmbeddedFiles"
)
func main() {
e := echo.New()
// read ufo.html from in-memory file system
htmlb, err := myEmbeddedFiles.ReadFile("ufo.html")
if err != nil {
log.Fatal(err)
}
// convert to string
html := string(htmlb)
// serve ufo.html through "/"
e.GET("/", func(c echo.Context) error {
// serve as html
return c.HTML(http.StatusOK, html)
})
e.Start(":1337")
}
Examples
…
Update files remotely
Having to upload an entire binary just to update some files in a b0x and restart a server isn't something that i like to do...
By enabling the updater option, the next time that you generate a b0x, it will include a http server, this http server will use a http basic auth and it contains 1 endpoint / that accepts 2 methods: GET, POST.
The GET method responds with a list of file names and sha256 hash of each file.
The POST method is used to upload files, it creates the directory tree of a new file and then creates the file or it updates an existing file from the virtual memory file system... it responds with a ok string when the upload is successful.
…
fileb0x -update=http://yourServer.com:8041 b0x.toml to update the files in the running server.Build Tags
To use build tags for a b0x package just add the tags to the tags property in the main object of your config file
# default: main
pkg: static
# destination
dest: "./static/"
# build tags for the main b0x.go file
tags: "!linux"
You can also have different build tags for a list of files, you must enable the spread property in the main object of your config file, then at the custom list, choose the set of files which you want a different build tag
…
the config above will make:
ab0x.go # // +build windows darwin
b0xfile_ufo.exe.go # // +build windows
b0xfile_start_space_ship.bat.go # // +build darwin
FS (File System)
var FS webdav.FileSystem
In-Memory File System.
Lets you read, write, remove, stat and rename files and make, remove and stat directories...
…
go var Handler *webdav.Handler
##### Type
[`webdav.Handler`](https://godoc.org/golang.org/x/net/webdav#Handler)
##### What is it?
A HTTP Handler implementation.
##### What it does?
Serve your embedded files.
##### How to use it?
```go
// ListenAndServer will create a http server at port 8080
// and use Handler as a http handler to serve your embedded files
http.ListenAndServe(":8080", myEmbeddedFiles.Handler)
ReadFile
func ReadFile(filename string) ([]byte, error)
A Helper function to read your embedded files.
Reads the specified file from the in-memory file system and return it as a byte slice.
// it works the same way that ioutil.ReadFile does.
// but it will read the file from the in-memory file system
// instead of the hard disk!
//
// the file name is passwords.txt
// topSecretFile is a byte slice ([]byte)
topSecretFile, err := myEmbeddedFiles.ReadFile("passwords.txt")
if err != nil {
log.Fatal(err)
}
log.Println(string(topSecretFile))
WriteFile
func WriteFile(filename string, data []byte, perm os.FileMode) error
A Helper function to write a file into the in-memory file system.
Writes the data into the specified filename in the in-memory file system, meaning you embedded a file!
-- IMPORTANT -- IT WON'T WRITE THE FILE INTO THE .GO GENERATED FILE, IT WILL BE TEMPORARY, WHILE YOUR APP IS RUNNING THE FILE WILL BE AVAILABLE, AFTER IT SHUTDOWN, IT IS GONE.
// it works the same way that ioutil.WriteFile does.
// but it will write the file into the in-memory file system
// instead of the hard disk!
//
// the file name is secret.txt
// data should be a byte slice ([]byte)
// 0644 is a unix file permission
data := []byte("jet fuel can't melt steel beams")
err := myEmbeddedFiles.WriteFile("secret.txt", data, 0644)
if err != nil {
log.Fatal(err)
}
WalkDirs
func WalkDirs(name string, includeDirsInList bool, files ...string) ([]string, error) {
[]string
A Helper function to walk dirs from the in-memory file system.
Returns a list of files (with option to include dirs) that are currently in the in-memory file system.
includeDirsInTheList := false
// WalkDirs returns a string slice with all file paths
files, err := myEmbeddedFiles.WalkDirs("", includeDirsInTheList)
if err != nil {
log.Fatal(err)
}
log.Println("List of all my files", files)
No open issues yet, or sync has not completed.