Description
I'm trying to follow the example in the README on how to handle local filesystem vs embedded assets with a dev
build tag.
My project looks like this:
├── Makefile
├── cmd
│ └── main.go
├── frontend
│ └── index.html
├── go.mod
├── go.sum
└── pkg
└── data
├── assets_generate.go
├── data.go
└── dev.go
In my data.go
I have:
// +build ignore
package main
import (
"log"
"github.com/shurcooL/vfsgen"
"github.com/gargath/vfstest/pkg/data"
)
func main() {
err := vfsgen.Generate(data.Assets, vfsgen.Options{
PackageName: "data",
BuildTags: "!dev",
VariableName: "Assets",
})
if err != nil {
log.Fatalln(err)
}
}
while in dev.go
I have:
// +build dev
package data
import (
"net/http"
)
var Assets http.FileSystem = http.Dir("../../frontend/")
My cmd/main.go
is a simple
package main
import (
"net/http"
"log"
"github.com/gargath/vfstest/pkg/data"
)
func main() {
fs := http.FileServer(data.Assets)
http.Handle("/", fs)
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}
When I run go generate ./pkg/...
and then go build -o vfserver github.com/gargath/vfstest/cmd
, I get a working binary that serves the embedded content.
However, when running go build -tags dev -o vfserver github.com/gargath/vfstest/cmd
, I get 404 errors from the HTTP server because it is now trying to find the path ../../frontend/
relative to the binary in the project root.
I can fix this by changing the path in data.Assets
to frontend/
, but then go generate
no longer works:
go generate ./pkg/...
2020/03/17 11:22:14 open frontend: no such file or directory
exit status 1
pkg/data/data.go:3: running "go": exit status 1
make: *** [generate] Error 1
How can I structure a project so that the same static assets directory can be used to both generate and serve locally?
Activity