Skip to content

a better customizable tool to embed files in go; also update embedded files remotely without restarting the server

License

Notifications You must be signed in to change notification settings

UnnoTed/fileb0x

Repository files navigation

fileb0xCircle CIGoDocGoReportCard

What is fileb0x?

A better customizable tool to embed files in go.

It is an alternative togo-bindatathat have better features and organized configuration.

TL;DR

a bettergo-bindata


How does it compare togo-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

What are the benefits of using a Virtual Memory File System?

By using a virtual memory file system you can have access to files like when they're stored in a hard drive instead of amap[string][]byteyou would be able to use IO writer and reader. This means you canread,write,remove,statandrenamefiles alsomake,removeandstatdirectories.

TL;DR

Virtual Memory File System has similar functions as a hdd stored files would have.

Features

  • 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

License

MIT

Get Started

TL;DR QuickStart™

Here's the get-you-going in 30 seconds or less:

git clone https://github /UnnoTed/fileb0x.git
cdfileb0x
cd_example/simple
go generate
go build
./simple
  • mod.godefines the package asexample /foo/simple
  • b0x.yamldefines the sub-packagestaticfrom the folderpublic
  • main.goincludes the comment//go:generate go run github /UnnoTed/fileb0x b0x.yaml
  • main.goalso includes the importexample /foo/simple/static
  • go generatelocally installsfileb0xwhich generates./staticaccording tobax.yaml
  • go buildcreates the binarysimplefrompackage mainin the current folder
  • ./simpleruns the self-contained standalone webserver with built-in files frompublic
How to use it?
1. Download
go get -u github /UnnoTed/fileb0x
2. Create a config file

First you need to create a config file, it can be*.json,*.yamlor*.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 exampleb0x.json

yaml config file exampleb0x.yaml

toml config file exampleb0x.toml

3. Run

if you prefer to use it from thecmd or terminaledit and run the command below.

fileb0x YOUR_CONFIG_FILE.yaml

or if you wish to generate the embedded files throughgo generatejust add and edit the line below into yourmain.go.

//go:generate fileb0x YOUR_CONFIG_FILE.yaml
What functions and variables fileb0x let me access and what are they for?

HTTP

varHTTPhttp.FileSystem
Type

http.FileSystem

What is it?

A In-Memory HTTP File System.

What it does?

Serve files through a HTTP FileServer.

How to use it?
// 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`?
packagemain

import(
"github /labstack/echo"
"github /labstack/echo/engine/standard"
// your embedded files import here...
"github /UnnoTed/fileb0x/_example/echo/myEmbeddedFiles"
)

funcmain() {
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")
}
How to serve a single file throughecho?
packagemain

import(
"github /labstack/echo"

// your embedded files import here...
"github /UnnoTed/fileb0x/_example/echo/myEmbeddedFiles"
)

funcmain() {
e:=echo.New()

// read ufo.html from in-memory file system
htmlb,err:=myEmbeddedFiles.ReadFile("ufo.html")
iferr!=nil{
log.Fatal(err)
}

// convert to string
html:=string(htmlb)

// serve ufo.html through "/"
e.GET("/",func(cecho.Context)error{

// serve as html
returnc.HTML(http.StatusOK,html)
})

e.Start(":1337")
}
Examples

simple example- main.go

echo example- main.go

packagemain

import(
"log"
"net/http"

// your generaTed package
"github /UnnoTed/fileb0x/_example/simple/static"
)

funcmain() {
files,err:=static.WalkDirs("",false)
iferr!=nil{
log.Fatal(err)
}

log.Println("ALL FILES",files)

// here we'll read the file from the virtual file system
b,err:=static.ReadFile("public/README.md")
iferr!=nil{
log.Fatal(err)
}

// byte to str
s:=string(b)
s+="#hello"

// write file back into the virtual file system
err:=static.WriteFile("public/README.md",[]byte(s),0644)
iferr!=nil{
log.Fatal(err)
}


log.Println(string(b))

// true = handler
// false = file system
as:=false

// try it -> http://localhost:1337/public/secrets.txt
ifas{
// as Handler
panic(http.ListenAndServe(":1337",static.Handler))
}else{
// as File System
panic(http.ListenAndServe(":1337",http.FileServer(static.HTTP)))
}
}
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...

How it works?

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.

TheGETmethod responds with a list of file names and sha256 hash of each file. ThePOSTmethod 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 aokstring when the upload is successful.

How to update files remotely?
  1. First enable the updater option in your config file:
##################
## yaml example ##
##################

#updater allows you to update a b0x in a running server
#without having to restart it
updater:
#disabled by default
enabled:false

#empty mode creates a empty b0x file with just the
#server and the filesystem, then you'll have to upload
#the files later using the cmd:
#fileb0x -update=http://server:port b0x.yaml
#
#it avoids long compile time
empty:false

#amount of uploads at the same time
workers:3

#to get a username and password from a env variable
#leave username and password blank (username: "" )
#then set your username and password in the env vars
#(no caps) -> fileb0x_username and fileb0x_password
#
#when using env vars, set it before generating a b0x
#so it can be applied to the updater server.
username:"user"#username: ""
password:"pass"#password: ""
port:8041
  1. Generate a b0x with the updater option enabled, don't forget to set the username and password for authentication.
  2. When your files update, just runfileb0x -update=http://yourServer:8041 b0x.tomlto update the files in the running server.
Build Tags

To use build tags for a b0x package just add the tags to thetagsproperty 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 thespreadproperty in the main object of your config file, then at thecustomlist, choose the set of files which you want a different build tag

#default: main
pkg:static

#destination
dest:"./static/"

#build tags for the main b0x.go file
tags:"windows darwin"

#[spread] means it will make a file to hold all fileb0x data
#and each file into a separaTed.go file
#
#example:
#theres 2 files in the folder assets, they're: hello.json and world.txt
#when spread is activaTed, fileb0x will make a file:
#b0x.go or [output]'s data, assets_hello.json.go and assets_world.txt.go
#
#
#type: bool
#default: false
spread:true

#type: array of objects
custom:
#type: array of strings
-files:
-"start_space_ship.exe"

#build tags for this set of files
#it will only work if spread mode is enabled
tags:"windows"

#type: array of strings
-files:
-"ufo.dmg"

#build tags for this set of files
#it will only work if spread mode is enabled
tags:"darwin"

the config above will make:

ab0x.go#// +build windows darwin

b0xfile_ufo.exe.go#// +build windows
b0xfile_start_space_ship.bat.go#// +build darwin

Functions and Variables

FS (File System)
varFSwebdav.FileSystem
Type

webdav.FileSystem

What is it?

In-Memory File System.

What it does?

Lets youread, write, remove, stat and renamefiles andmake, remove and statdirectories...

How to use it?
funcmain() {

// you have the following functions available
// they all control files/dirs from/to the in-memory file system!
funcMkdir(namestring,permos.FileMode)error
funcOpenFile(namestring,flagint,permos.FileMode) (File,error)
funcRemoveAll(namestring)error
funcRename(oldName,newNamestring)error
funcStat(namestring) (os.FileInfo,error)
// you should remove those lines ^

// 1. creates a directory
err:=myEmbeddedFiles.FS.Mkdir(myEmbeddedFiles.CTX,"assets",0777)
iferr!=nil{
log.Fatal(err)
}

// 2. creates a file into the directory we created before and opens it
// with fileb0x you can use ReadFile and WriteFile instead of this complicaTed thing
f,err:=myEmbeddedFiles.FS.OpenFile(myEmbeddedFiles.CTX,"assets/memes.txt",os.O_WRONLY|os.O_CREATE|os.O_TRUNC,0644)
iferr!=nil{
log.Fatal(err)
}

data:=[]byte("I are programmer I make computer beep boop beep beep boop")

// write the data into the file
n,err:=f.Write(data)
iferr==nil&&n<len(data) {
err=io.ErrShortWrite
}

// close the file
iferr1:=f.Close();err==nil{
log.Fatal(err1)
}

// 3. rename a file
// can also move files
err=myEmbeddedFiles.FS.Rename(myEmbeddedFiles.CTX,"assets/memes.txt","assets/programmer_memes.txt")
iferr!=nil{
log.Fatal(err)
}

// 4. checks if the file we renamed exists
if_,err=myEmbeddedFiles.FS.Stat(myEmbeddedFiles.CTX,"assets/programmer_memes.txt");os.IsExist(err) {
// exists!

// tries to remove the /assets/ directory
// from the in-memory file system
err=myEmbeddedFiles.FS.RemoveAll(myEmbeddedFiles.CTX,"assets")
iferr!=nil{
log.Fatal(err)
}
}

// 5. checks if the dir we removed exists
if_,err=myEmbeddedFiles.FS.Stat(myEmbeddedFiles.CTX,"public/");os.IsNotExist(err) {
// doesn't exists!
log.Println("works!")
}
}
Handler
varHandler*webdav.Handler
Type

webdav.Handler

What is it?

A HTTP Handler implementation.

What it does?

Serve your embedded files.

How to use it?
// 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
funcReadFile(filenamestring) ([]byte,error)
Type

ioutil.ReadFile

What is it?

A Helper function to read your embedded files.

What it does?

Reads the specified file from the in-memory file system and return it as a byte slice.

How to use it?
// 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")
iferr!=nil{
log.Fatal(err)
}

log.Println(string(topSecretFile))
WriteFile
funcWriteFile(filenamestring,data[]byte,permos.FileMode)error
Type

ioutil.WriteFile

What is it?

A Helper function to write a file into the in-memory file system.

What it does?

Writes thedatainto the specifiedfilenamein 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.

How to use it?
// 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)
iferr!=nil{
log.Fatal(err)
}
WalkDirs
funcWalkDirs(namestring,includeDirsInListbool,files...string) ([]string,error) {
Type

[]string

What is it?

A Helper function to walk dirs from the in-memory file system.

What it does?

Returns a list of files (with option to include dirs) that are currently in the in-memory file system.

How to use it?
includeDirsInTheList:=false

// WalkDirs returns a string slice with all file paths
files,err:=myEmbeddedFiles.WalkDirs("",includeDirsInTheList)
iferr!=nil{
log.Fatal(err)
}

log.Println("List of all my files",files)