Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding files in bin. #4

Open
frederikhors opened this issue Aug 20, 2019 · 1 comment
Open

Embedding files in bin. #4

frederikhors opened this issue Aug 20, 2019 · 1 comment

Comments

@frederikhors
Copy link

I think a good plus would be the embedding of files in go with some of these projects: https://github.com/avelino/awesome-go/blob/master/README.md#resource-embedding.

None of those projects has an hashing system/strategy as far as I know.

@catcombo
Copy link
Owner

If I understood correctly, all of these libraries implements the same behaviour: they convert static files into arrays of bytes and saves them into .go files. Then compiles application with these files so they can be accessible from the memory when the program runs. To access files content one can use an implementation of the http.FileSystem interface like in the following example of the packr library.

package main

import (
  "net/http"

  "github.com/gobuffalo/packr"
)

func main() {
  box := packr.NewBox("./templates")

  http.Handle("/", http.FileServer(box))
  http.ListenAndServe(":3000", nil)
}

I can change the type of the Storage.OutputDir to http.FileSystem to initialize new storage as storage, err := NewStorage(box). But http.FileSystem doesn't allow to list files in directory or get directory's path. Thus Storage.CollectStatic method will not work.

The only chance to make it work is to limit the workflow to the single use case:

  1. Collect static to output dir with the cli tool collectstatic --output output/staticfiles --input input/assets
  2. Embed output/staticfiles with packr or similar tool
  3. Use storage as a wrapper to a box:
box := packr.NewBox("./output/staticfiles")
storage, err := staticfiles.NewStorage(box)
http.Handle("/", http.FileServer(storage))

But this implementation is very limited. It will not be possible to use storage.CollectStatic, storage directories listing will not work and also will not be possible to disable storage.Enabled. On the other hand storage.Resolve will work as expected.

What are your thoughts on that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants