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.
[x] golint safe code output
[x] optional: gzip compression (with optional run-time decompression)
[x] optional: formatted code (gofmt)
[x] optional: spread files
[x] optional: unexporTed variables, functions and types
[x] optional: include multiple files and folders
[x] optional: exclude files or/and folders
[x] optional: replace text in files
[x] optional: custom base and prefix path
[x] Virtual Memory FileSystem - webdav
[x] HTTP FileSystem and Handler
[x] glob support - doublestar
[x] json / yaml / toml support
[x] optional: Update files remotely
[x] 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
package main
import (
"log"
"net/http"
// your generaTed package
"github.com/UnnoTed/fileb0x/_example/simple/static"
)
func main() {
files, err := static.WalkDirs("", false)
if err != 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")
if err != 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)
if err != nil {
log.Fatal(err)
}
log.Println(string(b))
// true = handler
// false = file system
as := false
// try it -> http://localhost:1337/public/secrets.txt
if as {
// 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...
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.
##################
## 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.com: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
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
# 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
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 func main() {
// you have the following functions available
// they all control files/dirs from/to the in-memory file system!
func Mkdir(name string, perm os.FileMode) error
func OpenFile(name string, flag int, perm os.FileMode) (File, error)
func RemoveAll(name string) error
func Rename(oldName, newName string) error
func Stat(name string) (os.FileInfo, error)
// you should remove those lines ^
// 1. creates a directory
err := myEmbeddedFiles.FS.Mkdir(myEmbeddedFiles.CTX, "assets", 0777)
if err != 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)
if err != 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)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
// close the file
if err1 := 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")
if err != 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")
if err != nil {
log.Fatal(err)
}
}
// 5. ch
$ claude mcp add fileb0x \
-- python -m otcore.mcp_server <graph>